How to plot Learning Curve in Python

How to plot Learning Curve in Python

When building a machine learning model, it’s important to evaluate its performance and make sure it’s not overfitting or underfitting. One way to do this is by plotting a learning curve.

A learning curve is a graph that shows the relationship between the model’s performance and the amount of data it’s trained on. It helps to understand how the model’s performance improves as more data is added.

In Python, the library scikit-learn provides an easy way to plot a learning curve using the function learning_curve().

The first step is to import the library and load the dataset into a pandas dataframe. Then, split the data into training and testing sets, and create an instance of the model you want to evaluate.

After that, use the learning_curve() function, which takes the model, the dataset, and the number of folds as inputs. The function will return the train and test scores for different training set sizes.

You can use the ‘train_sizes’ parameter to set different training set sizes, by default it uses np.linspace(0.1, 1.0, 5) that means the data is divided into 5 sets.

Finally, you can use the matplotlib library to plot the scores, by plotting the training and testing scores separately, you can get an idea of the model’s performance with different data size.

In summary, a learning curve is a powerful tool for evaluating the performance of a machine learning model and make sure it’s not overfitting or underfitting. By using the learning_curve() function in scikit-learn, it’s easy to plot a learning curve in Python, making it a valuable tool for data scientists and machine learning practitioners.


In this Learn through Codes example, you will learn: How to plot Learning Curve in Python.

Essential Gigs