Python Forum

Full Version: Code used to work, not anymore for no apparent reason?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am taking Microsoft Professional Program for Artificial Intelligence. This block of code used to work fine. After I tried to insert some new code before it, I start to get the NameError message. I know it is not valid because it worked before. I tried to shut down the server, log out and in a few times. Still getting it. Please help!

scaler = preprocessing.StandardScaler().fit(X_train[:,20:])
X_train[:,20:] = scaler.transform(X_train[:,20:])
X_test[:,20:] = scaler.transform(X_test[:,20:])
X_train[:2,]

NameErrorTraceback (most recent call last)
<ipython-input-4-8e772dc33ff2> in <module>()
2 ## Notice that the scaler is fit only on the training data. The trained scaler is then applied to the test data.
3 ## Test data should always be scaled using the parameters from the training data
----> 4 scaler = preprocessing.StandardScaler().fit(X_train[:,20:])
5 X_train[:,20:] = scaler.transform(X_train[:,20:])
6 X_test[:,20:] = scaler.transform(X_test[:,20:])

NameError: name 'preprocessing' is not defined
I would clear all outputs, restart the Kernal, and start the notebook from the top. The kernal has apparently forgotten that you imported preprocessing from sklearn.
Or, redo the import at the top of that cell, but this suggests that between sessions it lost references.