【问题标题】:what does this line mean这条线是什么意思
【发布时间】:2017-02-02 12:50:25
【问题描述】:

我对此很陌生,但谁能告诉我这条线是什么意思?

predicted= linear.predict(x_test)

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    我认为sklearn.linear_model.LinearRegression().fit() 需要(# of rows, 1) 形状的数组。

    演示:

    In [52]: x_train= [5.5997066,4.759385,2.573958,5.586931,3.019574,4.296047,1.586953,0.5997066,3.683957]
    
    In [53]: linear.fit(x_train, y_train)
    <PYTHON_PATH>\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data
     is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshap
    e(1, -1) if it contains a single sample.
      DeprecationWarning)
    ...
    skipped
    ...
    ValueError: Found arrays with inconsistent numbers of samples: [1 9]
    

    让我们快乐:

    In [54]: x_train = np.array(x_train).reshape(len(x_train), -1)
    
    In [55]: x_train.shape
    Out[55]: (9, 1)
    
    In [56]: linear.fit(x_train, y_train)
    Out[56]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
    

    更新:同样适用于x_test 数组:

    In [60]: x_test = x_test.reshape(-1, 1)
    
    In [61]: x_test.shape
    Out[61]: (5, 1)
    
    In [62]: predicted= linear.predict(x_test)
    
    In [63]: predicted
    Out[63]: array([ 5.7559457,  5.7559457,  5.7559457,  5.7559457,  5.7559457])
    

    【讨论】:

    • 嗨,对不起,但我在相应更改后收到此错误:“ValueError:形状(1,5)和(1,1)未对齐:5(dim 1)!= 1(dim 0 )"
    • @AwArAw,好吧,这同样适用于x_test
    • 不错!它不再有错误,非常感谢大家!但是有一个问题,因为我的目的是绘制一个线性图,但它不显示任何图形。根据这些数字集绘制图表,我在这里遗漏了什么?
    • 将在我的声望达到合格限制后投票!谢谢!
    • 嗨,对不起,当我重新打开浏览器时,我突然收到了这个错误“NameError: name 'x_train' is not defined”。我必须在某个地方定义它吗?
    猜你喜欢
    • 2015-01-27
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多