对tmdb电影榜单数据集进行可视化分析和建立模型进行评分预测的具体代码实现
时间: 2023-09-20 09:10:06 浏览: 62
以下是对tmdb电影榜单数据集进行可视化分析和建立模型进行评分预测的具体代码实现:
1. 数据准备和清洗
```python
# 导入相关库
import pandas as pd
import numpy as np
# 读取数据集
df = pd.read_csv('tmdb_5000_movies.csv')
# 选择需要的特征
features = ['budget', 'genres', 'homepage', 'id', 'keywords', 'original_language', 'original_title',
'overview', 'popularity', 'production_companies', 'production_countries', 'release_date',
'revenue', 'runtime', 'spoken_languages', 'status', 'tagline', 'title', 'vote_average', 'vote_count']
df = df[features]
# 进行数据清洗
df = df.dropna() # 删除空值行
df = df[df['status'] == 'Released'] # 只保留已发布的电影
df = df[df['vote_count'] >= 500] # 只保留投票数大于等于500的电影
```
2. 可视化分析
```python
# 导入相关库
import matplotlib.pyplot as plt
import seaborn as sns
# 绘制电影类型分布图
genres = df['genres'].str.split('|', expand=True).stack().value_counts()
plt.figure(figsize=(10, 6))
sns.barplot(x=genres.values, y=genres.index, palette='rocket')
plt.title('Genres Distribution')
plt.xlabel('Count')
plt.ylabel('Genres')
# 绘制投票人数分布图
plt.figure(figsize=(10, 6))
sns.distplot(df['vote_count'], kde=False, color='g')
plt.title('Vote Count Distribution')
plt.xlabel('Vote Count')
plt.ylabel('Frequency')
# 绘制发行年份分布图
df['release_year'] = pd.DatetimeIndex(df['release_date']).year
year_counts = df['release_year'].value_counts().sort_index()
plt.figure(figsize=(10, 6))
sns.barplot(x=year_counts.index, y=year_counts.values, color='b')
plt.title('Release Year Distribution')
plt.xlabel('Year')
plt.ylabel('Count')
```
3. 特征工程
```python
# 对电影类型进行独热编码
genres = df['genres'].str.get_dummies('|')
df = pd.concat([df, genres], axis=1)
# 对发行日期进行时间特征提取
df['year'] = pd.DatetimeIndex(df['release_date']).year
df['month'] = pd.DatetimeIndex(df['release_date']).month
df['day'] = pd.DatetimeIndex(df['release_date']).day
df = df.drop('release_date', axis=1)
```
4. 建立模型
```python
# 导入相关库
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
# 分离自变量和因变量
X = df.drop(['vote_average', 'title', 'homepage', 'keywords', 'original_title', 'overview', 'production_companies',
'production_countries', 'spoken_languages', 'status', 'tagline', 'genres'], axis=1)
y = df['vote_average']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 建立线性回归模型
lr = LinearRegression()
lr.fit(X_train, y_train)
# 预测测试集结果
y_pred = lr.predict(X_test)
# 评估模型
r2 = r2_score(y_test, y_pred)
print('R-squared: %.2f' % r2)
```
5. 模型预测
```python
# 对新数据进行评分预测
new_data = pd.DataFrame({'budget': [10000000], 'original_language': ['en'], 'popularity': [10.0], 'revenue': [50000000],
'runtime': [120], 'year': [2020], 'month': [1], 'day': [1], 'Action': [0], 'Adventure': [1],
'Animation': [0], 'Comedy': [0], 'Crime': [0], 'Documentary': [0], 'Drama': [0], 'Family': [0],
'Fantasy': [0], 'Foreign': [0], 'History': [0], 'Horror': [0], 'Music': [0], 'Mystery': [0],
'Romance': [0], 'Science Fiction': [0], 'TV Movie': [0], 'Thriller': [0], 'War': [0], 'Western': [0]})
y_pred_new = lr.predict(new_data)
print('Predicted Vote Average:', y_pred_new[0])
```
通过以上代码实现,可以对tmdb电影榜单数据集进行可视化分析和建立模型进行评分预测。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)