新实战项目-机器学习-纪录片播放量预测

介绍一下这个项目其实是一个真实的企业的项目比较新。项目也可以扩展到一些电影的预测,票房预测等等。

新实战项目-机器学习-纪录片播放量预测

新实战项目-机器学习-纪录片播放量预测

也可以降低一下数据的相关性!

开始

纪录片播放量预测,回归问题 实际项目!!!!!!!!!!!!!!!!

<code># %pip install wordcloud//没导包的可以这样下载
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.linear_model import LinearRegression,Ridge
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn import metrics
import jieba
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import TruncatedSVD
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import warnings
warnings.filterwarnings('ignore')
/<code>

一、数据检索

在此项目中,每部纪录片的名称都是独立的,如果片名参与运算的话,会提升模型复杂度,造成过拟合的情况

<code>data = pd.read_excel('./datas/纪录片播放量.xls',index_col='片名')//片名不考虑,做标签了
data.head()
/<code>
新实战项目-机器学习-纪录片播放量预测

像不像B站O(∩_∩)O哈哈~

提问:为什么播放数是object类型? 因为数据上里面的是1.2万这种数据!

<code>data.info()
/<code>
新实战项目-机器学习-纪录片播放量预测

删除干扰特征

<code>del data['标签']
del data['上传日期']//应该又会有关系的,但是删除就删除了

print('删除干扰特征')
/<code>

二、数据挖掘

2.1对上万条信息进行数据调整 处理Object的数据

<code>def f(s):
'''
如果特征中统计值出现‘万’字,将‘万’字删除,数字后面添加4个0
args:
s: 原始数据特征,object类型统计值(部分数据包含‘万’)
return:
强转成float类型后的数据
'''
str = '万'
if str in s:
return float(s[0:-1])*10000 #切片 0:-1 包前不包后
else:
return float(s)
/<code>
<code># 分别对有可能存在‘万’字的数据进行修改调整
col = ['播放数','弹幕数','收藏数']
for i in col:
# 使用map方法将上述所表示的三列数据进行数值调整
data[i] = data[i].map(f)
print(data.info())
/<code>
新实战项目-机器学习-纪录片播放量预测

可以看出,播放数量明显属于连续型数值

<code>data['播放数'].value_counts()
/<code>
新实战项目-机器学习-纪录片播放量预测

可以发现数据之间的差距还是非常大的

<code>data['播放数'].map(lambda x: float(x))
data['播放数'].plot('kde')
plt.show()
/<code>
新实战项目-机器学习-纪录片播放量预测

<code>data.describe() 
/<code>
新实战项目-机器学习-纪录片播放量预测

2.2对简介信息进行数据挖掘

中文分词操作

<code>#使用精确分词模式,将特征中的信息进行中文分词,简介中的信息是一个列表
data['简介'] = data['简介'].map(lambda x:jieba.lcut(x))
# 返回的是一个列表, 后续使用特征词进行处理,认为一个字的分词形式对于后续处理模型没有作用,删除掉
data['简介'] = data['简介'].map(lambda x:[i for i in x if len(i) > 1])
data['简介'].head()
/<code>
新实战项目-机器学习-纪录片播放量预测

停用词处理: 在jieba处理后的词语,有一些是对模型不适用的词语,所以要进行去除,利用之前做好的停用词表进行处理

<code># 分隔符\\n\\t 以及常见单词
stop = pd.read_csv('./datas/stopwords.txt',sep='\\n\\t',engine='python',encoding='utf-8')
stop.head()
/<code>
新实战项目-机器学习-纪录片播放量预测

去除特征中的停用词 将数据转化为可以使用词频统计的方式

<code># 如果特征中的单词不属于停用词,进行保留,否则去除
data['简介'] = data['简介'].map(lambda x:[i for i in x if i not in stop])
# 将处理后的特征拼接成一个字符串,每个单词之间使用空格连接
data['简介'] = data['简介'].map(lambda x:' '.join(i for i in x))
data['简介'].head()
/<code>

