0% found this document useful (0 votes)
2 views16 pages

Tested AI Modelsfinal5

The document discusses the implementation and evaluation of two machine learning models, Random Forest and XGBoost, for predicting various VM metrics such as CPU usage, memory, disk usage, and network bytes. It details the data preparation, feature engineering, model training, and performance evaluation processes, highlighting issues like overfitting in certain metrics and suggesting that larger datasets or neural networks may improve performance. Overall, the models show strong predictive capabilities, particularly with high R² scores for CPU and memory metrics.

Uploaded by

anas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Tested AI Modelsfinal5

The document discusses the implementation and evaluation of two machine learning models, Random Forest and XGBoost, for predicting various VM metrics such as CPU usage, memory, disk usage, and network bytes. It details the data preparation, feature engineering, model training, and performance evaluation processes, highlighting issues like overfitting in certain metrics and suggesting that larger datasets or neural networks may improve performance. Overall, the models show strong predictive capabilities, particularly with high R² scores for CPU and memory metrics.

Uploaded by

anas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tested AI Models:

[Link] Forests model: The random forest algorithm is an extension of the decision tree
algorithm, in which decision trees are combined and each decision tree is independently trained. The
training procedure was employed as follows: (1) from the training dataset, a bootstrap sample was
drawn as a randomized subset; (2) each individual tree was grown using the randomized subset of
predictor variables. Each tree model f(xi) was defined as f(x) = Σ Tt(x). The trees were grown to the
largest extent possible without pruning; (3) repeat the step (2) until the number of trees was grown.
Then the predicted results were aggregated by averaging them.

Key Specifics:

• Uses bootstrap sampling to create multiple decision trees.

• Each tree is trained independently on different subsets of data.

• No pruning is applied, allowing trees to grow fully.

• The final prediction is obtained by aggregating results from all trees.

1.1. Importing Libraries:

import pandas as pd (Handles reading and processing the CSV file)

import numpy as np(Used for mathematical operations)

from [Link] import StandardScaler(Normalizes feature values to improve model


performance)

from sklearn.model_selection import train_test_split(Splits data into training and test sets)

from [Link] import RandomForestRegressor(Machine learning model used for prediction)

from [Link] import mean_squared_error, mean_absolute_error, r2_score(Used to evaluate


model accuracy)

import [Link] as plt(Used to visualize predictions vs. actual values)

1.2. Reading and Preparing Data:

df = pd.read_csv('vm_metrics.csv'): Reads the CSV file (vm_metrics.csv) into a pandas DataFrame


(df).

df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='s'): Converts the 'Timestamp' column into a


datetime format for time-based feature extraction

1.3. Feature Engineering:

. Extracts time-based features from the Timestamp column:

def create_features(data):

data['hour'] = data['Timestamp'].[Link]

data['day'] = data['Timestamp'].[Link]

data['month'] = data['Timestamp'].[Link]
data['dayofweek'] = data['Timestamp'].[Link]

1.4. Creating Lag Features(previous measurements):

data['lag_1'] = data['Value'].shift(1)

data['lag_2'] = data['Value'].shift(2)

data['rolling_mean'] = data['Value'].rolling(window=3).mean()

return [Link]()

lag_1 and lag_2:Stores the previous values to help the model learn from past trends.

rolling_mean : Computes the average value over the last 3 records to smooth out fluctuations.

dropna() :Removes rows with NaN values (caused by lagging)

1.5. Training and Evaluating the Model: Calls create_features() to add new time-based and lag
features to the data

def train_evaluate_model(df_metric, metric_name):

df_processed = create_features(df_metric)

[Link] Selection & Scaling:

X = df_processed[['hour', 'day', 'month', 'dayofweek', 'lag_1', 'lag_2', 'rolling_mean']]

y = df_processed['Value']

Defines X (features) and y (target variable):

X = Time-based features (hour, day, etc.) + Lag values.

y = The actual metric value we want to predict.

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

StandardScaler() → Normalizes the values so that large numerical differences don’t affect model
performance.

1.5.2. Splitting Data into Training & Test Sets:

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

.Splits data into:

• X_train (80%) → Used for training.

• X_test (20%) → Used for testing.

• y_train, y_test → Corresponding target values.

. random_state=42 → Ensures reproducibility (same split every time).

1.5.3. Training the Random Forest Model:

rf = RandomForestRegressor(n_estimators=100, random_state=42)
.Creates a RandomForestRegressor model:

• n_estimators=100 → Uses 100 decision trees for prediction.

• random_state=42 → Ensures reproducibility

[Link](X_train, y_train)

Trains the model (fit) on X_train and y_train.

1.5.4. Making Predictions:

y_pred = [Link](X_test): Uses the trained model to predict values for X_test

1.5.5. Evaluating Model Performance:

print(f'\nMetrics for {metric_name}:')

print(f'RMSE: {[Link](mean_squared_error(y_test, y_pred)):.2f}'): Measures how much the


predictions deviate from actual values.

print(f'MAE: {mean_absolute_error(y_test, y_pred):.2f}'): Measures the absolute difference


