【Python機(jī)器學(xué)習(xí)案例】竟然很準(zhǔn)!!Python預(yù)測(cè)淘寶2021雙十一銷售額
昨天雙十一,大家對(duì)淘寶銷售額感興趣,自己做了個(gè)機(jī)器學(xué)習(xí)模型,竟然預(yù)測(cè)的挺準(zhǔn)的。
數(shù)據(jù)鏈接:https://zh.wikipedia.org/wiki/%E5%8F%8C%E5%8D%81%E4%B8%80
通過機(jī)器學(xué)習(xí)模型,預(yù)測(cè)今年的銷售額是5475.2億元
最終的新聞報(bào)告,是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、訓(xùn)練機(jī)器學(xué)習(xí)模型
from?sklearn.pipeline?import?Pipeline
from?sklearn.preprocessing?import?PolynomialFeatures
from?sklearn.preprocessing?import?StandardScaler
from?sklearn.linear_model?import?LinearRegression
#?用管道的方式調(diào)用多項(xiàng)式回歸算法
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())])
#?算法評(píng)分
poly_reg.score(x,?y)
0.9458618037222158
4、調(diào)用模型進(jìn)行預(yù)測(cè)
current_year?=?2021
predict?=?poly_reg.predict([[current_year]])
predict
array([5475.20483333])
5、繪制趨勢(shì)圖
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()

推薦下螞蟻老師的機(jī)器學(xué)習(xí)課程
評(píng)論
圖片
表情
