简体   繁体   English

预期的二维数组,得到的是一维数组:array=[1 3 5 6 7 8 9]?

[英]Expected 2D array, got 1D array instead: array=[1 3 5 6 7 8 9]?

x=[1,3,5,6,7,8,9]
y=[4,5,6,9,3,4,6]

def linear_model_main(X_parameters,Y_parameters,predict_value):
 
 # Create linear regression object
 regr = linear_model.LinearRegression()
 regr.fit(x, y)
 predict_outcome = regr.predict(predict_value)
 predictions = {}
 predictions['intercept'] = regr.intercept
 predictions['coefficient'] = regr.coef
 predictions['predicted_value'] = predict_outcome
 predicted_value = predict_outcome
 #return predicted_value
 return predictions



predictvalue = 7000
result = linear_model_main(x,y,predictvalue)
print ("Intercept value " , result['intercept'])
print ("coefficient" , result['coefficient'])
print ("Predicted value: ",result['predicted_value'])

I got this error when fit function is called: regr.fit(x, y)调用 fit function 时出现此错误: regr.fit(x, y)

ValueError: Expected 2D array, got 1D array instead: array=[1 3 5 6 7 8 9]. ValueError:预期的二维数组,得到的是一维数组:array=[1 3 5 6 7 8 9]。 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.如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1) 。

Here is your code corrected:这是您的代码更正:

from sklearn import linear_model
import numpy as np

x=[1,3,5,6,7,8,9]
y=[4,5,6,9,3,4,6]

def linear_model_main(X_parameters,Y_parameters,predict_value):
    # Create linear regression object
    regr = linear_model.LinearRegression()
    regr.fit(np.array(x).reshape(-1,1), np.array(y).reshape(-1,1))
    predict_outcome = regr.predict(np.array(predict_value).reshape(-1,1))
    predictions = {}
    predictions['intercept'] = regr.intercept_
    predictions['coefficient'] = regr.coef_
    predictions['predicted_value'] = predict_outcome
    predicted_value = predict_outcome
    #return predicted_value
    return predictions



predictvalue = 7000
result = linear_model_main(x,y,predictvalue)
print ("Intercept value " , result['intercept'])
print ("coefficient" , result['coefficient'])
print ("Predicted value: ",result['predicted_value'])

You had a couple of mistakes that I will explain below:你有几个错误,我将在下面解释:

1- First of all, you need to convert the input to NumPy array and instead of having 1 by n array, you need n by 1 array. 1- 首先,您需要将输入转换为 NumPy 数组,而不是 1 x n 数组,您需要 n x 1 数组。 The error that you got is because of that (that is how scikit-learn models are designed).你得到的错误是因为那个(这就是 scikit-learn 模型的设计方式)。

2- Second, you missed the underscore at the end of the attribute names like 'intercept_' 2-其次,您错过了属性名称末尾的下划线,例如“intercept_”

3- The value for prediction should be an n by 1 array too. 3- 预测值也应该是一个 n x 1 数组。

After fixing these issues here is the result (the dots are the input and the dashed line is the linear model):解决这些问题后就是结果(点是输入,虚线是线性模型): 在此处输入图像描述

EDIT: This is the code for the plot:编辑:这是 plot 的代码:

plt.scatter(x,y)

axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = result['intercept'][0] + result['coefficient'][0] * x_vals
plt.plot(x_vals, y_vals, '--')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM