- Machine Learning for Cybersecurity Cookbook
- Emmanuel Tsukerman
- 162字
- 2021-06-24 12:28:57
How to do it...
In the following steps, we will see a demonstration of how scikit-learn's K-means clustering algorithm performs on a toy PE malware classification:
- Start by importing and plotting the dataset:
import pandas as pd
import plotly.express as px
df = pd.read_csv("file_pe_headers.csv", sep=",")
fig = px.scatter_3d(
df,
x="SuspiciousImportFunctions",
y="SectionsLength",
z="SuspiciousNameSection",
color="Malware",
)
fig.show()
The following screenshot shows the output:

- Extract the features and target labels:
y = df["Malware"]
X = df.drop(["Name", "Malware"], axis=1).to_numpy()
- Next, import scikit-learn's clustering module and fit a K-means model with two clusters to the data:
from sklearn.cluster import KMeans
estimator = KMeans(n_clusters=len(set(y)))
estimator.fit(X)
- Predict the cluster using our trained algorithm:
y_pred = estimator.predict(X)
df["pred"] = y_pred
df["pred"] = df["pred"].astype("category")
- To see how the algorithm did, plot the algorithm's clusters:
fig = px.scatter_3d(
df,
x="SuspiciousImportFunctions",
y="SectionsLength",
z="SuspiciousNameSection",
color="pred",
)
fig.show()
The following screenshot shows the output:

The results are not perfect, but we can see that the clustering algorithm captured much of the structure in the dataset.
推薦閱讀
- 大學計算機信息技術導論
- Ansible Configuration Management
- 商戰數據挖掘:你需要了解的數據科學與分析思維
- PIC單片機C語言非常入門與視頻演練
- 21天學通C#
- Maya極速引擎:材質篇
- PyTorch Deep Learning Hands-On
- 現代傳感技術
- Ceph:Designing and Implementing Scalable Storage Systems
- 信息物理系統(CPS)測試與評價技術
- 水下無線傳感器網絡的通信與決策技術
- Statistics for Data Science
- 學練一本通:51單片機應用技術
- Linux系統管理員工具集
- Hands-On Dashboard Development with QlikView