评估 R^2 分数时出现错误

Getting errors while evalute R^2 score

我有超过 2000 rows23 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()

错误在这一行:

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))

注意末尾有两个右括号