1
$\begingroup$

Code:

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
house_price = [245, 312, 279, 308, 199, 219, 405, 324, 319, 255]
size = [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700]
size2 = np.array(size).reshape((-1, 1))
#fitting into the model
regr = linear_model.LinearRegression()
regr.fit(size2, house_price)
print("Coefficients: \n", regr.coef_)
print("intercept: \n", regr.intercept_)
#############################
#formula obtained for the trained model
def graph(formula, x_range):
   x = np.array(x_range)
   y = eval(formula)
   plt.plot(x, y)
#plotting the prediction line 
graph('regr.coef_*x + regr.intercept_', range(1000, 2700))
print(regr.score(size2, house_price))
plt.scatter (size,house_price, color='black')
plt.ylabel('house price')
plt.xlabel('size of house')
plt.show()

Error

**Error Line:print regr.predict([2000])**
Error: File "<ipython-input-4-9afa91ca7f9e>", line 1
    print regr.predict([2000])
             ^
SyntaxError: invalid syntax
$\endgroup$
13
  • $\begingroup$ what are you trying to do? $\endgroup$ Jan 31, 2018 at 7:34
  • $\begingroup$ I guess your problem is because of your print line. use print as a function: $\endgroup$ Jan 31, 2018 at 7:35
  • 1
    $\begingroup$ @Toros91 I guess it is because he is using python 3.6 $\endgroup$ Jan 31, 2018 at 7:48
  • 1
    $\begingroup$ Agreed, he needs brackets for the print. But, there is a much more nefarious problem in his way of using predict. $\endgroup$
    – JahKnows
    Jan 31, 2018 at 7:55
  • 2
    $\begingroup$ I was surprised this question got into HNQ with such generic title and code dump... $\endgroup$
    – Andrew T.
    Jan 31, 2018 at 11:03

1 Answer 1

3
$\begingroup$

In your current line

print regr.predict([2000])

This will not work. The first error is the lack of brackets around the contents of your print statement which is required in Python 3. Change this first to

print(regr.predict([2000]))

However, you will see that this does not work either. I suspect you are attempting to evaluate the price for a new $size = 2000$. You will need to reshape the input to your regression for this to work.

new_size = np.array([2000]).reshape((-1, 1))
print(regr.predict(new_size))

[ 317.78380528]

$\endgroup$
1
  • $\begingroup$ +1 I din't know about that bracket thing for python version above 3, nice will remember. $\endgroup$
    – Toros91
    Jan 31, 2018 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.