-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalware.py
More file actions
93 lines (55 loc) · 2.41 KB
/
Copy pathmalware.py
File metadata and controls
93 lines (55 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
br# -*- coding: utf-8 -*-
"""Untitled0.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1wE5LX4Zs6MiNwGw34xbAadUOYbNF2NNm
"""
!pip install pefile
import pandas as pd
malData = pd.read_csv("/content/sample_data/MalwareData.csv", sep="|")
malData.head()
malData.describe()
legit = malData[0:41323].drop(["legitimate"], axis=1)
mal = malData[41323::].drop(["legitimate"], axis=1)
print("The shape of the legit dataset is :%s samples, %s features "%(legit.shape[0],legit.shape[1]))
print("The shape of the mal dataset is :%s samples, %s features "%(mal.shape[0],mal.shape[1]))
print(malData.columns)
print(malData.head(5))
pd.set_option("display.max_columns", None)
print(malData.head(5))
print(legit.take([1]))
print(mal.take([1]))
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
data_in = malData.drop(['Name','md5','legitimate'], axis=1).values
labels = malData['legitimate']
extratrees = ExtraTreesClassifier().fit(data_in,labels)
select = SelectFromModel(extratrees, prefit=True)
data_in_new = select.transform(data_in)
print(data_in.shape,data_in_new.shape)
import numpy as np
features = data_in_new.shape[1]
importances = extratrees.feature_importances_
indices = np.argsort(importances)[::-1]
for f in range(features):
print("%d"%(f+1),malData.columns[2+indices[f]],importances[indices[f]])
from sklearn.ensemble import RandomForestClassifier
legit_train, legit_test, mal_train, mal_test = train_test_split(data_in_new, labels, test_size=0.2)
classif = RandomForestClassifier(n_estimators=50)
classif.fit(legit_train,mal_train)
print("The score of the algorithm:",classif.score(legit_test,mal_test)*100)
from sklearn.metrics import confusion_matrix
result = classif.predict(legit_test)
conf_mat = confusion_matrix(mal_test,result)
conf_mat.shape
type(conf_mat)
conf_mat
print("False positives: ",conf_mat[0][1]/sum(conf_mat[0])*100)
print("False negatives: ",conf_mat[1][0]/sum(conf_mat[1])*100)
from sklearn.ensemble import GradientBoostingClassifier
grad_boost = GradientBoostingClassifier(n_estimators=50)
grad_boost.fit(legit_train,mal_train)
print("the score of the Gradient Boosting Classifier is:",grad_boost.score(legit_test,mal_test)*100)