Ejemplo n.º 1
0
def main():
    data = datasets.load_digits()
    X = data.data
    y = data.target

    digit1 = 1
    digit2 = 8
    idx = np.append(np.where(y == digit1)[0], np.where(y == digit2)[0])
    y = data.target[idx]
    # Change labels to {-1, 1}
    y[y == digit1] = -1
    y[y == digit2] = 1
    X = data.data[idx]

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)

    # Adaboost classification with 5 weak classifiers
    clf = Adaboost(n_clf=5)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)
    print ("Accuracy:", accuracy)

    # Reduce dimensions to 2d using pca and plot the results
    Plot().plot_in_2d(X_test, y_pred, title="Adaboost", accuracy=accuracy)
Ejemplo n.º 2
0
def runWorker():

    # import DistML.DistML as worker
    if not ps.isWorker():
        return

    from model.logistic_regression import LogisticRegression

    # TODO split data for diff worker

    data = datasets.load_iris()
    X = normalize(data.data[data.target != 0])
    y = data.target[data.target != 0]
    y[y == 1] = 0
    y[y == 2] = 1

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.33,
                                                        seed=1)

    clf = LogisticRegression(gradient_descent=True)

    clf.fit(X_train, y_train)

    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)
Ejemplo n.º 3
0
def main():
    #load data
    data = datasets.load_iris()
    X = normalize(data.data[data.target != 0])
    y = data.target[data.target != 0]
    y[y == 1] = 0
    y[y == 2] = 1
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        seed=1)

    clf = LogisticRegression(gradient_descent=True)

    clf.fit(X_train, y_train)

    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy: ", accuracy)

    #Redurce dimense to two using Pca and plot the result
    """PCA降维,将结果画出"""
    Plot.plot_in_2d(X_train,
                    y_pred,
                    title="LogisticRegression",
                    accuracy=accuracy)
Ejemplo n.º 4
0
def main():
    data = datasets.load_digits()
    x = normalize(data.data)
    y = data.target

    X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

    clf = NaiveBayes()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy :", accuracy)
Ejemplo n.º 5
0
def main():
    data = datasets.load_iris()
    X = normalize(data.data)
    y = data.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

    clf = KNN(k=5)
    y_pred = clf.predict(X_test, X_train, y_train)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy: ", accuracy)
    # Reduce dimensions to 2d using pca and plot the results
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="K Nearest Neighbors",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
Ejemplo n.º 6
0
def main():
    data  = datasets.load_iris()
    X = normalize(data.data[data.target != 0])
    y = data.target[data.target != 0]
    y[y==1] = -1
    y[y==2] = 1

    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.33)

    clf = SuperVectorMachine(kernel=ploynomial_kernal,power=4,coef=1)

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

    accuracy = accuracy_score(y_test,y_pred)

    print("Accuracy :" ,accuracy)
    print(X_test.shape)
def main():

    print("-- Gradient Boosting Classification --")

    data = datasets.load_iris()
    X = data.data
    y = data.target

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

    clf = GradientBoostingClassifier()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy:", accuracy)

    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Gradient Boosting",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
 def acc(self, y, p):
     return accuracy_score(np.argmax(y, axis=1), np.argmax(p, axis=1))