Files
labs/Module-3/4.Performance-KMeans.ipynb

4.1 KiB

In [ ]:
#!pip install plotly
In [ ]:
# Start by importing and plotting the dataset
import pandas as pd
import plotly.express as px

Load & Train

In [ ]:
df = pd.read_csv("file_pe_headers.csv", sep=",")
fig = px.scatter_3d(
    df,
    x="SuspiciousImportFunctions",
    y="SectionsLength",
    z="SuspiciousNameSection",
    color="Malware",
)
fig.show()
In [ ]:
# Extract the features and target labels
y = df["Malware"]
x = df.drop(["Name", "Malware"], axis=1).to_numpy()
In [ ]:
# Now we will import scikit-learn's clustering module and fit a K-means model with 2 clusters to the data:
from sklearn.cluster import KMeans
estimator = KMeans(n_clusters=len(set(y)))
estimator.fit(x)
In [ ]:
# Predict the cluster using our trained algorithm
y_pred = estimator.predict(x)
df["pred"] = y_pred
df["pred"] = df["pred"].astype("category")
In [ ]:
# We will see how the algorithm did and plot the algorithm's clusters
fig = px.scatter_3d(
    df,
    x="SuspiciousImportFunctions",
    y="SectionsLength",
    z="SuspiciousNameSection",
    color="pred",
)
fig.show()

Performance Evaluation

In [ ]:
from sklearn.metrics import classification_report , confusion_matrix
In [ ]:
print("Confusion Matrix:")
print(confusion_matrix(y , y_pred))
In [ ]:
print("\nClassification Report:")
print(classification_report(y , y_pred))