线性分类的Jupyter实践

本文通过鸢尾花数据集实践线性分类,利用Fisher分类方法,达到93%的准确率。同时,通过seaborn库进行数据可视化,包括散点图、直方图、箱线图和琴图,揭示数据特征间的关联。
摘要由CSDN通过智能技术生成

一、鸢尾花数据集分类

鸢尾花数据集以鸢尾花的特征作为数据来源,数据集包含150个数据集,有4维,分为3类(setosa、versicolour、virginica),每类50个数据,每个数据包含4个属性,花萼长度、宽度和花瓣长度、宽度

1.选择分类

from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
import math

#prepare the data
iris = datasets.load_iris()
X = iris.data
y = iris.target
names = iris.feature_names
labels = iris.target_names
y_c = np.unique(y)

"""visualize the distributions of the four different features in 1-dimensional histograms"""
fig, axes = plt.subplots(2, 2, figsize=(12, 6))
for ax, column in zip(axes.ravel(), range(X.shape[1])):
  # set bin sizes
  min_b = math.floor(np.min(X[:, column]))
  max_b = math.ceil(np.max(X[:, column]))
  bins = np.linspace(min_b, max_b, 25)

  # plotting the histograms
  for i, color in zip(y_c, ('blue', 'red', 'green')):
      ax.hist(X[y == i, column], color=color, label='class %s' % labels[i],
              bins=bins, alpha=0.5, )
  ylims = ax.get_ylim()

  # plot annotation
  l = ax.legend(loc='upper right', fancybox=True, fontsize=8)
  l.get_frame().set_alpha(0.5)
  ax.set_ylim([0, max(ylims) + 2])
  ax.set_xlabel(names[column])
  ax.set_title('Iris histogram feature %s' % str(column + 1))

  # hide axis ticks
  ax.tick_params(axis='both', which='both', bottom=False, top=False, left=False, right=False,
                 labelbottom=True, labelleft=True)

  # remove axis spines
  ax.spines['top'].set_visible(False)
  ax.spines["right"].set_visible(False)
  ax.spines["bottom"].set_visible(False)
  ax.spines["left"].set_visible(False)

axes[0][0].set_ylabel('count')
axes[1][0].set_ylabel('count')
fig.tight_layout()
plt.show()

在这里插入图片描述
由上图可以看出花瓣的长度和宽度更适合分类

2.鸢尾花 数据集的Fisher分类

np.set_printoptions(precision=4)
 
mean_vector = []  # 类别的平均值
for i in y_c:
    mean_vector.append(np.mean(X[y == i], axis=0))
    print('Mean Vector class %s:%s\n' % (i, mean_vector[i]))
S_W = np.zeros((X.shape[1], X.shape[1]))
for i in y_c:
    Xi = X[y == i] - mean_vector[i]
    S_W += np.mat(Xi).T * np.mat(Xi)
print('S_W:\n', S_W)

S_B = np.zeros((X.shape[1], X.shape[1]))
mu = np.mean(X, axis=0)  # 所有样本平均值
for i in y_c:
    Ni = len(X[y == i])
    S_B += Ni * np.mat(mean_vector[i] - mu).T * np.mat(mean_vector[i] - mu)
print('S_B:\n', S_B) 
eigvals, eigvecs = np.linalg.eig(np.linalg.inv(S_W) * S_B)  # 求特征值,特征向量
np.testing.assert_array_almost_equal(np.mat(np.linalg.inv(S_W) * S_B) * np.mat(eigvecs[:, 0].reshape(4, 1)),
                                     eigvals[0] * np.mat(eigvecs[:, 0].reshape(4, 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值