首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线性回归问题及总结

线性回归问题及总结
EN

Stack Overflow用户
提问于 2021-01-12 23:54:41
回答 1查看 24关注 0票数 0

我想创建一个线性回归模型的图,显示每年自行车销量在一个点上的总和,而不是现在有两个点分开。

这是我的代码:

代码语言:javascript
复制
from sklearn.linear_model import LinearRegression
from sklearn import datasets, linear_model

## Wzrost lub maleje zakup rowerow
## (Purchase of bicycles increases or decreases)
plot1 = df.groupby('Year')['Product_Category'].value_counts().rename('count').reset_index()

x = plot1['Year'].values.reshape(-1, 1)
y = plot1['count'].values.reshape(-1, 1)

# plot #
## linear ##
regr = linear_model.LinearRegression()
regr.fit(x, y)
y_pred = regr.predict(x_test)

#plot#
plt.scatter(x, y,  color='black')
plt.plot(x, y, color='blue', linewidth=3)

这是我的图:

EN

回答 1

Stack Overflow用户

发布于 2021-01-13 00:50:01

就我从你的例子中所理解的,这可能是一个解决方案,用count代替value_counts

示例数据:

代码语言:javascript
复制
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Year': [ 2019, 2019, 2020, 2021], 'Product_Category': ['a', 'b', 'c', 'd']})
print(df)
   Year Product_Category
0  2019                a
1  2019                b
2  2020                c
3  2021                d

计数将返回:

代码语言:javascript
复制
plot1 = df.groupby('Year')['Product_Category'].count().rename('count').reset_index()
print(plot1)

  Year  count
0  2019      2
1  2020      1
2  2021      1


plot1 = df.groupby('Year')['Product_Category'].count().rename('count').reset_index()
#x,y#
x = plot1['Year'].values.reshape(-1, 1)
y = plot1['count'].values.reshape(-1, 1)
# plot #

#plot#
plt.scatter(x, y,  color='black')
plt.plot(x, y, color='blue', linewidth=3)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65687383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档