five modes duplication
This commit is contained in:
33
Knn_test.py
Normal file
33
Knn_test.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# Name:fang xiaoyu
|
||||
# Time: 2023/3/11 22:05
|
||||
import pandas as pd
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.metrics import classification_report
|
||||
|
||||
data = pd.read_csv('sufshark_openvpn_tcp+youdao_header.csv')
|
||||
|
||||
|
||||
data["class1"] = data["class1"].replace({"VPN": 1, "Non-VPN": 0})
|
||||
|
||||
#print(data)
|
||||
X_train, X_test, y_train, y_test = train_test_split(
|
||||
data.iloc[:, :-1], data.iloc[:, -1], test_size=0.2, random_state=42)
|
||||
|
||||
knn = KNeighborsClassifier(n_neighbors=3)
|
||||
knn.fit(X_train, y_train)
|
||||
|
||||
y_pred = knn.predict(X_test)
|
||||
accuracy = accuracy_score(y_test, y_pred)
|
||||
print(f"Accuracy: {accuracy}")
|
||||
print(classification_report(y_test, y_pred))
|
||||
#Accuracy: 0.8200959488272921
|
||||
# precision recall f1-score support
|
||||
#
|
||||
# 0 0.79 0.83 0.81 1767
|
||||
# 1 0.84 0.81 0.83 1985
|
||||
#
|
||||
# accuracy 0.82 3752
|
||||
# macro avg 0.82 0.82 0.82 3752
|
||||
# weighted avg 0.82 0.82 0.82 3752
|
||||
Reference in New Issue
Block a user