1.题干
国产电视剧的大众评分预测:基于某段时间国产电视剧的播放量和大众评分数据(电视剧播放数据.xlsx),采用K-近邻法,通过观众给出的点赞数和差评数,对电视剧的大众评分进行回归预测,并与一般线性回归模型进行对比。读入电视剧播放数据到数据框,并删除缺失数据;仅对点赞数和差评数低于200万以下的电视剧进行分析;分割数据为训练集和测试集;建立K在1-30步长为2的取值的K-近邻回归模型,计算测试误差,获得测试误差最小下的参数K;建立基于最优参数的K-近邻回归模型,训练模型,计算测试误差和预测误差;建立线性回归模型,训练模型,计算测试误差和预测误差。
2.数据格式
3.代码
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
def printf(n, strf):
print()
print('-' * n)
print(f"\033[1m{strf}\033[0m")
print()
data = pd.read_excel('电视剧播放数据.xlsx')
# print(data)
printf(100, '查看是否有缺失值')
print(data.isnull().sum())
data = data.dropna()
printf(100, '删除缺失值之后的数据分布情况')
print(data.isnull().sum())
printf(100, '回归分析')
filtered_data = data[(data['点赞'] < 2000000) & (data['差评'] < 2000000)]
X = filtered_data[['点赞', '差评']]
y = filtered_data['得分']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
k_range = range(1, 31, 2)
errors = []
for k in k_range:
knn = KNeighborsRegressor(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
error = mean_squared_error(y_test, y_pred)
errors.append(error)
optimal_k = k_range[np.argmin(errors)]
print(f"最优参数K: {optimal_k}")
plt.plot(k_range, errors, marker='o')
plt.xlabel('K值')
plt.ylabel('均方误差')
plt.title('K值与测试均方误差')
plt.show()
knn_optimal = KNeighborsRegressor(n_neighbors=optimal_k)
knn_optimal.fit(X_train, y_train)
y_pred_knn = knn_optimal.predict(X_test)
test_error_knn = mean_squared_error(y_test, y_pred_knn)
print(f"K-近邻回归模型的测试误差: {test_error_knn}")
lr = LinearRegression()
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
test_error_lr = mean_squared_error(y_test, y_pred_lr)
print(f"线性回归模型的测试误差: {test_error_lr}")