【Python機器學習案例】竟然很準?。ython預測淘寶2021雙十一銷售額
昨天雙十一,大家對淘寶銷售額感興趣,自己做了個機器學習模型,竟然預測的挺準的。
數(shù)據(jù)鏈接:https://zh.wikipedia.org/wiki/%E5%8F%8C%E5%8D%81%E4%B8%80
通過機器學習模型,預測今年的銷售額是5475.2億元
最終的新聞報告,是5403億元,差的很少

下面是我的代碼過程,代碼地址:
https://gitee.com/antpython/ant-livebroadcast-publish
1、加載Excel數(shù)據(jù)
import?pandas?as?pd
import?numpy?as?np
df?=?pd.read_excel("./歷史雙十一銷售額.xlsx")
df
年份?成交總額
0?2011?52.00
1?2012?191.00
2?2013?350.19
3?2014?571.00
4?2015?912.00
5?2016?1207.00
6?2017?1682.00
7?2018?2135.00
8?2019?2684.00
9?2020?4982.00
2、提取數(shù)據(jù)
x?=?np.array(df.iloc[:,?0]).reshape(-1,?1)
y?=?np.array(df.iloc[:,?1])
x.shape
(10,?1)
y.shape
(10,)
3、訓練機器學習模型
from?sklearn.pipeline?import?Pipeline
from?sklearn.preprocessing?import?PolynomialFeatures
from?sklearn.preprocessing?import?StandardScaler
from?sklearn.linear_model?import?LinearRegression
#?用管道的方式調(diào)用多項式回歸算法
poly_reg?=?Pipeline([
????('ploy',?PolynomialFeatures(degree=2)),
????('std_scaler',?StandardScaler()),
????('lin_reg',?LinearRegression())
])
poly_reg.fit(x,?y)
Pipeline(steps=[('ploy',?PolynomialFeatures()),
????????????????('std_scaler',?StandardScaler()),
????????????????('lin_reg',?LinearRegression())])
#?算法評分
poly_reg.score(x,?y)
0.9458618037222158
4、調(diào)用模型進行預測
current_year?=?2021
predict?=?poly_reg.predict([[current_year]])
predict
array([5475.20483333])
5、繪制趨勢圖
df_new?=?df.append({"年份":"2021",?"成交總額":predict[0]},?ignore_index=True)
df_new["成交總額"]?=?df_new["成交總額"].map(lambda?x?:?round(x,?2))
df_new
年份?成交總額
0?2011?52.00
1?2012?191.00
2?2013?350.19
3?2014?571.00
4?2015?912.00
5?2016?1207.00
6?2017?1682.00
7?2018?2135.00
8?2019?2684.00
9?2020?4982.00
10?2021?5475.20
!pip?install?plotly
import?plotly.express?as?px
fig?=?px.line(df_new,?x="年份",?y="成交總額",?text="成交總額")
fig.update_traces(textposition="top?center")
fig.show()

推薦下螞蟻老師的機器學習課程
評論
圖片
表情
