[PYTHON] How to adapt multiple machine learning libraries in one shot

This is a one-shot method for adapting multiple machine learning libraries.

In machine learning, you divide the data into two parts, one for training and the other for testing, and measure the accuracy. Therefore, KFold makes it easy to divide the data. There are also Static KFold and Shuffle Split, but this time I will use KFold.

First, about KFold of sklearn, which is one of the cross-validations.

KFold divides the data into k datasets, for example, if you divide it into 10 datasets, 9 will be used as training datasets and the remaining 1 will be used for testing. By the way, in this case, test 10 times so that the 10 separated data sets are always used once for testing. The default code looks like this:

KFold


from sklearn.cross_validation import KFold

KFold(n_splits=3, shuffle=False, random_state=None)

And below are the parameters in KFold.

This Official Document

Next is about cross_val_score.

cross_val_score is a convenient tool that allows you to specify the classifier, training data, and test data and determine their accuracy. Below is the default code.

cross_val_score



from sklearn.model_selection import cross_val_score

cross_val_score(estimator, X, y=None, groups=None, scoring=None, cv=None, n_jobs=1, verbose=0, fit_params=None, pre_dispatch='2*n_jobs')

Below is a description of the main parameters.

This Official Document

Multiple machine learning on the subject

It is a method to apply multiple main machine learning methods in one shot. This time, we will use DecisionTreeClassifier, KNeighborsClassifier, and SVC. First of all, please have the code. By the way, here, we will proceed to the extent that the training data set and the test data set have already been separated.

machine_learning



from sklearn.model_selection import KFold
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC

#Store machine learning models in a list
models = []
models.append(("KNC",KNeighborsClassifier()))
models.append(("DTC",DecisionTreeClassifier()))
models.append(("SVM",SVC()))

#Applying multiple classifiers
results = []
names = []
for name,model in models:
    kfold = KFold(n_splits=10, random_state=42)
    result = cross_val_score(model,X_train,Y_train, cv = kfold, scoring = "accuracy")
    names.append(name)
    results.append(result)

#Score display of applied classifier
for i in range(len(names)):
    print(names[i],results[i].mean())

The result is

KNC 0.88 DTC 0.91 SVM 0.79

I think it will be like that.

Recommended Posts

How to adapt multiple machine learning libraries in one shot
How to convert 0.5 to 1056964608 in one shot
How to embed multiple embeds in one message with Discord.py
How to collect machine learning data
Newton's method for machine learning (from one variable to multiple variables)
People memorize learned knowledge in the brain, how to memorize learned knowledge in machine learning
How to calculate "xx time" in one shot with Python timedelta
Introduction to Machine Learning: How Models Work
scikit-learn How to use summary (machine learning)
How to enjoy Coursera / Machine Learning (Week 10)
Coursera Machine Learning Challenges in Python: ex6 (How to Adjust SVM Parameters)
Bringing machine learning to a practical level in one month # 1 (Starting edition)
Connect multiple videos in one shot using OpenCV!
[For beginners] Introduction to vectorization in machine learning
A machine learning beginner tried to create a sheltie judgment AI in one day
Introduction to machine learning
How to get multiple model objects randomly in Django
How to write string concatenation in multiple lines in Python
How to define Decorator and Decomaker in one function
How to do zero-padding in one line with OpenCV
How to display multiple images of galaxies in tiles
How to perform learning in SageMaker without session timeout
Attempt to include machine learning model in python package
Cross-entropy to review in Coursera Machine Learning week 2 assignments
How to retrieve multiple arrays using slice in python.
How to split machine learning training data into objective variables and others in Pandas
How to build Anaconda virtual environment used in Azure Machine Learning and link with Jupyter
Machine learning in Delemas (practice)
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
How to slice a block multiple array from a multiple array in Python
An introduction to machine learning
How to check ORM behavior in one file with django
How to interactively draw a machine learning pipeline with scikit-learn and save it in HTML
[Python] How to write an if statement in one sentence.
How to define multiple variables in a python for statement
How to use machine learning for work? 03_Python coding procedure
How to create and use static / dynamic libraries in C
How to increase the number of machine learning dataset images
How to write type hints for variables that are assigned multiple times in one line
Used in machine learning EDA
How to develop in Python
Super introduction to machine learning
How to turn the for statement when there are multiple values for one key in the dictionary
How to send a file in one shot by connecting to a host on the other side of the platform with SCP in multiple stages
How to handle multiple versions of CUDA in the same environment
How about Anaconda for building a machine learning environment in Python?
How to combine all CSVs in a folder into one CSV
How to use machine learning for work? 01_ Understand the purpose of machine learning
Two ways to display multiple graphs in one image with matplotlib
How to delete multiple specified positions (indexes) in a Python list
Shell script to build pyenv environment on ubuntu in one shot
How to display legend marks in one with Python 2D plot
How to create a serverless machine learning API with AWS Lambda
How to handle session in SQLAlchemy
Introduction to machine learning Note writing
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
How to update Spyder in Anaconda
How to use SQLite in Python
Automate routine tasks in machine learning