【问题标题】:UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names用户警告:X 没有有效的特征名称,但 LogisticRegression 配备了特征名称
【发布时间】:2022-10-07 16:20:44
【问题描述】:

我在 Flask 中编写了一个程序来获取用户的输入以输入长度和宽度来预测鱼的类型,但是一旦我输入它就会显示一个错误,称为

UserWarning: X does not have valid feature names, but LogisticRegression was fitted 
with feature names
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

df=pd.read_csv(\'Fish.csv\')
df.head()

X = df.drop(\'Species\', axis=1)
y = df[\'Species\']

cols = X.columns
index = X.index

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)

from sklearn.ensemble import RandomForestClassifier
random=RandomForestClassifier()
random.fit(X_train,y_train)
y_pred=random.predict(X_test)

from sklearn.metrics import accuracy_score
score=accuracy_score(y_test,y_pred)

# Create a Pickle file  
import pickle
pickle_out = open(\"model.pkl\",\"wb\")
pickle.dump(logistic_model, pickle_out)
pickle_out.close()

logistic_model.predict([[242.0,23.2,25.4,30.0,11.5200,4.0200]])

import numpy as np
import pickle
import pandas as pd
from flask import Flask, request, jsonify, render_template

app=Flask(__name__)
pickle_in = open(\"model.pkl\",\"rb\")
random = pickle.load(pickle_in)

@app.route(\'/\')
def home():
    return render_template(\'index.html\')


@app.route(\'/predict\',methods=[\"POST\"])
def predict():
    \"\"\"
    For rendering results on HTML GUI
    \"\"\"
    int_features = [x for x in request.form.values()]
    final_features = [np.array(int_features)]
    prediction = random.predict(final_features)
    return render_template(\'index.html\', prediction_text = \'The fish belongs to species {}\'.format(str(prediction)))

if __name__==\'__main__\':
    app.run()

数据集 https://www.kaggle.com/datasets/aungpyaeap/fish-market

    标签: python flask machine-learning scikit-learn


    【解决方案1】:

    您的 X 和 y 是熊猫数据框。在将其拟合到随机森林分类器之前,使其成为一个 numpy 数组,例如,

    X = X.values
    y = y.values
    

    在此之后进行火车测试拆分,

    from sklearn.model_selection import train_test_split
    X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)
    

    现在拟合模型(代码与下面的代码相同),

    from sklearn.ensemble import RandomForestClassifier
    random = RandomForestClassifier()
    random.fit(X_train,y_train)
    y_pred=random.predict(X_test)
    

    在烧瓶应用程序中,您在 numpy 数组中提供输入,但在训练期间您有 pandas 数据框,这就是引发该警告的原因。现在,它应该可以正常工作了!

    【讨论】:

      【解决方案2】:

      我也面临同样的警告: UserWarning: X 没有有效的特征名称,但 LogisticRegression 配备了特征名称。

      此警告实际上是在 model.fit() 期间将数据拟合到我们的模型时说,dataframe X_train 具有属性名称,但是当您尝试使用转换为行向量的数据框或 numpy 数组进行预测时,您没有提供特征/属性名称您要对其进行预测的元组。

      为了清楚地理解我的意思,请看下面的示例图片:

      希望这可以帮助初学者同时通过模型对看不见的数据进行预测

      【讨论】:

        猜你喜欢
        • 2022-11-25
        • 2022-11-27
        • 2022-11-07
        • 1970-01-01
        • 2022-01-19
        • 2017-11-14
        • 2012-12-17
        • 2020-03-01
        相关资源
        最近更新 更多