import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.linear_model import LinearRegression
data=pd.read_csv('real_estate_price_size.csv')
y=data['price']
x=data['size']
y.shape
x.shape
both out put is same (100,)
x_matrix = x.values.reshape(-1,1)
reg = LinearRegression()
reg.fit(x_matrix,y)
reg.predict(750)
when i run this i get error as
ValueError: Expected 2D array, got scalar array instead:
array=750.
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.
i cant under stand what to do i tried reshaping it but it dint work out.
and (2) previously when i typed
reg = LinearRegression()
reg.fit(x_matrix,y)
i used to get output like this
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
now i just get text like
LinearRegression()
help would be much appriciated .
CodePudding user response:
You are missing a dimension. Your prediction input should have the shape (n_samples, n_features)
. Try something like this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.linear_model import LinearRegression
y=np.random.random((100, 1))
x=np.random.random((100, 1))
reg = LinearRegression()
reg.fit(x,y)
predict_input = np.array([[750]])
predict_input.shape
reg.predict(predict_input)
(1, 1)
array([[-26.07506481]])