my_train_list = {"area": [1000,2000,3000,4000,5000], "price":[1000000,2000000,3000000,4000000,5000000]}
my_df = pd.DataFrame(my_train_list)
my_x = my_df['area']
my_y = my_df.price
my_lin_pre = linear_model.LinearRegression()
my_lin_pre.fit([my_x], my_y)
Can anyone please explain why it saying it the error it saying require 2d array given 1d array but series in pandas are 1d right
can anyone please explain how to solve this?
CodePudding user response:
Scikit-learn estimators and transformers excepect 2D array. Your input should be an array of features (where each feature is an array itself).
You can do as follow to fix your code:
from sklearn.linear_model import LinearRegression
X = my_df[['area']]
y = my_df.price
model = LinearRegression()
model.fit(X, Y)
The trick here is that my_df[['area']]
will return a pd.DataFrame
(2D) instead of a pd.Series
(1D)