Two Lines of Code to Apply 40 Machine Learning Models!

Today, let’s learn how to use the lazypredict library, where we can implement many ML models on our dataset with just one line of code, allowing us to briefly understand which models suit our dataset.

Step 1

Install the lazypredict library using the following method:

pip install lazypredict

Step 2

Import pandas to load our dataset.

<span>import</span> pandas <span>as</span> pd

Step 3

Load the dataset.

df = pd.read_csv(<span>'Mal_Customers.csv'</span>)

Step 4

Print the first few rows of the dataset

Two Lines of Code to Apply 40 Machine Learning Models!

Here, the Y variable is the Spending Score column, while the other columns are the X variables.

Now, after determining the X and Y variables, we will split them into training and testing datasets.

<span># Import train_test_split to split the dataset</span><span>from</span> sklearn.model_selection <span>import</span> train_test_split<span># Define X and y variables</span>X = df.loc[:, df.columns != <span>'Spending Score (1-100)'</span>]y = df[<span>'Spending Score (1-100)'</span>] <span># Split the data.</span><span># Split the data</span>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=<span>0.3</span>)

Step 5

We import the previously installed lazypredict library, which contains two classes, one for classification and one for regression.

<span># Import lazypredict</span><span>import</span> lazypredict<span># Import the regression class from lazypredict</span><span>from</span> lazypredict.Supervised <span>import</span> LazyRegressor<span># Import the classification class from lazypredict.Supervised</span><span>from</span> lazypredict.Supervised <span>import</span> LazyClassifier

After importing, we will use LazyRegressor since we are dealing with a regression problem; if you are dealing with a classification problem, the steps are the same for both types.

<span># Define the model using LazyRegressor</span>multiple_ML_model = LazyRegressor(verbose=<span>0</span>, ignore_warnings=<span>True</span>, predictions=<span>True</span>)<span># Fit the model while predicting the output of each model</span>models, predictions = multiple_ML_model.fit(X_train, X_test, y_train, y_test)

Here, prediction = True indicates that you want to obtain the accuracy of each model and the predicted values from each model.

The variable models contains the accuracy of each model along with some other important information.

Two Lines of Code to Apply 40 Machine Learning Models!

It achieved 42 ML models on my regression problem, as this guide focuses more on how to test many models rather than improving their accuracy. Therefore, I am not interested in the accuracy of each model.

Check the predictions of each model.

Two Lines of Code to Apply 40 Machine Learning Models!

You can use these predictions to create a confusion matrix.

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

<span># Define the model using LazyClassifier</span>multiple_ML_model = LazyClassifier(verbose=<span>0</span>, ignore_warnings=<span>True</span>, predictions=<span>True</span>)<span># Fit the model and predict the output of each model</span>models, predictions = multiple_ML_model.fit( X_train, X_test, y_train, y_test)

Key points to remember:

  1. This library is only for testing purposes, to provide information on which model performs well on your dataset.
  2. It is recommended to create a virtual environment separately using conda, as it provides an isolated environment to avoid version conflicts with other environments.Two Lines of Code to Apply 40 Machine Learning Models!Two Lines of Code to Apply 40 Machine Learning Models!
Two Lines of Code to Apply 40 Machine Learning Models!

Two Lines of Code to Apply 40 Machine Learning Models!

END

Editor / Fan Ruiqiang

Reviewer / Fan Ruiqiang

Verifier / Fan Ruiqiang

Click below

Follow us

Leave a Comment