![](https://crypto4nerd.com/wp-content/uploads/2023/06/0cm-Z3F6cGI6IA6l.jpeg)
- Loading the libraries —
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import train_test_split, cross_val_score
2. Making Dataset —
# Making the regression dataset with make_regression method.
X, y = make_regression(n_samples=1000,
n_features=10,
n_informative=3,
random_state=42)
3. Splitting train and test datasets —
# Splitting the train test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
4. Fitting the Decision Tree Regressor and calculating the error
# Decision Tree Regressor Model
dt = DecisionTreeRegressor(max_depth=4, min_samples_leaf=0.14, random_state=42)# Cross Validation Score with Negative Mean Squared Error as Scoring.
MSE_CV = - cross_val_score(dt, X_train, y_train, cv=10, scoring='neg_mean_squared_error', n_jobs=-1)
# Fit the training dataset
dt.fit(X_train, y_train)
# Predicting the training target values
y_pred_train = dt.predict(X_train)
# Predicting the testing target values
y_pred_test = dt.predict(X_test)
5. Printing the error —
print("Cross Val Mean Squared Error : {:.2f}".format(MSE_CV.mean()))print("Training Mean Squared Error : {:.2f}".format(MSE(y_train, y_pred_train)))
print("Testing Mean Squared Error : {:.2f}".format(MSE(y_test, y_pred_test)))