[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
使用广告收益数据,建立广告投入和销量的关系模型,并按照下个月的投入量预测销量
# coding: utf-8 import numpy as np import pandas as pd import matplotlib.pyplot
as plt filename='data\\advertising.csv' data=pd.read_csv(filename,index_col=0)
print(data.iloc[0:5,:]) #1.观察变量的相关性,选择合适的列作为模型的特征量 data.plot(kind='scatter',x=
'TV',y='Sales')#可以选择部分数据绘制散点图 data.plot(kind='scatter',x='Weibo',y='Sales')
#多次plot()绘制在不同图上,plt.scatter()绘制在一张图 data.plot(kind='scatter',x='Weibo',y=
'Sales') data.plot(kind='scatter',x='WeChat',y='Sales') plt.show() x=data.iloc[:
,0:3].values.astype(float) y=data.iloc[:,3].values.astype(float) #模型初始化 #from
sklearn.linear_model import LinearRegression #linreg=LinearRegression() #模型训练
#linreg.fit(x,y) #模型训练学习 print(linreg.intercept_,linreg.coef_)#线性回归模型的截距和系数 from
sklearnimport cross_validation from sklearn import model_selection
#2.将数据分为训练集和测试集0.35 x_train,x_test,y_train,y_test=model_selection.
train_test_split(x,y,test_size=0.35,random_state=1) #3.模型初始化 linregTr=
LinearRegression() #4.模型训练 linregTr.fit(x_train,y_train) #5.模型预测 y_train_pred=
linregTr.predict(x_train) y_test_pred=linregTr.predict(x_test)
#分别计算误差,确定是否过拟合;评估模型的泛化能力 from sklearn import metrics train_err=metrics.
mean_squared_error(y_train,y_train_pred) test_err=metrics.mean_squared_error(
y_test,y_test_pred) #6.性能评估 predict_score=linregTr.score(x_test,y_test)
#性能评估,决定系数 print('The decision coeficient is:{:.2f}'.format(predict_score))
#决定系数相比于RMSE均方根误差去除了纲量
性能评估方法原理
引用:数据科学与技术(宋晖)