【问题标题】:how to predict from an array of data-python scikit learn pandas如何从数据数组中预测-python scikit learn pandas
【发布时间】:2017-05-07 05:55:02
【问题描述】:

我找到了一个使用 python scikit-learn 线性回归预测下一个值的代码。

我能够预测单个数据..但实际上我需要预测 6 个值并打印六个值的预测。

这里是代码

def linear_model_main(x_parameters, y_parameters, predict_value):
    # Create linear regression object
    regr = linear_model.LinearRegression()<
    regr.fit(x_parameters, y_parameters)
    # noinspection PyArgumentList
    predict_outcome = regr.predict(predict_value)
    score = regr.score(X, Y)
    predictions = {'intercept': regr.intercept_, 'coefficient': regr.coef_,   'predicted_value': predict_outcome, 'accuracy' : score}
    return predictions

predicted_value = 9 #I NEED TO PREDICT 9,10,11,12,13,14,15

result = linear_model_main(X, Y, predicted_value)
print('Constant Value: {0}'.format(result['intercept']))
print('Coefficient: {0}'.format(result['coefficient']))
print('Predicted Value: {0}'.format(result['predicted_value']))
print('Accuracy: {0}'.format(result['accuracy']))

我试过这样做:

predicted_value = {9,10,11,12,13,14,15}

result = linear_model_main(X, Y, predicted_value)
print('Constant Value: '.format(result['intercept']))
print('Coefficient: '.format(result['coefficient']))
print('Predicted Value: '.format(result['predicted_value']))
print('Accuracy: '.format(result['accuracy']))

错误信息是:

Traceback (most recent call last):
File "C:Python34\data\cp.py", line 28, in <module>
result = linear_model_main(X, Y, predicted_value)
File "C:Python34\data\cp.py", line 22, in linear_model_main
predict_outcome = regr.predict(predict_value)
File "C:\Python34\lib\site-packages\sklearn\linear_model\base.py", line 200,  in predict return self._decision_function(X)
 File "C:\Python34\lib\site-packages\sklearn\linear_model\base.py", line 183, in _decision_function
X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
File "C:\Python34\lib\site-packages\sklearn\utils\validation.py", line 393, in check_array array = array.astype(np.float64)
TypeError: float() argument must be a string or a number, not 'set'

C:\>

predicted_value = 9,10,11,12,13,14,15

result = linear_model_main(X, Y, predicted_value)
print('Constant Value: '.format(result['intercept']))
print('Coefficient: '.format(result['coefficient']))
print('Predicted Value: '.format(result['predicted_value']))
print('Accuracy: '.format(result['accuracy']))

遇到这些错误

   C:\Python34\lib\site-packages\sklearn\utils\validation.py:386:    DeprecationWarnin
g: 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.reshape(1, -1) if it contains a single sample.
 DeprecationWarning)
 Traceback (most recent call last):
  File "C:Python34\data\cp.py", line 28, in <module>
  result = linear_model_main(X, Y, predicted_value)
  File "C:Python34\data\cp.py", line 22, in linear_model_main
predict_outcome = regr.predict(predict_value)
    File "C:\Python34\lib\site-packages\sklearn\linear_model\base.py", line 200, in predict return self._decision_function(X)
    File "C:\Python34\lib\site-packages\sklearn\linear_model\base.py", line   185, in _decision_function dense_output=True) + self.intercept_
    File "C:\Python34\lib\site-packages\sklearn\utils\extmath.py", line 184, in safe_sparse_dot return fast_dot(a, b)
    ValueError: shapes (1,3) and (1,1) not aligned: 3 (dim 1) != 1 (dim 0)

C:\>

如果我做出这样的改变:

predicted_value = 9
result = linear_model_main(X, Y, predicted_value)
print('Constant Value: {1}'.format(result['intercept']))
print('Coefficient: {1}'.format(result['coefficient']))
print('Predicted Value: {}'.format(result['predicted_value']))
print('Accuracy: {1}'.format(result['accuracy']))

它会再次给我错误,说它超过了限制。必须做什么?

【问题讨论】:

  • 请给出错误信息
  • 我已经添加了错误信息

标签: python pandas scikit-learn


【解决方案1】:

这是一个工作示例。我没有构建你的函数,只是向你展示了正确的语法。您似乎没有正确地将数据传递到 fit

import numpy as np
from sklearn import linear_model

x = np.random.uniform(-2,2,101)
y = 2*x+1 + np.random.normal(0,1, len(x))

#Note that x and y must be in specific shape.

x = x.reshape(-1,1)
y = y.reshape(-1,1)


LM  = linear_model.LinearRegression().fit(x,y) #Note I am passing in x and y in column shape

predict_me = np.array([ 9,10,11,12,13,14,15])

predict_me = predict_me.reshape(-1,1)

score = LM.score(x,y)


predicted_values = LM.predict(predict_me)

predictions = {'intercept': LM.intercept_, 'coefficient': LM.coef_,   'predicted_value': predicted_values, 'accuracy' : score}

【讨论】:

  • x 和 y 是数据帧
  • @VictorJohnzon 您可以使用df.values 将它们放入numpy 数组中,然后使用reshape 函数。
  • 我的 x 和 y 值是 pandas 数据框。nd 值是通过查询 db 获得的
  • 如果它们是数据帧,那么问题不在于拟合,而在于预测。您不能将标量传递给预测例程。它需要是一个数组。
  • X = pd.read_sql('select stamp from test;', con=mysql_cn) Y = pd.read_sql('select temp from test;', con=mysql_cn) mysql_cn.close() print (X) print(Y) x = pd.values(X) y = pd.values(Y) x = X.reshape(-1,1) y = Y.reshape(-1,1)
【解决方案2】:

Regr.predict() 期待一个与 X 形状相同的集合。(read the docs) 而不是这个,你正在证明一个标量值 (9)。这就是为什么您会收到关于对象形状不匹配的错误。

您需要使用与 X 具有相同“列”数(尽管不一定相同的行数)的对象进行预测,并且您将获得每行的预测结果。

您的 predict_value 变量似乎没有任何用处,因为 Y 应该包含您尝试为 X 的每一行预测的标签。

【讨论】:

  • 如果我只是给出一个值 9,它将预测我相应的值.. 但我试图同时预测更多的值,如 9、10、11 等
  • 假设您的 X 是 1 列深度 np.array 那么您必须输入要以相同格式预测的值。 np.array([ 9,10,11,12,13,14,15])
  • 回溯(最近一次调用最后):文件“C:Python34\data\cp.py”,第 17 行,在 x = X.reshape(-1,1) 文件“C :\Python34\lib\site-packages\pandas\core\generic.py",第 2744 行,在 __getattr__return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'reshape' C:\>跨度>
猜你喜欢
  • 2016-04-01
  • 2021-07-21
  • 2016-02-09
  • 2016-04-03
  • 2018-01-27
  • 2022-07-19
  • 2019-06-07
  • 2015-03-11
  • 2023-03-09
相关资源
最近更新 更多