读取图片数据
1 | import os |
1 | path = 'water_images' |
1 | imgs = [] # 用来存储每次读取到的图片数据 |
数据预处理
切分出每张图片的中心$100\times 100$区域图像
1 | import matplotlib.pyplot as plt |
1 | # 1. 获取每张图片的尺寸 |
1 | plt.imshow(new_img[:, :, ::-1]) |
1 | # 按照上述操作,循环切分每张图片 |
1 | # 将列表转化为数组 |
1 | new_imgs.shape |
(197, 100, 100, 3)
计算每张图像的一阶矩、二阶矩和三阶矩数据
1 | # 将三个通道分开 |
1 | # Python内部默认是无法对负数求奇次方根 |
(1.0000000000000002+1.7320508075688772j)
1 | # 定义存储矩特征的容器 |
1 | # 循环计算所有数据的3阶矩 |
1 | featues.shape |
(197, 9)
数据集的切分(训练集+测试集)
1 | from sklearn.model_selection import train_test_split |
1 | print(x_train.shape) |
(157, 9)
(40, 9)
训练模型和测试
支持向量机
1 | from sklearn.svm import SVC |
1 | svm_model = SVC() |
1 | svm_model.fit(x_train, y_train) |
SVC()
1 | svm_model.score(x_test, y_test) |
0.7
决策树
1 | from sklearn.tree import DecisionTreeClassifier |
1 | tree_model = DecisionTreeClassifier() |
1 | tree_model.fit(x_train, y_train) |
DecisionTreeClassifier()
1 | tree_model.score(x_test, y_test) |
0.875
神经网络
1 | from sklearn.neural_network import MLPClassifier |
1 | mlp_model = MLPClassifier() |
1 | mlp_model.fit(x_train, y_train) |
D:\Users\Python\Anaconda3.8\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:692: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.
warnings.warn(
MLPClassifier()
1 | mlp_model.score(x_test, y_test) |
0.625
随机森林
1 | from sklearn.ensemble import RandomForestClassifier |
1 | random_model = RandomForestClassifier() |
1 | random_model.fit(x_train, y_train) |
RandomForestClassifier()
1 | random_model.score(x_test, y_test) |
0.95
模型名称 | 精度 |
---|---|
支持向量机 | 0.7 |
决策树 | 0.875 |
神经网络 | 0.625 |
随机森林 | 0.95 |
从结果上来看,随机森林模型的精度达到了0.95,为4个模型中最高的一个,所以在解决该任务时,最好的分类模型是随机森林模型。