Home  >  Article  >  Backend Development  >  Automatic Machine Learning Python Equivalent Code Explained

Automatic Machine Learning Python Equivalent Code Explained

PHPz
PHPzforward
2023-08-22 20:25:101146browse

Automatic Machine Learning Python Equivalent Code Explained

introduce

Machine learning is a rapidly developing field, and new technologies and algorithms are constantly emerging. However, creating and enhancing machine learning models can be a time-consuming and challenging task that requires a high degree of expertise. Automated machine learning, often referred to as autoML, aims to simplify the process of creating and optimizing machine learning models by automating some of the tedious tasks such as feature engineering, hyperparameter tuning, and model selection.

auto-sklearn is a powerful open source automated machine learning framework built on scikit-learn, one of the most famous machine learning libraries in Python. It automatically searches for potential machine learning pipelines on a given dataset through Bayesian optimization and meta-learning, and automatically identifies the best models and hyperparameters. This tutorial will introduce the use of Auto-sklearn in Python, including guidance on installation, importing data, data preparation, creating and training models, and evaluating model effects. Even beginners can use Auto-sklearn to quickly and easily create powerful machine learning models.

How to handle errors in node-red

Auto-sklearn

Automate the creation and continuous improvement of machine learning models using the efficient open source software program Auto-sklearn. Automatically find the ideal model and hyperparameters for a specific data set using Bayesian optimization and meta-learning, itself based on the well-known machine learning program scikit-learn.

Only a few applications created by Autosklearn for classification and regression problems include natural language processing, image classification, and time series prediction.

The library operates by searching through a collection of potential machine learning processes, including feature engineering, model selection, and data preparation processes. It uses Bayesian optimization to efficiently search this space and continuously improves search efficiency from previous tests through meta-learning.

In addition, Auto-sklearn also provides a series of powerful functions, including dynamic integration selection, automatic model integration and active learning. Additionally, it provides an easy-to-use API for developing, testing, and training models.

AutoML code

Let us now examine the AutoML code in more detail using Auto-sklearn. We will use the Digits dataset from scikit-learn, which is a dataset of handwritten digits. Predicting numbers from pictures of numbers is the goal. Here is the code -

The Chinese translation of

Program

is:

program

import autosklearn.classification
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

# Load the dataset
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Create and fit the AutoML model
automl = autosklearn.classification.AutoSklearnClassifier(time_left_for_this_task=180, per_run_time_limit=30)
automl.fit(X_train, y_train)

# Evaluate the model on the test set
print("Accuracy:", automl.score(X_test, y_test))

Output

Accuracy: 0.9866666666666667

Code explanation

This program uses automated machine learning (AutoML) to classify handwritten digits from the MNIST dataset, including using the Auto-sklearn module. Here is a brief overview of the code −

  • Import the AutoSklearnClassifier class from the autosklearn.classification module. This class contains the AutoML classification model that will be used. Import the autosklearn.classification module.

  • Import load_digits function from sklearn.datasets: This will import the load_digits function of the MNIST dataset from the sklearn.datasets package.

  • Select a model from sklearn. The MNIST data set is divided into a training set and a test set using the train test split function in the sklearn.model selection module, which is imported here.

  • The MNIST dataset is loaded, the input features are stored in X, and the corresponding labels are stored in y. X, y = load_digits(return_X_y=True): This will load the MNIST dataset.

  • X training set, set and test set, and set the random seed to 1 to ensure repeatability

  • Automl is equivalent to autosklearn.classification. AutoSklearnClassifier (time limit per run = 30, time remaining on this task = 180): Form an AutoML model trained on the MNIST dataset into an instance of the AutoSklearnClassifier class. The time-per-run limit represents the maximum time (in seconds) that each individual model can run, while the remaining time for this task represents the maximum time (in seconds) that the AutoML process can run.
  • Use the automl.fit function (X train, y train) to train the AutoSklearnClassifier model through the training set X train and the related label Y train.
  • accuracy:", print(X test, y test), automl.score This determines the accuracy of the AutoSklearnClassifier model on the test set after evaluating its performance on the X test and Y test related labels The .score method gives the accuracy of the model on a given data set.
  • The above code implements the AutoML method, a machine learning technology that automates every step of the model building process, including feature selection, hyperparameter tuning, and data preparation. Even non-experts can create powerful models with the help of AutoML, which reduces the amount of manual effort required to create machine learning models.

First, import the required libraries, such as pandas, numpy, sklearn and tpot, into the code. Sklearn is used for machine learning tasks such as data preprocessing, model selection and evaluation, Pandas is used for data manipulation, and NumPy is used for numerical calculations. The main library that implements AutoML algorithms is TPOT.

Then use the read_csv function of pandas to load the dataset and store the input features and output labels separately in different variables. The 'y' variable holds the labels of the output, while the 'X' variable stores the features of the input.

To fit the data and generate a machine learning model, the code first loads the dataset and then creates an instance of the TPOTRegressor class. The TPOTSRegressor class is a subclass of the TPOTBase class and uses a genetic algorithm to select features and adjust hyperparameters. The TPOTRegressor class handles regression problems, while the TPOTClassifier class handles classification problems.

Use Sklearn's train-test-split method to divide the data set into a training set and a test set. It is a common practice in machine learning to split the data into two sets: a training set for fitting the model, and a test set for evaluating the model's performance.

Once the data is split, the fit method of the TPOTRegressor instance is called, which adjusts the model based on the training data. With fit technology, a genetic algorithm is used to find the optimal subset of features and hyperparameters for the given data. The best model is then returned.

The code then evaluates the model's performance on the test set to determine the accuracy of the model, using a scoring method. The accuracy score indicates how well the model fits the data, with values ​​closer to 1 indicating a better fit.

The best model is then exported to a Python file using the export function, along with its accuracy score on the test set.

in conclusion

In summary, Auto-sklearn is a powerful library that simplifies the process of creating and improving machine learning models. It saves time and effort by automatically finding the best model and hyperparameters for a given dataset. This tutorial explains how to use Auto-sklearn in Python, including guidance on installing it, importing data, preparing data, creating and training models, and evaluating model performance. Even novices can use Auto-sklearn to quickly and easily create powerful machine learning models.

The above is the detailed content of Automatic Machine Learning Python Equivalent Code Explained. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete