机器学习100天-简单线性回归 [代码实现细节分析]
作者:互联网
预测学生 学习时间 与 考试分数 之间的关系
原始数据:
(1)导入python包,加载.csv文件中的数据
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('studentscores.csv')
X = dataset.iloc[:,:1].values
Y = dataset.iloc[:,1].values
(2)将数据以3:1的比例分为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size = 1/4, random_state = 0)
(3)训练线性回归模型:利用训练集训练归回预测器,最后得到一个理想的预测器regressor
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor = regressor.fit(X_train, Y_train)
(4)测试
Y_pred = regressor.predict(X_test)
(5)可视化训练集上的结果:
plt.scatter画散点图
plt.plot画曲线图
plt.scatter(X_train, Y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.show()
(6)可视化测试集上的结果:
plt.scatter(X_test, Y_test, color = 'red')
plt.plot(X_test, regressor.predict(X_test), color = 'blue')
plt.show()
简单线性回归模型
标签:plt,color,regressor,test,细节,train,线性,import,100 来源: https://blog.csdn.net/STILLxjy/article/details/86497958