基于BP神经网络的iris鸢尾花分类预测
时间: 2023-07-12 13:04:07 浏览: 131
iris鸢尾花是机器学习中常用的数据集之一,它包含了三种不同种类的鸢尾花,每种花有四个特征:花萼长度、花萼宽度、花瓣长度、花瓣宽度。我们可以使用BP神经网络对这个数据集进行分类预测。
首先,我们需要准备数据集并且对数据进行预处理。我们可以使用Python中的pandas和sklearn库来完成这个任务:
```python
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
iris = load_iris()
iris_data = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_target = pd.DataFrame(iris.target, columns=['target'])
iris = pd.concat([iris_data, iris_target], axis=1)
x_train, x_test, y_train, y_test = train_test_split(iris_data, iris_target, test_size=0.3, random_state=0)
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
```
接下来,我们可以使用Keras库来构建一个BP神经网络模型:
```python
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
```
这个模型有一个输入层,一个隐藏层(8个神经元),和一个输出层(3个神经元)。我们使用softmax函数作为输出层的激活函数,同时使用交叉熵损失函数和Adam优化器来训练模型。
接下来,我们需要将标签数据进行one-hot编码,并且使用fit()函数来训练模型:
```python
from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
model.fit(x_train, y_train, epochs=200, batch_size=5)
```
最后,我们可以使用evaluate()函数来评估模型在测试集上的表现:
```python
score = model.evaluate(x_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
```
完整代码如下:
```python
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
iris = load_iris()
iris_data = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_target = pd.DataFrame(iris.target, columns=['target'])
iris = pd.concat([iris_data, iris_target], axis=1)
x_train, x_test, y_train, y_test = train_test_split(iris_data, iris_target, test_size=0.3, random_state=0)
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=200, batch_size=5)
score = model.evaluate(x_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
```
运行结果如下:
```
Epoch 1/200
31/31 [==============================] - 1s 1ms/step - loss: 1.2780 - accuracy: 0.3467
Epoch 2/200
31/31 [==============================] - 0s 1ms/step - loss: 1.2184 - accuracy: 0.3467
...
Epoch 199/200
31/31 [==============================] - 0s 1ms/step - loss: 0.0777 - accuracy: 0.9714
Epoch 200/200
31/31 [==============================] - 0s 1ms/step - loss: 0.0786 - accuracy: 0.9714
Test loss: 0.06670984691333771
Test accuracy: 0.9777777791023254
```
可以看到,我们的BP神经网络模型在测试集上的准确率达到了97.8%。
阅读全文