bpnn 分类python
时间: 2023-11-13 13:55:46 浏览: 131
BPNN(Back Propagation Neural Network)是一种常见的神经网络模型,用于分类和回归问题。在Python中,可以使用多种库来实现BPNN分类,如TensorFlow、Keras、PyTorch等。以下是一个使用Keras实现BPNN分类的示例代码:
```python
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# 加载数据集
iris = load_iris()
X, y = iris.data, iris.target
# 将标签转换为one-hot编码
y = np_utils.to_categorical(y)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 构建模型
model = Sequential()
model.add(Dense(10, 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=100, batch_size=5)
# 评估模型
score = model.evaluate(X_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
```
阅读全文