机器学习 pickle文件_使用pickle构建部署机器学习模型

机器学习 pickle文件

We all know that machine learning becoming popular in today business, industry and in education as well, everyone want to learn machine learning so, this article will develop your understanding towards machine learning and data science.

我们都知道,机器学习在当今的商业,工业和教育中也越来越流行,每个人都想学习机器学习,因此,本文将使您对机器学习和数据科学有所了解。

Let’s Start discussion towards building a simple machine learning model with the help of python-jupyter notebook and then deploy it for future use as back-end for web,desktop and android applications.

让我们开始讨论如何借助python-jupyter Notebook建立一个简单的机器学习模型,然后将其部署以供将来用作Web,桌面和android应用程序的后端。

First you need to download Anaconda Navigator and install it with instructions given on official download page. and then run jupyter-notebook.

首先,您需要下载Anaconda Navigator并按照官方下载页面上的说明进行安装。 然后运行jupyter-notebook。

we will use jupyter-notebook and need some libraries to be installed before move towards building model. libraries are listed below.

我们将使用jupyter-notebook并在构建模型之前需要安装一些库。 库在下面列出。

1-Numpy: derived from two words Num means “Numerical” and py means “Python”, use for working with arrays,for fast processing and for mathematical operations. Already downloaded with anaconda installation. if not so download it with below command

1-Numpy:由两个词派生,Num表示“数值”,而py表示“ Python”,用于处理数组,快速处理和数学运算。 已经下载了anaconda安装。 如果不是这样,请使用以下命令下载

pip install numpy

2-Pandas: used for reading, making,writing dataset from different resources and make dataset by own. download it with below command

2-Pandas:用于从不同资源读取,制作,写入数据集,并自行创建数据集。 使用以下命令下载

pip install pandas
机器学习 pickle文件_使用pickle构建部署机器学习模型_第1张图片
pandas library installation 熊猫库安装

3-Seaborn & Matplotlib: most popular and useful libraries for data visualization. you can download them with below command

3-Seaborn&Matplotlib:用于数据可视化的最受欢迎和最有用的库。 您可以使用以下命令下载它们

pip install matplotlib seaborn
机器学习 pickle文件_使用pickle构建部署机器学习模型_第2张图片
Matplotlib & Seaborn libraries installation Matplotlib和Seaborn库安装

4-Sklearn: one of the most popular and useful library that support almost all machine learning algorithms like classification, regression, clustering etc. you can download it with below command

4-Sklearn:最流行和最有用的库之一,支持几乎所有的机器学习算法,例如分类,回归,聚类等。您可以使用以下命令下载它

pip install sklearn
机器学习 pickle文件_使用pickle构建部署机器学习模型_第3张图片
Sklearn library installation Sklearn库安装

Now, we will move towards building model so let start with step by step.

现在,我们将朝着构建模型的方向发展,所以让我们从逐步开始。

Pickle5: used for deploying machine learning model. you can download it with below command.

Pickle5:用于部署机器学习模型。 您可以使用以下命令下载它。

pip install pickle5
机器学习 pickle文件_使用pickle构建部署机器学习模型_第4张图片
Pickle library installation 泡菜库安装

Step-1: importing necessary libraries.

步骤1:导入必要的库。

import numpy as np             //used for working with arrays
import pandas as pd //used for reading dataset
import seaborn as sns //use for attractive visualization
from sklearn.linear_model import LinearRegression // importing model
from sklearn.model_selection import train_test_split
//for splitting of test and train.
import pickle5 as pickle //used for deploying machine learning model

Step-2: Read dataset. You can download dataset from link and then need to pasted it in same directory where your jupyter-notebook file exists and then read it.

步骤2:读取资料集。 您可以从链接下载数据集,然后将其粘贴到jupyter-notebook文件所在的目录中,然后读取它。

data = pd.read_csv(‘USA_Housing.csv’)   // read csv dataset

Step-3: Pre-Processing using pandas.

步骤3:使用熊猫进行预处理。

data.head()    //checking first five rows of data
data.tail() //checking last five rows of data
data.info() //checking null or not null values/information of data
data.columns //checking features and target variable in data
data.describe()

Step-4: Visualization of data using seaborn.

步骤4:使用seaborn可视化数据。

sns.pairplot(data) 
//plotting all features except string/alphanumeric/alphabetic features

because we have not large data so simple visualization will maximum clear understanding about data but some times data is too large so in pre-processing we need to learn more inside data then we need more visualization. you can find how to do visualization of data from link.

因为我们没有大数据,所以简单的可视化将最大程度地使您对数据有更清晰的了解,但是有时数据太大,因此在预处理中我们需要学习更多的内部数据,然后需要更多的可视化。 您可以从link找到如何可视化数据。

Step-5: Selection of features and target variable.

步骤5:选择特征和目标变量。

X = data[[‘Avg. Area Income’, ‘Avg. Area House Age’, ‘Avg. Area Number of Rooms’,‘Avg. Area Number of Bedrooms’, ‘Area Population’]]             //FeaturesY = data[‘Price’] ## Target Variable

we selected Price as target variable becuase we need to predict prices of house.

我们选择“价格”作为目标变量,因为我们需要预测房屋价格。

Step-6: Split Data into Train & Test.

步骤6:将数据拆分为训练和测试。

X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.2,train_size = 0.8, random_state = 1)

we splitted data into train and test where test-size = 20% and train-size=80%.

我们将数据分为训练和测试,其中test-size = 20%和train-size = 80%。

Step-7: fit training data into machine learning model

步骤7:将训练数据拟合到机器学习模型中

LR = LinearRegression() //create an instance for LinearRegression
LR.fit(X_train,Y_train) //fit data in model

Step-8: Testing model with testing data.

步骤8:使用测试数据测试模型。

from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
//importing accuracy library.predict = LR.predict(X_test)

Step-9: Now we will check accuracy,r2-score etc of built model

步骤9:现在,我们将检查构建模型的准确性,r2-得分等

print(“Accuracy of Linear Model will be = “,r2_score(Y_test,predict))
机器学习 pickle文件_使用pickle构建部署机器学习模型_第5张图片
Accuracy = 91% 准确度= 91%

our model accuracy is 91%, which means our model is best now we will deploy it using pickle.

我们的模型精度为91%,这意味着我们的模型是最好的,现在我们将使用pickle进行部署。

Step-10: Deploying model using pickle5.

步骤10:使用pickle5部署模型。

pickle.dump(LR, open(“House_Prediction_Model.pkl”, ‘wb’))

after running above command you can see in your file directory that one pickle file would be created with name “House_Prediction_Model.pkl”.

运行上述命令后,您可以在文件目录中看到将创建一个名为“ House_Prediction_Model.pkl”的泡菜文件。

机器学习 pickle文件_使用pickle构建部署机器学习模型_第6张图片
Pickle File Created View. 泡菜文件创建视图。

That’s all about building and deploying machine learning model, you can try at your own to built different machine learning model and deploy these with pickle.

这就是构建和部署机器学习模型的全部内容,您可以自己尝试构建不同的机器学习模型,然后使用pickle进行部署。

Project GitHub repository Link

项目GitHub存储库链接

翻译自: https://medium.com/how-to-built-and-deploy-machine-learning-model/building-deploying-machine-learning-model-using-pickle-d5ad5c979acd

机器学习 pickle文件

你可能感兴趣的:(机器学习,python,人工智能,大数据,java)