- 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.
推薦閱讀
- Learning Microsoft Azure Storage
- 走入IBM小型機世界
- 工業機器人工程應用虛擬仿真教程:MotoSim EG-VRC
- 城市道路交通主動控制技術
- 21天學通Java
- 基于32位ColdFire構建嵌入式系統
- 學會VBA,菜鳥也高飛!
- Python:Data Analytics and Visualization
- Visual FoxPro程序設計
- Spatial Analytics with ArcGIS
- INSTANT VMware vCloud Starter
- 智能鼠原理與制作(進階篇)
- Access 2007數據庫入門與實例應用金典
- 中文版Flash CS6高手速成
- 編程大講壇:Visual Basic核心開發技術從入門到精通