สมมุติว่าถ้าเกิดเราอยากจะสร้าง ชุดข้อมูลของนักเรียนในโรงเรียนของตัวเองนั้น เพื่อมาดูว่าควรจะมาปรับอะไรในโรงเรียนของตัวเองเพื่อสอดคล้องต่อสิ่งที่เด็กนักเรียนต้องการเนี่ย หนึ่งในวิธีที่ง่ายและเหมาะสมที่ใช้ คือ Multiple Linear Regression
บทความนี้ เราจะมาดู Multiple Linear Regression ใน Python เราจะใช้ Google Colab ในการรันโค้ด โดย Data ที่จะนำมาใช้ก็คือ ข้อมูลของนักเรียน 10,000 รายการโดยแต่ละบันทึกประกอบด้วยข้อมูลเกี่ยวกับตัวทำนายต่างๆ และดัชนีประสิทธิภาพ
โดยมีข้อมูลตั้งต้นดังนี้
Hours Studied: จำนวนชั่วโมงทั้งหมดที่ใช้ในการศึกษาของนักเรียนแต่ละคน
Previous Scores: คะแนนที่นักเรียนได้รับในการทดสอบครั้งก่อน
Extracurricular Activities: ไม่ว่านักเรียนจะมีส่วนร่วมในกิจกรรมนอกหลักสูตรหรือไม่ (Yes or No)
Sleep Hours: จำนวนชั่วโมงการนอนหลับเฉลี่ยที่นักเรียนมีต่อวัน
Sample Question Papers Practiced: จำนวนตัวอย่างข้อสอบที่นักเรียนฝึก
ทำการนำเข้าข้อมูล และ Import Libraries ที่เกี่ยวข้อง
นี่คือ ข้อมูล และ Libraries ที่เกี่ยวข้องทำการ import เข้ามา
import numpy as np
import pandas as pd
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
ตัวอย่างของข้อความเมื่อ run ออกมา
/kaggle/input/student-performance-multiple-linear-regression/Student_Performance.csv
ทำการสร้าง Multiple Linear Regression
เริ่มจากการ Import Libraries เข้ามาก่อน
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
ทำการอ่านข้อมูลจากไฟล์ CSV โดยใช้ pandas.read_csv() และเก็บข้อมูลไว้ใน DataFrame ที่ชื่อว่า "df"
df = pd.read_csv("/kaggle/input/student-performance-multiple-linear-regression/Student_Performance.csv")
ทำการแบ่งข้อมูลใน DataFrame "df" เป็น input variables (x) และ output variable (y)
x = df.iloc[:,:-1].values
y = df.iloc[:,-1].values
ทำการแปลงข้อมูลที่เป็น categorical ในคอลัมน์ที่ 2 โดยใช้ LabelEncoder เพื่อให้เป็นข้อมูลที่เหมาะสมสำหรับการนำเข้าโมเดล Linear Regression
from sklearn.preprocessing import LabelEncoder
labelEncoder_X = LabelEncoder()
x[:,2] = labelEncoder_X.fit_transform(x[:,2])
ทำการลบคอลัมน์แรกออกจากตัวแปร x
x = x[:,1:]
ทำการ import function train_test_split จากโมดูล sklearn.model_selection เพื่อทำการแบ่งข้อมูลออกเป็นชุด train และ test โดยสุ่มข้อมูลออกมาตามสัดส่วนที่กำหนด
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)
ต่อไปนำเข้าคลาส LinearRegression จากโมดูล sklearn.linear_model เพื่อใช้ในการสร้างโมเดล Linear Regression
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)
ต่อไปก็ทำนายผลลัพธ์
y_pred = regressor.predict(x_test)
ลำดับต่อไปก็นำเข้าฟังก์ชัน mean_squared_error จากโมดูล sklearn.metrics เพื่อใช้ในการคำนวณค่า Mean Squared Error (MSE)
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, y_pred)
จะได้ค่าออกมาเป็น
58.78022231198216
ต่อไปก็จะทำการให้แสดงข้อมูลของ data ออกมา ดังนี้
df.head()
นี่คือตารางเมื่อ run ออกมาแล้ว จะได้ผลออกมาตามนี้
ลองทำการ เขียนกราฟออกมา เพื่อจะได้เห็นชัดมากขึ้น
import seaborn as sns
prev_score = df['Previous Scores']
per = df['Performance Index']
plt.figure(figsize=(10, 6))
sns.scatterplot(x=prev_score, y=per, color='blue', label='Data Points')
sns.regplot(x=prev_score, y=per, scatter=False, color='red', label='Mutliple Linear Regression')
plt.xlabel('Previous Score')
plt.ylabel('Performance Index')
plt.title('Relation between Previous Score and Performance Index with Linear Regression Line')
plt.legend()
plt.show()
รูปที่ได้เมื่อ run ออกมาแล้ว
สรุปผล
เราสามารถทำให้รู้ได้ว่า ในการทำ Multiple Linear Regression และ visualization ของผลลัพธ์โมเดลที่ได้ โดยใช้ข้อมูลจาก CSV file นี้ จากการทำงานของโค้ดนี้ช่วยให้เราสามารถวิเคราะห์ความสัมพันธ์ระหว่างตัวแปรต้นและตัวแปรตามของข้อมูลได้อย่างมีประสิทธิภาพและตรงไปตามวัตถุประสงค์ที่ต้องการ


Top comments (0)