集成学习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下载

你可能感兴趣的:(机器学习基础,python,集成学习,机器学习)