实验要求:

  1. 以kNN和SVM算法为例,理解分类分析算法的基本原理及流程,理解kNN和SVM算法的区别。
  2. 利用kNN算法,以参数k=3编程对鸢尾花进行分类,建议使用sklearn中内置的已经预处理好的数据集。
  3. 输出训练好的模型在训练集与测试集上的分类准确度。
  4. 对测试集上数据预测的结果进行可视化输出,与真值进行对比。
  5. 使用SVM算法训练模型,计算训练好的模型在训练集与测试集上的分类准确度。
  6. 选择数据的其中两维,可视化输出SVM模型的分类边界。

注:

  1. 训练集与测试集均可通过原数据集抽样取得,可考虑使用train_test_split方法(在sklearn.model_selection中)。
  2. kNN算法和SVM算法在sklearn中均有实现,不建议各位同学手动编写,否则工作量会过于庞大。若感兴趣,也欢迎在课后自行研究。
  3. kNN算法及SVM算法都涉及到模型参数,同学们可以尝试不同的参数并观察准确率。
  4. 由于我们在二维平面上绘制SVM模型分类边界,视代码实现方式可能需要重新训练模型。

可视化输出示例:
image.png

image.png

image.png

上机实现:

KNN实现:

导入需要的数据集和包

  1. from sklearn.datasets import load_iris
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.neighbors import KNeighborsClassifier
  4. from sklearn.metrics import accuracy_score
  5. import matplotlib.pyplot as plt
  6. import seaborn as sns
  7. import pandas as pd
  8. plt.rcParams["font.sans-serif"] = ["SimHei"]

