使用鸢尾花数据集写一段DBN和BP分类的代码
时间: 2023-05-16 15:06:44 浏览: 108
鸢尾花数据集bp算法matlab分类
5星 · 资源好评率100%
以下是使用鸢尾花数据集写的DBN和BP分类的代码:
DBN分类:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neural_network import BernoulliRBM
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义DBN模型
rbm = BernoulliRBM(n_components=2, learning_rate=0.01, n_iter=100)
logistic = LogisticRegression()
# 构建Pipeline
model = Pipeline(steps=[('rbm', rbm), ('logistic', logistic)])
# 训练模型
model.fit(X_train, y_train)
# 预测测试集
y_pred = model.predict(X_test)
# 输出准确率
print("Accuracy:", model.score(X_test, y_test))
```
BP分类:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义BP模型
clf = MLPClassifier(hidden_layer_sizes=(5, 2), max_iter=1000)
# 训练模型
clf.fit(X_train, y_train)
# 预测测试集
y_pred = clf.predict(X_test)
# 输出准确率
print("Accuracy:", clf.score(X_test, y_test))
```
阅读全文