Reputation: 31
New to machine learning
I am trying to figure out the accuracy score
of a linear model using accuracy_score(y_test,y_pred)
.both the variables are defined. But getting error
'name 'y_test' is not defined'.
Can anyone help me with this? variables are defined like this:
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=123)
y_pred = linreg.predict(X_test)
detailed error message.. NameError Traceback (most recent call last) in () 1 #for checking the accuracy and details 2 from sklearn.metrics import accuracy_score ----> 3 accuracy_score(y_test,y_pred)
NameError: name 'y_test' is not defined
Keeping code here...
#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)
Upvotes: 0
Views: 20919
Reputation: 13401
You have defined:
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2,random_state=123)
inside the train_test_rmse()
function. That's why scope of these variable (y_test
and y_pred
) are inside the function only.
They can not be used outside the function.
Use below code:
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import accuracy_score
#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)
print(accuracy_score(y_test, y_pred)) # or you can save it in variable and return it
return np.sqrt(metrics.mean_squared_error(y_test, y_pred))
Upvotes: 2