收到错误“未定义名称'y_test'”

机器学习的新手,我试图找出accuracy score使用线性模型accuracy_score(y_test,y_pred)的定义。但是出现错误


“名称'y_test'未定义”。


谁能帮我这个?变量的定义如下:


X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=123)

y_pred = linreg.predict(X_test)

详细的错误消息。NameError Traceback(最近一次通话最后一次)在()1中用于检查准确性和详细信息2从sklearn.metrics导入precision_score ----> 3 precision_score(y_test,y_pred)


NameError:名称“ y_test”未定义


在这里保存代码...


#creating a function for models

from sklearn.model_selection import train_test_split

from sklearn import metrics

#function

def train_test_rmse(x,y):

    x = Iris_data[x]

    y = Iris_data[y]

    X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2,random_state=123)

    linreg = LinearRegression()

    linreg.fit(X_train, y_train)

    y_pred = linreg.predict(X_test)

    return np.sqrt(metrics.mean_squared_error(y_test, y_pred))


print(train_test_rmse(['Sepal.Length'],['Sepal.Width']))

print(train_test_rmse(['Petal.Length'],['Sepal.Width']))

print(train_test_rmse(['Sepal.Length'],['Petal.Width']))

print(train_test_rmse(['Petal.Length'],['Petal.Width'])) #this one has least rmse

print(train_test_rmse(['Sepal.Width'],['Sepal.Length']))

print(train_test_rmse(['Petal.Width'],['Sepal.Width']))


#for checking the accuracy and details

from sklearn.metrics import accuracy_score

accuracy_score(y_test,y_pred)


繁花不似锦
浏览 280回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python