Пример #1
0
 def test_adaboost(self):
     train_X,train_y,test_X,test_y = loadHorseColic()
     adaboost = AdaBoostClassifier()
     adaboost.fit(train_X,train_y)
     preds = adaboost.predict(test_X)
     print(accuracy_score(preds,test_y))
     assert accuracy_score(preds,test_y)>0.7
Пример #2
0
def mk_dataset():
    face_dir = "/Users/zoushuai/Python/lab/datasets/original/face"
    nonface_dir = "/Users/zoushuai/Python/lab/datasets/original/nonface"
    face_list = resize_image(face_dir)
    nonface_list = resize_image(nonface_dir)
    train_set = face_list[0:250]
    train_set.extend(
        nonface_list[0:250])  # trainset contains 250 faces and 250 nonfaces
    train_set = np.array(train_set)
    validate_set = face_list[250:500]
    validate_set.extend(nonface_list[250:500]
                        )  # validateset contains 250 faces and 250 nonfaces
    validate_set = np.array(validate_set)
    train_img2feature_list = []
    validate_img2feature_list = []

    for i in range(500):
        npdFeature_train = NPDFeature(train_set[i])
        train_img2feature_list.append(npdFeature_train.extract())
        npdFeature_validate = NPDFeature(validate_set[i])
        validate_img2feature_list.append(npdFeature_validate.extract())
    train_img2feature_list = np.array(train_img2feature_list)
    validate_img2feature_list = np.array(validate_img2feature_list)
    AdaBoostClassifier.save(train_img2feature_list, 'train')
    AdaBoostClassifier.save(validate_img2feature_list, 'validate')
Пример #3
0
def organizeData(inputFileName, outputFileName):
    '''
    函数作用:读取图片,转化成灰度格式图片,抽取NPD特征,保存到本地
    :param outputFileName:输出文件名
    :param inputFileName:输入文件名
    :return:
    '''
    ImageList = getAllImage(inputFileName)
    grayList = convertImage(ImageList, 24)
    featureList = getNPDFeature(grayList)
    AdaBoostClassifier.save(featureList, outputFileName)
Пример #4
0
def test_breast_cancer():
    clf = AdaBoostClassifier(n_weakers_limit=50)
    X, y = load_breast_cancer(True)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.33, random_state=42)

    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    print(classification_report(y_test, y_pred))

    skclf = SkAdaBoostClassifier()
    skclf.fit(X_train, y_train)
    print(classification_report(y_test, skclf.predict(X_test)))
Пример #5
0
def process_boost():
    x_train, y_train, x_valid, y_valid = load_and_split()
    n_weakers_limit = 20
    adaBoost = AdaBoostClassifier(DecisionTreeClassifier, n_weakers_limit)
    adaBoost.fit(x_train, y_train)
    # 测试集预测
    predict_list = adaBoost.predict(x_valid)
    target_names = ['face', 'non_face']
    report = classification_report(y_valid,
                                   predict_list,
                                   target_names=target_names)
    with open("D:/testing/python/classifier_report.txt", "w") as f:
        f.write(report)
Пример #6
0
    def get_features(path):

        image_paths = [os.path.join(path, f) for f in os.listdir(path)]
        features = []
        # read the images and extract the features
        for image_path in image_paths:
            img = cv2.imread(image_path)
            # convert into gray image
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            img_reshape = cv2.resize(gray_img, (25, 25), interpolation=cv2.INTER_CUBIC)
            image = feature.NPDFeature(img_reshape)
            pre_features = feature.NPDFeature.extract(image)
            AdaBoostClassifier.save(pre_features, "save.p")
            face_feature = AdaBoostClassifier.load("save.p")
            features.append(face_feature)
        return features
