官术网_书友最值得收藏!

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:

  1. 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:

  1. Extract the features and target labels:
y = df["Malware"]
X = df.drop(["Name", "Malware"], axis=1).to_numpy()
  1. 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)
  1. Predict the cluster using our trained algorithm:
y_pred = estimator.predict(X)
df["pred"] = y_pred
df["pred"] = df["pred"].astype("category")
  1. 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.

主站蜘蛛池模板: 屏东市| 达日县| 邵阳市| 珲春市| 习水县| 泽州县| 长岛县| 青海省| 澄城县| 舟曲县| 翁牛特旗| 仲巴县| 类乌齐县| 庆城县| 兴和县| 锦州市| 永嘉县| 太和县| 集安市| 彭阳县| 青浦区| 秀山| 五华县| 东阿县| 新丰县| 包头市| 富民县| 新昌县| 建始县| 清水河县| 资阳市| 垣曲县| 什邡市| 关岭| 南靖县| 敦化市| 旬邑县| 乌兰浩特市| 汉沽区| 珠海市| 增城市|