Ejemplo n.º 1
0
    def train(self):

        if (self.status != 'init'):
            print("Please load train data and init W first.")
            return self.W

        self.status = 'train'

        self.svm_processor = svm.BinaryClassifier()
        self.svm_processor.load_train_data()
        self.svm_processor.train_X = self.train_X
        self.svm_processor.train_Y = self.train_Y
        self.svm_processor.set_param(svm_kernel=self.svm_kernel, gamma=self.gamma, C=self.C)
        self.svm_processor.init_W()
        self.svm_processor.train()

        # slow
        svm_transform_X = np.apply_along_axis(self.svm_score, axis=1, arr=self.train_X)
        svm_transform_X = np.reshape(svm_transform_X, (-1, 1))
        svm_transform_X0 = np.reshape(np.ones(self.data_num), (-1, 1))
        svm_transform_X = np.concatenate((svm_transform_X0, svm_transform_X), axis=1)

        self.logistic_processor = logistic_regression.LogisticRegression()
        self.logistic_processor.load_train_data()
        self.logistic_processor.train_X = svm_transform_X
        self.logistic_processor.train_Y = self.train_Y
        self.logistic_processor.set_param(feed_mode=self.feed_mode, step_eta=self.step_eta, updates=self.updates)
        self.logistic_processor.init_W()
        self.logistic_processor.train()

        return self.W
Ejemplo n.º 2
0
# encoding=utf8

import os
import numpy as np
import FukuML.Utility as utility
import FukuML.SupportVectorMachine as svm

input_train_data_file = os.path.join(
    os.path.join(os.getcwd(), os.path.dirname(__file__)),
    'FukuML/dataset/emotion.dat')

cross_validator = utility.CrossValidator()

svm_mc = svm.MultiClassifier()
svm_mc.load_train_data(input_train_data_file)
svm_mc.set_param(svm_kernel='soft_gaussian_kernel', gamma=1, C=5)
cross_validator.add_model(svm_mc)
avg_errors = cross_validator.excute()
print(avg_errors)

svm_mc = svm.MultiClassifier()
svm_mc.load_train_data(input_train_data_file)
svm_mc.set_param(svm_kernel='soft_gaussian_kernel', gamma=1, C=5)
svm_mc.init_W()
svm_mc.train()
'''
for class_item in svm_mc.class_list:
    print(class_item)
    print(svm_mc.classifier_list[class_item].alpha)
    error_sv = (svm_mc.classifier_list[class_item].alpha > 0.999999999)
    print(np.arange(len(svm_mc.classifier_list[class_item].alpha))[error_sv])
Ejemplo n.º 3
0
#encoding=utf8

import os
import FukuML.Utility as utility
import FukuML.SupportVectorMachine as svm

input_train_data_file = os.path.join(
    os.path.join(os.getcwd(), os.path.dirname(__file__)),
    'library/iNDIEVOX-Dataset/dataset/emotion_combine_song_train.dataset')

cross_validator = utility.CrossValidator()

svm_mc1 = svm.MultiClassifier()
svm_mc1.load_train_data(input_train_data_file)
svm_mc1.set_param(svm_kernel='soft_gaussian_kernel', C=1)
svm_mc2 = svm.MultiClassifier()
svm_mc2.load_train_data(input_train_data_file)
svm_mc2.set_param(svm_kernel='soft_gaussian_kernel', C=10)
svm_mc3 = svm.MultiClassifier()
svm_mc3.load_train_data(input_train_data_file)
svm_mc3.set_param(svm_kernel='soft_gaussian_kernel', C=100)

print("\n10 fold cross validation:")

cross_validator.add_model(svm_mc1)
cross_validator.add_model(svm_mc2)
cross_validator.add_model(svm_mc3)
avg_errors = cross_validator.excute()

print("\n各模型驗證平均錯誤:")
print(avg_errors)