Sklearn implementation of linear regression

Linear regression sklearn implementation

1. Import the necessary modules
2. Build the data set
3. Data division
4. Model construction
5. Model training
6. Model prediction
7. View linear regression model
8. Calculate the evaluation index MSE

线性回归sklearn的实现
# 安装sklearn
!pip3 install sklearn
# 1.导入必要的模块
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
2.构建数据集
x=np.array([50,30,15,40,55,20,45,10,60,25])
y=np.array([5.9,4.6,2.7,4.8,6.5,3.6,5.1,2.0,6.3,3.8])
# 画出数据集散点图
plt.scatter(x,y)
plt.xlabel('area')
plt.ylabel('price')
plt.grid(True) # 绘制出有网格的效果
plt.show()

Insert picture description here

3.数据划分
from sklearn.model_selection import train_test_split
x_tarin,x_test,y_train,y_test=train_test_split(
    x,y,test_size=0.3,random_state=23)
x_tarin
# len(x_tarin)
4.模型搭建
from sklearn.linear_model import LinearRegression
model=LinearRegression()
5.模型训练
# x_tarin的shape由(10,)变为shape(10,1)
x_tarin=x_tarin.reshape(-1,1)
model.fit(X=x_tarin,y=y_train)
6.模型预测
x_test=x_test.reshape(-1,1)
y_test_pred=model.predict(X=x_test)
7.查看线性回归模型的系数w和截距b
w,b=model.coef_[0],model.intercept_
print('Weight={0} bias={1}'.format(w,b))
# 画出数据集的散点图和预测直线
plt.scatter(x_test,y_test,color='g',label='test dataset')
plt.scatter(x_tarin,y_train,color='b',label='train dataset')

linear=np.sort(x_test)*model.coef_[0]+model.intercept_
plt.plot(np.sort(x_test),linear,color='r',label='linear regression')
plt.legend()
plt.show()

Insert picture description here

8.计算评价指标MSE
from sklearn.metrics import mean_squared_error
mse=mean_squared_error(y_true=y_test,y_pred=y_test_pred)
print('MSE:{}'.format(mse))

Guess you like

Origin blog.csdn.net/weixin_42961082/article/details/113800914