Pandas 中的线性模型
Linear model in Pandas
我有一些准备好的数据框:
Customer_Age Customer_Gender 计数
0 17 米 846
1 17 楼 460
2 18 米 970
3 18 楼 790
4 19 米 1188
………………
129 84 米 4
130 85 米 16
131 86 前锋 4
132 86 米 4
133 87 楼 6
我想创建线性模型和回归。这是我的代码:
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.
在模型 运行 之前添加此行:
x = x.reshape(-1, 1)
我有一些准备好的数据框: Customer_Age Customer_Gender 计数 0 17 米 846 1 17 楼 460 2 18 米 970 3 18 楼 790 4 19 米 1188 ……………… 129 84 米 4 130 85 米 16 131 86 前锋 4 132 86 米 4 133 87 楼 6
我想创建线性模型和回归。这是我的代码:
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.
在模型 运行 之前添加此行:
x = x.reshape(-1, 1)