Пример #7
0
    def train(train_X, train_y):
        weak_classifier = DecisionTreeClassifier(max_depth=3)
        ada = AdaBoostClassifier(weak_classifier, 5)
        ada.fit(train_X, train_y)
        result = ada.predict(train_X)
        diff = np.abs(result - train_y)
        diff[diff > ep] = 1
        t = np.sum(diff)
        print("错误预测的个数为: ", t)
        target_names = ['人脸', '非人脸']
        report = (classification_report(train_y,
                                        result,
                                        target_names=target_names))

        re_path = "/home/sun/ComputerScience/MachineLearning/Experiments/Experiment_three/ML2017-lab-03/report.txt"
        write_report(re_path, report)
        return ada
Пример #8
0
def test(X_test, y_test):
    print("Test the AdaBoost Classifier.")
    clf = AdaBoostClassifier.load(adb_model_path)
    pre_y = clf.predict(X_test)
    target_names = ['face', 'non face']
    report = classification_report(y_test, pre_y, target_names=target_names)
    print(report)
    with open(report_path, "w") as f:
        f.write(report)
        f.close()
        print("report has been wrote into %s" % report_path)
Пример #9
0
def pre_process():
    face_features = numpy.array([])
    nonface_features = numpy.array([])
    features = numpy.array([])

    #转化图片
    process_image(path_face_origin, path_face_purpose)
    process_image(path_nonface_origin, path_nonface_purpose)
    
    num_face = len(os.listdir('datasets/original/face'))
    num_nonface = len(os.listdir('datasets/original/nonface'))

    #获取特征
    face_features = get_feature(path_face_purpose)
    nonface_features = get_feature(path_nonface_purpose)

    print(face_features.shape,nonface_features.shape)
    print(num_face,num_nonface)
    
    #改变特征形状
    face_features.shape = num_face, 165600 
    nonface_features.shape = num_nonface, 165600

    #准备加入label
    face_y = numpy.ones(shape=(num_face, 1))
    nonface_y = numpy.zeros(shape=(num_nonface, 1))
    nonface_y -= numpy.ones(shape=(num_nonface, 1))

    #加入label
    face_features = numpy.concatenate([face_features, face_y], axis=1)
    nonface_features = numpy.concatenate([nonface_features, nonface_y], axis=1)

    #将所有数据合并
    features = numpy.row_stack((face_features,nonface_features))

    #写入缓存
    AdaBoostClassifier.save(features,"feature.data")
    return features
Пример #10
0
def train(X_train, y_train):
    clf = AdaBoostClassifier(DecisionTreeClassifier(max_depth=1),
                             n_weakers_limit=20)
    print("Training a AdaBoost Classifier.")
    clf.fit(X_train, y_train)
    # If model directories don't exist, create them
    if not os.path.isdir(os.path.split(adb_model_path)[0]):
        os.makedirs(os.path.split(adb_model_path)[0])
    clf.save(clf, adb_model_path)
Пример #11
0
    def Feature_extract():
        #提取特征
        face_path = '.\\datasets\\original\\face\\face_%03d.jpg'
        faces_path = []
        for i in range(500):
            faces_path.append(face_path % i)

        nonface_path = '.\\datasets\\original\\nonface\\nonface_%03d.jpg'
        nonfaces_path = []
        for i in range(500):
            nonfaces_path.append(nonface_path % i)

        train = np.zeros((1000, 165600))
        for i in range(500):
            img = Image.open(faces_path[i])
            img = img.convert('L').resize((24, 24))
            nf = NPDFeature(np.array(img))
            train[i * 2] = nf.extract()

            img = Image.open(nonfaces_path[i])
            img = img.convert('L').resize((24, 24))
            nf = NPDFeature(np.array(img))
            train[i * 2 + 1] = nf.extract()
        AdaBoostClassifier.save(train, 'train.txt')
Пример #12
0
def test_xor():
    X_train = np.array([
        [1, 1],
        [1, 0],
        [0, 1],
        [0, 0]
    ])

    y_train = np.array([
        0,
        1,
        1,
        0
    ])

    clf = AdaBoostClassifier(n_weakers_limit=1000)

    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_train)
    print(classification_report(y_train, y_pred))

    skclf = SkAdaBoostClassifier()
    skclf.fit(X_train, y_train)
    print(classification_report(y_train, skclf.predict(X_train)))
