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

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.

主站蜘蛛池模板: 修武县| 翁牛特旗| 米泉市| 怀化市| 潍坊市| 石狮市| 崇礼县| 益阳市| 库伦旗| 柞水县| 高密市| 亚东县| 九台市| 全州县| 乌审旗| 十堰市| 宝兴县| 滦平县| 鄂州市| 九龙县| 霸州市| 进贤县| 武宣县| 囊谦县| 内丘县| SHOW| 哈尔滨市| 天峨县| 湘西| 卢龙县| 平泉县| 绩溪县| 青州市| 渑池县| 松滋市| 扬中市| 栖霞市| 绥棱县| 泽库县| 拉孜县| 泸西县|