为什么我在用 sklearn 进行线性回归时只有一个 coef_? [英] Why I get just one coef_, when I am doing my linear regression with sklearn?

查看:38
本文介绍了为什么我在用 sklearn 进行线性回归时只有一个 coef_?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码和下面的输出.我正在尝试使用 sklearn-lib,女巫有效.也许 x.reshape 是假的?

This is my code and the output below. I am trying it with sklearn-lib, witch works. Is maybe x.reshape false ?

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sympy.stats import E

#My Data
y = np.array([1.88,3.65,5.86,8.43,11.47,15.98])
x = np.array([1,2,3,4,5,6])

linreg = LinearRegression()

#Grafikgrösse einstellen
x = x.reshape(-1,1)
linreg.fit(x,y)

y_pred = linreg.predict(x)

plt.scatter(x,y)
plt.plot(x,y_pred, color="red")
plt.title("'Linearer Verlauf' durch t=0")
plt.xlabel("Anzahl Umdrehungen")
plt.ylabel("Periode(s)")
plt.legend(['t1'])
plt.show()
print("r^2 =",linreg.score(x,y))
print(linreg.coef_)

推荐答案

Equation: y = a*x + b

您的 alinreg.coef_ 项计算,它有一个值(因为只有一个项依赖于 x.+b 项被 linreg.intercept_ 访问,得到 -1.7746.

Equation: y = a*x + b

Your a is calculated by linreg.coef_ term and it has one value (since only one term dependent on x. The +b term is accessed by linreg.intercept_, which gives -1.7746.

这篇关于为什么我在用 sklearn 进行线性回归时只有一个 coef_?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