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

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.

主站蜘蛛池模板: 永胜县| 垦利县| 清涧县| 白河县| 博客| 翼城县| 闻喜县| 平塘县| 中江县| 光泽县| 西乌珠穆沁旗| 新野县| 古田县| 正镶白旗| 黑山县| 黔西| 凤山县| 林甸县| 女性| 子长县| 庆安县| 民丰县| 肇庆市| 郴州市| 凉城县| 长寿区| 蕲春县| 平山县| 邻水| 精河县| 孝感市| 防城港市| 深泽县| 广灵县| 井冈山市| 青川县| 四川省| 萨嘎县| 黄梅县| 莒南县| 大冶市|