示例#1
0
def run_all(gpu=False, double_precision=False):

    print("\nLasso.")
    print "Solve time:\t{:.2e} seconds\n".format(
        Lasso(200, 2000, gpu=gpu, double_precision=double_precision))

    print("\nLasso Path.")
    print "Solve time:\t{:.2e} seconds\n".format(
        LassoPath(200, 1000, gpu=gpu, double_precision=double_precision))

    print("\nLogistic Regression.")
    print "Solve time:\t{:.2e} seconds\n".format(
        Logistic(1000, 100, gpu=gpu, double_precision=double_precision))

    print("\nLinear Program in Equality Form.")
    print "Solve time:\t{:.2e} seconds\n".format(
        LpEq(1000, 200, gpu=gpu, double_precision=double_precision))

    print("\nLinear Program in Inequality Form.")
    print "Solve time:\t{:.2e} seconds\n".format(
        LpIneq(1000, 200, gpu=gpu, double_precision=double_precision))

    print("\nNon-Negative Least Squares.")
    print "Solve time:\t{:.2e} seconds\n".format(
        NonNegL2(1000, 200, gpu=gpu, double_precision=double_precision))

    print("\nSupport Vector Machine.")
    print "Solve time:\t{:.2e} seconds\n".format(
        Svm(1000, 200, gpu=gpu, double_precision=double_precision))
示例#2
0
def recognize():
    with AudioRecorder(rate=conf.sampling_rate) as recorder:
        svm = Svm()
        with open('trained_data.pickle.b64') as f:
            svm.import_model(f.read())
        while True:
            print()
            print('\x1b[A\x1b[2K' 'Press Enter to start recording or Ctrl+C to exit')
            input()
            recorder.start_recording()
            print('\x1b[A\x1b[2K' '\x1b[31mREC\x1b[0m Press Enter to stop recording')
            input()
            data = list(recorder.bytes_to_numseq(recorder.finish_recording()))
            transformed_data = list(sliding_diff(data, conf.sliding_diff_winsize))
            scaled_data = scale_array(transformed_data, conf.svm_inputs)
            pred = svm.get(scaled_data)
            print('\x1b[A\x1b[2K' 'Predicted output: {}'.format(pred))
示例#3
0
def learn():
    labels = get_labels()
    # mlr = MultiLogisticRegression(labels, conf.lr_inputs)
    svm = Svm()
    dataset = list(read_examples('data'))

    rd.shuffle(dataset)
    m = len(dataset)
    train_m = int(0.7 * m)
    train_set = dataset[:train_m]
    test_set = dataset[train_m:]
    test_m = len(test_set)
    print('Learning...')
    svm.learn(train_set)
    print('Learnt')


    correct = 0
    for x, y in train_set:
        pred = svm.get(x)
        if pred == y:
            correct += 1
    print('Results on train set: {:2f}% accuracy'.format(100 * correct / train_m))

    correct = 0
    for x, y in test_set:
        pred = svm.get(x)
        if pred == y:
            correct += 1
    print('Results on test set: {:2f}% accuracy'.format(100 * correct / test_m))
    with open('trained_data.pickle.b64', 'w') as wf:
        wf.write(svm.export_model())
    print('Weights written to trained_data.pickle.b64')
