Challenge: Solving Task Using Bagging Regressor
Task
Swipe to start coding
The load_diabetes
dataset contains ten baseline variables (age, sex, BMI, average blood pressure, and six blood serum measurements) for 442 diabetes patients. The target variable is a quantitative measure of disease progression one year after baseline. This dataset is used for predicting the continuous variable, representing diabetes progression, based on the given features.
Your task is to use Bagging Regressor to solve the regression problem on load_diabetes
dataset:
- Use a simple
LinearRegression
model as the base model of the ensemble. - Use the
BaggingRegressor
class to create an ensemble. - Use Mean Squared Error(MSE) to evaluate the results.
Solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Load the Diabetes dataset
data = load_diabetes()
X, y = data.data, data.target
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a base model (Linear Regression)
base_model = LinearRegression()
# Create the Bagging Regressor
bagging_model = BaggingRegressor(base_model, n_estimators=10)
# Train the Bagging Regressor
bagging_model.fit(X_train, y_train)
# Make predictions on the test data
predictions = bagging_model.predict(X_test)
# Calculate mean squared error (MSE)
mse = mean_squared_error(y_test, predictions)
print(f'MSE: {mse:.4f}')
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Load the Diabetes dataset
data = load_diabetes()
X, y = data.data, data.target
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a base model (Linear Regression)
base_model = ___()
# Create the Bagging Regressor
bagging_model = ___(base_model, n_estimators=10)
# Train the Bagging Regressor
bagging_model.fit(X_train, y_train)
# Make predictions on the test data
predictions = bagging_model.predict(X_test)
# Calculate mean squared error (MSE)
mse = ___(y_test, predictions)
print(f'MSE: {mse:.4f}')
Ask AI
Ask anything or try one of the suggested questions to begin our chat