Expected 2D array, got scalar array instead:

Viewed 32
    import pandas as pd
    import NumPy as np
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LinearRegression 
    model=LinearRegression()
    %matplotlib inline
    hp=pd.read_csv(r"C:\Users\book1.csv")
 
    plt.title('Home Price wrt Area')
    plt.xlabel('area(sqrvft)')
    plt.ylabel('price($)')
    plt.plot(hp.area,hp.price,color='g', marker='*')
    
    model.fit(hp[['area']],hp.price)

    plt.title('Home Price wrt Area')
    plt.xlabel('area(sqrvft)')
    plt.ylabel('price($)')
    plt.scatter(hp.area,hp.price,color='r',marker='D')
    plt.plot(hp.area,model.predict(hp[['area']]),color='g', marker='*')
    model.coef_

    model.intercept_
    model.predict(1300) 
    # I have a value error in this line value error plz help me to eliminate it
    # model.predict() line show value error
1 Answers

LinearRegression.predict expects 2D array as an input.

Example:

import numpy as np
from sklearn.linear_model import LinearRegression

model = LinearRegression()

area = np.random.normal(size=(10, 1))
price = np.random.random(size=(10,))
model.fit(area, price)
print(model.coef_)

print(model.intercept_)
model.predict([[1300]])

Related