ML04: Regression Supervised Learning Using Scikit-Learn

Posted by

In this tutorial, we will be learning about supervised learning with scikit-learn specifically focusing on regression. Regression is a type of supervised learning where the goal is to predict a continuous value output based on one or more input features.

Scikit-learn is a popular machine learning library in Python that provides tools for various machine learning tasks, including regression. In this tutorial, we will be using scikit-learn to build regression models and make predictions.

Step 1: Importing the necessary libraries
To get started, we need to import scikit-learn and other libraries that we will be using in this tutorial. Here is the code snippet to import the necessary libraries:

<!DOCTYPE html>
<html>
<body>

<h2>ML04:Supervised Learning With Scikit-Learn-Regression</h2>

<p>In this tutorial, we will be learning about supervised learning with scikit-learn specifically focusing on regression.</p>
<p>Regression is a type of supervised learning where the goal is to predict a continuous value output based on one or more input features.</p>
<p>Scikit-learn is a popular machine learning library in Python that provides tools for various machine learning tasks, including regression.</p>
<p>In this tutorial, we will be using scikit-learn to build regression models and make predictions.</p>

<h3>Step 1: Importing the necessary libraries</h3>

<p>To get started, we need to import scikit-learn and other libraries that we will be using in this tutorial. Here is the code snippet to import the necessary libraries:</p>

<pre>
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
</pre>

</body>
</html>

Step 2: Loading the dataset
Next, we need to load a dataset that we will be using to build our regression model. For this tutorial, we will be using the Boston housing dataset which is included in scikit-learn datasets. Here is the code snippet to load the Boston housing dataset:

<h3>Step 2: Loading the dataset</h3>

<p>Next, we need to load a dataset that we will be using to build our regression model. For this tutorial, we will be using the Boston housing dataset which is included in scikit-learn datasets. Here is the code snippet to load the Boston housing dataset:</p>

<pre>
from sklearn.datasets import load_boston

boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
df['target'] = boston.target
</pre>

Step 3: Splitting the data
Before building a regression model, we need to split the dataset into training and testing sets. This is done to evaluate the performance of our model on unseen data. Here is the code snippet to split the data into training and testing sets:

<h3>Step 3: Splitting the data</h3>

<p>Before building a regression model, we need to split the dataset into training and testing sets. This is done to evaluate the performance of our model on unseen data. Here is the code snippet to split the data into training and testing sets:</p>

<pre>
X = df.drop('target', axis=1)
y = df['target']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
</pre>

Step 4: Building the regression model
Now, we are ready to build our regression model. In this tutorial, we will be using linear regression as our regression algorithm. Here is the code snippet to build a linear regression model:

<h3>Step 4: Building the regression model</h3>

<p>Now, we are ready to build our regression model. In this tutorial, we will be using linear regression as our regression algorithm. Here is the code snippet to build a linear regression model:</p>

<pre>
model = LinearRegression()
model.fit(X_train, y_train)
</pre>

Step 5: Evaluating the model
Once the model is built, we need to evaluate its performance on the testing set. One common metric used to evaluate regression models is mean squared error (MSE). Here is the code snippet to evaluate the model using mean squared error:

<h3>Step 5: Evaluating the model</h3>

<p>Once the model is built, we need to evaluate its performance on the testing set. One common metric used to evaluate regression models is mean squared error (MSE). Here is the code snippet to evaluate the model using mean squared error:</p>

<pre>
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print('Mean Squared Error:', mse)
</pre>

Step 6: Making predictions
Finally, we can use our trained regression model to make predictions on new data. Here is the code snippet to make predictions using the trained model:

<h3>Step 6: Making predictions</h3>

<p>Finally, we can use our trained regression model to make predictions on new data. Here is the code snippet to make predictions using the trained model:</p>

<pre>
new_data = np.array([[0.00632, 18.0, 2.31, 0.0, 0.538, 6.575, 65.2, 4.0900, 1, 296.0, 15.3, 396.90, 4.98]])
prediction = model.predict(new_data)
print('Predicted price:', prediction)
</pre>

Congratulations! You have successfully completed this tutorial on supervised learning with scikit-learn focusing on regression. You have learned how to build a regression model, evaluate its performance, and make predictions. Keep practicing and experimenting with different regression algorithms to enhance your machine learning skills. Happy learning!