Пример #13
0
def test_image():
    path = 'datasets/original/'
    face = io.imread_collection(path + 'face/*.jpg')
    nonface = io.imread_collection(path + 'nonface/*.jpg')
    labels = ['face', 'nonface']

    X = []
    y = []

    # face_list = [get_features(i) for i in face]
    # nonface_list = [get_features(i) for i in nonface]
    # face_list = Parallel(n_jobs=4)(delayed(get_features)(i) for i in face)
    # nonface_list = Parallel(n_jobs=4)(
    #     delayed(get_features)(i) for i in nonface)

    # X += face_list
    # y += list(np.zeros(len(face_list), dtype=int))
    # X += nonface_list
    # y += list(np.ones(len(nonface_list), dtype=int))

    # AdaBoostClassifier.save(X, 'X.pkl')
    # AdaBoostClassifier.save(y, 'y.pkl')
    X = AdaBoostClassifier.load('X.pkl')
    y = AdaBoostClassifier.load('y.pkl')

    X_train, X_test, y_train, y_test = train_test_split(
        np.array(X), np.array(y), test_size=0.33, random_state=42)

    print('start training')

    clf = AdaBoostClassifier(n_weakers_limit=50)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    with open('report.txt', 'w') as f:
        print(classification_report(y_test, y_pred, target_names=labels), file=f)
Пример #14
0
    np.random.shuffle(features_dataset)
    np.save("./datasets/extract_features", features_dataset)


if __name__ == "__main__":
    get_original_features("face")
    get_original_features("nonface")
    face_features = np.load("./datasets/face_features.npy")
    nonface_features = np.load("./datasets/nonface_features.npy")
    get_features_dataset(face_features, nonface_features)
    features_dataset = np.load("./datasets/extract_features.npy")
    print(features_dataset.shape, face_features.shape, nonface_features.shape)
    num_face_feature = features_dataset.shape[1] - 1

    training_size = 800
    X_train = features_dataset[:training_size, :num_face_feature]
    X_validation = features_dataset[training_size:, :num_face_feature]

    y_train = features_dataset[:training_size, -1]
    y_validation = features_dataset[training_size:, -1]
    # print(X_train.shape,y_train.shape,X_validation.shape,y_validation.shape)
    adaboost_classifier = AdaBoostClassifier(
        DecisionTreeClassifier(max_depth=4), 5)
    pred_y = adaboost_classifier.fit(X_train, y_train).predict(X_validation)

    with open("report.txt", "wb") as f:
        report = classification_report(y_validation,
                                       pred_y,
                                       target_names=["nonface", "face"])
        f.write(report.encode())
