我在 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 https://www.kaggle.com/datasets/aungpyaeap/fish-market