【问题标题】:Linar regression on pandas gives null as regression scorepandas 的线性回归给出 null 作为回归分数
【发布时间】:2020-08-26 01:02:48
【问题描述】:
from sklearn.linear_model import LinearRegression

my_y = np.array([2, 5, 6, 10]).reshape(1, -1)
my_x = np.array([19, 23, 22, 30]).reshape(1,-1)

lm = LinearRegression()
lm = lm.fit(my_x, my_y)
result = lm.score(my_x, my_y)   
print(result)

Why does  this  give Nan as output 

【问题讨论】:

    标签: python pandas machine-learning scikit-learn linear-regression


    【解决方案1】:

    您需要将reshape(-1, 1) 用于数组

    my_y = np.array([2,5,6,10]).reshape(-1, 1)
    my_x = np.array([19,23,22,30]).reshape(-1, 1)
    
    lm = sk.LinearRegression()
    lm = lm.fit(my_x, my_y)
    result = lm.score(my_x, my_y)
    print(result)
    
    0.9302407516147975
    

    【讨论】:

    • 谢谢,现在工作正常,所以数组必须长而不宽?
    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 2020-06-10
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2020-12-24
    • 2012-06-27
    • 2017-12-16
    相关资源
    最近更新 更多