加空格是为了配合TF-IDF的英文特点

新实战项目-机器学习-纪录片播放量预测

<code># 将所有样本单词转化成一个列表
list_data = data['简介'].tolist() # 首先转化为一个列表 变成list结构 每个元素是一句话

list_data
/<code>
新实战项目-机器学习-纪录片播放量预测

<code>wordcloud = WordCloud(background_color="white",width=1000, height=860, margin=2).generate(list_data[1])
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
/<code>
新实战项目-机器学习-纪录片播放量预测

三、数据预处理

词频处理

<code>x1 = CountVectorizer().fit_transform(list_data)
x1.shape
/<code>
<code>(1186, 12893)//(数据量,特征)
/<code>

词频中特征过多,复杂度较高进行降维处理

<code>pc = TruncatedSVD(n_components=10)   # 设置降为10个维度
x1 = pc.fit_transform(x1)
x1
/<code>
新实战项目-机器学习-纪录片播放量预测

去除简介列

<code>del data['简介']
print('去除简介列')
/<code>

四、模型创建及评估

提取特征和标签

<code># 标签
target = ['播放数']
# 特征
feature = [x for x in data.columns if x not in target]
/<code>

对标签值进行对数处理,原因详见PPT

<code>data[target] = np.log(data[target])
/<code>
<code># 数据进行标准化处理
# 一次性切片多列
data[feature] = StandardScaler().fit_transform(data[feature])
/<code>

对特征和标签进行切片处理

<code>y = data.loc[:,target]
x2 = data.loc[:,feature].as_matrix()
print(x2.shape)
# 合并数据 x1就是简介后的数据 (向量化)
x = np.concatenate((x1,x2),axis=1)
print(x.shape)
/<code>
<code>(1186, 7)
(1186, 17)//17个特征
/<code>
<code># 划分数据集
trainx,testx,trainy,testy = train_test_split(x,y,test_size=0.3,random_state=123)
print(trainx.shape)
print(testx.shape)
/<code>
<code>(830, 17)
(356, 17)
/<code>

线性模型预测效果

<code>l2 = Ridge()
param_grid = {'alpha': [0.1, 1, 10, 100, 1000, 10000]}
model = GridSearchCV(l2, param_grid=param_grid, cv=10)
model.fit(trainx, trainy)
print(model.best_params_)
print(model.score(testx, testy))
/<code>

这个LR线性回归模型,效果就很辣鸡了!!

<code>{'alpha': 1000}
0.3317827436601407
/<code>

随机森林模型预测效果

<code>rf = RandomForestRegressor()

param_grid = {

'n_estimators': [10, 100, 100, 500],
'max_depth': [2, 3, 4, 5, 6, 7]
}
model = GridSearchCV(rf, param_grid=param_grid, cv=10)//网格搜索交叉验证
model.fit(trainx, trainy)
print(model.best_params_)
print(model.score(testx, testy))
/<code>
<code>{'max_depth': 7, 'n_estimators': 500}
0.8476172334620827
/<code>

GBDT模型预测效果

<code>gbdt = GradientBoostingRegressor()

param_grid = {

'n_estimators': [10, 100, 100, 500, 1000],
'max_depth': [2, 3, 4, 5]
}
model = GridSearchCV(gbdt, param_grid=param_grid, cv=10)
model.fit(trainx, trainy)
print(model.best_params_)
print(model.score(testx, testy))
/<code>
<code>{'max_depth': 5, 'n_estimators': 1000}
0.8522771940332649
/<code>

注意上面的模型的调参,举个例子,最大深度设置是5,实际也是5,那得放大设计的参数!

随机森林和GBDT你选哪个? 当然是随机森林啦,在实际的项目部署中随机森林可以分布式部署,多棵树的扩容改造!!

私聊我给你发代码!!!!


分享到:


相關文章: