机器学习:
计算机无需明确编程即可从经验中学习。机器学习是当前进入的热门领域之一,世界各地的顶级公司都在使用它来改善其服务和产品。但是没有使用在Jupyter Notebook中训练的机器学习模型。因此,我们需要部署这些模型,以便每个人都可以使用它们。在本文中,我们将首先训练Iris Species分类器,然后使用Streamlit部署模型,Streamlit是用于轻松部署ML模型的开源应用程序框架。
Streamlit库:
Streamlit使您可以使用简单的python脚本为您的机器学习项目创建应用程序。它还支持热重载,因此您的应用可以在编辑和保存文件时实时更新。使用Streamlit API只能用几行代码(如下所示)构建应用程序。添加小部件与声明变量相同。无需编写后端,定义不同的路由或处理HTTP请求。易于部署和管理。可以在其网站上找到更多信息– https://www.streamlit.io/
所以首先我们将训练我们的模型。我们将不做太多预处理,因为本文的主要目的不是建立准确的ML模型而是显示其部署。
首先,我们需要安装以下内容–
pip install熊猫
pip install numpy
pip install sklearn
pip install streamlit
码:
filter_none
play_arrow
亮度_4
import pandas as pd
import numpy as np
df = pd.read_csv('BankNote_Authentication.csv')
df.head()
输出:
现在,我们首先删除Id列,因为对于Iris种类进行分类并不重要。然后,我们将数据集分为训练和测试数据集,并将使用随机森林分类器。您可以使用您选择的任何其他分类器,例如逻辑回归,支持向量机等。
码:
filter_none
编辑
play_arrow
亮度_4
# Dropping the Id column
df.drop('Id', axis = 1, inplace = True)
# Renaming the target column into numbers to aid training of the model
df['Species']= df['Species'].map({'Iris-setosa':0, 'Iris-versicolor':1, 'Iris-virginica':2})
# splitting the data into the columns which need to be trained(X) and the target column(y)
X = df.iloc[:, :-1]
y = df.iloc[:, -1]
# splitting data into training and testing data with 30 % of data as testing data respectively
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)
# importing the random forest classifier model and training it on the dataset
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier()
classifier.fit(X_train, y_train)
# predicting on the test dataset
y_pred = classifier.predict(X_test)
# finding out the accuracy
from sklearn.metrics import accuracy_score
score = accuracy_score(y_test, y_pred)
我们得到95.55%的准确度,这是非常不错的。
现在,为了使用此模型预测其他未知数据,我们需要保存它。我们可以使用pickle来保存它,pickle用于对Python对象结构进行序列化和反序列化。
码:
filter_none
编辑
play_arrow
亮度_4
# pickling the model
import pickle
pickle_out = open("classifier.pkl", "wb")
pickle.dump(classifier, pickle_out)
pickle_out.close()
在同一目录中将创建一个名为“ classifier.pkl”的新文件。现在,我们可以开始使用Streamlit部署模型–
将以下代码粘贴到另一个python文件中。
码:
filter_none
编辑
play_arrow
亮度_4
import pandas as pd
import numpy as np
import pickle
import streamlit as st
from PIL import Image
# loading in the model to predict on the data
pickle_in = open('classifier.pkl', 'rb')
classifier = pickle.load(pickle_in)
def welcome():
return 'welcome all'
# defining the function which will make the prediction using
# the data which the user inputs
def prediction(sepal_length, sepal_width, petal_length, petal_width):
prediction = classifier.predict(
[[sepal_length, sepal_width, petal_length, petal_width]])
print(prediction)
return prediction
# this is the main function in which we define our webpage
def main():
# giving the webpage a title
st.title("Iris Flower Prediction")
# here we define some of the front end elements of the web page like
# the font and background color, the padding and the text to be displayed
html_temp = """
<div style ="background-color:yellow;padding:13px">
<h1 style ="color:black;text-align:center;">Streamlit Iris Flower Classifier ML App </h1>
</div>
"""
# this line allows us to display the front end aspects we have
# defined in the above code
st.markdown(html_temp, unsafe_allow_html = True)
# the following lines create text boxes in which the user can enter
# the data required to make the prediction
sepal_length = st.text_input("Sepal Length", "Type Here")
sepal_width = st.text_input("Sepal Width", "Type Here")
petal_length = st.text_input("Petal Length", "Type Here")
petal_width = st.text_input("Petal Width", "Type Here")
result =""
# the below line ensures that when the button called 'Predict' is clicked,
# the prediction function defined above is called to make the prediction
# and store it in the variable result
if st.button("Predict"):
result = prediction(sepal_length, sepal_width, petal_length, petal_width)
st.success('The output is {}'.format(result))
if __name__=='__main__':
main()
您可以通过在终端中键入以下命令来运行此命令–
流式运行app.py
Host List
hot news