示例#4
0
文件: main.py 项目: mles2/UM
def display_f_test(inputs_from_feature_selection, dataset):
    print("ftest results: (f, p) =")
    print(
        " NN, KNN: ",
        combined_ftest_5x2cv(NeuralNet(10000).clf,
                             Knn(2).clf,
                             inputs_from_feature_selection,
                             dataset.target,
                             random_seed=1))
    print(
        " NN, SVM: ",
        combined_ftest_5x2cv(NeuralNet(10000).clf,
                             Svm().clf,
                             inputs_from_feature_selection,
                             dataset.target,
                             random_seed=1))
    print(
        " KNN, SVM: ",
        combined_ftest_5x2cv(Knn(2).clf,
                             Svm().clf,
                             inputs_from_feature_selection,
                             dataset.target,
                             random_seed=1))
    def run(self, x, nbit, resolution, error):

        nbits = str(nbit)
        test_data = np.load("./data/" + nbits + "bit" + "/" + nbits + "bit" +
                            "_" + str(resolution) + "x" + str(resolution) +
                            "_test_images.npy")
        train_data = np.load("./data/" + nbits + "bit" + "/" + nbits + "bit" +
                             "_" + str(resolution) + "x" + str(resolution) +
                             "_train_images.npy")

        x = int(x)
        if (error != 0):
            test_data = np.array(
                [self.pixel_bit_error(error, i, nbit) for i in test_data])

        if (x == 1):
            generations = input("enter the number of generations")
            batchSize = input("enter the size of each batch")
            generations = int(generations)
            batchSize = int(batchSize)
            Jesus = Cnn()
            Jesus.run(train_data, test_data, resolution, error, generations,
                      batchSize)
        if (x == 2):
            if (error != 0):
                train_data = np.array(
                    [self.pixel_bit_error(error, i, nbit) for i in train_data])
            Jesus = Svm()
            Jesus.run(train_data, test_data, resolution)
        if (x == 3):
            if (error != 0):
                train_data = np.array(
                    [self.pixel_bit_error(error, i, nbit) for i in train_data])
            k = input("k ?")
            k = int(k)
            Jesus = Knn(k)
            Jesus.run(train_data, test_data, resolution)
        if (x == 4):
            Jesus = Caliente([], error)
            batchSize = input("enter the size of each batch")
            generations = input("enter the number of generations")
            generations = int(generations)
            batchSize = int(batchSize)
            Jesus.run(train_data, test_data, resolution, generations,
                      batchSize)
xTest = np.hstack([xTest, np.ones((xTest.shape[0], 1))])

print('Train image shape after add bias column:   {0}'.format(xTrain.shape))
print('Val image shape after add bias column:     {0}'.format(xVal.shape))
print('Test image shape after add bias column:    {0}'.format(xTest.shape))
print(
    '\n##############################################################################################'
)

######################################################################################################
#                                       SVM CLASSIFIER                                               #
######################################################################################################
from svm import Svm
numClasses = np.max(yTrain) + 1
print('Start training Svm classifier')
classifier = Svm(xTrain.shape[1], numClasses)

# Show weight for each class before training
if classifier.W is not None:
    tmpW = classifier.W[:-1, :]
    tmpW = tmpW.reshape(32, 32, 3, 10)
    tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
    for i in range(numClasses):
        plt.subplot(2, 5, i + 1)
        plt.title(classesName[i])
        wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax -
                                                                  tmpWMin)
        plt.imshow(wPlot.astype('uint8'))
    plt.savefig(baseDir + 'svm1.png')
    plt.clf()
示例#7
0
from probcpredict import Probcpredict
from kfoldcv import Kfoldcv
from bootstrapping import Bootstrapping
from hypotest import Hypotest

#case 2_1
np.set_printoptions(precision=4)
X = np.array([[-3, 2],
[-2, 1.5],
[-1, 1],
[0, 0.5],
[1, 0]])
y = np.array([[1], [1], [1], [-1], [-1]])
pe=Perceptron(10,X,y)
theta_perceptron, num = pe.perceptron()
sv=Svm(X,y)
theta_svm = sv.svm()
pr=Predict(theta_perceptron,np.array([[1], [-2]]))
print("From preceptron algorithm, the theta is:",theta_perceptron,"using direction [[1], [-2]], it will be predicted as:",pr.predict())
pr2=Predict(theta_svm,np.array([[1], [-2]]))
print("From svm, the theta is:",theta_svm,"using direction [[1], [-2]], it will be predicted as:",pr2.predict())


#case 3_1
np.set_printoptions(precision=4)
X = np.array([[-3, 2],
                  [-2, 1.5],
                  [-1, 1],
                  [0, 0.5],
                  [1, 0]])
y = np.array([[1], [1], [1], [-1], [-1]])
示例#8
0
文件: main.py 项目: mles2/UM
def evaluateSvm(X_train, X_test, y_train, y_test):
    svm = Svm()
    return evaluate_classifier(svm, X_train, X_test, y_train, y_test)
