Home>Article>Technology peripherals> Apply 40 machine learning models in two lines of Python code

Apply 40 machine learning models in two lines of Python code

WBOY
WBOY forward
2023-04-12 23:37:04 1158browse

We will use the lazypredict library, which allows us to implement many machine learning models on our dataset with only one line of code. This article will demonstrate the quick use of lazypredict.

Step 1,Use the following command to install the lazypredict library:

pip install lazypredict

Step 2,Import the pandas library to load our machine learning data set .

Dataset link: https://raw.githubusercontent.com/tirthajyoti/Machine-Learning-with-Python/master/Datasets/Mall_Customers.csv

import pandas as pd
df=pd.read_csv("Mall_Customers.csv")

Step 3.View the first few rows of the machine learning data set.

df.head()

Step 4,Split the training set and test set. Here the Y variable is the Spending Score column and the remaining columns are the X variables.

from sklearn.model_selection import train_test_split
x=df.loc[:,df.columns!='Spending Score (1-100)']
y=df['Spending Score (1-100)']


x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)

Step 5.Let us import the lazypredict library installed before. There are two classes in lazypredict, one is the Classification class and the other is the Regression class.

import lazypredict
from lazypredict.Supervised import LazyRegressor
from lazypredict.Supervised import LazyClassifier

After importing, we will use LazyRegressor because we are dealing with regression problem, if you are dealing with classification problem, both types of problems require similar steps.

multiple_ML_model=LazyRegressor(verbose=0,ignore_warnings=True,predictions=True)
models,predictions=multiple_ML_model.fit(x_train,x_test,y_train,y_test)

Here, prediction = True means you want to get the accuracy of each model and want to make predictions for each model.

The model variables contain the accuracy of each model, as well as some other important information.

models

Apply 40 machine learning models in two lines of Python code

As you can see it has implemented 42 machine learning models on my regression problem, this guide is more focused on how to test many models rather than improving them accuracy.

View the predictions of each machine learning model below:

predictions

Apply 40 machine learning models in two lines of Python code

You can use these predictions to create a confusion matrix.

If you are dealing with a classification problem, this is how you use the lazypredict library.

multiple_ML_model=LazyClassifier(verbose=0,ignore_warnings=True,predictions=True)
models,predictions=multiple_ML_model.fit(x_train,x_test,y_train,y_test)

Key points to remember:

  1. This library is for testing purposes only, to provide you with information about which model performs well on your dataset.
  2. Because the library I will use requires a specific version, it is recommended to use a separate environment.

The above is the detailed content of Apply 40 machine learning models in two lines of Python code. For more information, please follow other related articles on the PHP Chinese website!

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