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

add random forest classifier notebook

parent d1c3af53
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
from sklearn import tree
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
df = pd.read_csv("../../dataset/DM_data.csv")
df.info()
```
%% Output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 25192 entries, 0 to 25191
Data columns (total 40 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 duration 25192 non-null int64
1 protocol_type 25192 non-null int64
2 service 25192 non-null int64
3 flag 25192 non-null int64
4 src_bytes 25192 non-null int64
5 dst_bytes 25192 non-null int64
6 land 25192 non-null int64
7 wrong_fragment 25192 non-null int64
8 hot 25192 non-null int64
9 num_failed_logins 25192 non-null int64
10 logged_in 25192 non-null int64
11 num_compromised 25192 non-null int64
12 root_shell 25192 non-null int64
13 su_attempted 25192 non-null int64
14 num_root 25192 non-null int64
15 num_file_creations 25192 non-null int64
16 num_shells 25192 non-null int64
17 num_access_files 25192 non-null int64
18 is_guest_login 25192 non-null int64
19 count 25192 non-null int64
20 srv_count 25192 non-null int64
21 serror_rate 25192 non-null float64
22 srv_serror_rate 25192 non-null float64
23 rerror_rate 25192 non-null float64
24 srv_rerror_rate 25192 non-null float64
25 same_srv_rate 25192 non-null float64
26 diff_srv_rate 25192 non-null float64
27 srv_diff_host_rate 25192 non-null float64
28 dst_host_count 25192 non-null int64
29 dst_host_srv_count 25192 non-null int64
30 dst_host_same_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
33 dst_host_srv_diff_host_rate 25192 non-null float64
34 dst_host_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
37 dst_host_srv_rerror_rate 25192 non-null float64
38 class 25192 non-null int64
39 index_num 25192 non-null int64
dtypes: float64(15), int64(25)
memory usage: 7.7 MB
%% Cell type:code id: tags:
``` python
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.reset_index(drop=True, inplace=True)
X_test.reset_index(drop=True, inplace=True)
y_train.reset_index(drop=True, inplace=True)
X_test.reset_index(drop=True, inplace=True)
X_train.info()
```
%% Output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 20153 entries, 0 to 20152
Data columns (total 38 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 duration 20153 non-null int64
1 protocol_type 20153 non-null int64
2 service 20153 non-null int64
3 flag 20153 non-null int64
4 src_bytes 20153 non-null int64
5 dst_bytes 20153 non-null int64
6 land 20153 non-null int64
7 wrong_fragment 20153 non-null int64
8 hot 20153 non-null int64
9 num_failed_logins 20153 non-null int64
10 logged_in 20153 non-null int64
11 num_compromised 20153 non-null int64
12 root_shell 20153 non-null int64
13 su_attempted 20153 non-null int64
14 num_root 20153 non-null int64
15 num_file_creations 20153 non-null int64
16 num_shells 20153 non-null int64
17 num_access_files 20153 non-null int64
18 is_guest_login 20153 non-null int64
19 count 20153 non-null int64
20 srv_count 20153 non-null int64
21 serror_rate 20153 non-null float64
22 srv_serror_rate 20153 non-null float64
23 rerror_rate 20153 non-null float64
24 srv_rerror_rate 20153 non-null float64
25 same_srv_rate 20153 non-null float64
26 diff_srv_rate 20153 non-null float64
27 srv_diff_host_rate 20153 non-null float64
28 dst_host_count 20153 non-null int64
29 dst_host_srv_count 20153 non-null int64
30 dst_host_same_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
33 dst_host_srv_diff_host_rate 20153 non-null float64
34 dst_host_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
37 dst_host_srv_rerror_rate 20153 non-null float64
dtypes: float64(15), int64(23)
memory usage: 5.8 MB
%% Cell type:code id: tags:
``` python
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}")
```
%% Output
Shape of X_train: (20153, 38)
Shape of X_test: (5039, 38)
Shape of y_train: (20153,)
Shape of y_test: (5039,)
%% Cell type:code id: tags:
``` python
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=20, max_depth=5,random_state=0)
clf.fit(X_train, y_train)
```
%% Output
RandomForestClassifier(max_depth=5, n_estimators=20, random_state=0)
%% Cell type:code id: tags:
``` python
from IPython import display
import graphviz
import os
os.environ["PATH"] += os.pathsep + 'C:\\Users\\s\\anaconda3\\pkgs\\graphviz-2.38-hfd603c8_2\\Library\\bin\\graphviz'
# 시각화
dot_data1 = tree.export_graphviz(clf.estimators_[5],
out_file = None,
feature_names = X_train.columns,
class_names = ["0", "1"],
filled = True,
rounded = True,
special_characters = True)
graph = graphviz.Source(dot_data1)
graph
```
%% Cell type:code id: tags:
``` python
from sklearn.metrics import precision_recall_fscore_support
clf_y_pred = clf.predict(X_test)
precision_recall_fscore_support(y_test, clf_y_pred, average="binary")
```
%% Output
(0.9974059662775616, 0.9754756871035941, 0.9863189397178281, None)
%% Cell type:code id: tags:
``` python
from sklearn.metrics import accuracy_score
accuracy_score(y_test ,clf_y_pred)
```
%% Output
0.9872990672752531
%% Cell type:code id: tags:
``` python
```
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