How to use machine learning models for data prediction in FastAPI
Introduction:
With the development of machine learning, more and more application scenarios require the integration of machine learning models into actual systems. . FastAPI is a high-performance Python web framework based on an asynchronous programming framework. It provides a simple and easy-to-use API development method and is very suitable for building machine learning prediction services. This article will introduce how to use machine learning models for data prediction in FastAPI and provide relevant code examples.
Part One: Preparation
Before we start, we need to complete some preparations.
pip install fastapi pip install uvicorn pip install scikit-learn
from sklearn.linear_model import LinearRegression import numpy as np # 构建模型 model = LinearRegression() # 准备训练数据 X_train = np.array(...).reshape(-1, 1) # 输入特征 y_train = np.array(...) # 目标变量 # 训练模型 model.fit(X_train, y_train)
Part 2: Building the FastAPI application
After the preparation work is completed, we can start building the FastAPI application.
from fastapi import FastAPI from pydantic import BaseModel # 导入模型 from sklearn.linear_model import LinearRegression
class InputData(BaseModel): input_value: float class OutputData(BaseModel): output_value: float
app = FastAPI()
POST
method to handle the data prediction request and InputData
as the input data for the request. @app.post('/predict') async def predict(input_data: InputData): # 调用模型进行预测 input_value = input_data.input_value output_value = model.predict([[input_value]]) # 构造输出数据 output_data = OutputData(output_value=output_value[0]) return output_data
Part 3: Running the FastAPI application
After completing the construction of the FastAPI application, we can run the application and test the data prediction function.
uvicorn main:app --reload
POST
request to http://localhost:8000/predict
, and pass an input_value
parameter in the request body. For example, send the following request body:
{ "input_value": 5.0 }
{ "output_value": 10.0 }
Conclusion:
This article introduces how to use machine learning models in FastAPI for data prediction. By following the guidance in this article, you can easily integrate your own machine learning model into your FastAPI application and provide prediction services.
Sample code:
from fastapi import FastAPI from pydantic import BaseModel from sklearn.linear_model import LinearRegression import numpy as np # 创建模型和训练数据 model = LinearRegression() X_train = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) y_train = np.array([2, 4, 6, 8, 10]) model.fit(X_train, y_train) # 定义输入输出数据模型 class InputData(BaseModel): input_value: float class OutputData(BaseModel): output_value: float # 创建FastAPI应用实例 app = FastAPI() # 定义数据预测的路由 @app.post('/predict') async def predict(input_data: InputData): input_value = input_data.input_value output_value = model.predict([[input_value]]) output_data = OutputData(output_value=output_value[0]) return output_data
I hope that through the introduction and sample code of this article, you can successfully use machine learning models for data prediction in FastAPI. I wish you success!
The above is the detailed content of How to use machine learning models for data prediction in FastAPI. For more information, please follow other related articles on the PHP Chinese website!