- Machine Learning for Cybersecurity Cookbook
- Emmanuel Tsukerman
- 256字
- 2021-06-24 12:29:06
How to do it...
The code for the following can be found on https://github.com/PacktPublishing/Machine-Learning-for-Cybersecurity-Cookbook/blob/master/Chapter02/Classifying%20Files%20by%20Type/File%20Type%20Classifier.ipynb. We build a classifier using this data to predict files as JavaScript, Python, or PowerShell:
- Begin by importing the necessary libraries and specifying the paths of the samples we will be using to train and test:
import os
from sklearn.feature_extraction.text import HashingVectorizer, TfidfTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.pipeline import Pipeline
javascript_path = "/path/to/JavascriptSamples/"
python_path = "/path/to/PythonSamples/"
powershell_path = "/path/to/PowerShellSamples/"
- Next, we read in all of the file types. We also create an array of labels with -1, 0, and 1 representing the JavaScript, Python, and PowerShell scripts, respectively:
corpus = []
labels = []
file_types_and_labels = [(javascript_path, -1), (python_path, 0), (powershell_path, 1)]
for files_path, label in file_types_and_labels:
files = os.listdir(files_path)
for file in files:
file_path = files_path + "/" + file
try:
with open(file_path, "r") as myfile:
data = myfile.read().replace("\n", "")
except:
pass
data = str(data)
corpus.append(data)
labels.append(label)
- We go on to create a train-test split and a pipeline that will perform basic NLP on the files, followed by a random forest classifier:
X_train, X_test, y_train, y_test = train_test_split(
corpus, labels, test_size=0.33, random_state=11
)
text_clf = Pipeline(
[
("vect", HashingVectorizer(input="content", ngram_range=(1, 3))),
("tfidf", TfidfTransformer(use_idf=True,)),
("rf", RandomForestClassifier(class_weight="balanced")),
]
)
- We fit the pipeline to the training data, and then use it to predict on the testing data. Finally, we print out the accuracy and the confusion matrix:
text_clf.fit(X_train, y_train)
y_test_pred = text_clf.predict(X_test)
print(accuracy_score(y_test, y_test_pred))
print(confusion_matrix(y_test, y_test_pred))
This results in the following output:

推薦閱讀
- Word 2000、Excel 2000、PowerPoint 2000上機指導與練習
- Hands-On Internet of Things with MQTT
- 人工免疫算法改進及其應用
- Learning Apache Spark 2
- Expert AWS Development
- Blender Compositing and Post Processing
- Spark大數據技術與應用
- 塊數據5.0:數據社會學的理論與方法
- Implementing Splunk 7(Third Edition)
- 工業機器人運動仿真編程實踐:基于Android和OpenGL
- Deep Reinforcement Learning Hands-On
- 多媒體制作與應用
- Microsoft Dynamics CRM 2013 Marketing Automation
- 手把手教你學Photoshop CS3
- 常用傳感器技術及應用(第2版)