评估 R^2 分数时出错
Posted
技术标签:
【中文标题】评估 R^2 分数时出错【英文标题】:Getting errors while evalute R^2 score 【发布时间】:2021-02-10 12:39:04 【问题描述】:我的数据集包含多个 2000 rows
和 23 columns
,包括 age column
。我已经完成了 SVR 的所有流程。早些时候,我使用了具有默认值的 SVR 模型。因此,我无法找到 r_scored 错误的最佳准确度。所以,现在我需要修改我的代码以找到输出最高 R_squared 值的最佳参数组合。为了获得最佳结果,我使用以下参数值进行搜索。
c = [0.01, 0.1, 1, 10, 100]
Gamma = [0.001, 0.01, 0.1, 1]
Epsilon = [0.01, 0.1, 1]
但我在以下几行中有一些错误
parameter['r_squared'] = parameter.apply(lambda x:fitsvm(c=x[0], gamma = x[1], epsilon = x[2] , X_train, y_train, X_test, y_test,axis=1)
parameter.sort_values('r_squared',ascending=False).head()
错误是:
SyntaxError: unexpected EOF while parsing
SyntaxError: invalid syntax
请帮我解决这个问题。
我的代码:
import pandas as pd
import numpy as np
# Make fake dataset
dataset = pd.DataFrame(data= np.random.rand(2000,22))
dataset['age'] = np.random.randint(2, size=2000)
# Separate the target from the other features
target = dataset['age']
data = dataset.drop('age', axis = 1)
# train_data, train_target = data.loc[:1000], target.loc[:1000] - alternate naming scheme
X_train, y_train = data.loc[:1000], target.loc[:1000]
# test_data, test_target = data.loc[1001], target.loc[1001] - alternate naming scheme
X_test, y_test = data.loc[1001], target.loc[1001]
X_test = np.array(X_test).reshape(1, -1)
print(X_test.shape)
c = [0.01, 0.1, 1, 10, 100]
Gamma = [0.001, 0.01, 0.1, 1]
Epsilon = [0.01, 0.1, 1]
c_,Gamma_,Ep_ = np.meshgrid(c,Gamma,Epsilon)
parameter = pd.DataFrame('c':c_.flatten(),'Gamma':Gamma_.flatten(),'Epsilon':Ep_.flatten())
def fitsvm(c,gamma,epsilon,X_train, y_train, X_test, y_test):
SupportVectorRefModel = SVR(C=c,gamma=gamma,epsilon=epsilon)
SupportVectorRefModel.fit(X_train, y_train)
R_Sqr = SupportVectorRefModel.score(X_test,y_test)
return R_Sqr
np.random.seed
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_boston
s = StandardScaler()
dataset, y = load_boston(return_X_y=True)
X = s.fit(dataset).transform(dataset)
X_train, y_train = X[:100], y[:100]
X_test, y_test = X[1001:], y[1001:]
parameter['r_squared'] = parameter.apply(lambda x:fitsvm(c=x[0], gamma = x[1], epsilon = x[2] , X_train, y_train, X_test, y_test,axis=1)
parameter.sort_values('r_squared',ascending=False).head()
【问题讨论】:
【参考方案1】:错误在这一行:
parameter['r_squared'] = parameter.apply(lambda x:fitsvm(c=x[0], gamma = x[1], epsilon = x[2] , X_train, y_train, X_test, y_test,axis=1)
您有两个左括号,但只有一个右括号。这样做:
parameter['r_squared'] = parameter.apply(lambda x:fitsvm(c=x[0], gamma = x[1], epsilon = x[2] , X_train, y_train, X_test, y_test,axis=1))
注意末尾的两个右括号
【讨论】:
但我仍然面临“语法错误:位置参数遵循关键字参数”@thisisjaymehta 改变参数的顺序,像这样:fitsvm(X_train, y_train, X_test, y_test, c=x[0], gamma = x[1], epsilon = x[2] , axis=1)
@Asif 请不要使用 cmets 来询问后续问题 - 所以不能这样工作。如果答案解决了您询问的特定问题,请accept it 并针对新问题提出新问题。以上是关于评估 R^2 分数时出错的主要内容,如果未能解决你的问题,请参考以下文章