Robotics
HCW1202 commited on
Commit
9650bca
1 Parent(s): 51198a3

Upload data.py

Browse files
Files changed (1) hide show
  1. data.py +84 -0
data.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Fri Apr 26 16:31:20 2019
4
+
5
+ @author: ELİF NUR
6
+ """
7
+ import pandas as pd
8
+ import numpy as np
9
+ from sklearn.model_selection import train_test_split
10
+ from sklearn.preprocessing import LabelEncoder
11
+ from sklearn import preprocessing
12
+
13
+ def loadData(fromPath,LabelColumnName,labelCount):#This method to read the csv file and change the label feature
14
+
15
+ data_=pd.read_csv(fromPath)
16
+ if labelCount==2:
17
+ dataset=data_
18
+ dataset[LabelColumnName]=dataset[LabelColumnName].apply({'DoS':'Anormal','BENIGN':'Normal' ,'DDoS':'Anormal', 'PortScan':'Anormal'}.get)
19
+ else:
20
+ dataset=data_
21
+ data=dataset[LabelColumnName].value_counts()
22
+ data.plot(kind='pie')
23
+ featureList= dataset.drop([LabelColumnName],axis=1).columns
24
+ return dataset,featureList
25
+
26
+ def datasetSplit(df,LabelColumnName):#This method is to separate the dataset as X and y.
27
+ labelencoder = LabelEncoder()
28
+ df.iloc[:, -1] = labelencoder.fit_transform(df.iloc[:, -1])
29
+ X = df.drop([LabelColumnName],axis=1)
30
+ X = np.array(X)
31
+ X = X.T
32
+ for column in X: #Control of values in X
33
+ median = np.nanmedian(column)
34
+ column[np.isnan(column)] = median
35
+ column[column == np.inf] = 0
36
+ column[column == -np.inf] = 0
37
+ X = X.T
38
+ scaler = preprocessing.MinMaxScaler()
39
+ X= scaler.fit_transform(X)
40
+ y=df[[LabelColumnName]]
41
+ return X,y
42
+
43
+ def train_test_dataset(df): #This method is to separate the dataset as X_train,X_test,y_train and y_test.
44
+ labelencoder = LabelEncoder()
45
+ df.iloc[:, -1] = labelencoder.fit_transform(df.iloc[:, -1])
46
+ X = df.drop([LabelColumnName],axis=1)
47
+ y=df[[LabelColumnName]]
48
+ X_train, X_test, y_train, y_test = train_test_split(X,y, train_size = 0.7, test_size = 0.3, random_state = 0, stratify = y)
49
+ X_train = np.array(X_train)
50
+ X_train = X_train.T
51
+ for column in X_train:
52
+ median = np.nanmedian(column)
53
+ column[np.isnan(column)] = median
54
+ column[column == np.inf] = 0
55
+ column[column == -np.inf] = 0
56
+ X_train = X_train.T
57
+ y_train = np.array(y_train)
58
+ y_train = y_train.T
59
+ for column in y_train:
60
+ median = np.nanmedian(column)
61
+ column[np.isnan(column)] = median
62
+ column[column == np.inf] = 0
63
+ column[column == -np.inf] = 0
64
+ y_train = y_train.T
65
+ X_test = np.array(X_test)
66
+ X_test = X_test.T
67
+ for column in X_test:
68
+ median = np.nanmedian(column)
69
+ column[np.isnan(column)] = median
70
+ column[column == np.inf] = 0
71
+ column[column == -np.inf] = 0
72
+ X_test = X_test.T
73
+ y_test = np.array(y_test)
74
+ y_test = y_test.T
75
+ for column in y_test:
76
+ median = np.nanmedian(column)
77
+ column[np.isnan(column)] = median
78
+ column[column == np.inf] = 0
79
+ column[column == -np.inf] = 0
80
+ y_test = y_test.T
81
+
82
+
83
+ return X_train, X_test, y_train, y_test
84
+