Esempio n. 1
0
def clf_vote(dataVolume,no_rand_rate=0):
    data = createData.createData(dataVolume)  # 星期(2) 天气(2) 时间(6) 行进方向(4) 离基站方向(6) 前一个BS(6)
    x, y = data[:, :-1], data[:, -1]
    if no_rand_rate != 0 :
        for i in range(len(y)):
            if y[i] % no_rand_rate == 0:
                y[i] = random.randint(1, 6)
    X_train, X_test, y_train, y_test = train_test_split(x , y, test_size = 0.5, random_state = 42)
    svm_clf = Classifiers.svm_estimator(X_train,y_train)
    RF_clf = Classifiers.RandomForest_best_estimator(X_train,y_train)
    DT_clf = Classifiers.DecisionTree_best_estimator(X_train,y_train)
    mlp_clf = Classifiers.MLPClassifier_estimator(X_train,y_train)
    count = 0.0
    time_start = time.time()
    pre_svm = list(svm_clf.predict(X_test))
    pre_RF = list(RF_clf.predict(X_test))
    pre_DT = list(DT_clf.predict(X_test))
    pre_MLP = list(mlp_clf.predict(X_test))
    time_end = time.time()
    time_pre = time_end - time_start
    for i in range(len(pre_svm)):
        vote_dict = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
        vote_dict[pre_svm[i]] += 1
        vote_dict[pre_RF[i]] += 1
        vote_dict[pre_DT[i]] += 1
        vote_dict[pre_MLP[i]] += 1
        vote_result = max(vote_dict, key=vote_dict.get)
        if vote_result == y_test[i]:
            count +=1
    acc = count / len(y_test)
    return time_pre,acc
def dataVolume_runtime(dataVolume, no_rand_rate=2):
    # data = cd.createData(dataVolume)
    data = createData.createData(
        dataVolume)  #星期(2) 天气(2) 时间(6) 行进方向(4) 离基站方向(6) 前一个BS(6)
    # data = data_normalized.createData(dataVolume)
    x, y = data[:, :-1], data[:, -1]
    for i in range(len(y)):
        if y[i] % no_rand_rate == 0:
            y[i] = random.randint(1, 6)
    print('data set done!')

    #DecisionTree:
    bestTree = clf.DecisionTree_best_estimator(x, y)
    DTScore = cross_val_score(bestTree, x, y)

    #k-Neighbors:
    k_best = clf.k_neighbors_best_estimator(x, y)
    k_Neighbors_score = cross_val_score(k_best, x, y)

    #randomforest:
    rf_best = clf.RandomForest_best_estimator(x, y)
    rf_score = cross_val_score(rf_best, x, y)

    #svm:
    svm_model = clf.svm_estimator(x, y)
    svm_score = cross_val_score(svm_model, x, y)

    #MLPClassifier
    nn_best = clf.MLPClassifier_estimator(x, y)
    nn_score = cross_val_score(nn_best, x, y, cv=6)

    #vote
    vote_time, vote_acc = vote.clf_vote(dataVolume * 2,
                                        no_rand_rate=no_rand_rate)

    # return DTScore,k_Neighbors_score,rf_score,svm_score,dttime,knntime,rftime,svmtime
    return float(sum(DTScore)) / len(DTScore),float(sum(k_Neighbors_score)) / len(k_Neighbors_score),\
           float(sum(rf_score)) / len(rf_score),float(sum(svm_score)) / len(svm_score), \
           float(sum(nn_score)) / len(nn_score),vote_acc,vote_time