获取数据

  1. load_iris()
  2. iris = load_iris()
  3. iris
  1. {'data': array([[5.1, 3.5, 1.4, 0.2],
  2. [4.9, 3. , 1.4, 0.2],
  3. [4.7, 3.2, 1.3, 0.2],
  4. [4.6, 3.1, 1.5, 0.2],
  5. [5. , 3.6, 1.4, 0.2],
  6. [5.4, 3.9, 1.7, 0.4],
  7. [4.6, 3.4, 1.4, 0.3],
  8. [5. , 3.4, 1.5, 0.2],
  9. [4.4, 2.9, 1.4, 0.2],
  10. [4.9, 3.1, 1.5, 0.1],
  11. [5.4, 3.7, 1.5, 0.2],
  12. [4.8, 3.4, 1.6, 0.2],
  13. [4.8, 3. , 1.4, 0.1],
  14. [4.3, 3. , 1.1, 0.1],
  15. [5.8, 4. , 1.2, 0.2],
  16. [5.7, 4.4, 1.5, 0.4],
  17. [5.4, 3.9, 1.3, 0.4],
  18. [5.1, 3.5, 1.4, 0.3],
  19. [5.7, 3.8, 1.7, 0.3],
  20. [5.1, 3.8, 1.5, 0.3],
  21. [5.4, 3.4, 1.7, 0.2],
  22. [5.1, 3.7, 1.5, 0.4],
  23. [4.6, 3.6, 1. , 0.2],
  24. [5.1, 3.3, 1.7, 0.5],
  25. [4.8, 3.4, 1.9, 0.2],
  26. [5. , 3. , 1.6, 0.2],
  27. [5. , 3.4, 1.6, 0.4],
  28. [5.2, 3.5, 1.5, 0.2],
  29. [5.2, 3.4, 1.4, 0.2],
  30. [4.7, 3.2, 1.6, 0.2],
  31. [4.8, 3.1, 1.6, 0.2],
  32. [5.4, 3.4, 1.5, 0.4],
  33. [5.2, 4.1, 1.5, 0.1],
  34. [5.5, 4.2, 1.4, 0.2],
  35. [4.9, 3.1, 1.5, 0.2],
  36. [5. , 3.2, 1.2, 0.2],
  37. [5.5, 3.5, 1.3, 0.2],
  38. [4.9, 3.6, 1.4, 0.1],
  39. [4.4, 3. , 1.3, 0.2],
  40. [5.1, 3.4, 1.5, 0.2],
  41. [5. , 3.5, 1.3, 0.3],
  42. [4.5, 2.3, 1.3, 0.3],
  43. [4.4, 3.2, 1.3, 0.2],
  44. [5. , 3.5, 1.6, 0.6],
  45. [5.1, 3.8, 1.9, 0.4],
  46. [4.8, 3. , 1.4, 0.3],
  47. [5.1, 3.8, 1.6, 0.2],
  48. [4.6, 3.2, 1.4, 0.2],
  49. [5.3, 3.7, 1.5, 0.2],
  50. [5. , 3.3, 1.4, 0.2],
  51. [7. , 3.2, 4.7, 1.4],
  52. [6.4, 3.2, 4.5, 1.5],
  53. [6.9, 3.1, 4.9, 1.5],
  54. [5.5, 2.3, 4. , 1.3],
  55. [6.5, 2.8, 4.6, 1.5],
  56. [5.7, 2.8, 4.5, 1.3],
  57. [6.3, 3.3, 4.7, 1.6],
  58. [4.9, 2.4, 3.3, 1. ],
  59. [6.6, 2.9, 4.6, 1.3],
  60. [5.2, 2.7, 3.9, 1.4],
  61. [5. , 2. , 3.5, 1. ],
  62. [5.9, 3. , 4.2, 1.5],
  63. [6. , 2.2, 4. , 1. ],
  64. [6.1, 2.9, 4.7, 1.4],
  65. [5.6, 2.9, 3.6, 1.3],
  66. [6.7, 3.1, 4.4, 1.4],
  67. [5.6, 3. , 4.5, 1.5],
  68. [5.8, 2.7, 4.1, 1. ],
  69. [6.2, 2.2, 4.5, 1.5],
  70. [5.6, 2.5, 3.9, 1.1],
  71. [5.9, 3.2, 4.8, 1.8],
  72. [6.1, 2.8, 4. , 1.3],
  73. [6.3, 2.5, 4.9, 1.5],
  74. [6.1, 2.8, 4.7, 1.2],
  75. [6.4, 2.9, 4.3, 1.3],
  76. [6.6, 3. , 4.4, 1.4],
  77. [6.8, 2.8, 4.8, 1.4],
  78. [6.7, 3. , 5. , 1.7],
  79. [6. , 2.9, 4.5, 1.5],
  80. [5.7, 2.6, 3.5, 1. ],
  81. [5.5, 2.4, 3.8, 1.1],
  82. [5.5, 2.4, 3.7, 1. ],
  83. [5.8, 2.7, 3.9, 1.2],
  84. [6. , 2.7, 5.1, 1.6],
  85. [5.4, 3. , 4.5, 1.5],
  86. [6. , 3.4, 4.5, 1.6],
  87. [6.7, 3.1, 4.7, 1.5],
  88. [6.3, 2.3, 4.4, 1.3],
  89. [5.6, 3. , 4.1, 1.3],
  90. [5.5, 2.5, 4. , 1.3],
  91. [5.5, 2.6, 4.4, 1.2],
  92. [6.1, 3. , 4.6, 1.4],
  93. [5.8, 2.6, 4. , 1.2],
  94. [5. , 2.3, 3.3, 1. ],
  95. [5.6, 2.7, 4.2, 1.3],
  96. [5.7, 3. , 4.2, 1.2],
  97. [5.7, 2.9, 4.2, 1.3],
  98. [6.2, 2.9, 4.3, 1.3],
  99. [5.1, 2.5, 3. , 1.1],
  100. [5.7, 2.8, 4.1, 1.3],
  101. [6.3, 3.3, 6. , 2.5],
  102. [5.8, 2.7, 5.1, 1.9],
  103. [7.1, 3. , 5.9, 2.1],
  104. [6.3, 2.9, 5.6, 1.8],
  105. [6.5, 3. , 5.8, 2.2],
  106. [7.6, 3. , 6.6, 2.1],
  107. [4.9, 2.5, 4.5, 1.7],
  108. [7.3, 2.9, 6.3, 1.8],
  109. [6.7, 2.5, 5.8, 1.8],
  110. [7.2, 3.6, 6.1, 2.5],
  111. [6.5, 3.2, 5.1, 2. ],
  112. [6.4, 2.7, 5.3, 1.9],
  113. [6.8, 3. , 5.5, 2.1],
  114. [5.7, 2.5, 5. , 2. ],
  115. [5.8, 2.8, 5.1, 2.4],
  116. [6.4, 3.2, 5.3, 2.3],
  117. [6.5, 3. , 5.5, 1.8],
  118. [7.7, 3.8, 6.7, 2.2],
  119. [7.7, 2.6, 6.9, 2.3],
  120. [6. , 2.2, 5. , 1.5],
  121. [6.9, 3.2, 5.7, 2.3],
  122. [5.6, 2.8, 4.9, 2. ],
  123. [7.7, 2.8, 6.7, 2. ],
  124. [6.3, 2.7, 4.9, 1.8],
  125. [6.7, 3.3, 5.7, 2.1],
  126. [7.2, 3.2, 6. , 1.8],
  127. [6.2, 2.8, 4.8, 1.8],
  128. [6.1, 3. , 4.9, 1.8],
  129. [6.4, 2.8, 5.6, 2.1],
  130. [7.2, 3. , 5.8, 1.6],
  131. [7.4, 2.8, 6.1, 1.9],
  132. [7.9, 3.8, 6.4, 2. ],
  133. [6.4, 2.8, 5.6, 2.2],
  134. [6.3, 2.8, 5.1, 1.5],
  135. [6.1, 2.6, 5.6, 1.4],
  136. [7.7, 3. , 6.1, 2.3],
  137. [6.3, 3.4, 5.6, 2.4],
  138. [6.4, 3.1, 5.5, 1.8],
  139. [6. , 3. , 4.8, 1.8],
  140. [6.9, 3.1, 5.4, 2.1],
  141. [6.7, 3.1, 5.6, 2.4],
  142. [6.9, 3.1, 5.1, 2.3],
  143. [5.8, 2.7, 5.1, 1.9],
  144. [6.8, 3.2, 5.9, 2.3],
  145. [6.7, 3.3, 5.7, 2.5],
  146. [6.7, 3. , 5.2, 2.3],
  147. [6.3, 2.5, 5. , 1.9],
  148. [6.5, 3. , 5.2, 2. ],
  149. [6.2, 3.4, 5.4, 2.3],
  150. [5.9, 3. , 5.1, 1.8]]),
  151. 'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  152. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  154. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  155. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  156. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  157. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
  158. 'frame': None,
  159. 'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'),
  160. 'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 150 (50 in each of three classes)\n :Number of Attributes: 4 numeric, predictive attributes and the class\n :Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n - Iris-Virginica\n \n :Summary Statistics:\n\n ============== ==== ==== ======= ===== ====================\n Min Max Mean SD Class Correlation\n ============== ==== ==== ======= ===== ====================\n sepal length: 4.3 7.9 5.84 0.83 0.7826\n sepal width: 2.0 4.4 3.05 0.43 -0.4194\n petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\n petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n ============== ==== ==== ======= ===== ====================\n\n :Missing Attribute Values: None\n :Class Distribution: 33.3% for each of 3 classes.\n :Creator: R.A. Fisher\n :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n :Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n.. topic:: References\n\n - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics" (John Wiley, NY, 1950).\n - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3 classes in the data.\n - Many, many more ...',
  161. 'feature_names': ['sepal length (cm)',
  162. 'sepal width (cm)',
  163. 'petal length (cm)',
  164. 'petal width (cm)'],
  165. 'filename': 'iris.csv',
  166. 'data_module': 'sklearn.datasets.data'}

