【问题标题】:Linear model in PandasPandas 中的线性模型
【发布时间】:2021-01-09 10:01:22
【问题描述】:

我有一些准备好的数据框: 客户_年龄客户_性别计数 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 F 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.

【问题讨论】:

    标签: python pandas dataframe linear-regression linear-algebra


    【解决方案1】:

    在运行模型之前添加这一行:

    x = x.reshape(-1, 1)
    

    【讨论】:

    • x = x.reshape(-1, 1) x = select2['Customer_Age'] y = select2['count'] model = LinearRegression() 我得到错误 AttributeError: 'Series' object has没有属性“重塑”
    • 我添加了 x = select2['Customer_Age'].values.reshape(-1, 1) 但我什么也没得到
    • 如果是系列则将代码改为:x = pd.DataFrame(select2['Customer_Age'])
    • 好的,我得到了分散,但是我怎样才能在绘图中添加线条?
    猜你喜欢
    • 2014-11-25
    • 2019-08-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2014-04-17
    相关资源
    最近更新 更多