between actual and predicted values.

print(f'R2: {r2_score(y_test, y_pred):.2f}'): Measures how well the model explains the variability
(closer to 1.0 is better).

1.5.6. Plotting Actual vs. Predicted Values: Creates a line plot comparing actual vs. predicted values.

[Link](figsize=(12,6))

[Link](y_test.values, label='Actual')

[Link](y_pred, label='Predicted')

[Link](f'{metric_name} - Actual vs Predicted')

[Link]()

[Link]()

1.6. Looping Through Multiple Metrics: List of metrics that the script will process separately.

metrics = ['CPU Usage', 'Memory Used', 'Disk Usage', 'Network RX Bytes', 'Network TX Bytes', 'Device
Status']

1.6.1. Training & Evaluating for Each Metric:

for metric in metrics:

df_metric = df[df['Metric'] == metric].copy()

train_evaluate_model(df_metric, metric)

.Filters the dataset (df[df['Metric'] == metric]) to process each metric independently.

.Calls train_evaluate_model() for each metric.


[Link] of the model:

CPU:

The model performs well in predicting CPU usage, with an R² score of 0.92, meaning it explains 92%
of the variance in CPU usage. The RMSE (2.52) and MAE (1.05) indicate small errors, suggesting a
fairly accurate prediction.

Memory:

The R² score of 0.97 is excellent, meaning the model explains 97% of the variance in memory usage.
However, the RMSE and MAE values are very large, likely because the memory values are naturally in
the range of millions of bytes. If the dataset contains large values, these errors may still be
acceptable.

Disk:

The R² score of 1.00 suggests that the model fits the data almost perfectly, which is unusual. The
RMSE and MAE values are also extremely low, suggesting that disk usage is highly predictable and has
very little fluctuation in the data. However, an R² of 1.00 could indicate that the model might be
overfitting.

Network RX Bytes:
The model perfectly fits the data with an R² score of 1.00, meaning it captures all variance in the
network RX bytes. The RMSE and MAE values are large, but if the actual values are in the range of
millions of bytes, the error could be reasonable. The perfect R² might indicate the model has either a
very strong correlation in the data or possible overfitting.

Network TX Bytes:

Similar to Network RX Bytes, this model also achieves an R² of 1.00, meaning it explains 100% of the
variance. The RMSE and MAE are quite large, but their significance depends on the scale of actual
values.

Overfitting Reasons in Disk and Network Metrics(possible issues):

. disk usage and network metrics follow a repetitive or nearly constant pattern,and the dataset has
very little change over time the model learns these patterns perfectly, leading to an R² of 1.00.

. the dataset is too small or the test data is very similar to the training data, the model might
memorize instead of learning general trends.

. the train/test split is not random (e.g., if all test data comes from a period similar to training data),
the model performs well on the test set but may fail in real-world scenarios.

[Link] model: The XGBoost model uses a gradient boosting framework and is also a decision-
tree-based ensemble method. As the tree structure, f(x), the final prediction was calculated by
summing up the scores across all leaves and this can be expressed as ŷ = Σ fk(x). XGBoost makes
improvement on objective optimization function which is to optimize the loss function and
complexity punishment. We denoted the loss function and complexity punishment as Σ l(yi, ŷi) + Σ
Ω(fk), respectively.

Key Specifics:

• Uses gradient boosting, meaning trees are trained sequentially.


• Final prediction is the sum of all leaf scores in the decision trees.

• Optimizes both loss function and complexity penalty for better performance.

• Balances speed and accuracy better than traditional gradient boosting models.

2.1. Importing Required Libraries:

import pandas as pd(Used for handling and manipulating data in tabular format.)

import numpy as np(Helps with numerical operations.)

from [Link] import StandardScaler (Scales features to ensure they have the same
range, which improves model performance.)

from sklearn.model_selection import train_test_split(Splits data into training and testing sets.)

from [Link] import mean_squared_error, mean_absolute_error, r2_score(Computes


evaluation metrics like RMSE, MAE, and R² score.)

import [Link] as plt (Used for plotting graphs to visualize model performance.)

from xgboost import XGBRegressor (The main machine-learning model used for prediction.)

2.2. Reading & Preparing the Data:

df = pd.read_csv('vm_metrics.csv'): Reads the CSV file into a DataFrame.

df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='s'): Converts the "Timestamp" column into


a datetime format for easier manipulation.

2.3. Creating Features:

def create_features(data): data['hour'] = data['Timestamp'].[Link]

data['day'] = data['Timestamp'].[Link]

data['month'] = data['Timestamp'].[Link]

data['dayofweek'] = data['Timestamp'].[Link]

Lag features (previous values for time-series prediction):

data['lag_1'] = data['Value'].shift(1)

data['lag_2'] = data['Value'].shift(2)

data['rolling_mean'] = data['Value'].rolling(window=3).mean()

Drop NaN values created due to shifting/rolling:

[Link](inplace=True)

return data

. Handling Empty Data:

