Update Knn_test.py
This commit is contained in:
17
Knn_test.py
17
Knn_test.py
@@ -6,21 +6,30 @@ 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分类器
|
||||
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}")
|
||||
# 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
|
||||
@@ -30,4 +39,4 @@ print(classification_report(y_test, y_pred))
|
||||
#
|
||||
# accuracy 0.82 3752
|
||||
# macro avg 0.82 0.82 0.82 3752
|
||||
# weighted avg 0.82 0.82 0.82 3752
|
||||
# weighted avg 0.82 0.82 0.82 3752
|
||||
|
||||
Reference in New Issue
Block a user