0
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)

ValueError: Expected 2D array, got 1D array instead: 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.

Amin Gheibi
7881 gold badge10 silver badges19 bronze badges
asked Nov 11, 2020 at 4:19
1
  • 1
    You're making us guess where the error happens. Please update the question to include the full error traceback message. Commented Nov 11, 2020 at 4:21

1 Answer 1

0

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. The error that you got is because of that (that is how scikit-learn models are designed).

2- Second, you missed the underscore at the end of the attribute names like 'intercept_'

3- The value for prediction should be an n by 1 array too.

After fixing these issues here is the result (the dots are the input and the dashed line is the linear model): enter image description here

EDIT: This is the code for the 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()
answered Nov 11, 2020 at 4:50
Sign up to request clarification or add additional context in comments.

5 Comments

Thank for the solution. I have one more doubt how can we plot the precdited value with other values
Your predicted value will be on the line (since it is a linear regression. I have used this piece to plot: plt.scatter(x,y) and 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()
It is ugly in the comments. I am gonna edit the post and put it there. If that solves your problem please mark the answer as the correct one.
yes sir, solved. Can you explain the what is get_xlim(), and y axis expression so that I can have clear understanding
Yeah, in order to plot a line we need some points of that line. By get_xlim() we get values on the x-axis and then put those values in the line formula to get some point to draw the line.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.