随机森林RandomForest实现鸢尾花分类
程序员文章站
2022-07-14 13:46:28
...
- 导入需要的包
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
import numpy as np
from sklearn.datasets import load_iris
- 加载自带数据集
#加载数据集
iris = load_iris()
iris
#features:萼片长度、萼片宽度、花瓣长度、花瓣宽度
#labels:setosa、vesicolor、virginica
- 建立随机森林模型
RF = RandomForestRegressor()#默认参数
RF.fit(iris.data,iris.target)
4. 预测
instance = iris.data[[100,109]]#挑两个样本
print("instance 0 prediction is {}".format(RF.predict(instance[[0]])))
print("instance 1 prediction is {}".format(RF.predict(instance[[1]])))
print("instance 0 actually is {}".format(iris.target[100]))
print("instance 1 actually is {}".format(iris.target[109]))