gpt4 book ai didi

python - Scikit-Learn SVR 预测总是给出相同的值

转载 作者:太空狗 更新时间:2023-10-29 23:57:38 28 4
gpt4 key购买 nike

我即将使用 Scikit-Learn 中的支持向量回归来预测 IMDB 评分(电影放映率)。问题是它总是为每个输入给出相同的预测结果。

当我使用数据训练进行预测时,它会给出各种结果。但是在使用数据测试时,总是给出相同的值。

数据训练预测:

Data training prediction

数据测试预测:

Data testing prediction

这是数据集的链接:IMDB 5000 Movie Dataset

我的代码:

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import numpy as np
import seaborn as sb
from sklearn import metrics as met


df = pd.read_csv("movie_metadata.csv")
df.head()


original = df.shape[0]
df = df.drop_duplicates(["movie_title"])
notDuplicated = df.shape[0]
df.reset_index(drop = True, inplace = True)
print(original, notDuplicated)


df["num_critic_for_reviews"].fillna(0, inplace = True)
df["num_critic_for_reviews"] = df["num_critic_for_reviews"].astype("int")

df["director_facebook_likes"].fillna(0, inplace = True)
df["director_facebook_likes"] = df["director_facebook_likes"].astype("int")

df["actor_3_facebook_likes"].fillna(0, inplace = True)
df["actor_3_facebook_likes"] = df["actor_3_facebook_likes"].astype(np.int64)

df["actor_2_facebook_likes"].fillna(0, inplace = True)
df["actor_2_facebook_likes"] = df["actor_2_facebook_likes"].astype(np.int64)

df["actor_1_facebook_likes"].fillna(0, inplace = True)
df["actor_1_facebook_likes"] = df["actor_1_facebook_likes"].astype(np.int64)

df["movie_facebook_likes"].fillna(0, inplace = True)
df["movie_facebook_likes"] = df["movie_facebook_likes"].astype(np.int64)

df["content_rating"].fillna("Not Rated", inplace = True)
df["content_rating"].replace('-', "Not Rated", inplace = True)
df["content_rating"] = df["content_rating"].astype("str")

df["imdb_score"].fillna(0.0, inplace = True)

df["title_year"].fillna(0, inplace = True)
df["title_year"].replace("NA", 0, inplace = True)
df["title_year"] = df["title_year"].astype("int")

df["genres"].fillna("", inplace = True)
df["genres"] = df["genres"].astype("str")


df2 = df[df["title_year"] >= 1980]
df2.reset_index(drop = True, inplace = True)

nRow = len(df2)
print("Number of data:", nRow)
nTrain = np.int64(np.floor(0.7 * nRow))
nTest = nRow - nTrain
print("Number of data training (70%):", nTrain, "\nNumber of data testing (30%):", nTest)

dataTraining = df2[0:nTrain]
dataTesting = df2[nTrain:nRow]
dataTraining.reset_index(drop = True, inplace = True)
dataTesting.reset_index(drop = True, inplace = True)


xTrain = dataTraining[["num_critic_for_reviews", "director_facebook_likes", "actor_3_facebook_likes", "actor_2_facebook_likes", "actor_1_facebook_likes", "movie_facebook_likes"]]
yTrain = dataTraining["imdb_score"]

xTest = dataTesting[["num_critic_for_reviews", "director_facebook_likes", "actor_3_facebook_likes", "actor_2_facebook_likes", "actor_1_facebook_likes", "movie_facebook_likes"]]
yTest = dataTesting["imdb_score"]

movieTitle = dataTesting["movie_title"].reset_index(drop = True)


from sklearn.svm import SVR

svrModel = SVR(kernel = "rbf", C = 1e3, gamma = 0.1, epsilon = 0.1)
svrModel.fit(xTrain,yTrain)


predicted = svrModel.predict(xTest)
[print(movieTitle[i], ":", predicted[i]) for i in range(10)]

最佳答案

gamma0.1 更改为 1e-8,同时保持其他一切不变。

当 gamma 设置为 0.1 时,唯一预测的数量为 8,它们都接近 6.37。当 gamma 设置为 1e-8 时,输出 1366 个唯一预测(xTest 包含 1368 个样本)。

为什么 Gamma 很重要?

Intuitively, the gamma parameter defines how far the influence of a single training example reaches, with low values meaning ‘far’ and high values meaning ‘close’. The gamma parameters can be seen as the inverse of the radius of influence of samples selected by the model as support vectors.

RBF SVM Parameters 有更深入的解释和示例.

这里也有类似的解释: Output of Scikit SVM in multiclass classification always gives same label

就个人而言,我会在您的脚本底部使用 GridSearchCV。下面是一个寻找理想 gammaC 值的示例:

from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV

#svrModel = SVR(kernel = "rbf", C = 1e3, gamma = 1e-8, epsilon = 0.1)
#svrModel.fit(xTrain,yTrain)


#predicted = svrModel.predict(xTest)
#[print(movieTitle[i], ":", predicted[i]) for i in range(10)]

#print('Unique predictions:', np.unique(predicted))

parameters = {
"kernel": ["rbf"],
"C": [1,10,10,100,1000],
"gamma": [1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1]
}

grid = GridSearchCV(SVR(), parameters, cv=5, verbose=2)
grid.fit(xTrain, yTrain)

关于python - Scikit-Learn SVR 预测总是给出相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com