数据集的说明

  1. #特征
  2. fea = iris.data
  3. #目标
  4. lab = iris.target#【重要】
  5. #目标的名称
  6. tar_nm = iris.target_names
  7. #特征列名称
  8. fea_nm = iris.feature_names
  9. pd.DataFrame(data = fea, columns = fea_nm)
  10. pd.DataFrame(tar_nm[lab],columns=['labels'])
  11. #制作dataframe
  12. fea_df = pd.DataFrame(data=fea,columns=fea_nm)
  13. lab_df = pd.DataFrame(tar_nm[lab],columns=['labels'])#【重要】
  14. df =pd.concat([fea_df,lab_df],axis=1,join='inner')
  15. df.describe()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000

统计描述分析

  1. #散布图矩阵
  2. sns.pairplot(df,hue='labels')
  3. #数据分割 随机抽取数据,那么测试数据中又几个异常值

image.png
实验5 分类分析 - 图5

训练和打分,参数设置为k=3

  1. # 加载鸢尾花数据集
  2. iris = load_iris()
  3. X = iris.data
  4. y = iris.target
  5. # 将数据集划分为训练集和测试集
  6. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
  7. # 创建kNN分类器
  8. knn = KNeighborsClassifier(n_neighbors=3)
  9. # 在训练集上训练模型
  10. knn.fit(X_train, y_train)
  11. # 在训练集和测试集上进行预测
  12. y_train_pred = knn.predict(X_train)
  13. y_test_pred = knn.predict(X_test)
  14. # 计算训练集和测试集上的分类准确度
  15. train_accuracy = accuracy_score(y_train, y_train_pred)
  16. test_accuracy = accuracy_score(y_test, y_test_pred)
  17. print("kNN算法分类训练集的准确度为:", train_accuracy)
  18. print("kNN算法分类测试集的准确度为:", test_accuracy)
  1. kNN算法分类训练集的准确度为: 0.9428571428571428
  2. kNN算法分类测试集的准确度为: 1.0

