cover

44. Mushroom Classification Model Deployment and Inference#

44.1. Introduction#

This challenge focuses on the saving and deployment of a scikit-learn trained model. You need to complete the online deployment of the model as required and enable it to perform inference on new data.

44.2. Key Points#

  • Poisonous Mushroom Classification Prediction

  • Model Deployment and Inference

The challenge selects the Mushroom Classification Dataset provided by UCI Machine Learning. It has collected 8,124 mushroom samples, including various physical characteristics of these mushrooms, such as smell, size, color, etc. Finally, these samples are labeled into 2 categories: edible and poisonous.

Image Description

The challenge sampled 8,000 pieces of data from the original dataset. You can directly preview these samples:

# 下载数据集
wget -nc https://cdn.aibydoing.com/aibydoing/files/mushrooms.csv
import pandas as pd

# 挑战所需训练数据集,复制链接粘贴到浏览器即可下载
df = pd.read_csv("mushrooms.csv")
df.head()
class cap-shape cap-surface cap-color bruises odor gill-attachment gill-spacing gill-size gill-color ... stalk-surface-below-ring stalk-color-above-ring stalk-color-below-ring veil-type veil-color ring-number ring-type spore-print-color population habitat
0 p x s w f c f c n p ... s w w p w o p n s d
1 p x s e f s f c n b ... k w w p w o e w v p
2 p k s e f y f c n b ... s p p p w o e w v d
3 p f f g f f f c b p ... k b n p w o l h y g
4 e f f n f n f w b h ... s w w p w o e k s g

5 rows × 23 columns

Among them, class=e indicates edible, and class=p indicates poisonous. The remaining columns are feature data.

Next, you need to use this dataset to train a poisonous mushroom classifier and deploy the saved model as an API interface using Flask. The inference results can be obtained through HTTP requests.

Exercise 44.1

Challenge: Refer to the Titanic survival prediction model in the previous experiment, train a poisonous mushroom classifier, and complete the model deployment.

Regulation: There are no restrictions on data feature processing, algorithm selection, etc. You can give full play to your creativity, as long as you can finally obtain the result of whether the mushroom is poisonous or not.

Challenge Test Instructions

This challenge is recommended to be completed offline. At the same time, you need to use scikit-learn to train and save the model, and finally use Flask to complete the construction of the Web application. After starting Flask, you can send a POST request to localhost locally to obtain the inference results. When testing, it is recommended to use the samples in the original dataset, and the input data needs to be in JSON format.

The test sample code is as follows:

wget -nc https://cdn.aibydoing.com/aibydoing/files/mushrooms_test.csv
import json
import requests
import pandas as pd

df = pd.read_csv("mushrooms_test.csv")  # Read the test dataset
sample_data = df.sample(1).to_json()  # Randomly select 1 sample from the original data for testing inference and convert it to JSON format
sample_json = json.loads(sample_data)  # Process the JSON-formatted data converted by Pandas into JSON type
requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request

Expected output

The content and format returned by the request can be customized, but at least the inference category result needs to be returned.

# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# 加载数据集
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# 模型训练和保存
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # 读取特征并独热编码
y = df['class']  # 目标值

model = RandomForestClassifier()  # 随机森林
print(cross_val_score(model, X, y, cv=5).mean())  # 交叉验证结果

model.fit(X, y)  # 训练模型
joblib.dump(model, "mushrooms.pkl")  # 保存模型
print("model saved.")
# 构建 Flask Web
%%writefile predict.py
# 将此单元格代码写入 predict.py 文件方便后面执行
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # 请求方法为 POST
def inference():
    query_df = pd.DataFrame(request.json)  # 将 JSON 变为 DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # 读取数据
    X = pd.get_dummies(df.iloc[:, 1:])  # 读取特征并独热编码
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # 将请求数据 DataFrame 处理成独热编码样式
    
    clf = joblib.load('mushrooms.pkl')  # 加载模型
    prediction = clf.predict(query)  # 模型推理
    return jsonify({"prediction": list(prediction)})  # 返回推理结果
# Notebook 中必须以子进程才能正常启动 Flask
import time
import subprocess as sp

# 启动子进程执行 Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # 等待 5 秒保证 Flask 启动成功
server
import json

# 从测试数据中取 1 条用于测试推理
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # 建立 POST 请求,并发送数据请求
server.terminate()  # 结束子进程,关闭端口占用
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample_data)
sample_json
import requests

requests.post(url="http://localhost:5000", json=sample_json).content  # Establish a POST request and send a data request
server.terminate()  # Terminate the subprocess and release the port occupation
# Load the dataset
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("mushrooms.csv")
df.tail()
# Model training and saving
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import joblib

X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
y = df['class']  # Target value

model = RandomForestClassifier()  # Random Forest
print(cross_val_score(model, X, y, cv=5).mean())  # Cross-validation result

model.fit(X, y)  # Train the model
joblib.dump(model, "mushrooms.pkl")  # Save the model
print("model saved.")
# Build Flask Web
%%writefile predict.py
# Write the code in this cell to the predict.py file for later execution
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/", methods=["POST"])  # Request method is POST
def inference():
    query_df = pd.DataFrame(request.json)  # Convert JSON to DataFrame
    
    df = pd.read_csv("mushrooms.csv")  # Read data
    X = pd.get_dummies(df.iloc[:, 1:])  # Read features and perform one-hot encoding
    query = pd.get_dummies(query_df).reindex(columns=X.columns, fill_value=0)  # Process the request data DataFrame into one-hot encoded format
    
    clf = joblib.load('mushrooms.pkl')  # Load the model
    prediction = clf.predict(query)  # Model inference
    return jsonify({"prediction": list(prediction)})  # Return the inference result
# In the notebook, Flask must be started as a subprocess to work properly
import time
import subprocess as sp

# Start a subprocess to execute the Flask app
server = sp.Popen("FLASK_APP=predict.py flask run", shell=True)
time.sleep(5)  # Wait for 5 seconds to ensure Flask starts successfully
server
import json

# Select 1 sample from the test data for testing inference
df_test = pd.read_csv("mushrooms_test.csv")
sample_data = df.sample(1).to_json(orient='records')
sample_json = json.loads(sample

○ Sharethis article link to your social media, blog, forum, etc. More external links will increase the search engine ranking of this site.