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

Tested AI Model3

The document outlines a process for training and evaluating a Random Forest model using Python libraries to predict various metrics from a CSV file. It includes steps for data preparation, feature engineering, model training, and performance evaluation, along with visualizing predictions. The script processes multiple metrics independently, ensuring reproducibility and accuracy in predictions.

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 views3 pages

Tested AI Model3

The document outlines a process for training and evaluating a Random Forest model using Python libraries to predict various metrics from a CSV file. It includes steps for data preparation, feature engineering, model training, and performance evaluation, along with visualizing predictions. The script processes multiple metrics independently, ensuring reproducibility and accuracy in predictions.

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:

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.

You might also like