Sklearn警告解決辦法: UserWarning: X has feature names, but MLPClassifier was fitted without feature names

警告出現(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容