【问题标题】:How to use a linear regression model to produce a single prediction value?如何使用线性回归模型生成单个预测值?
【发布时间】:2021-01-15 01:44:11
【问题描述】:

我在 Jupyter Notebook 中使用 Scikit-learn 创建了三个机器学习模型(线性回归、Dtree 和随机森林)。模型的目的是根据几个旋风参数(预测器/输入)预测旋风器的大小(预测/输出 ROCI)。有 9004 行。下面是一个线性回归模型的例子。

In[31]: df.head()
Out[31]:    NAME    LAT    LON   Pc    Penv   ROCI  Vmax  Pdc
         0  HECTOR  -15    128   985   1000   541   18    -15
         1  HECTOR  -15    127   990   1000   541   15.4  -10         
         2  HECTOR  -16    126   992   1000   530   15    -8
         3  HECTOR  -16.3  126   992   1000   480   15.4  -8
         4  HECTOR  -16.5  126   992   1000   541   15.4  -8

In [32]: X=df[['LAT','LON','Pc','Vmax','Pdc=Pc-Penv']]
         y=df['ROCI']

In [33]: X_train, X_test, y_train, y_test = train_test_split(X, y, 
         test_size=0.4) 

In [34]: lm=LinearRegression()

In [35]: lm.fit(X_train,y_train)
Out [35]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, 
          normalize=False)

In [36]: print(lm.intercept_)
         lm.coef_
         -3464.3452921023572
Out [36]: array([-2.94229126,  0.29875575,  3.65214265, -1.25577799, 
          -6.43917746])

In [37]: predictions=lm.predict(X_test)
         predictions
Out [37]:array([401.02108725, 420.01451472, 434.4241271 , ..., 
         287.67803538, 343.80516896, 340.1007666 ])

In [38]: plt.scatter(y_test,predictions)
         plt.xlabel('Recorded')
         plt.ylabel('Predicted')
      
         *figure to display accuracy*

现在,当我尝试在 lm.predict() 中输入单个值时,出现以下错误:

ValueError: Expected 2D array, got scalar array instead:
array=300.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我认为这是因为我的模型是使用 5 列训练的,因此尝试输入数据集的第一行:

In [39]: lm.predict(-15,128,985,18,-15)
         ...
         ...
         TypeError: predict() takes 2 positional arguments but 6 were 
         given

按照我得到的建议尝试 array.reshape:

In [49]: lm.predict(X_test.reshape(-1, 1))
         ...
         ...
         AttributeError: 'DataFrame' object has no attribute 'reshape'

现在我很困惑! 请您协助我使用我的模型给我一个预测值。我应该在 lm.predict() 中输入什么? 我基本上只想说“Pc=990,Vmax=18,Pdc=-12”,然后得到类似“ROCI=540”的信息。感谢您的宝贵时间。

【问题讨论】:

  • |我不明白你为什么要尝试lm.predict(X_test.reshape(-1, 1)),因为你已经成功预测了上面几行的X_test
  • 是的,我知道,我只是被卡住了,需要让这件事起作用来预测,所以我正在尝试任何事情

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


【解决方案1】:

如果你想预测数据的第一行,你应该先把它做成一个数组

import numpy as np

first_row = np.array([-15, 128, 985, 18, -15])

那么,当

lm.predict(first_row)

产生与您报告的错误相似的错误,

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

遵循消息中的建议,即:

lm.predict(first_row.reshape(1, -1))

【讨论】:

    【解决方案2】:

    为此你必须写

    X=df[['Pc','Vmax','Pdc=Pc-Penv']]
    

    而不是

    X=df[['LAT','LON','Pc','Vmax','Pdc=Pc-Penv']]
    

    请记住,您提供给模型进行训练的输入,是您在必须进行预测时必须提供的输入

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      相关资源
      最近更新 更多