当前位置: 首页 > news >正文

郑州做网站公司电话西安网页设计培训机构

郑州做网站公司电话,西安网页设计培训机构,跨境贸易公司名字,应用商店关键词优化机器学习: 计算机能够从经验中学习,而无需明确编程。机器学习是目前最热门的领域之一,世界各地的顶级公司都在使用它来改善他们的服务和产品。但是没有使用在Jupyter Notebook中训练的机器学习模型。因此,我们需要部署这些模型&am…

机器学习:
计算机能够从经验中学习,而无需明确编程。机器学习是目前最热门的领域之一,世界各地的顶级公司都在使用它来改善他们的服务和产品。但是没有使用在Jupyter Notebook中训练的机器学习模型。因此,我们需要部署这些模型,以便每个人都可以使用它们。在本文中,我们将首先训练Iris Species分类器,然后使用Streamlit部署模型,Streamlit是一个开源应用程序框架,用于轻松部署ML模型。

Streamlit库:
Streamlit允许您使用简单的Python脚本为机器学习项目创建应用程序。它还支持热加载,以便您的应用可以在您编辑和保存文件时实时更新。一个应用程序只需要几行代码就可以使用Streamlit API构建(我们将在下面看到)。添加小部件与声明变量是一样的。不需要编写后端,定义不同的路由或处理HTTP请求。它易于部署和管理。更多信息可以在他们的网站上找到https://www.streamlit.io/

首先,我们将训练我们的模型。我们不会做太多的预处理,因为本文的主要目的不是建立一个准确的ML模型,而是展示它的部署。

首先,我们需要安装以下内容

pip install pandas
pip install numpy
pip install sklearn
pip install streamlit

机器学习模型示例

import pandas as pd
import numpy as npdf = pd.read_csv('BankNote_Authentication.csv')
df.head()

在这里插入图片描述
现在,我们首先删除Id列,因为它对于分类Iris物种并不重要。然后我们将数据集分为训练和测试数据集,并使用随机森林分类器。您可以使用您选择的任何其他分类器,例如,逻辑回归,支持向量机等。

# 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对象结构。

# pickling the model
import pickle
pickle_out = open("classifier.pkl", "wb")
pickle.dump(classifier, pickle_out)
pickle_out.close()

在同一目录中将创建一个名为“classifier.pkl”的新文件。

部署模型

现在我们可以开始使用Streamlit来部署模型了。
将下面的代码粘贴到另一个python文件中。

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 titlest.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 displayedhtml_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 codest.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 predictionsepal_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 resultif st.button("Predict"):result = prediction(sepal_length, sepal_width, petal_length, petal_width)st.success('The output is {}'.format(result))if __name__=='__main__':main()

然后,您可以在终端中键入以下命令来运行应用

streamlit run app.py

在这里插入图片描述
app.py是我们编写Streamlit代码的文件名。
该网站将在您的浏览器中打开,然后您可以对其进行测试。这种方法也可以用于部署其他机器和深度学习模型。

http://www.yayakq.cn/news/163523/

相关文章:

  • 加盟网官方网站重庆网站建设中心
  • 网站建设设计指标系统没有安装wordpress
  • 法拍重庆网站一个空间只能放一个网站吗
  • 山东省建设厅网站查网站降权后 换域名
  • 赣州网站建设如何百度一下app
  • 微建站平台苏州招聘网站建设
  • 广州腾虎网络网站建设熊掌号ps切片怎么做网站
  • 网站功能报价明细表123建站
  • 沧州市建设局网站招聘网站开发人员
  • 免费进入电影网站人人网入口wordpress 批量添加用户
  • 英文网站建设 江门怎么在百度上发布帖子
  • 北京网站制作网络推广公司wordpress工单系统
  • 德州俱乐部网站开发南宁市住房城乡建设厅网站
  • 自建网站营销是什么东莞短视频seo需要多少钱
  • jsp个人网站怎样做做响应式网站制作
  • 潍坊企业网站制作wordpress 头部不显示
  • 餐饮营销型网站案例app合作开发
  • 网站建设申报方案国际公司名称大全名头
  • 网站关键词的选择企业可以做哪些网站
  • 怎样把自己做的网站放到网上淘宝做基础销量怎么网站
  • 网站建立连接不安全怎么解决潍坊专业做网站
  • 泰安营销网站建设内江如何做百度的网站
  • 推荐十个国外网站企业信息免费查询系统
  • 网站开发需求文档怎么写wordpress后台怎么进
  • 电子商务平台网站建设方式三站合一的网站怎么做
  • 网站文字公告代码今晚12点上海又要封控了吗
  • 上海哪家公司做网站比较好揭阳企业网站模板建站
  • 企业展示网站 数据库设计网站建设书籍资料
  • 电烤箱做蛋糕网站最新国际新闻头条新闻
  • 网站开发维护报价单网站制作导航栏怎么做