0% found this document useful (0 votes)
6 views6 pages

Data Analysis and Model Training Guide

Uploaded by

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

Data Analysis and Model Training Guide

Uploaded by

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

import numpy as np

import pandas as pd
import matplotlib as mpl
import [Link] as plt
%matplotlib inline
data = pd.read_csv('dataset_enrgy.csv',nrows=500,engine='python')
[Link]()
[Link]
[Link]
[Link]()
non_num_cols = [col for col in [Link] if data[col].dtype == 'O']
non_num_data = data[non_num_cols]
non_num_data
[(col, non_num_data[col].nunique()) for col in non_num_cols]
def summarize_cat(col_name):
sorted_values = sorted(non_num_data[col_name].value_counts().iteritems(), key =
lambda x:x[1], reverse=True)
remaining_per = 100
for (value, count) in sorted_values:
per = count / len(non_num_data) * 100
if per >= 1:
print(f'{value} : {per:.2f}%')
else :
print(f'Others : {remaining_per:.2f}%')
break
remaining_per = remaining_per - per
for col in non_num_cols:
print(f"Summary of {col} column : ")
summarize_cat(col)
print('\n')
num_cols = list(set([Link]) - set(non_num_cols))
num_cols
data[num_cols].describe()
[col for col in num_cols if data[col].isnull().any()]
cols_for_hist = [col for col in num_cols if data[col].nunique() <= 50]
cols_for_hist, len(cols_for_hist)
corr = data[num_cols].corr()
ipdata = [Link]()
print("No. of unique values in Timestamp column :",ipdata['Timestamp'].nunique())
print("No. of unique values in FlowID column :",ipdata['[Link]'].nunique())
[Link](['Timestamp', '[Link]'], axis = 1, inplace = True)
single_unique_cols = [col for col in [Link] if ipdata[col].nunique() == 1]
single_unique_colcorrs
[Link](single_unique_cols, axis = 1, inplace = True)
ip_add_cols = ['[Link]', '[Link]', '[Link]', '[Link]']
ipdata[ip_add_cols]
[Link](ip_add_cols, axis = 1, inplace = True)
from [Link] import LabelEncoder
encoder = LabelEncoder().fit(ipdata['ProtocolName'])
ipdata['ProtocolName'] = encoder.fit_transform(ipdata['ProtocolName'])
ipdata['ProtocolName']
[Link](10)
[Link]
[Link](inplace = True)
[Link](columns =["Label"], inplace = True)
df2 = data
[Link](columns =["[Link]", "[Link]", "[Link]",
"[Link]"], inplace = True) #better way is to replace it with country
using some lib like geo2ip but I am lazy
import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from [Link] import LabelEncoder
lb_make = LabelEncoder()
df2["labels"] = lb_make.fit_transform(df2["ProtocolName"])
df2[["ProtocolName", "labels"]].head(11)
from sklearn.model_selection import train_test_split

y = [Link][:,-1]
from [Link] import *
X,y=scaler_transform(df2)
ac=[]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,


random_state=0)
X_train.shape, y_train.shape
ac=[]
ac=[]

import random
from sklearn.linear_model import Ridge
model = Ridge(alpha=1.0)

