Machine learning python code summary (updated from time to time)

Machine learning python code summary

Data split

#Holdout method
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

#k-Partition cross validation(Cross-validation)
from sklearn.model_selection import cross_val_score
cores = cross_val_score(svc, X, y, cv=5)

Model using Scikit-learn (classification)

Random Forest

from sklearn.ensemble import RandomForestClassifier

rfc= RandomForestClassifier(max_depth=100)
rfc.fit(X_train, y_train)
predicted= rfc.predict(X_test)

Logistic regression

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression(random_state=42)
lr.fit(X_train, y_train)
predicted= lr.predict(X_test)

Support vector machine

from sklearn.svm import SVC

svc = SVC()
svc.fit(X_train,y_train)
predicted= svc.predict(X_test)

Evaluation of the model

model.score(X_train, y_train)
model.score(X_test, y_test)

#Confusion matrix
from sklearn.metrics import  confusion_matrix
confmat = confusion_matrix(y_test,predicted)

#Correct answer rate
from sklearn import metrics
accuracy = metrics.accuracy_score(y_test, predicted)

#accuracy
from sklearn.metrics import precision_score
precision = metrics.precision_score(y_test, predicted)

#Recall
from sklearn.metrics import recall_score
recall = metrics.recall_score(y_test, predicted)

#F value
from sklearn.metrics import f1_score
f1 = metrics.f1_score(y_test, predicted)

When evaluating other classifications, micro-average or macro-average may be specified as an argument.

f1 = metrics.f1_score(y_test, predicted, average='micro')

Save model

#Save model
import pickle
name = 'test.sav'
pickle.dump(model, open(name, 'wb'))

#Load model
loaded_model = pickle.load(open(name, 'rb'))
result = loaded_model.score(X_test, y_test)

The site that I used as a reference

--svm code
https://qiita.com/sotetsuk/items/3a5718bb1f945a383ceb --Model evaluation
https://aidemy.net/courses/2010 --Save model
https://localab.jp/blog/save-and-load-machine-learning-models-in-python-with-scikit-learn/

Recommended Posts

Machine learning python code summary (updated from time to time)
Notes on machine learning (updated from time to time)
progate Python learning memo (updated from time to time)
[Note] AI / machine learning / python related websites [updated from time to time]
vtkXMLUnstructuredGridReader Summary (updated from time to time)
Private Python handbook (updated from time to time)
vtkClipPolyData / DataSet Summary (Updated from time to time)
Summary of vtkThreshold (updated from time to time)
Summary of gcc options (updated from time to time)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
[Updated from time to time] Review of Let Code NumPy
Engineer vocabulary (updated from time to time)
Machine learning summary by Python beginners
Tensorflow memo [updated from time to time]
Python (from first time to execution)
[Updated from time to time] Summary of design patterns in Java
Python Machine Learning Programming Chapter 1 Gives Computers the Ability to Learn from Data Summary
(Updated from time to time) Summary of machine learning APIs that allow you to quickly build apps by Team AI
Updated to Python 2.7.9
scikit-learn How to use summary (machine learning)
Use machine learning APIs A3RT from Python
I installed Python 3.5.1 to study machine learning
"Python Machine Learning Programming" Summary Note (Jupyter)
[Updated from time to time] PostmarketOS related notes
An introduction to Python for machine learning
[Python] Easy introduction to machine learning with python (SVM)
[Updated from time to time] LetCode algorithm and library
Machine learning algorithms (from two-class classification to multi-class classification)
OpenFOAM post-processing cheat sheet (updated from time to time)
Python vs Ruby "Deep Learning from scratch" Summary
I want to make C ++ code from Python code!
Useful help sites, etc. (updated from time to time)
Machine learning tutorial summary
Machine learning ⑤ AdaBoost Summary
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Rewrite Python2 code to Python3 (2to3)
Introduction to machine learning
Try to write code from 1 using the machine learning framework chainer (mnist edition)
Apache settings, log confirmation, etc. (* Updated from time to time)
Python learning memo for machine learning by Chainer from Chapter 2
Preparing to start "Python machine learning programming" (for macOS)
Site summary to learn machine learning with English video
Summary of the basic flow of machine learning with Python
Attempt to include machine learning model in python package
I read the Chainer reference (updated from time to time)
How to create a kubernetes pod from python code
An introduction to machine learning from a simple perceptron
[Updated from time to time] Python memos often used for data analysis [N division, etc.]
[Python] Save PDF from Google Colaboratory to Google Drive! -Let's collect data for machine learning-
(Updated from time to time) Storage location of various VS Code configuration files Memorandum memo
Post from Python to Slack
Cheating from PHP to Python
An introduction to machine learning
Machine learning with Python! Preparation
Machine learning ② Naive Bayes Summary
Machine learning article summary (self-authored)
Stop Omxplayer from Python code
Switch from python2.7 to python3.6 (centos7)
Python Machine Learning Programming> Keywords
Connect to sqlite from python