if df_metric.empty:
print(f'Skipping {metric_name} (No data available)')

return

. Checks if the DataFrame df_metric is empty.

. If there is no data for the given metric, it skips processing to avoid errors.

2.4. Feature Engineering:

df_processed = create_features(df_metric)

if df_processed.empty:

print(f'Skipping {metric_name} (No data after feature creation)')

return

. Calls create_features() to generate time-based and lag features.

. If all data is removed due to missing values (e.g., NaNs from lag features), it skips processing.

2.5. Preparing Feature Matrix (X) and Target Variable (y):

X = df_processed[['hour', 'day', 'month', 'dayofweek', 'lag_1', 'lag_2', 'rolling_mean']]

X (Feature Matrix): Contains independent variables:

• Time Features: hour, day, month, dayofweek

• Lag Features: lag_1, lag_2 (previous values)

• Rolling Mean: rolling_mean (smooths out fluctuations)

y = df_processed['Value']

y (Target Variable): The metric values we want to predict.

2.6. Scaling Features:

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

. Standardizes X to have mean = 0 and variance = 1.

. Helps improve model performance, especially for gradient-based models like XGBoost.

2.7. Splitting Data into Training & Testing Sets:

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

Splits the data into:

• 80% Training Data (X_train, y_train) → Used to train the model.

• 20% Test Data (X_test, y_test) → Used to evaluate the model.

random_state=42 ensures reproducibility.


Training the model:

2.8.1 Initializing & Training XGBoost Model:

xgb = XGBRegressor(n_estimators=100, random_state=42)

. Initializes an XGBoost Regressor with 100 decision trees.

. random_state=42 ensures the results are the same every time.

eval_set = [(X_train, y_train), (X_test, y_test)]

[Link](X_train, y_train, eval_set=eval_set, verbose=False)

. eval_set helps monitor training vs validation performance.

. verbose=False disables extra logs.

2.8.2. Making Predictions:

y_pred = [Link](X_test): Uses the trained XGBoost model to predict y values for the test set.

2.8.3. Computing Model Performance Metrics(Evaluates the model using):

rmse = [Link](mean_squared_error(y_test, y_pred)): Measures prediction error in the same unit as


y.

• mae = mean_absolute_error(y_test, y_pred): Average absolute difference between actual


and predicted values.

• r2 = r2_score(y_test, y_pred): Average absolute difference between actual and predicted


values.

print(f'\nMetrics for {metric_name}:')

print(f'RMSE: {rmse:.2f}')

print(f'MAE: {mae:.2f}')

print(f'R2: {r2:.2f}')

2.9. Plotting Actual vs Predicted Values:

[Link](figsize=(12, 6))

[Link](y_test.values, label='Actual')

[Link](y_pred, label='Predicted')

[Link](f'{metric_name} - Actual vs Predicted')

[Link]()

[Link]()

. Visualizes how well predictions align with actual values.

. The closer the two curves, the better the model.


2.10. Plotting Training & Validation Loss:

results = xgb.evals_result()

[Link](figsize=(10, 5))

[Link](results['validation_0']['rmse'], label='Training Loss')

[Link](results['validation_1']['rmse'], label='Validation Loss')

[Link](f'Training & Validation Loss - {metric_name}')

[Link]('Epochs')

[Link]('RMSE')

[Link]()

[Link]()

. Tracks RMSE (loss) during training.

. Helps detect overfitting:

• If training loss is much lower than validation loss then its Overfitting.

• If both decrease smoothly then Good training.

2.11. Looping Through Each Metric(Loop through each metric and train the model):

metrics = ['CPU Usage', 'Memory Used', 'Disk Usage', 'Network RX Bytes', 'Network TX Bytes',
'Device Status']

for metric in metrics:

df_metric = df[df['Metric'] == metric].copy()

train_evaluate_model(df_metric, metric)

.calls train_evaluate_model() to train and evaluate each metric.

.Loops through the list of metrics and filters data for each metric separately.

[Link] of the model:

CPU:
The model is performing well, but small improvements could be made by refining features or
hyperparameters.
Memory:

Model is performing very well, but it might be overfitting slightly. A more generalized approach
(reducing complexity or regularization) might be needed.

Disk:
Likely overfitting due to constant values or insufficient variability in the dataset.

Network RX:
network RX values are highly predictable and the model is overfitting.

Network Tx:
the data has low variability and the model is also overfitting.
Overfitting Reasons in Disk and Network Metrics(possible issues):

.The model has learned the exact training patterns instead of generalizing.

.Some metrics dont change over time .

. XGBoost and Random Forest are complex models with many parameters.

Solutions:

With a larger dataset, both Random Forests and XGBoost would likely perform better due to:

. More training examples reducing overfitting.

. Better pattern recognition across diverse scenarios.

. More robust feature importance detection.

However, given the complexity of VM metrics, neural networks (particularly LSTM or Transformer
architectures) might be more suitable for capturing temporal dependencies and non-linear
relationships in the data. This would be especially true if the dataset grows to include more complex
patterns and interactions.

You might also like