Sklearn警告解决办法: UserWarning: X ha
2022-01-29 本文已影响0人
抹茶口味注心饼干
警告出现于在使用sklearn中的MLPClassifier(多层神经网络分类器)中
完整警告信息:
/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
分析和解决办法:因为MLPClassifier需要包含feature names的输入变量,但是x由于经过了StandardScaler()的feature scaling,导致其被转化为了array格式,也就不存在feature names了。因此解决方法也很直观,再把scaling后的x转回dataframe就可以了,唯一要注意的是要提前把columns储存起来。
修改后代码:
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