对测试集上数据预测的结果进行可视化输出,与真值进行对比

  1. plt.figure(figsize=(10, 6))
  2. plt.scatter(range(len(y_test)), y_test, c='red',s=80)
  3. plt.scatter(range(len(y_test)), y_test_pred, c='green',s=20)
  4. plt.title('kNN classification results')
  5. plt.legend()
  6. plt.show()

image.png
实验5 分类分析 - 图7

SVM训练

需要用的的数据集
iris.txt(使用时转换为.data格式)

  1. import numpy as np
  2. from sklearn import svm
  3. from sklearn import model_selection
  4. import matplotlib.pyplot as plt
  5. import matplotlib as mpl
  6. #=============== 数据准备 ===============
  7. file_path = "D:\DataAnalysis\iris.data"
  8. # 该方法可将输入的字符串作为字典it 的键进行查询,输出对应的值
  9. # 该方法就是相当于一个转换器,将数据中非浮点类型的字符串转化为浮点
  10. def iris_type(s):
  11. it = {b'Iris-setosa':0, b'Iris-versicolor':1, b'Iris-virginica':2}
  12. return it[s]
  13. # 加载data文件,类型为浮点,分隔符为逗号,对第四列也就是data 中的鸢尾花类别这一列的字符串转换为0-2 的浮点数
  14. data = np.loadtxt(file_path, dtype=float, delimiter=',', converters={4:iris_type})
  15. # print(data)
  16. # 对data 矩阵进行分割,从第四列包括第四列开始后续所有列进行拆分
  17. x, y = np.split(data, (4,), axis=1)
  18. # 对x 矩阵进行切片,所有行都取,但只取前两列
  19. x = x[:, 0:2]
  20. print(x)
  21. # 随机分配训练数据和测试数据,随机数种子为1,测试数据占比为0.3
  22. data_train, data_test, tag_train, tag_test = model_selection.train_test_split(x, y, random_state=1, test_size=0.3)
  23. #=============== 模型搭建 ===============
  24. def classifier():
  25. clf = svm.SVC(C=0.5, # 误差惩罚系数,默认1
  26. kernel='linear', # 线性核 kenrel='rbf':高斯核
  27. decision_function_shape='ovr') # 决策函数
  28. return clf
  29. # 定义SVM(支持向量机)模型
  30. clf = classifier()
  31. #=============== 模型训练 ===============
  32. def train(clf, x_train, y_train):
  33. clf.fit(x_train, # 训练集特征向量
  34. y_train.ravel()) # 训练集目标值
  35. # 训练SVM 模型
  36. train(clf, data_train, tag_train)
  37. #=============== 模型评估 ===============
  38. def show_accuracy(a, b, tip):
  39. acc = a.ravel() == b.ravel()
  40. print('%s Accuracy:%.3f' % (tip, np.mean(acc)))
  41. def print_accuracy(clf, x_train, y_train, x_test, y_test):
  42. # 分别打印训练集和测试集的准确率
  43. # score(x_train, y_train):表示输出x_train, y_train 在模型上的准确率
  44. print('training prediction:%.3f' % (clf.score(x_train, y_train)))
  45. print('test data prediction:%.3f' % (clf.score(x_test, y_test)))
  46. # 原始结果与预测结果进行对比
  47. # predict() 表示对x_train 样本进行预测,返回样本类别
  48. show_accuracy(clf.predict(x_train), y_train, 'training data')
  49. show_accuracy(clf.predict(x_test), y_test, 'testing data')
  50. # 计算决策函数的值,表示x到各分割平面的距离
  51. print('decision_function:\n', clf.decision_function(x_train))
  52. print_accuracy(clf, data_train, tag_train, data_test, tag_test)
  53. #=============== 模型可视化 ===============
  54. def draw(clf, x):
  55. iris_feature = 'sepal length', 'sepal width', 'petal length', 'petal width'
  56. # 开始画图
  57. # 第0 列的范围
  58. x1_min, x1_max = x[:, 0].min(), x[:, 0].max()
  59. # 第1 列的范围
  60. x2_min, x2_max = x[:, 1].min(), x[:, 1].max()
  61. x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j]
  62. grid_test = np.stack((x1.flat, x2.flat), axis=1)
  63. print('grid_test:\n', grid_test)
  64. # 输出样本到决策面的距离
  65. z = clf.decision_function(grid_test)
  66. print('the distance to decision plane:\n', z)
  67. # 预测分类值
  68. grid_hat = clf.predict(grid_test)
  69. print('grid_hat:\n', grid_hat)
  70. grid_hat = grid_hat.reshape(x1.shape)
  71. cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF'])
  72. cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b'])
  73. plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light)
  74. # 样本点
  75. plt.scatter(x[:, 0], x[:, 1], c=np.squeeze(y), edgecolor='k', s=50, cmap=cm_dark)
  76. # 测试点
  77. plt.scatter(data_test[:, 0], data_test[:, 1], s=120, facecolor='none', zorder=10)
  78. plt.xlabel(iris_feature[0], fontsize=20)
  79. plt.ylabel(iris_feature[1], fontsize=20)
  80. plt.xlim(x1_min, x1_max)
  81. plt.ylim(x2_min, x2_max)
  82. plt.title('svm in iris data classification', fontsize=30)
  83. plt.grid()
  84. plt.show()
  85. draw(clf, x)
  1. [[5.1 3.5]
  2. [4.9 3. ]
  3. [4.7 3.2]
  4. [4.6 3.1]
  5. [5. 3.6]
  6. [5.4 3.9]
  7. [4.6 3.4]
  8. [5. 3.4]
  9. [4.4 2.9]
  10. [4.9 3.1]
  11. [5.4 3.7]
  12. [4.8 3.4]
  13. [4.8 3. ]
  14. [4.3 3. ]
  15. [5.8 4. ]
  16. [5.7 4.4]
  17. [5.4 3.9]
  18. [5.1 3.5]
  19. [5.7 3.8]
  20. [5.1 3.8]
  21. [5.4 3.4]
  22. [5.1 3.7]
  23. [4.6 3.6]
  24. [5.1 3.3]
  25. [4.8 3.4]
  26. [5. 3. ]
  27. [5. 3.4]
  28. [5.2 3.5]
  29. [5.2 3.4]
  30. [4.7 3.2]
  31. [4.8 3.1]
  32. [5.4 3.4]
  33. [5.2 4.1]
  34. [5.5 4.2]
  35. [4.9 3.1]
  36. [5. 3.2]
  37. [5.5 3.5]
  38. [4.9 3.1]
  39. [4.4 3. ]
  40. [5.1 3.4]
  41. [5. 3.5]
  42. [4.5 2.3]
  43. [4.4 3.2]
  44. [5. 3.5]
  45. [5.1 3.8]
  46. [4.8 3. ]
  47. [5.1 3.8]
  48. [4.6 3.2]
  49. [5.3 3.7]
  50. [5. 3.3]
  51. [7. 3.2]
  52. [6.4 3.2]
  53. [6.9 3.1]
  54. [5.5 2.3]
  55. [6.5 2.8]
  56. [5.7 2.8]
  57. [6.3 3.3]
  58. [4.9 2.4]
  59. [6.6 2.9]
  60. [5.2 2.7]
  61. [5. 2. ]
  62. [5.9 3. ]
  63. [6. 2.2]
  64. [6.1 2.9]
  65. [5.6 2.9]
  66. [6.7 3.1]
  67. [5.6 3. ]
  68. [5.8 2.7]
  69. [6.2 2.2]
  70. [5.6 2.5]
  71. [5.9 3.2]
  72. [6.1 2.8]
  73. [6.3 2.5]
  74. [6.1 2.8]
  75. [6.4 2.9]
  76. [6.6 3. ]
  77. [6.8 2.8]
  78. [6.7 3. ]
  79. [6. 2.9]
  80. [5.7 2.6]
  81. [5.5 2.4]
  82. [5.5 2.4]
  83. [5.8 2.7]
  84. [6. 2.7]
  85. [5.4 3. ]
  86. [6. 3.4]
  87. [6.7 3.1]
  88. [6.3 2.3]
  89. [5.6 3. ]
  90. [5.5 2.5]
  91. [5.5 2.6]
  92. [6.1 3. ]
  93. [5.8 2.6]
  94. [5. 2.3]
  95. [5.6 2.7]
  96. [5.7 3. ]
  97. [5.7 2.9]
  98. [6.2 2.9]
  99. [5.1 2.5]
  100. [5.7 2.8]
  101. [6.3 3.3]
  102. [5.8 2.7]
  103. [7.1 3. ]
  104. [6.3 2.9]
  105. [6.5 3. ]
  106. [7.6 3. ]
  107. [4.9 2.5]
  108. [7.3 2.9]
  109. [6.7 2.5]
  110. [7.2 3.6]
  111. [6.5 3.2]
  112. [6.4 2.7]
  113. [6.8 3. ]
  114. [5.7 2.5]
  115. [5.8 2.8]
  116. [6.4 3.2]
  117. [6.5 3. ]
  118. [7.7 3.8]
  119. [7.7 2.6]
  120. [6. 2.2]
  121. [6.9 3.2]
  122. [5.6 2.8]
  123. [7.7 2.8]
  124. [6.3 2.7]
  125. [6.7 3.3]
  126. [7.2 3.2]
  127. [6.2 2.8]
  128. [6.1 3. ]
  129. [6.4 2.8]
  130. [7.2 3. ]
  131. [7.4 2.8]
  132. [7.9 3.8]
  133. [6.4 2.8]
  134. [6.3 2.8]
  135. [6.1 2.6]
  136. [7.7 3. ]
  137. [6.3 3.4]
  138. [6.4 3.1]
  139. [6. 3. ]
  140. [6.9 3.1]
  141. [6.7 3.1]
  142. [6.9 3.1]
  143. [5.8 2.7]
  144. [6.8 3.2]
  145. [6.7 3.3]
  146. [6.7 3. ]
  147. [6.3 2.5]
  148. [6.5 3. ]
  149. [6.2 3.4]
  150. [5.9 3. ]]
  151. training prediction:0.819
  152. test data prediction:0.778
  153. training data Accuracy:0.819
  154. testing data Accuracy:0.778
  155. decision_function:
  156. [[-0.5 1.20887337 2.29112663]
  157. [ 2.06328814 -0.0769677 1.01367956]
  158. [ 2.16674973 0.91702835 -0.08377808]
  159. [ 2.11427813 0.99765248 -0.11193061]
  160. [ 0.9925538 2.06392138 -0.05647518]
  161. [ 2.11742969 0.95255534 -0.06998503]
  162. [ 2.05615004 -0.041847 0.98569697]
  163. [-0.31866596 1.02685964 2.29180632]
  164. [-0.27166251 1.09150338 2.18015913]
  165. [-0.37827567 1.14260447 2.2356712 ]
  166. [-0.22150749 1.11104997 2.11045752]
  167. [-0.18331208 2.10066724 1.08264485]
  168. [-0.05444966 0.99927764 2.05517201]
  169. [-0.46977766 1.17853774 2.29123992]
  170. [-0.05760122 2.04437478 1.01322644]
  171. [ 2.1747228 0.93698124 -0.11170404]
  172. [-0.13315707 2.12021384 1.01294323]
  173. [-0.21752096 2.12102642 1.09649454]
  174. [ 2.11427813 0.99765248 -0.11193061]
  175. [ 2.16359817 0.96212549 -0.12572366]
  176. [-0.21038286 1.08590572 2.12447714]
  177. [ 2.21291822 0.9265985 -0.13951672]
  178. [-0.13399204 1.06514025 2.06885179]
  179. [-0.18016052 1.0555701 2.12459042]
  180. [-0.2334671 1.08112064 2.15234646]
  181. [-0.08782356 2.0747104 1.01311315]
  182. [-0.20324476 1.05078502 2.15245974]
  183. [-0.11489433 1.05994888 2.05494545]
  184. [ 2.17787437 -0.1081159 0.93024154]
  185. [-0.23578369 2.18129137 1.05449232]
  186. [-0.20639632 1.09588216 2.11051416]
  187. [-0.21038286 1.08590572 2.12447714]
  188. [-0.02969547 2.11420989 0.91548558]
  189. [-0.12685394 1.03001955 2.09683439]
  190. [-0.09496166 2.1098311 0.98513056]
  191. [ 2.10547008 -0.07737399 0.97190391]
  192. [ 2.11029159 0.98767604 -0.09796763]
  193. [ 2.20411017 -0.14842797 0.9443178 ]
  194. [-0.20324476 1.05078502 2.15245974]
  195. [ 2.19066895 0.97688701 -0.16755596]
  196. [-0.16022784 2.10545232 1.05477553]
  197. [-0.23661866 1.12621778 2.11040088]
  198. [-0.09579663 2.05475752 1.04103911]
  199. [ 2.11344315 -0.05742111 0.94397795]
  200. [ 2.10231852 0.96772315 -0.07004167]
  201. [-0.12203243 2.09506958 1.02696285]
  202. [ 2.11029159 0.98767604 -0.09796763]
  203. [-0.41248455 1.16296364 2.2495209 ]
  204. [-0.16820091 1.08549943 2.08270149]
  205. [-0.42045762 1.14301076 2.27744686]
  206. [-0.24857827 1.09628845 2.15228982]
  207. [-0.27796564 2.18169766 1.09626798]
  208. [-0.09264507 1.00966038 2.08298469]
  209. [-0.25339978 1.03123843 2.22216135]
  210. [-0.05361468 2.05435123 0.99926346]
  211. [ 2.15395516 -0.16797456 1.01401941]
  212. [-0.12203243 2.09506958 1.02696285]
  213. [ 2.06579305 1.08825305 -0.15404611]
  214. [-0.11007283 2.12499891 0.98507392]
  215. [-0.27166251 1.09150338 2.18015913]
  216. [ 2.13652739 0.94736397 -0.08389137]
  217. [-0.29789831 1.13181544 2.16608287]
  218. [ 2.15163856 0.93219616 -0.08383473]
  219. [ 2.1747228 0.93698124 -0.11170404]
  220. [-0.11174277 1.01485174 2.09689103]
  221. [-0.06872585 2.06951904 0.99920682]
  222. [-0.23745364 1.0711442 2.16630944]
  223. [ 2.12141623 0.96253178 -0.08394801]
  224. [ 2.1627632 -0.09294809 0.93018489]
  225. [-0.06557429 1.0244219 2.04115239]
  226. [ 2.16758471 0.97210193 -0.13968664]
  227. [-0.12203243 2.09506958 1.02696285]
  228. [ 2.1293893 0.98248467 -0.11187396]
  229. [-0.21038286 1.08590572 2.12447714]
  230. [ 2.01962457 1.0786829 -0.09830747]
  231. [ 2.18269588 0.95693412 -0.13963 ]
  232. [-0.16106282 1.05037873 2.11068408]
  233. [ 2.20976665 0.97169564 -0.1814623 ]
  234. [-0.03850351 2.03918342 0.9993201 ]
  235. [ 2.17555778 0.99205482 -0.1676126 ]
  236. [-0.11007283 2.12499891 0.98507392]
  237. [-0.07502898 2.15971332 0.91531566]
  238. [ 2.13254086 0.93738753 -0.06992839]
  239. [ 2.09518042 1.00284385 -0.09802427]
  240. [ 1.0045134 2.09385071 -0.09836411]
  241. [ 2.24314055 0.89626288 -0.13940344]
  242. [-0.09579663 2.05475752 1.04103911]
  243. [-0.14910321 1.08030806 2.06879515]
  244. [ 2.13652739 0.94736397 -0.08389137]
  245. [-0.2334671 1.08112064 2.15234646]
  246. [-0.07271239 2.05954259 1.0131698 ]
  247. [-0.2739791 2.1916741 1.082305 ]
  248. [-0.27564905 1.08152693 2.19412211]
  249. [-0.12203243 2.09506958 1.02696285]
  250. [ 2.06013657 -0.03187056 0.97173399]
  251. [ 2.07608272 1.00803521 -0.08411793]
  252. [-0.19443672 2.12581149 1.06862523]
  253. [-0.16421438 2.09547587 1.06873851]
  254. [-0.3440668 1.12224529 2.22182151]
  255. [-0.1180459 2.10504603 1.01299987]
  256. [-0.20240979 1.10585861 2.09655118]
  257. [-0.17617399 1.06554654 2.11062744]
  258. [-0.2477433 2.15136204 1.09638126]
  259. [-0.2334671 1.08112064 2.15234646]
  260. [ 2.11029159 0.98767604 -0.09796763]]
  261. grid_test:
  262. [[4.3 2. ]
  263. [4.3 2.0120603]
  264. [4.3 2.0241206]
  265. ...
  266. [7.9 4.3758794]
  267. [7.9 4.3879397]
  268. [7.9 4.4 ]]
  269. the distance to decision plane:
  270. [[ 2.04663576 1.0980928 -0.14472856]
  271. [ 2.04808477 1.09663836 -0.14472313]
  272. [ 2.04953377 1.09518392 -0.1447177 ]
  273. ...
  274. [-0.21454554 0.96016146 2.25438408]
  275. [-0.21309653 0.95870702 2.25438951]
  276. [-0.21164753 0.95725258 2.25439495]]
  277. grid_hat:
  278. [0. 0. 0. ... 2. 2. 2.]

image.png