Exemplo n.º 1
0
def oobTest(X, y, oobs, forest):
    """
    use the out of bags to test the randorm forest classifier;
    @X: NxD numpyarray, the data whose each row is an example;
    @y: 1D numpyarray, labels;
    @oobs: list, a 2D list to store the list of oob;
    @forest: list, store the trees
    """
    
    # for binary classification
    if len(np.unique(y)) == 2:
        accuracy  = 0
        precision = 0
        recall    = 0
        Fmeasure  = 0
    
        for i in range(len(forest)):
            X_i = X[oobs[i]]
            y_i = y[oobs[i]]
            y_pred = predict(X_i, forest)
            accuracy += pm.accuracy(y_i, y_pred)
            recall += pm.recall(y_i, y_pred)
            precision += pm.precision(y_i, y_pred)
            Fmeasure += pm.F_measure(y_i, y_pred)

        return (accuracy/len(forest), precision/len(forest), 
                recall/len(forest), Fmeasure/len(forest))
    
    else:
        accuracy  = 0
        for i in range(len(forest)):
            X_i = X[oobs[i]]
            y_i = y[oobs[i]]
            y_pred = predict(X_i, forest)
            accuracy += pm.accuracy(y_i, y_pred)
            
        return accuracy/len(forest)
Exemplo n.º 2
0
    def score(self, X, y):
        """
        check the performance of the model;
        @X: NxD numpyarray, the data whose each row is an example;
        @y: 1D numpyarray, labels;
        """
        y_pred, _  = self.predict(X)
        y_pred[y_pred == -1] = 0
        
        accuracy  = pm.accuracy(y, y_pred)
        precision = pm.precision(y, y_pred)
        recall    = pm.recall(y, y_pred)
        Fmeasure  = pm.F_measure(y, y_pred)

        return (accuracy, precision, recall, Fmeasure)
Exemplo n.º 3
0
 F_measure_hist = np.zeros(K)
 
 for k in range(K):
     accuracy = 0
     precision = 0
     recall = 0
     F_measure = 0  
     for train, val in kfold.split(data):
         X_train = data[train, :-1]
         y_train = data[train, -1]
         X_val = data[val, :-1]
         y_val = data[val, -1]
         knn = KNearestNeighbors()
         knn.train(X_train, y_train)
         y_pred = knn.predict(X_val, k+1, "l2")
         accuracy += pm.accuracy(y_val, y_pred)
         precision += pm.precision(y_val, y_pred)
         recall += pm.recall(y_val, y_pred)
         F_measure += pm.F_measure(y_val, y_pred)
         
     accuracy = accuracy/10
     precision = precision/10
     recall = recall/10
     F_measure = F_measure/10    
     
     accuracy_hist[k] = accuracy
     precision_hist[k] = precision
     recall_hist[k] = recall
     F_measure_hist[k] = F_measure
 
 '''
Exemplo n.º 4
0
        #Forward Propagation
        out, net_in, net_in_bias = forwardprop.forward(num_layers,
                                                       out[0].shape[0],
                                                       net_in_bias, out,
                                                       net_in, theta)

        #Backpropagation
        error, dtheta, theta = backprop.back(num_layers, out[0].shape[0],
                                             error, out, hidden, net_in_bias,
                                             theta, dtheta, alpha, temp_Y)

    out, _, _, _, _, _, _ = initialize.init(X, num_layers, hidden, num_batches)
    out, _, _ = forwardprop.forward(num_layers, num_trainset, net_in_bias, out,
                                    net_in, theta)
    loss[epoch] = performance_metrics.loss(Y, out[num_layers - 1], Y.shape[0])
    #plot.plot_boundary(X, Y_nb, out, num_layers, net_in_bias, net_in, theta, epoch, Z, xx, yy)

out, _, _, _, _, _, _ = initialize.init(X, num_layers, hidden, num_batches)
out, _, _ = forwardprop.forward(num_layers, num_trainset, net_in_bias, out,
                                net_in, theta)
print performance_metrics.confusion_matrix(Y, out[num_layers - 1], num_class)
print "Accuracy: " + str(performance_metrics.accuracy(Y, out[num_layers - 1]))
print "Precision: " + str(
    performance_metrics.precision(Y, out[num_layers - 1], num_class))
print "Recall: " + str(
    performance_metrics.recall(Y, out[num_layers - 1], num_class))
print "Error: " + str(performance_metrics.sum_error(error, num_layers))

#plot.plot_boundary(X, Y_nb, out, num_layers, net_in_bias, net_in, theta, 0)
plot.plot_loss(np.arange(num_epochs), loss)