Exemple #1
0
    n = x.shape[0]
    x = x.reshape(n, -1)
    trainx, testx, trainy, testy = train_test_split(x, y, test_size=0.32, random_state=32)
    train_ind, test_ind = train_test_split(range(n), range(n), test_size=0.32, random_state=32)[:2]
    #   assert ((trainx==x[train_ind]).all())
    #   assert ((testx==x[test_ind]).all())

    duration_model = LMNN_duration.load("./BIT/models/[K=300][rs=32]duration.model")
    duration_trainx, duration_trainy = expand(trainx, trainy, K, G)
    duration_testx, duration_testy = expand(testx, testy, K, G)
    duration_trainx = duration_model.transform(duration_trainx)
    duration_testx = duration_model.transform(duration_testx)
    duration_knn = KNN(n_neighbor).fit(duration_trainx, duration_trainy)
    print duration_trainx.shape
    print duration_trainy.shape
    prediction = duration_knn.predict(duration_testx)
    print prediction.tolist()

    mlmnn = MLMNN.load("./BIT/models/[K=300][rs=32]mlmnn.model")
    mlmnn_knns = []
    trunc = lambda x: x.reshape(x.shape[0], G, -1)[:, :g, :].reshape(x.shape[0], -1)
    for g in range(1, G + 1):
        mlmnn_knn = KNN(mlmnn.K).fit(mlmnn.transform(trunc(trainx), g), trainy)
        mlmnn_knns.append(mlmnn_knn)

    for g in range(1, G + 1):
        #       print '='*10
        #       acc = 0.
        #       for count, ind in enumerate(test_ind):
        #           ttest = mlmnn.transform(trunc(x[ind:ind+1]), g)
        #           if mlmnn_knns[g-1].predict(ttest)[0]==y[ind]:
Exemple #2
0
#    x = normalize(x)
    print sum(pca.explained_variance_ratio_)

    trainx, testx, trainy, testy = train_test_split(x, y, test_size=0.10) 
    print trainx.shape
    print trainy.shape
    print len(set(trainy))

    from sklearn.svm import SVC 
#    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.metrics import classification_report 
#    svm = OneVsRestClassifier(SVC(C=0.11)) # 0.90
    svm = SVC(C=100.11) # 0.90
    svm.fit(trainx, trainy)
    print classification_report(testy, svm.predict(testx))

    import sys; sys.path.append('/home/shaofan/Projects') 
    from FastML import NCA, KNN, MCML
    knn = KNN(n_neighbors=8)
    knn.fit(trainx, trainy)
    print classification_report(testy, knn.predict(testx))

#   nca = NCA()
#   for i in range(100):
#       trainx = nca.fit(trainx, trainy, max_iter=5, lr=5e-3).transform(trainx)
#       testx = nca.transform(testx)
#      
#       print classification_report(testy, knn.predict(testx))
#       sys.stdout.flush()