Question: Do this on Pycharm: You need to create a AI - model to perform the intrusion detection. The source code of decision tree - based

Do this on Pycharm:
You need to create a AI-model to perform the intrusion detection. The source code of decision tree-based AI models and the datasets (training and testing) are provided in the attachments.
____________________________________________________________________
Python code:
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import Normalizer
from sklearn.metrics import accuracy_score
# read the training data set and the test data set.
train_data = pd.read_csv('kddtrain.csv', header=None)
test_data = pd.read_csv('kddtest.csv', header=None)
# Normalize the training data set
X = train_data.iloc[:,1:42]
scaler = Normalizer().fit(X)
X_train = scaler.transform(X)
X_train = np.array(X_train)
y_train = np.array(train_data.iloc[:,0])
# Normal the test data set
T = test_data.iloc[:,1:42]
scaler = Normalizer().fit(T)
X_test = scaler.transform(T)
X_test = np.array(X_test) #X_test
y_test = np.array(test_data.iloc[:,0])
# Create a decision tree model.
model = DecisionTreeClassifier()
# Train the decision tree model
model.fit(X_train, y_train)
# make predictions
y_pred = model.predict(X_test)
# Calculate the prediction accuracy
accuracy = accuracy_score(y_test, y_pred)
# Print out the prediction accuracy
print("accuracy: %.3f"%accuracy)
# print out the feature values of the 31th sample.
print(X_test[31, :])
print("The true lable (Normal:0 or Intrusion:1): ", y_test[31])
print("The predicted lable (Normal:0 or Intrusion:1): ", y_pred[31])

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!