示例#9
0
xTest = np.hstack([xTest, np.ones((xTest.shape[0], 1))])

print('Train image shape after add bias column:   {0}'.format(xTrain.shape))
print('Val image shape after add bias column:     {0}'.format(xVal.shape))
print('Test image shape after add bias column:    {0}'.format(xTest.shape))
print(
    '\n##############################################################################################'
)

######################################################################################################
#                                       SVM CLASSIFIER                                               #
######################################################################################################
from svm import Svm
numClasses = np.max(yTrain) + 1
print('Start training Svm classifier')
classifier = Svm(xTrain.shape[1], numClasses)

# Show weight for each class before training
if classifier.W is not None:
    tmpW = classifier.W[:-1, :]
    tmpW = tmpW.reshape(32, 32, 3, 10)
    tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
    for i in range(numClasses):
        plt.subplot(2, 5, i + 1)
        plt.title(classesName[i])
        wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax -
                                                                  tmpWMin)
        plt.imshow(wPlot.astype('uint8'))
    plt.savefig(baseDir + 'svm1.png')
    plt.clf()
示例#10
0
def main():
    # files names
    x_train_name = sys.argv[1]
    y_train_name = sys.argv[2]
    x_test_name = sys.argv[3]
    # y_test_name = "test_y.txt" # todo

    # read data
    train_x = read_points(x_train_name)
    train_y = read_labels(y_train_name)
    test_x = read_points(x_test_name)
    # test_y = read_labels(y_test_name) # todo

    try:
        # try to normalize according mean and std
        mean, std = get_mean_std(train_x)
        train_x = apply_mean_std_bias(train_x, mean, std)
        test_x = apply_mean_std_bias(test_x, mean, std)

        # try to normalize according min and max
        # min, max = get_min_max(train_x)
        # train_x = apply_min_max_bias(train_x, min, max)
        # test_x = apply_min_max_bias(test_x, min, max)
    except:
        nothing = "nothing"

    # prepare set
    train_set = list(zip(train_x, train_y))
    # test_set = list(zip(test_x, test_y)) # todo

    # choose better (probably) trained
    max_accs, best_algos = [0, 0, 0], [None, None, None]
    num_tries = 10
    for i in range(num_tries):
        try:
            algos = [
                Perceptron(list(train_set), list(test_x), 5, 0.01),
                Svm(list(train_set), list(test_x), 10, 0.01, 0.001),
                Pa(list(train_set), list(test_x), 10)
            ]

            accs, vaccs = [], []
            for algo in algos:
                algo.train()
                accs.append(algo.validate(train_set))
                # vaccs.append(algo.k_cross_validation()) # todo
            # print(accs)
            # print(vaccs)
            # print("\n")

            # take algo of better acc
            for i in range(len(algos)):
                if accs[i] > max_accs[i]:
                    best_algos[i], max_accs[i] = algos[i], accs[i]
        except:
            nothing = "nothing"

    # print([best_algos[0].validate(train_set),
    #        best_algos[1].validate(train_set),
    #        best_algos[2].validate(train_set)])
    # print([best_algos[0].validate(test_set),
    #        best_algos[1].validate(test_set),
    #        best_algos[2].validate(test_set)])

    for best_alg in best_algos:
        best_alg.predict_all()

    printing_format(best_algos[0].test_y, best_algos[1].test_y,
                    best_algos[2].test_y)
示例#11
0
xTest = np.hstack([xTest, np.ones((xTest.shape[0], 1))])

print('Train image shape after add bias column:   {0}'.format(xTrain.shape))
print('Val image shape after add bias column:     {0}'.format(xVal.shape))
print('Test image shape after add bias column:    {0}'.format(xTest.shape))
print(
    '\n##############################################################################################'
)

######################################################################################################
#                                       SVM CLASSIFIER                                               #
######################################################################################################
from svm import Svm
numClasses = np.max(yTrain) + 1
print('Start training Svm classifier')
classifier = Svm(xTrain.shape[1], numClasses)

