Skip to content

Instantly share code, notes, and snippets.

@asukaminato0721
Created June 12, 2021 06:50
Show Gist options
  • Save asukaminato0721/b46c84dd4b08ca865fa2988fc72e60a7 to your computer and use it in GitHub Desktop.
Save asukaminato0721/b46c84dd4b08ca865fa2988fc72e60a7 to your computer and use it in GitHub Desktop.
SVM 实验
# https://scikit-learn.org/stable/modules/svm.html
# https://blog.csdn.net/Liii_NN/article/details/108437424
from pathlib import Path as 路径
from numpy import loadtxt as 载入数据
from numpy import ndarray as n维数组
from sklearn import svm
当前目录: 路径 = 路径(__file__).parent.absolute()
训练样本: n维数组 = 载入数据(当前目录 / "train_sample.txt")
训练标签: n维数组 = 载入数据(当前目录 / "train_sample_label.txt")
clf = svm.SVC()
clf.fit(训练样本, 训练标签)
训练结果: n维数组 = clf.predict(载入数据(当前目录 / "test_sample.txt"))
参照结果: n维数组 = 载入数据(当前目录 / "test_sample_label.txt")
print(f"{训练结果 = }")
print(f"{参照结果 = }")
def 精确率(训练结果: n维数组, 参照结果: n维数组):
实际预测为正的样本 = sum(x == y == 1 for x, y in zip(训练结果, 参照结果))
print(f"{实际预测为正的样本 = }")
找到的 = sum(训练结果)
print(f"{找到的 = }")
return 实际预测为正的样本 / 找到的
def 查全率(训练结果: n维数组, 参照结果: n维数组):
实际预测为正的样本 = sum(x == y == 1 for x, y in zip(训练结果, 参照结果))
print(f"{实际预测为正的样本 = }")
实际为正的样本 = sum(参照结果)
print(f"{实际为正的样本 = }")
return 实际预测为正的样本 / 实际为正的样本
print(f"{精确率(训练结果, 参照结果) = }")
print(f"{查全率(训练结果, 参照结果) = }")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment