This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
grityu-model-duplication/SVM_test.py

44 lines
1.2 KiB
Python
Raw Normal View History

2023-03-16 22:42:35 +08:00
# Name:fang xiaoyu
# Time: 2023/3/11 22:30
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report
# 读取CSV文件
data = pd.read_csv('sufshark_openvpn_tcp+youdao_header.csv')
# 将类别转换为数字标签
# le = preprocessing.LabelEncoder()
# data['label'] = le.fit_transform(data['label'])
data["class1"] = data["class1"].replace({"VPN": 1, "Non-VPN": 0})
# 分离特征和类别
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# # 分离特征和标签
# X = data.drop('label', axis=1)
# y = data['label']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 创建SVM模型
svm_model = SVC()
# 在训练集上训练模型
svm_model.fit(X_train, y_train)
# 在测试集上评估模型
predictions = svm_model.predict(X_test)
print(classification_report(y_test, predictions))
# precision recall f1-score support
#
# 0 0.59 0.42 0.49 1720
# 1 0.61 0.76 0.67 2032
#
# accuracy 0.60 3752
# macro avg 0.60 0.59 0.58 3752
# weighted avg 0.60 0.60 0.59 3752