Пример #15
0
        dataset = np.array(samples)
        with open('tmp.pkl', 'wb') as output:
            pickle.dump(dataset, output, True)

    with open('tmp.pkl', 'rb') as input:
        dataset = pickle.load(input)
        print(dataset.shape)
    # 将数据集切分为训练集和验证集
    X_train = dataset[:dataset.shape[0] * 3 // 4, :dataset.shape[1] - 1]
    y_train = dataset[:dataset.shape[0] * 3 // 4, dataset.shape[1] - 1]
    X_validation = dataset[dataset.shape[0] * 3 // 4:, :dataset.shape[1] - 1]
    y_validation = dataset[dataset.shape[0] * 3 // 4:, dataset.shape[1] - 1]
    return X_train, X_validation, y_train, y_validation


if __name__ == "__main__":
    X_train, X_validation, y_train, y_validation = loadDataSet()
    abc = AdaBoostClassifier(DecisionTreeClassifier, 20)
    abc.fit(X_train, y_train)
    final_pre_y = abc.predict(X_validation)
    error = 0
    for i in range(final_pre_y.shape[0]):
        if final_pre_y[i] != y_validation[i]:
            error = error + 1
    accuracy = 1 - error / y_validation.shape[0]
    print('accuracy: %f' % accuracy)
    target_names = ['face', 'nonface']
    report = classification_report(y_validation, final_pre_y, target_names=target_names)
    print(report)
    with open('report.txt', 'w') as f:
        f.write(report)
Пример #16
0
import numpy as np
import feature
from ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

import os

if __name__ == "__main__":
    # write your code here
    features = []
    y = np.ones((500, 1))
    y = np.append(y, np.ones((500, 1)) * -1).reshape((-1, 1))
    if os.path.exists('features.dump'):
        features = AdaBoostClassifier.load('features.dump')
    else:
        pathes = np.array(
            list(
                map(lambda s: 'datasets/original/face/' + s,
                    os.listdir('datasets/original/face'))))
        pathes = np.append(
            pathes,
            np.array(
                list(
                    map(lambda s: 'datasets/original/nonface/' + s,
                        os.listdir('datasets/original/nonface')))))
        for index, path in enumerate(pathes):
            with Image.open(path) as image:
                print(index, path)
                image = image.convert('L')
Пример #17
0
 def fit(self, X, y):
     # clear the adaboost container
     self.adaboosts.clear()
     # data preprocess
     y_face_nonface = Hierarchy_Adaboost.seperate_face_nonface(y)
     X_male_female, y_male_female = Hierarchy_Adaboost.extract_male_female(
         X, y)
     X_animal_object, y_animal_object = Hierarchy_Adaboost.extract_animal_object(
         X, y)
     # initialize a decision tree classifier
     dt = DecisionTreeClassifier(max_depth=4)
     # train an adaboost for each different situation
     # adaboost for classifying face images and nonface images
     print("train adaboost_face_nonface")
     adaboost_face_nonface = AdaBoostClassifier(dt, self.maximum_weakers)
     adaboost_face_nonface.fit(X, y_face_nonface)
     self.adaboosts.append(adaboost_face_nonface)
     # adaboost for classifying male images and female images
     print("train adaboost_male_female")
     adaboost_male_female = AdaBoostClassifier(dt, self.maximum_weakers)
     adaboost_male_female.fit(X_male_female, y_male_female)
     self.adaboosts.append(adaboost_male_female)
     # adaboost for classifying animal images and object images
     print("train adaboost_animal_object")
     adaboost_animal_object = AdaBoostClassifier(dt, self.maximum_weakers)
     adaboost_animal_object.fit(X_animal_object, y_animal_object)
     self.adaboosts.append(adaboost_animal_object)
Пример #18
0
    if min == max:
        return 1

    length = max - min
    num_repeat = (weight - min) * (range_right - 1) / length + 1
    return num_repeat


trainset = torchvision.datasets.MNIST('./data',
                                      train=True,
                                      download=True,
                                      transform=transforms.ToTensor())
trainloader = torch.utils.data.DataLoader(dataset=trainset, shuffle=False)

CLASSIFIER_NUM = 9
classifier = AdaBoostClassifier(mlpClassifier)
classifier.train(trainloader, classifier_num=CLASSIFIER_NUM)

test_dataset = torchvision.datasets.MNIST(root='./data',
                                          train=False,
                                          download=True,
                                          transform=transforms.ToTensor())
test_dataloader = torch.utils.data.DataLoader(test_dataset, shuffle=False)

# Test the AdaBoostClassifier
correct = 0
for batch_index, (data, target) in enumerate(test_dataloader):
    # Copy data to GPU if needed
    data = data.to(device)
    target = target.to(device)
                         str(i).rjust(3, '0') + ".jpg")
        img = cv2.resize(img, dsize=(24, 24))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 转换了灰度化
        npd = NPDFeature(img)
        x.append(npd.extract())
        y.append(1)
    for i in tqdm(range(500)):
        img = cv2.imread("./datasets/original/nonface/nonface_" +
                         str(i).rjust(3, '0') + ".jpg")
        img = cv2.resize(img, dsize=(24, 24))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 转换了灰度化
        npd = NPDFeature(img)
        x.append(npd.extract())
        y.append(-1)
    x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2)

    print('begin train data')
    ada = AdaBoostClassifier()
    ada.fit(x_train, y_train)
    y_predict = ada.predict(x_val, threshold=0)
    print(
        classification_report(y_val,
                              y_predict,
                              target_names=["nonface", "face"],
                              digits=4))
    with open("report.txt", "w") as f:
        f.write(
            classification_report(y_val,
                                  y_predict,
                                  target_names=["nonface", "face"],
                                  digits=4))
Пример #20
0
import numpy as np
import pickle
from numpy import *
import matplotlib.image as mpimg
from skimage import io
import os
from feature import *
from PIL import Image
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from ensemble import AdaBoostClassifier
#这里是直接读取灰度图,灰度图在original文件夹里面
path1=[os.path.join('G:\\Users\\qqqqqq1997520\\Desktop\\original\\face\\face',f) for f in os.listdir('G:\\Users\\qqqqqq1997520\\Desktop\\original\\face\\face')]
path2 = [os.path.join('G:\\Users\\qqqqqq1997520\\Desktop\\original\\face\\nonface',f) for f in os.listdir('G:\\Users\\qqqqqq1997520\\Desktop\\original\\face\\nonface')]
ABC=AdaBoostClassifier(DecisionTreeClassifier(), 1)
im=[0 for i in range(1000)]
for i in range(500):
    im[i]=plt.imread(path1[i])
for i in arange(500,1000):
    im[i]=plt.imread(path2[(i%500)])
_feature=[0 for i in range(1000)]
for i in range(1000):
    feature=NPDFeature(im[i])
    _feature[i]=feature.extract()
    
feature_data=array(_feature)
y=[1 for i in range(1000)]
for i in range(500,1000):
    y[i]=-1;
y=array(y)
Пример #21
0
        X_train, X_vali, y_train, y_vali = train_test_split(X,
                                                            y,
                                                            test_size=0.2,
                                                            random_state=24)
        output = open(datafile, 'wb')
        pickle.dump(X_train, output)
        pickle.dump(X_vali, output)
        pickle.dump(y_train, output)
        pickle.dump(y_vali, output)
        output.close()

    #create adaboost/weak classifier
    dtc = DecisionTreeClassifier(random_state=0,
                                 max_depth=3,
                                 max_features="sqrt")
    classifier = AdaBoostClassifier(dtc, 15)
    #train classifiers
    classifier.fit(X_train, y_train)
    dtc.fit(X_train, y_train)
    #do prediction
    result = classifier.predict(X_vali)
    weakresult = dtc.predict(X_vali)

    #calculate predicting accuracy for both
    adacount = 0
    weakcount = 0
    for i in range(0, result.shape[0]):
        if (np.abs(result[i] - 1) < np.abs(result[i] + 1)):
            result[i] = 1
        else:
            result[i] = -1
Пример #22
0
    for i in range(0,500):
        img_nonface=mpimg.imread(currentpath2+"{:0>3d}".format(i)+".jpg")
        img_nonface_=rgb2gray(img_nonface)
        f=NPDFeature(img_nonface_)
        feature_=f.extract()
        feature.append(feature_)
        label.append(-1)

if __name__ == "__main__":
    # write your code here  
    
    readimg()
    train_feature,validation_feature,train_label,validation_label=train_test_split(feature,label,test_size=0.3)
    #adaboost
    adaboostClassifier=AdaBoostClassifier(DecisionTreeClassifier(max_depth = 1, random_state = 1),20)
    
    adaboostClassifier.save(train_feature,'train_feature')
    adaboostClassifier.save(train_label,'train_label')
    adaboostClassifier.save(validation_feature,'validation_feature')
    adaboostClassifier.save(validation_label,'validation_label')

    adaboostClassifier.fit(train_feature,train_label)
    adaboostClassifier.drawPic()
    

    '''
    #debug
    adaboostClassifier=AdaBoostClassifier(DecisionTreeClassifier(max_depth = 1, random_state = 1),20)
    train_feature=adaboostClassifier.load("train_feature")
    train_label=adaboostClassifier.load("train_label")
Пример #23
0
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = torchvision.datasets.CIFAR10(root='./data',
                                        train=True,
                                        download=True,
                                        transform=transform_train)
train_dataloader = torch.utils.data.DataLoader(trainset,
                                               batch_size=BATCH_SIZE,
                                               shuffle=False)
validation_dataloader = torch.utils.data.DataLoader(trainset,
                                                    batch_size=1,
                                                    shuffle=False)

# Define and train the AdaBoostClassifier
classifier = AdaBoostClassifier(ResNetBibdGcClassifier)
classifier.train(train_dataloader,
                 validation_dataloader,
                 classifier_num=CLASSIFIER_NUM)

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
test_dataset = torchvision.datasets.CIFAR10(root='./data',
                                            train=False,
                                            download=True,
                                            transform=transform_test)
test_dataloader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=1,
                                              shuffle=False)
Пример #24
0
def preprocess():
    x, y = to_gray_resize('datasets/original/face/')
    x, y = to_gray_resize('datasets/original/nonface/', x, y)

    #write binary
    with open('datasets/features/feature', 'wb') as file:
        pickle.dump(x, file)
    with open('datasets/features/label', 'wb') as file:
        pickle.dump(y, file)
    print(x.shape, y.shape)


if __name__ == "__main__":
    print('loading data...')
    # preprocess()
    with open('datasets/features/feature', 'rb') as file:
        x = pickle.load(file)
    with open('datasets/features/label', 'rb') as file:
        y = pickle.load(file)

    X_train, X_test, y_train, y_test = train_test_split(x,
                                                        y,
                                                        test_size=0.25,
                                                        random_state=42)
    print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)

    print('start training...')
    ada_clf = AdaBoostClassifier(DecisionTreeClassifier(max_depth=3), 10)
    ada_clf.fit(X_train, y_train, X_test, y_test)
    ada_clf.plotting()
Пример #25
0
        img_features.append(features)


if __name__ == "__main__":
    load_img()
    npd_feature()
    img_features = np.array(img_features)
    img_labels = np.array(img_labels).reshape((-1, 1))
    print(img_features.shape)
    print(img_features)
    X_train, X_val, y_train, y_val = train_test_split(img_features,
                                                      img_labels,
                                                      test_size=0.25)
    print(X_train.shape, X_val.shape, y_train.shape, y_val.shape)

    ada = AdaBoostClassifier(DecisionTreeClassifier, WEAKERS_LIMIT)
    ada.fit(X_train, y_train)

    y_predict = ada.predict(X_val)
    acc = ada.predict_scores(X_val, y_val)

    print(acc)

    y_val = np.array(list(map(lambda x: int(x), y_val.reshape(1, -1)[0])))
    y_predict = np.array(
        list(map(lambda x: int(x),
                 y_predict.reshape(1, -1)[0])))

    print(y_predict)
    print(y_val)
Пример #26
0
        with open('NPD_feature.pkl', 'wb') as f:
            pickle.dump(NPD_feature_list, f)

        # 将标签保存起来
        data = pd.DataFrame({'label': label})
        data.to_csv('label.csv')

    else:
        print("preprocessing has been performed")


if __name__ == "__main__":
    # 预处理数据
    preprocess_data()
    # 加载数据
    feature = AdaBoostClassifier.load('NPD_feature.pkl')
    label = pd.read_csv('label.csv')['label']
    feature = np.array(feature)
    label = np.array(label)

    # 划分训练集和验证集
    X_train, X_val, y_train, y_val = train_test_split(feature,
                                                      label,
                                                      test_size=0.4,
                                                      random_state=2019,
                                                      shuffle=True)
    print('X_train.shape', X_train.shape)
    print('X_val.shape', X_val.shape)
    print('y_train.shape', y_train.shape)
    print('y_val.shape', y_val.shape)
Пример #27
0
    plt.ylabel('Accuracy')
    plt.plot(range(len(validation_score_list)), validation_score_list)
    #plt.grid()
    plt.show()


if __name__ == "__main__":

    pre_image()
    with open('features', "rb") as f:
        x = pickle.load(f)
    with open('labels', "rb") as f:
        y = pickle.load(f)
    X_train, X_test, y_train, y_test = train_test_split(x,
                                                        y,
                                                        test_size=0.33,
                                                        random_state=0)
    maxIteration = 10
    s, validation_score_list = AdaBoostClassifier(
        DecisionTreeClassifier(max_depth=3),
        maxIteration).fit(X_train, y_train)
    predict_y = s.predict(X_test)

    acc_plot(validation_score_list)

    with open('report.txt', "wb") as f:
        report = classification_report(y_test,
                                       predict_y,
                                       target_names=["face", "nonface"])
        f.write(report.encode())
Пример #28
0

def split_dataset(dataset, train_ratio=0.8):
    """
    :return: X_train, y_train, X_valid, y_valid
    """
    pivot = int(2 * SAMPLES_N * train_ratio)
    train_set = dataset[0][:pivot], dataset[1][:pivot]
    valid_set = dataset[0][pivot:], dataset[1][pivot:]
    return train_set + valid_set


if __name__ == "__main__":
    X_train, y_train, X_valid, y_valid = split_dataset(load_dataset())

    adaBoost = AdaBoostClassifier(DecisionTreeClassifier, WEAKERS_LIMIT)
    accs = adaBoost.fit(X_train, y_train, X_valid, y_valid)

    plt.figure(figsize=[8, 5])
    plt.title('Accuracy')
    plt.xlabel('Num of weak classifiers')
    plt.ylabel('Accuracy')
    plt.plot(accs[0], '--', c='b', linewidth=3, label='train')
    plt.plot(accs[1], c='r', linewidth=3, label='valid')
    plt.legend()
    plt.grid()
    plt.savefig('AdaBoost-accuracy.png')
    plt.show()

    AdaBoostClassifier.save(adaBoost, 'AdaBoost-Model.pkl')
Пример #29
0
    # 数据集加标签,并划分训练集,验证集
    Data = loadData("data")
    label = np.ones(1000)
    label[500:] = -1
    train_x, train_y, validation_x, validation_y = split(Data, label, 0.4)
    saveData("train", train_x)
    saveData("label", train_y)
    saveData("validation", validation_x)
    saveData("target", validation_y)

    train = loadData("train")
    train_x = np.array(train)
    label = loadData("label")
    train_y = np.array(label)
    validation = loadData("validation")
    test_x = np.array(validation)
    target = loadData("target")
    test_y = np.array(target)
    weakClassifier = DecisionTreeClassifier(max_depth=3)
    cls = AdaBoostClassifier(weakClassifier, num_classifier)
    cls = cls.fit(train_x, train_y)
    result_adaboost = cls.predict(test_x, 0)
    print('adaboost result: ', result_adaboost)
    print('accuracy: ', validate_result(result_adaboost, test_y))
    target_names = {'nonface', 'face'}
    output = open('report.txt', 'w')
    output.write(
        classification_report(test_y,
                              result_adaboost,
                              target_names=target_names))
Пример #30
0
        y.append(1)
        print(i)

    for i in range(0, 500):
        path = "C:\\Users\\Administrator\\Desktop\\ML2017-lab-03\\datasets\\original\\nonface\\nonface_%.3d.jpg" % (
            i)
        img = Image.open(path).convert('L').resize((24, 24))
        X.append(NPDFeature(np.array(img)).extract())
        y.append(-1)
        print(i)

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.33,
                                                        random_state=42)
    ada = AdaBoostClassifier(DecisionTreeClassifier, 20)

    X_train = np.array(X_train)
    X_test = np.array(X_test)
    y_train = np.array(y_train)
    y_test = np.array(y_test)
    y_train = y_train.reshape((y_train.shape[0], 1))
    y_test = y_test.reshape((y_test.shape[0], 1))

    ada.fit(X_train, y_train)
    h = ada.predict(X_test)
    yes = 0
    no = 0
    for i in range(0, len(h)):
        if (h[i] == y_test[i]): yes += 1
        if (h[i] != y_test[i]): no += 1