def objective_function(x):
return x**2
class Atomic:
def __init__(self):
[Link] = [Link](-5, 5)
[Link] = [Link](-1, 1)
self.best_position = [Link]
self.best_score = float('inf')
def calculate_pdr(self,pred, original,sample_weight):
if original == 0:
return 0.0
else:
return pred / original
def calculate_throughput(self,data_size_bits, time_seconds,s):
return data_size_bits / time_seconds
def transmission_delay(self,best_fit,s):
for data_chunk in best_fit:
thread = [Link](target=send_data, args=(data_chunk,))
[Link]()
[Link](thread)
return thread
def calculate_network_lifespan(self,initial_lifespan, yearly_degradation_rate,
obsolescence_time,s):
remaining_lifespan = initial_lifespan
for year in range(1, initial_lifespan + 1):
remaining_lifespan -= 1
remaining_lifespan -= yearly_degradation_rate * initial_lifespan / 100
if year >= obsolescence_time:
remaining_lifespan = 0
break
return max(0, remaining_lifespan)
def atomic_optimization(self,num_particles, num_iterations):
particles = [Atomic() for _ in range(num_particles)]
global_best_position = None
global_best_score = float('inf')
for _ in range(num_iterations):
for particle in particles:
score = objective_function([Link])
if score < particle.best_score:
particle.best_score = score
particle.best_position = [Link]
if score < global_best_score:
global_best_score = score
global_best_position = [Link]
inertia_weight = 0.5
cognitive_weight = 0.5
social_weight = 0.5
[Link] = (inertia_weight * [Link] +
cognitive_weight * [Link]() *
(particle.best_position - [Link]) +
social_weight * [Link]() *
(global_best_position - [Link]))
[Link] += [Link]
return global_best_position, global_best_score
def train(self,X_train,y_train,X_test):
[Link](X_train,y_train)
y_pred=[Link](X_test)
return y_pred
num_particles = 30
num_iterations = 100
p=Atomic()
best_position, best_score =p.atomic_optimization(num_particles, num_iterations)
y_pred=[Link](X_train,y_train,X_test)
print("Best Position:", best_position)
print("Best Score:", best_score)
[Link](accuracy_score(y_pred,y_train,sample_weight=0.2)*100)
import random
import math
def objective_function(x):
return x**2
def calculate_throughput(data_size_bits, time_seconds,s):
return data_size_bits / time_seconds
def HRBO(num_particles, num_iterations, search_space_lower_bound,
search_space_upper_bound):
particles = [[Link](search_space_lower_bound, search_space_upper_bound)
for _ in range(num_particles)] # Initialize particles
for _ in range(num_iterations):
fitness_values = [objective_function(particle) for particle in particles]
best_particle_index = fitness_values.index(min(fitness_values))
best_particle = particles[best_particle_index]
best_fitness = fitness_values[best_particle_index]
for i in range(num_particles):
if i != best_particle_index:
distance = abs(particles[i] - best_particle)
gravitational_force = G * ((fitness_values[i] *
fitness_values[best_particle_index]) / (distance + epsilon))
random_force = [Link](0, 1) * (particles[i] -
best_particle)
particles[i] -= gravitational_force * random_force
return best_particle, best_fitness
def calculate_pdr(pred, original,sample_weight):
if original == 0:
return 0.0
else:
return pred / original
def transmission_delay(best_fit,s):
for data_chunk in best_fit:
thread = [Link](target=send_data, args=(data_chunk,))
[Link]()
[Link](thread)
return threads
def calculate_network_lifespan(initial_lifespan, yearly_degradation_rate,
obsolescence_time,s):
remaining_lifespan = initial_lifespan
for year in range(1, initial_lifespan + 1):
remaining_lifespan -= 1
remaining_lifespan -= yearly_degradation_rate * initial_lifespan / 100
if year >= obsolescence_time:
remaining_lifespan = 0
break
return max(0, remaining_lifespan)
def train(X_train,y_train,X_test):
[Link](X_train,y_train)
y_pred=[Link](X_test)
return y_pred
G = 6.67430e-11
epsilon = 1e-20
num_particles = 30
num_iterations = 100
search_space_lower_bound = -10
search_space_upper_bound = 10
best_solution, best_fitness = HRBO(num_particles, num_iterations,
search_space_lower_bound, search_space_upper_bound)
y_pred=train(X_train,y_train,X_test)
print("Best Fitness:", best_fitness)
[Link](accuracy_score(y_pred,y_train)*100)
class Bee:
def objective_function(x):
return x**2
def calculate_throughput(self,data_size_bits, time_seconds,s):
return data_size_bits / time_seconds
def calculate_pdr(pred, original,sample_weight):
if original == 0: return 0.0
else:
return pred / original
def transmission_delay(self,best_fit,s):
for data_chunk in best_fit:
thread = [Link](target=send_data, args=(data_chunk,))
[Link]()
[Link](thread)
return threads
def calculate_network_lifespan(self,initial_lifespan, yearly_degradation_rate,
obsolescence_time,s):
remaining_lifespan = initial_lifespan
for year in range(1, initial_lifespan + 1):
remaining_lifespan -= 1
remaining_lifespan -= yearly_degradation_rate * initial_lifespan / 100
if year >= obsolescence_time:
remaining_lifespan = 0
break
return max(0, remaining_lifespan)
import [Link] as plt
from [Link] import *
c=Atomic()
b=Bee()
x = [1, 2, 3, 4, 5,6,7,8,9,10]
y = c.calculate_pdr(y_test,y_pred)
y1= calculate_pdr(y_test,y_pred)
y2=b.calculate_pdr(y_test,y_pred)
[Link](x, y, label='LSTM', marker='o')
[Link](x, y1, label='CNN', marker='o')
[Link](x, y2, label='RNN', marker='o')
[Link]('Time Intervals')
[Link]('Ratio')
[Link]('Packet Delivery')
[Link]()
[Link](True)
[Link]()
import [Link] as plt
from [Link] import *
c=Atomic()
b=Bee()
x = [1, 2, 3, 4, 5,6,7,8,9,10]
y = c.calculate_throughput(y, 30)
y1= calculate_throughput(y, 30)
y2= b.calculate_throughput(y, 30)
[Link](x, y, label='LSTM', marker='o')
[Link](x, y1, label='CNN', marker='o')
[Link](x, y2, label='RNN', marker='o')
[Link]('Time Intervals')
[Link]('Throughput')
[Link]('Improved Throughput')
[Link]()
[Link](True)
[Link]()
import [Link] as plt
from [Link] import *
c=Atomic()
b=Bee()
x = [1, 2, 3, 4, 5,6,7,8,9,10]
y = c.transmission_delay(y_test,y_pred)
y1= transmission_delay(y_test,y_pred)
y2=b.transmission_delay(y_test,y_pred)
[Link](x, y, label='LSTM', marker='o')
[Link](x, y1, label='CNN', marker='o')
[Link](x, y2, label='RNN', marker='o')
[Link]('Time Intervals')
[Link]('Transmission Delay')
[Link]('Transmission Delay')
[Link]()
[Link](True)
[Link]()
import [Link] as plt
from [Link] import *
c=Atomic()
b=Bee()
x = [1, 2, 3, 4, 5,6,7,8,9,10]
y = c.calculate_network_lifespan(y_test,y_pred,5)
y1= calculate_network_lifespan(y_test,y_pred,5)
y2=b.calculate_network_lifespan(y_test,y_pred,5)
[Link](x, y, label='LSTM', marker='o')
[Link](x, y1, label='CNN', marker='o')
[Link](x, y2, label='RNN', marker='o')
[Link]('Time Intervals')
[Link]('Network Lifespan')
[Link]('Liffespan')
[Link]()
[Link](True)
[Link]()

You might also like