Wandondi
Wandondi

Reputation: 3

Key Error : 1 while testing my testing samples and trying to print out the MSE

I am practicing Using MLP to simulate the 10x10 table. I thought I had figured it out but I'm getting a KeyError:1 in the section below.

I am unable to figure out what I need to change to get the correct output. [This is the output on this particular chunk][1]

[These are the values for y_test and y_hat][2]

y_pred = mlp.predict(X_test) 
for i in range(len(y_test)):
    print("y_test: {}    y_prediction: {}".format(y_test[i], y_pred[i]))
    
# find MSE
mse = mean_squared_error(y_test, y_pred)
print("MSE: {}".format(mse))

# y_hat is the int type of y_pred after rounding, use y_hat and y values to find new mse. 
print("\n\nAfter Rounding off...") 
y_hat = np.around(y_pred, decimals=2) 
for i in range(len(y_test)):
    print("y_test: {}    y_hat: {}".format(y_test[i], y_hat[i]))
    
# new MSE
new_mse = mean_squared_error(y_test, y_hat)
print("New MSE: {}".format(new_mse)) ```


  [1]: https://i.sstatic.net/LXFwm.png
  [2]: https://i.sstatic.net/5ofWe.png

Upvotes: 0

Views: 240

Answers (1)

mrCatlost
mrCatlost

Reputation: 236

Error fast alaysis

KeyError: 1 notices that the value that is related with 1 is not possible to acced to it, so y_test[1] or y_pred[1] does not exist. The strangest thing is that you could print y_hat all values.

The problem can be related with your setters before the predict function, cause ML functions are so sensitive.

Example where it works correctly

I execute a code predicting a random group of data generated by make_classification to print the values, as i don´t know how you trained your data and fitted your model, but with that example it shows the values properly.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier

# Simulate train / test / validation sets
X, y = make_classification(n_samples=1000)
X_train, X_hold, y_train, y_hold = train_test_split(X, y, train_size=.6)
X_valid, X_test, y_valid, y_test = train_test_split(X_hold, y_hold, train_size=.5)

# Initialize
clf = MLPClassifier()
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
for i in range(len(y_test)):
    print("id: {} y_test: {}    y_prediction: {}".format(i, y_test[i], y_pred[i]))

It shows the list of test values and prediction values.

Values printed

Upvotes: 0

Related Questions