<jupyter_start><jupyter_text>Encoding Categorical columns<jupyter_code>from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
data_train["saddr_enc"]= le.fit_transform(data_train.saddr)
data_train["daddr_enc"]= le.fit_transform(data_train.daddr)
data_train["proto_enc"]= le.fit_transform(data_train.proto)
data_train["target_enc"]= le.fit_transform(data_train.target)
data_train.head()
# Dropping Redundant Columns
data_train.drop(['saddr','daddr','proto','target'], axis=1, inplace=True)
data_test["saddr_enc"]= le.fit_transform(data_test.saddr)
data_test["daddr_enc"]= le.fit_transform(data_test.daddr)
data_test["proto_enc"]= le.fit_transform(data_test.proto)
data_test["target_enc"]= le.fit_transform(data_test.target)
data_test.drop(['saddr','daddr','proto','target'], axis=1, inplace=True)
data_train['target_enc'].value_counts()<jupyter_output><empty_output><jupyter_text>5 - DoS_UDP                          1 - DDoS_TCP                         2 - DDoS_UDP                         4 - DoS_TCP                          8 - Reconnaissance_Service_Scan       7 - Reconnaissance_OS_Fingerprint     3 - DoS_HTTP                           0 - DDoS_HTTP                           6 - Normal_Normal<jupyter_code><jupyter_output><empty_output><jupyter_text>Scaling<jupyter_code>y = data_train['target_enc']
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
features = data_train.iloc[:,:-1]
cols=features.columns
scaled_features= scaler.fit_transform(features)
data_train= pd.DataFrame(scaled_features,columns=cols)
data_train.head()
ytest = data_test['target_enc']
features = data_test.iloc[:,:-1]
cols=features.columns
scaled_features= scaler.fit_transform(features)
data_test= pd.DataFrame(scaled_features,columns=cols)
data_test.head()
y.value_counts()
x = data_train
xtest = data_test<jupyter_output><empty_output><jupyter_text>Sampling<jupyter_code>import imblearn
from imblearn.under_sampling import RandomUnderSampler
samp_strat= {5:70000, 1:70000, 2:70000, 4:65000, 8:58592, 7:14267, 3:1179, 0:786, 6:332}
random_under= RandomUnderSampler(sampling_strategy=samp_strat,random_state=1)
X_rus,y_rus = random_under.fit_resample(x,y)
pd.Series(y_rus).value_counts()
from imblearn.over_sampling import RandomOverSampler
samp_strat= {5:70000, 1:70000, 2:70000, 4:65000, 8:58592, 7:30000, 3:20000, 0:15000, 6:8000}
random_under= RandomOverSampler(sampling_strategy=samp_strat,random_state=1)
Xres,yres = random_under.fit_resample(X_rus,y_rus)
plt.figure(figsize=(10,5))
sns.countplot(yres,palette='magma')
from sklearn import model_selection
X_train, X_test, y_train, y_test = model_selection.train_test_split(Xres,yres, test_size=0.20, random_state=42, stratify=yres)
import time
start = time.time()
from sklearn.linear_model import LogisticRegression
model_1 = LogisticRegression(solver='liblinear')
model_1.fit(X_train, y_train)
pred_1= model_1.predict(X_test)
score1 = model_1.score(X_test, y_test)
end = time.time()
print(end - start, "seconds\n")
print("Accuracy of base model: ",score1)

pred_2 = model_1.predict(xtest)
score2 = model_1.score(xtest,ytest)
print("Accuracy of test model: ",score2)
from sklearn.metrics import multilabel_confusion_matrix
multilabel_confusion_matrix(y_test,pred_1)
from sklearn.model_selection import KFold,StratifiedKFold,cross_val_score
model_1 = LogisticRegression(solver='liblinear')
model_1.fit(X_train,y_train)
score3 = cross_val_score(model_1, X_train, y_train)
score3
score3.mean()
from sklearn.linear_model import LogisticRegression
logModel=LogisticRegression()
param_grid= [
             {'solver' : ['lbfgs'],'penalty' : ['l1','l2'],'C':[0.0001,.009,0.01,1,5,10,25], 'max_iter' : [1000], 'n_jobs' : [100] }  ]
from sklearn.model_selection import GridSearchCV
clf = GridSearchCV(logModel, param_grid = param_grid, cv=3, verbose=3)
best_clf = clf.fit(X_train,y_train)
best_clf.best_score_
best_clf.best_params_
import time
start = time.time()
from sklearn.linear_model import LogisticRegression
model_2 = LogisticRegression(C= 25, max_iter= 1000, n_jobs= 100, penalty='l2', solver= 'lbfgs')
model_2.fit(X_train, y_train)
pred_3= model_2.predict(X_test)
score5 = model_2.score(X_test, y_test)
end = time.time()
print(end - start, "seconds\n")
print("Accuracy of model with best parameters: ",score5)
pred_4 = model_2.predict(xtest)
score6 = model_2.score(xtest,ytest)
print("Accuracy of test model with best parameters: ",score6)
from sklearn.metrics import multilabel_confusion_matrix
multilabel_confusion_matrix(y_test,pred_3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12288042281435807, 'fraction_numerical': 0.042942083241576744, 'mean_word_length': 6.885416666666667, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '401894', 'license': 'MIT', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1681, 'n_words': 268, 'path': 'LogisticRegression.ipynb', 'repo_name': 'notakshit/Intrusion-Detection-System', 'size': '4541'}