[Python] Saving learning results (models) in machine learning

Introduction

In the previous article , I touched on a simple machine learning program. Very small data like the previous article is not actually used. Machine learning basically has the advantage of having a lot of data, so it usually results in a very large amount of data. Of course, if you have a lot of data, it will take a lot of time to learn. You may be surprised if you don't know it, but it is natural to leave your PC for a week to study. There is no practicality in doing such a long learning every time a program is executed. Therefore, you can save time by saving the learning result as a model and executing it in the actual program using the model. This time, I would like to create a "save program" and a "prediction program using a model".

If you don't know what you're doing, see previous article .

environment

python 3.8.5 scikit-learn 0.231

Source code (save model)

Some of the code used in the previous article is used.

save_model.py


import numpy as np
from sklearn import svm
import pickle

#Read csv file
npArray = np.loadtxt("data.csv", delimiter = ",", dtype = "float")

#Storage of explanatory variables
x = npArray[:, 0:4]

#Storage of objective variable
y = npArray[:, 4:5].ravel()

#Select SVM as the learning method
clf = svm.SVC()

#Learning
clf.fit(x,y)

#Saving the learning model
with open('model.pickle', mode='wb') as f:
    pickle.dump(clf,f,protocol=2)

Source code (use of saved model)

open_model.py


import pickle

#Model open
with open('model.pickle', mode='rb') as f:
    clf = pickle.load(f)

#Evaluation data
weather = [[9,0,7.9,6.5]]

#Prediction using a model
ans = clf.predict(weather)

if ans == 0:
    print("It's sunny")
if ans == 1:
    print("It's cloudy")
if ans == 2:
    print("It's rain")

Execution result

$ python3 open_model.py
It's sunny

in conclusion

Thank you for your hard work. By saving the model trained in this way, it becomes possible to evaluate and predict the data without training each execution. Currently, AI, which is said in the world, is created and operated as a set of "program" + "learning data". Looking at the learning model created from the collected data, it is a pleasure different from creating a large-scale program. See you again.

Recommended Posts

[Python] Saving learning results (models) in machine learning
Python: Preprocessing in Machine Learning: Overview
[python] Frequently used techniques in machine learning
Python: Preprocessing in machine learning: Data acquisition
Python: Preprocessing in machine learning: Data conversion
Get a glimpse of machine learning in Python
Machine learning in Delemas (practice)
Build an interactive environment for machine learning in Python
Tool MALSS (application) that supports machine learning in Python
Coursera Machine Learning Challenges in Python: ex2 (Logistic Regression)
Machine learning with Python! Preparation
About testing in the implementation of machine learning models
Python Machine Learning Programming> Keywords
Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
Used in machine learning EDA
Attempt to include machine learning model in python package
Beginning with Python machine learning
MALSS, a tool that supports machine learning in Python
The result of Java engineers learning machine learning in Python www
Coursera Machine Learning Challenges in Python: ex7-2 (Principal Component Analysis)
Implement stacking learning in Python [Kaggle]
Automate routine tasks in machine learning
Widrow-Hoff learning rules implemented in Python
Classification and regression in machine learning
<For beginners> python library <For machine learning>
Machine learning in Delemas (data acquisition)
Implemented Perceptron learning rules in Python
Preprocessing in machine learning 2 Data acquisition
Random seed research in machine learning
"Scraping & machine learning with Python" Learning memo
Preprocessing in machine learning 4 Data conversion
How about Anaconda for building a machine learning environment in Python?
Coursera Machine Learning Challenges in Python: ex5 (Adjustment of Regularization Parameters)
Machine learning
python learning
Python & Machine Learning Study Memo: Environment Preparation
Introduction to Machine Learning: How Models Work
Amplify images for machine learning with python
Use machine learning APIs A3RT from Python
Machine learning with python (2) Simple regression analysis
I installed Python 3.5.1 to study machine learning
Why Python is chosen for machine learning
"Python Machine Learning Programming" Summary Note (Jupyter)
[Shakyo] Encounter with Python for machine learning
[Python] First data analysis / machine learning (Kaggle)
[Python] When an amateur starts machine learning
[Python] Web application design for machine learning
Python and machine learning environment construction (macOS)
An introduction to Python for machine learning
Python & Machine Learning Study Memo ③: Neural Network
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Preprocessing in machine learning 1 Data analysis process
Python & Machine Learning Study Memo ⑥: Number Recognition
Build AI / machine learning environment with Python
Coursera Machine Learning Challenges in Python: ex6 (How to Adjust SVM Parameters)
Coursera Machine Learning Challenges in Python: ex7-1 (Image compression with K-means clustering)
Python: Preprocessing in machine learning: Handling of missing, outlier, and imbalanced data
[Python] Easy introduction to machine learning with python (SVM)
Python in optimization
CURL in python
Metaprogramming in Python