机器学习svm--正确率和召回率以及基于不平衡数据的分类调参

code:

import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score, fbeta_score
from sklearn.metrics import precision_recall_fscore_support, classification_report
from sklearn import svm
import matplotlib as mpl
import matplotlib.pyplot as plt
import warnings

def R_P():
	y_true = np.array([1, 1, 1, 1, 0, 0])
	y_hat = np.array([1, 0, 1, 1, 1, 1])

	print('Accuracy:\t', accuracy_score(y_true, y_hat))

	precision = precision_score(y_true, y_hat)
	print('Precision:\t', precision)

	recall = recall_score(y_true, y_hat)
	print('Recall:\t', recall)

	print('f1 score:\t', f1_score(y_true, y_hat))
	# print(2*(precision*recall)/(precision + recall))

	print('F-beta:\n')
	for beta in np.logspace(-3, 3, num=7, base=10):
		fbeta = fbeta_score(y_true, y_hat, beta=beta)
		print('\tbeta=%9.3f\tF-beta=%.3f' % (beta, fbeta))

	print(precision_recall_fscore_support(y_true, y_hat))
	print(classification_report(y_true, y_hat))

def show_accuracy(a, b):
	# 计算预测值和真实值一样的正确率
	acc = a.ravel() == b.ravel()
	print('precision:%.2f%%' % ((100*float(acc.sum()))/a.size))

def show_recall(y, y_hat):
	# 提取出那个小样本集中的预测和真实一样的正确率
	print('Recall"%.2f%%' % (100*float(np.sum(y_hat[y == 1] == 1)) / np.extract(y == 1, y).size))


if __name__ == '__main__':
	# 忽视警告
	warnings.filterwarnings('ignore')
	# 保证每次生成的数据一样
	np.random.seed(0)

	R_P()

	c1 = 990
	c2 = 10
	N = c1 + c2
	x_c1 = 3*np.random.randn(c1, 2)
	x_c2 = 0.5*np.random.randn(c2, 2) + (4, 4)
	x = np.vstack((x_c1, x_c2))
	y = np.ones(N)
	y[:c1] = -1
	# 显示出大小
	s = np.ones(N) * 30
	s[:c1] = 10
	# 分类器
	clfs = [
		svm.SVC(C=1, kernel='linear'),
		svm.SVC(C=1, kernel='linear', class_weight={-1:1, 1:50}),
		svm.SVC(C=0.8, kernel='rbf', gamma=0.5, class_weight={-1:1, 1:2}),
		svm.SVC(C=0.8, kernel='rbf', gamma=0.5, class_weight={-1:1, 1:10}),
	]
	titles = 'Linear', 'Linear Weights=50', 'RBF, Weight=2', 'RBF Weights=10'

	x1_min, x1_max = x[:, 0].min(), x[:, 0].max()
	x2_min, x2_max = x[:, 1].min(), x[:, 1].max()
	# 生成网格采样点
	x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j]
	# 测试点
	grid_test = np.stack((x1.flat, x2.flat), axis=1)
	cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0'])
	cm_dark = mpl.colors.ListedColormap(['g', 'b'])

	plt.figure(figsize=(10, 10), facecolor='w')
	for i, clf in enumerate(clfs):
		clf.fit(x, y)
		y_hat = clf.predict(x)
		print('===========coding myself function=============')
		show_accuracy(y, y_hat)
		show_recall(y, y_hat)
		print('===========sklearn package function============')
		print('Acc:\t', accuracy_score(y, y_hat))
		print('prediction:\t', precision_score(y, y_hat, pos_label=1))
		print('recall:\t', recall_score(y, y_hat, pos_label=1))
		print('F1-score:\t', f1_score(y, y_hat, pos_label=1))

		# 开始画图
		plt.subplot(2, 2, i+1)
		grid_hat = clf.predict(grid_test)
		grid_hat.shape = x1.shape
		plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light, alpha=0.8)
		plt.scatter(x[:, 0], x[:, 1], c=y, edgecolor='k', s=s, cmap=cm_dark)
		plt.xlim(x1_min, x1_max)
		plt.ylim(x2_min, x2_max)
		plt.title(titles[i])
		plt.grid()
	plt.suptitle('Unbalance Data Handling', fontsize=18)
	plt.tight_layout(2.0)
	plt.subplots_adjust(top=0.92)
	plt.show()


  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SVM分类器的召回率全是1的情况是指在多分类问题中,所有的类别都被完全正确地分类为正例的情况。在多分类中,SVM模型本身是二分类模型,因此需要通过一种策略来实现多类分类。常用的策略包括一对一(ovo)和一对多(ovr)。在ovo策略中,针对每两个类别之间都构建一个二分类器进行分类,最终的分类结果由所有二分类器的投票决定。而在ovr策略中,将每个类别与其他所有类别组合成一对,构建多个二分类器进行分类,最终通过投票确定每个样本的类别。 针对问题中的召回率全为1的情况,对于每个类别来说,所有属于该类别的样本都被正确地分类为该类别,没有出现漏判的情况。这可能是因为SVM模型在处理该数据集时具有较好的分离能力,能够有效地将不同类别的样本区分开来。同时,召回率全为1也意味着没有出现误判的情况,即没有将其他类别的样本错误地分类为该类别。这可能是由于在训练过程中选取了适当的核函数、合适的gamma值和惩罚系数c,使得模型能够更好地拟合数据集并达到最佳分类效果。 召回率是评估分类器性能的指标之一,它衡量了模型正确分类为正例的能力。除了召回率,还有其他指标可以用来评估SVM分类器的性能,包括准确率、精确率、F1-Measure等。此外,还可以通过绘制AUC曲线和混淆矩阵来进一步了解分类器的性能和分类结果的分布情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [机器学习分类器——案例(opencv sklearn svm ann)](https://blog.csdn.net/shan_5233/article/details/124397795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值