特征: 花萼长度 + 花萼宽度 预测正确数目: 123 准确率: 82.00%
特征: 花萼长度 + 花瓣长度 预测正确数目: 143 准确率: 95.33%
特征: 花萼长度 + 花瓣宽度 预测正确数目: 145 准确率: 96.67%
特征: 花萼宽度 + 花瓣长度 预测正确数目: 144 准确率: 96.00%
特征: 花萼宽度 + 花瓣宽度 预测正确数目: 145 准确率: 96.67%
特征: 花瓣长度 + 花瓣宽度 预测正确数目: 145 准确率: 96.67%
实现代码
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn.ensemble import RandomForestClassifier
def iris_type(s):
it = {'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}
return it[s]
# 'sepal length', 'sepal width', 'petal length', 'petal width'
iris_feature = u'花萼长度', u'花萼宽度', u'花瓣长度', u'花瓣宽度'
if __name__ == "__main__":
mpl.rcParams['font.sans-serif'] = [u'SimHei'] # 黑体 FangSong/KaiTi
mpl.rcParams['axes.unicode_minus'] = False
path = '10.iris.data' # 数据文件路径
data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type})
x_prime, y = np.split(data, (4,), axis=1)
#不同的列交叉组合成不同的特征
feature_pairs = [[0, 1], [0, 2], [0, 3], [1, 2