Next we will see how we can implement this model in Python. To do this, we will use the xgboost
library.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y = True, as_frame = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
X_train.head()
The train set will be used to train the model, while the test set will be used to evaluate the effectiveness of the model. In addition, the predictor variables do not need to be normalized, since the decision trees that make up the XGBoost models are not affected by the scale of the data because of the way they work: they make decisions based on certain feature thresholds, regardless of their scale.
However, if models other than decision trees are added for boosting, data standardization is necessary.
from xgboost import XGBClassifier
model = XGBClassifier(random_state = 42)
model.fit(X_train, y_train)
The training time of a model will depend, first of all, on the size of the dataset (instances and features), and also on the model type and its configuration.
Once the model has been trained, it can be used to predict with the test sample of the dataset.
y_pred = model.predict(X_test)
y_pred
With raw data, it is very difficult to know whether the model is getting it right or not. To do this, we must compare it with reality. There are many metrics to measure the effectiveness of a model in predicting, including accuracy, which is the fraction of predictions that the model makes correctly.
from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)
The model is perfect!
Once we have the model we were looking for (presumably after hyperparameter optimization), in order to be able to use it in the future, it is necessary to store it in our directory, together with the seed.
model.save_model("xgb_classifier_default_42.json")
Adding an explanatory name to the model is vital because, in the event of losing the code that generated it, we will know several important details. Firstly, we will understand its configuration (in this case, we use default
because we haven't customized any of the model's hyperparameters; we've kept the defaults of the function). Secondly, we will have the seed necessary to replicate the random components of the model, indicated by adding a number to the filename, such as 42
.
import pandas as pd
train_data = pd.read_csv("https://raw.githubusercontent.com/4GeeksAcademy/machine-learning-content/master/assets/clean_petrol_consumption_train.csv")
test_data = pd.read_csv("https://raw.githubusercontent.com/4GeeksAcademy/machine-learning-content/master/assets/clean_petrol_consumption_test.csv")
train_data.head()
X_train = train_data.drop(["Petrol_Consumption"], axis = 1)
y_train = train_data["Petrol_Consumption"]
X_test = test_data.drop(["Petrol_Consumption"], axis = 1)
y_test = test_data["Petrol_Consumption"]
The train set will be used to train the model, while the test set will be used to evaluate the effectiveness of the model. In addition, the predictor variables do not need to be normalized, since the decision trees that make up the XGBoost models are not affected by the scale of the data because of the way they work: they make decisions based on certain feature thresholds, regardless of their scale.
However, if models other than decision trees are added for boosting, data standardization is necessary.
from xgboost import XGBRegressor
model = XGBRegressor(random_state = 42)
model.fit(X_train, y_train)
Once the model has been trained, it can be used to predict with the test sample of the dataset.
y_pred = model.predict(X_test)
y_pred
To calculate the effectiveness of the model we will use the mean squared error (MSE):
from sklearn.metrics import mean_squared_error
print(f"Mean squared error: {mean_squared_error(y_test, y_pred)}")
Once we have the model we were looking for (presumably after hyperparameter optimization), in order to be able to use it in the future, it is necessary to store it in our directory, together with the seed.
model.save_model("xgb_regressor_default_42.json")
Adding an explanatory name to the model is vital because, in the event of losing the code that generated it, we will know several important details. Firstly, we will understand its configuration (in this case, we use default
because we haven't customized any of the model's hyperparameters; we've kept the defaults of the function). Secondly, we will have the seed necessary to replicate the random components of the model, indicated by adding a number to the filename, such as 42
.