Skip to content
Snippets Groups Projects
Commit 230e8e68 authored by Lani Jung's avatar Lani Jung
Browse files

add roc curve

parent be23b7d3
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
from sklearn import tree from sklearn import tree
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
import numpy as np import numpy as np
import pandas as pd import pandas as pd
df = pd.read_csv("../../dataset/DM_data.csv") df = pd.read_csv("../../dataset/DM_data.csv")
df.info() df.info()
``` ```
%% Output %% Output
<class 'pandas.core.frame.DataFrame'> <class 'pandas.core.frame.DataFrame'>
RangeIndex: 25192 entries, 0 to 25191 RangeIndex: 25192 entries, 0 to 25191
Data columns (total 40 columns): Data columns (total 40 columns):
# Column Non-Null Count Dtype # Column Non-Null Count Dtype
--- ------ -------------- ----- --- ------ -------------- -----
0 duration 25192 non-null int64 0 duration 25192 non-null int64
1 protocol_type 25192 non-null int64 1 protocol_type 25192 non-null int64
2 service 25192 non-null int64 2 service 25192 non-null int64
3 flag 25192 non-null int64 3 flag 25192 non-null int64
4 src_bytes 25192 non-null int64 4 src_bytes 25192 non-null int64
5 dst_bytes 25192 non-null int64 5 dst_bytes 25192 non-null int64
6 land 25192 non-null int64 6 land 25192 non-null int64
7 wrong_fragment 25192 non-null int64 7 wrong_fragment 25192 non-null int64
8 hot 25192 non-null int64 8 hot 25192 non-null int64
9 num_failed_logins 25192 non-null int64 9 num_failed_logins 25192 non-null int64
10 logged_in 25192 non-null int64 10 logged_in 25192 non-null int64
11 num_compromised 25192 non-null int64 11 num_compromised 25192 non-null int64
12 root_shell 25192 non-null int64 12 root_shell 25192 non-null int64
13 su_attempted 25192 non-null int64 13 su_attempted 25192 non-null int64
14 num_root 25192 non-null int64 14 num_root 25192 non-null int64
15 num_file_creations 25192 non-null int64 15 num_file_creations 25192 non-null int64
16 num_shells 25192 non-null int64 16 num_shells 25192 non-null int64
17 num_access_files 25192 non-null int64 17 num_access_files 25192 non-null int64
18 is_guest_login 25192 non-null int64 18 is_guest_login 25192 non-null int64
19 count 25192 non-null int64 19 count 25192 non-null int64
20 srv_count 25192 non-null int64 20 srv_count 25192 non-null int64
21 serror_rate 25192 non-null float64 21 serror_rate 25192 non-null float64
22 srv_serror_rate 25192 non-null float64 22 srv_serror_rate 25192 non-null float64
23 rerror_rate 25192 non-null float64 23 rerror_rate 25192 non-null float64
24 srv_rerror_rate 25192 non-null float64 24 srv_rerror_rate 25192 non-null float64
25 same_srv_rate 25192 non-null float64 25 same_srv_rate 25192 non-null float64
26 diff_srv_rate 25192 non-null float64 26 diff_srv_rate 25192 non-null float64
27 srv_diff_host_rate 25192 non-null float64 27 srv_diff_host_rate 25192 non-null float64
28 dst_host_count 25192 non-null int64 28 dst_host_count 25192 non-null int64
29 dst_host_srv_count 25192 non-null int64 29 dst_host_srv_count 25192 non-null int64
30 dst_host_same_srv_rate 25192 non-null float64 30 dst_host_same_srv_rate 25192 non-null float64
31 dst_host_diff_srv_rate 25192 non-null float64 31 dst_host_diff_srv_rate 25192 non-null float64
32 dst_host_same_src_port_rate 25192 non-null float64 32 dst_host_same_src_port_rate 25192 non-null float64
33 dst_host_srv_diff_host_rate 25192 non-null float64 33 dst_host_srv_diff_host_rate 25192 non-null float64
34 dst_host_serror_rate 25192 non-null float64 34 dst_host_serror_rate 25192 non-null float64
35 dst_host_srv_serror_rate 25192 non-null float64 35 dst_host_srv_serror_rate 25192 non-null float64
36 dst_host_rerror_rate 25192 non-null float64 36 dst_host_rerror_rate 25192 non-null float64
37 dst_host_srv_rerror_rate 25192 non-null float64 37 dst_host_srv_rerror_rate 25192 non-null float64
38 class 25192 non-null int64 38 class 25192 non-null int64
39 index_num 25192 non-null int64 39 index_num 25192 non-null int64
dtypes: float64(15), int64(25) dtypes: float64(15), int64(25)
memory usage: 7.7 MB memory usage: 7.7 MB
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
X, y = df.drop(columns=["class", "index_num"]), df['class'] X, y = df.drop(columns=["class", "index_num"]), df['class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=True, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=True, random_state=42)
X_train.reset_index(drop=True, inplace=True) X_train.reset_index(drop=True, inplace=True)
X_test.reset_index(drop=True, inplace=True) X_test.reset_index(drop=True, inplace=True)
y_train.reset_index(drop=True, inplace=True) y_train.reset_index(drop=True, inplace=True)
X_test.reset_index(drop=True, inplace=True) X_test.reset_index(drop=True, inplace=True)
X_train.info() X_train.info()
``` ```
%% Output %% Output
<class 'pandas.core.frame.DataFrame'> <class 'pandas.core.frame.DataFrame'>
RangeIndex: 20153 entries, 0 to 20152 RangeIndex: 20153 entries, 0 to 20152
Data columns (total 38 columns): Data columns (total 38 columns):
# Column Non-Null Count Dtype # Column Non-Null Count Dtype
--- ------ -------------- ----- --- ------ -------------- -----
0 duration 20153 non-null int64 0 duration 20153 non-null int64
1 protocol_type 20153 non-null int64 1 protocol_type 20153 non-null int64
2 service 20153 non-null int64 2 service 20153 non-null int64
3 flag 20153 non-null int64 3 flag 20153 non-null int64
4 src_bytes 20153 non-null int64 4 src_bytes 20153 non-null int64
5 dst_bytes 20153 non-null int64 5 dst_bytes 20153 non-null int64
6 land 20153 non-null int64 6 land 20153 non-null int64
7 wrong_fragment 20153 non-null int64 7 wrong_fragment 20153 non-null int64
8 hot 20153 non-null int64 8 hot 20153 non-null int64
9 num_failed_logins 20153 non-null int64 9 num_failed_logins 20153 non-null int64
10 logged_in 20153 non-null int64 10 logged_in 20153 non-null int64
11 num_compromised 20153 non-null int64 11 num_compromised 20153 non-null int64
12 root_shell 20153 non-null int64 12 root_shell 20153 non-null int64
13 su_attempted 20153 non-null int64 13 su_attempted 20153 non-null int64
14 num_root 20153 non-null int64 14 num_root 20153 non-null int64
15 num_file_creations 20153 non-null int64 15 num_file_creations 20153 non-null int64
16 num_shells 20153 non-null int64 16 num_shells 20153 non-null int64
17 num_access_files 20153 non-null int64 17 num_access_files 20153 non-null int64
18 is_guest_login 20153 non-null int64 18 is_guest_login 20153 non-null int64
19 count 20153 non-null int64 19 count 20153 non-null int64
20 srv_count 20153 non-null int64 20 srv_count 20153 non-null int64
21 serror_rate 20153 non-null float64 21 serror_rate 20153 non-null float64
22 srv_serror_rate 20153 non-null float64 22 srv_serror_rate 20153 non-null float64
23 rerror_rate 20153 non-null float64 23 rerror_rate 20153 non-null float64
24 srv_rerror_rate 20153 non-null float64 24 srv_rerror_rate 20153 non-null float64
25 same_srv_rate 20153 non-null float64 25 same_srv_rate 20153 non-null float64
26 diff_srv_rate 20153 non-null float64 26 diff_srv_rate 20153 non-null float64
27 srv_diff_host_rate 20153 non-null float64 27 srv_diff_host_rate 20153 non-null float64
28 dst_host_count 20153 non-null int64 28 dst_host_count 20153 non-null int64
29 dst_host_srv_count 20153 non-null int64 29 dst_host_srv_count 20153 non-null int64
30 dst_host_same_srv_rate 20153 non-null float64 30 dst_host_same_srv_rate 20153 non-null float64
31 dst_host_diff_srv_rate 20153 non-null float64 31 dst_host_diff_srv_rate 20153 non-null float64
32 dst_host_same_src_port_rate 20153 non-null float64 32 dst_host_same_src_port_rate 20153 non-null float64
33 dst_host_srv_diff_host_rate 20153 non-null float64 33 dst_host_srv_diff_host_rate 20153 non-null float64
34 dst_host_serror_rate 20153 non-null float64 34 dst_host_serror_rate 20153 non-null float64
35 dst_host_srv_serror_rate 20153 non-null float64 35 dst_host_srv_serror_rate 20153 non-null float64
36 dst_host_rerror_rate 20153 non-null float64 36 dst_host_rerror_rate 20153 non-null float64
37 dst_host_srv_rerror_rate 20153 non-null float64 37 dst_host_srv_rerror_rate 20153 non-null float64
dtypes: float64(15), int64(23) dtypes: float64(15), int64(23)
memory usage: 5.8 MB memory usage: 5.8 MB
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
print(f"Shape of X_train: {X_train.shape}\nShape of X_test: {X_test.shape}") print(f"Shape of X_train: {X_train.shape}\nShape of X_test: {X_test.shape}")
print(f"Shape of y_train: {y_train.shape}\nShape of y_test: {y_test.shape}") print(f"Shape of y_train: {y_train.shape}\nShape of y_test: {y_test.shape}")
``` ```
%% Output %% Output
Shape of X_train: (20153, 38) Shape of X_train: (20153, 38)
Shape of X_test: (5039, 38) Shape of X_test: (5039, 38)
Shape of y_train: (20153,) Shape of y_train: (20153,)
Shape of y_test: (5039,) Shape of y_test: (5039,)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
clf1 = tree.DecisionTreeClassifier() clf1 = tree.DecisionTreeClassifier()
clf1 = clf1.fit(X_train, y_train) clf1 = clf1.fit(X_train, y_train)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
from IPython import display from IPython import display
import graphviz import graphviz
import os import os
os.environ["PATH"] += os.pathsep + 'C:\\Users\\s\\anaconda3\\pkgs\\graphviz-2.38-hfd603c8_2\\Library\\bin\\graphviz' os.environ["PATH"] += os.pathsep + 'C:\\Users\\s\\anaconda3\\pkgs\\graphviz-2.38-hfd603c8_2\\Library\\bin\\graphviz'
# 시각화 # 시각화
dot_data1 = tree.export_graphviz(clf1, dot_data1 = tree.export_graphviz(clf1,
out_file = None, out_file = None,
feature_names = X_train.columns, feature_names = X_train.columns,
class_names = ["0", "1"], class_names = ["0", "1"],
filled = True, filled = True,
rounded = True, rounded = True,
special_characters = True) special_characters = True)
graph1 = graphviz.Source(dot_data1) graph1 = graphviz.Source(dot_data1)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
clf2 = tree.DecisionTreeClassifier(criterion = "entropy") clf2 = tree.DecisionTreeClassifier(criterion = "entropy")
clf2 = clf2.fit(X_train, y_train) clf2 = clf2.fit(X_train, y_train)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
dot_data2 = tree.export_graphviz(clf2, dot_data2 = tree.export_graphviz(clf2,
out_file = None, out_file = None,
feature_names = X_train.columns, feature_names = X_train.columns,
class_names = ["0", "1"], class_names = ["0", "1"],
filled = True, filled = True,
rounded = True, rounded = True,
special_characters = True) special_characters = True)
graph2 = graphviz.Source(dot_data2) graph2 = graphviz.Source(dot_data2)
#graph2 #graph2
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
clf1_y_pred = clf1.predict(X_test) clf1_y_pred = clf1.predict(X_test)
clf2_y_pred = clf2.predict(X_test) clf2_y_pred = clf2.predict(X_test)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
from sklearn.metrics import precision_recall_fscore_support from sklearn.metrics import precision_recall_fscore_support
precision_recall_fscore_support(y_test, clf1_y_pred, average="binary") precision_recall_fscore_support(y_test, clf1_y_pred, average="binary")
``` ```
%% Output %% Output
(0.9953625632377741, 0.99830866807611, 0.9968334388853705, None) (0.9941052631578947, 0.99830866807611, 0.9962025316455696, None)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
precision_recall_fscore_support(y_test, clf2_y_pred, average="binary") precision_recall_fscore_support(y_test, clf2_y_pred, average="binary")
``` ```
%% Output %% Output
(0.9957788096243141, 0.9974630021141649, 0.9966201943388256, None) (0.9957805907172996, 0.9978858350951374, 0.996832101372756, None)
%% Cell type:code id: tags:
```
from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt
def plot_roc_curve(fper, tper):
plt.plot(fper, tper, color='red', label='ROC')
plt.plot([0, 1], [0, 1], color='green', linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic Curve')
plt.legend()
plt.show()
prob = clf2.predict_proba(X_test)
prob = prob[:, 1]
fper, tper, thresholds = roc_curve(y_test, prob)
plot_roc_curve(fper, tper)
```
%% Output
%% Cell type:code id: tags:
```
```
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment