警告出現(xiàn)于在使用sklearn中的MLPClassifier(多層神經(jīng)網(wǎng)絡(luò)分類(lèi)器)中
完整警告信息:
/usr/local/lib/python3.9/site-packages/sklearn/base.py:443: UserWarning: X has feature names, but MLPClassifier was fitted without feature names
warnings.warn(
警告部分代碼:
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
# Scale the data
def scale(x):
scaler = StandardScaler()
scaler.fit(x)
x = scaler.transform(x)
return x
# 3 Multi-layer Perceptron Classifier
def MLPClf(x, y):
x = scale(x)
clf = MLPClassifier(solver='sgd', alpha=1e-5, max_iter=400, hidden_layer_sizes=(5,), random_state=1)
clf = clf.fit(x, y)
return clf
分析和解決辦法:因?yàn)镸LPClassifier需要包含feature names的輸入變量,但是x由于經(jīng)過(guò)了StandardScaler()的feature scaling,導(dǎo)致其被轉(zhuǎn)化為了array格式,也就不存在feature names了。因此解決方法也很直觀,再把scaling后的x轉(zhuǎn)回dataframe就可以了,唯一要注意的是要提前把columns儲(chǔ)存起來(lái)。
修改后代碼:
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
# Scale the data
def scale(x):
# store columns in advance
cols = x.columns
scaler = StandardScaler()
scaler.fit(x)
x = scaler.transform(x)
# avoid warning, transform it back to df
x = pd.DataFrame(x, columns=cols)
return x
# 3 Multi-layer Perceptron Classifier
def MLPClf(x, y):
x = scale(x)
clf = MLPClassifier(solver='sgd', alpha=1e-5, max_iter=400, hidden_layer_sizes=(5,), random_state=1)
clf = clf.fit(x, y)
return clf