# Show weight for each class before training
if classifier.W is not None:
    tmpW = classifier.W[:-1, :]
    tmpW = tmpW.reshape(32, 32, 3, 10)
    tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
    for i in range(numClasses):
        plt.subplot(2, 5, i + 1)
        plt.title(classesName[i])
        wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax -
                                                                  tmpWMin)
        plt.imshow(wPlot.astype('uint8'))
    plt.savefig(baseDir + 'svm1.png')
    plt.clf()
示例#12
0
def builder(c, vectors, classes):
    return Svm(c, vectors, classes)
示例#13
0
xTrain = np.hstack([xTrain, np.ones((xTrain.shape[0], 1))])
xVal = np.hstack([xVal, np.ones((xVal.shape[0], 1))])
xTest = np.hstack([xTest, np.ones((xTest.shape[0], 1))])

print ('Train image shape after add bias column:   {0}'.format(xTrain.shape))
print ('Val image shape after add bias column:     {0}'.format(xVal.shape))
print ('Test image shape after add bias column:    {0}'.format(xTest.shape))
print ('\n##############################################################################################')

######################################################################################################
#                                       SVM CLASSIFIER                                               #
######################################################################################################
from svm import Svm
numClasses = np.max(yTrain) + 1
print ('Start training Svm classifier')
classifier = Svm(xTrain.shape[1], numClasses)

# Show weight for each class before training
if classifier.W is not None:
    tmpW = classifier.W[:-1, :]
    tmpW = tmpW.reshape(32, 32, 3, 10)
    tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
    for i in range(numClasses):
        plt.subplot(2, 5, i+1)
        plt.title(classesName[i])
        wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax - tmpWMin)
        plt.imshow(wPlot.astype('uint8'))
    plt.savefig(baseDir+'svm1.png')
    plt.clf()

# Training classifier
示例#14
0
print('Train image shape after add bias column:   {0}'.format(xTrain.shape))
print('Val image shape after add bias column:     {0}'.format(xVal.shape))
print('Test image shape after add bias column:    {0}'.format(xTest.shape))
print(
    '\n##############################################################################################'
)

######################################################################################################
#                                       SVM CLASSIFIER                                               #
######################################################################################################
from svm import Svm

numClasses = np.max(yTrain) + 1
print('Start training Svm classifier')
classifier = Svm(xTrain.shape[1], numClasses)

# Show weight for each class before training
if classifier.W is not None:
    tmpW = classifier.W[:-1, :]
    tmpW = tmpW.reshape(32, 32, 3, 10)
    tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
    for i in range(numClasses):
        plt.subplot(2, 5, i + 1)
        plt.title(classesName[i])
        wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax -
                                                                  tmpWMin)
        plt.imshow(wPlot.astype('uint8'))
    plt.savefig(baseDir + 'svm1.png')
    plt.clf()
示例#15
0
            # update weights and biases
            optimizer.update_params(L1)
            optimizer.update_params(L2)
        print('predictions: ')
        print(predictions)
        confusion_nn = confusion_matrix(Y, predictions)
        fig2 = plt.figure()
        df_cm = pd.DataFrame(confusion_nn, range(num_classes),
                             range(num_classes))
        # plt.figure(figsize=(10,7))
        sn.set(font_scale=1.4)  # for label size
        sn.heatmap(df_cm, annot=True, annot_kws={"size": 16})  # font size

    if model_selector[2]:
        SVM = Svm(X, Y, 0.5, 50)
        SVM.fit(num_classes)
        SVM.ecoc_predict(X)
        predictions = np.array(SVM.ecoc_predict)
        print(predictions)
        print(Y)
        print("acc: %f" % np.mean(SVM.ecoc_predict == Y))
        confusion_svm = confusion_matrix(Y, predictions)
        fig3 = plt.figure()
        df_cm = pd.DataFrame(confusion_svm, range(num_classes),
                             range(num_classes))
        # plt.figure(figsize=(10,7))
        sn.set(font_scale=1.4)  # for label size
        sn.heatmap(df_cm, annot=True, annot_kws={"size": 16})  # font size

    plt.show()