一维线性回归sklearn实现

一维线性回归sklearn实现

#coding:utf-8
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt

#x轴数据
x_data = np.arange(20)
#y轴数据
y_data = np.array([0.4, 0.8, 1.1, 2.1, 2.8, 2.7, 3.5, 4.6, 5.1, 4.5, 6.0, 5.5, 6.9, 6.8, 7.6, 8.0, 8.8, 8.5, 9.5, 9.3])
print(x_data)
print(y_data)

plt.scatter(x_data,y_data)
plt.xlabel("x_data")
plt.ylabel("y_data")
# plt.show()

#转一下维度,sklearn框架才能识别
x_data = x_data[:,np.newaxis]

y_data = y_data[:,np.newaxis]
print("*"*100)
print(x_data)
print(y_data)
#建立模型
model = LinearRegression()

#开始训练
model.fit(x_data,y_data)

#斜率
print("coefficients : ",model.coef_)
w = model.coef_[0]

#截距
print("intercept: ",model.intercept_)
b = model.intercept_

#测试
x_test = np.array([[7]])
predict = model.predict(x_test)
print("predict: ",predict)

#回归最终结果
plt.plot(x_data,(w*x_data + b),"b-")
plt.scatter(x_test,predict,color= 'r',marker = 'o')

plt.show()

注意几个细节:
       斜率和截距是怎么算的:
       斜率计算方法:
       print("coefficients : ",model.coef_)
       w = model.coef_[0]
       #截距:
       print("intercept: ",model.intercept_)
       b = model.intercept_

显示结果如下如:

在这里插入图片描述

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[ 0.4  0.8  1.1  2.1  2.8  2.7  3.5  4.6  5.1  4.5  6.   5.5  6.9  6.8  7.6
  8.   8.8  8.5  9.5  9.3]
****************************************************************************************************
[[ 0]
 [ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]
 [11]
 [12]
 [13]
 [14]
 [15]
 [16]
 [17]
 [18]
 [19]]
[[ 0.4]
 [ 0.8]
 [ 1.1]
 [ 2.1]
 [ 2.8]
 [ 2.7]
 [ 3.5]
 [ 4.6]
 [ 5.1]
 [ 4.5]
 [ 6. ]
 [ 5.5]
 [ 6.9]
 [ 6.8]
 [ 7.6]
 [ 8. ]
 [ 8.8]
 [ 8.5]
 [ 9.5]
 [ 9.3]]
coefficients :  [[ 0.49150376]]
intercept:  [ 0.55571429]
predict:  [[ 3.9962406]]

Process finished with exit code 0

参考:
https://blog.csdn.net/alionsss/article/details/86809687

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值