Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have some prepared dataframe : Customer_Age Customer_Gender count 0 17 M 846 1 17 F 460 2 18 M 970 3 18 F 790 4 19 M 1188 ... ... ... ... 129 84 M 4 130 85 M 16 131 86 F 4 132 86 M 4 133 87 F 6

I want to create linear model and regression. This is my code:

from sklearn.linear_model import LinearRegression
select2 = df.groupby('Customer_Age')['Customer_Gender'].value_counts().rename('count').reset_index()
select2
x = select2['Customer_Age']
y = select2['count']
model = LinearRegression()
model.fit(x, y)
model = LinearRegression().fit(x, y)```

I get error:
ValueError: Expected 2D array, got 1D array instead:
array=[number]
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.

question from:https://stackoverflow.com/questions/65641358/linear-model-in-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
83 views
Welcome To Ask or Share your Answers For Others

1 Answer

Before running the model add this line:

x = x.reshape(-1, 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...