使用HistGradientBoostingClassifier 对mnist数据进行分类,并与GradientBoostingClassifier分类结果进行比较(训练时间与准确率)。
时间: 2024-02-18 09:00:28 浏览: 22
好的,我可以给你讲解一下如何使用HistGradientBoostingClassifier和GradientBoostingClassifier来对mnist数据进行分类,并进行比较。
首先,需要导入mnist数据集:
```python
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
```
接着,对数据进行切分,分为训练集和测试集:
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(mnist.data, mnist.target, test_size=0.2, random_state=42)
```
然后,可以使用HistGradientBoostingClassifier和GradientBoostingClassifier进行模型训练和预测:
```python
from sklearn.experimental import enable_hist_gradient_boosting
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score
import time
# 使用HistGradientBoostingClassifier进行训练和预测
start_time = time.time()
hist_clf = HistGradientBoostingClassifier(max_iter=100).fit(X_train, y_train)
hist_pred = hist_clf.predict(X_test)
hist_time = time.time() - start_time
hist_acc = accuracy_score(y_test, hist_pred)
# 使用GradientBoostingClassifier进行训练和预测
start_time = time.time()
grad_clf = GradientBoostingClassifier(max_iter=100).fit(X_train, y_train)
grad_pred = grad_clf.predict(X_test)
grad_time = time.time() - start_time
grad_acc = accuracy_score(y_test, grad_pred)
```
最后,可以输出训练时间和准确率进行比较:
```python
print("HistGradientBoostingClassifier:")
print("Training time: {:.2f}s".format(hist_time))
print("Accuracy: {:.2f}%".format(hist_acc * 100))
print("\nGradientBoostingClassifier:")
print("Training time: {:.2f}s".format(grad_time))
print("Accuracy: {:.2f}%".format(grad_acc * 100))
```
这样就可以使用HistGradientBoostingClassifier和GradientBoostingClassifier对mnist数据进行分类,并进行比较了。需要注意的是,由于HistGradientBoostingClassifier和GradientBoostingClassifier的参数不同,训练时间和准确率也会有所不同,具体结果可能会因参数和机器性能而异。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)