集成学习python代码简单示例

from imutils import paths
import numpy as np
import cv2
import os

def extract_histogram(image, bins=(8, 8, 8)):
    hist = cv2.calcHist([image], [0, 1, 2], None, bins, [0, 256, 0, 256, 0, 256])
    cv2.normalize(hist, hist)
    return hist.flatten()

#加载数据

imagePaths = sorted(list(paths.list_images('./Data/train_4')))
trainData = []
labels = []

for (i, imagePath) in enumerate(imagePaths):
    image = cv2.imread(imagePath, 1)
    label = imagePath.split(os.path.sep)[-1].split(".")[0]
    hist = extract_histogram(image)
    trainData.append(hist)
    labels.append(label)


#设置标签

Y = [1 if x == 'cat' else 0 for x in labels]


#调用决策树

from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier

tree = DecisionTreeClassifier(criterion='entropy',
                              min_samples_leaf=10, 
                              max_leaf_nodes=20, 
                              random_state=220)
bagging = BaggingClassifier(tree,
                            n_estimators=19, 
                            random_state=220)
bagging.fit(trainData, Y)


#调用支持向量机

from sklearn.svm import LinearSVC

svm = LinearSVC(random_state = 220, C = 1.74)
svm.fit(trainData, Y)

#%%调用随机森林

from sklearn.ensemble import RandomForestClassifier

forest = RandomForestClassifier(n_estimators=19, #количество деревьев
                             criterion='entropy', #критерий разделения
                              min_samples_leaf=10, #минимальное число объектов в листе
                              max_leaf_nodes=20, #максимальное число листьев
                              random_state=220)
forest.fit(trainData, Y)


#调用逻辑回归

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression(solver='lbfgs', random_state=220)

#使用Stacking方法集成

from sklearn.ensemble import StackingClassifier

base_estimators = [('SVM', svm), ('Bagging DT', bagging), ('DecisionForest', forest)]
sclf = StackingClassifier(estimators=base_estimators, final_estimator=lr, cv=2)
sclf.fit(trainData, Y)




#1

print("Acurracy:",sclf.score(trainData, Y))


#2

singleImage = cv2.imread('./Data/test_4/cat.1009.jpg')
histt = extract_histogram(singleImage)
histt2 = histt.reshape(1, -1)
prediction = sclf.predict(histt2)
print("\n1:",np.round(sclf.predict_proba(histt2)[0][1],3))
#3
singleImage = cv2.imread('./Data/test_4/cat.1015.jpg')
histt = extract_histogram(singleImage)
histt2 = histt.reshape(1, -1)
prediction = sclf.predict(histt2)
print("\n2:",np.round(sclf.predict_proba(histt2)[0][1],3))
#4
singleImage = cv2.imread('./Data/test_4/dog.1014.jpg')
histt = extract_histogram(singleImage)
histt2 = histt.reshape(1, -1)
prediction = sclf.predict(histt2)
print("\n3:",np.round(sclf.predict_proba(histt2)[0][1],3))
#5
singleImage = cv2.imread('./Data/test_4/cat.1028.jpg')
histt = extract_histogram(singleImage)
histt2 = histt.reshape(1, -1)
prediction = sclf.predict(histt2)
print("\n4:",np.round(sclf.predict_proba(histt2)[0][1],3))

数据集下载地址:集成学习训练模型用的数据集成学习训练模型用的数据集成学习训练模型用的数据-机器学习文档类资源-CSDN下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

фора 快跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值