def useLRtoPredictScore(targetFileName, exam_mark=None, needNorm=True):
    if exam_mark is None:
        exam_mark = DATA_MARK

    _file_Relative_Path = os.path.join(exam_mark, targetFileName)
    student_data, headerArray = load_data_from_file(_file_Relative_Path)

    _score_map = get_final_score_map()
    _score_array = []
    for _student_record in student_data:
        _score_array.append(_score_map[_student_record[0]])

    targetFeatureIndexArray = [i for i in range(1, headerArray.__len__())]
    featureMatrix = getSerevalColumn(student_data, targetFeatureIndexArray)

    if needNorm:
        featureMatrix = normizeMatrix(featureMatrix)

    _lr = LinearRegression(fit_intercept=True)
    _lr.fit(featureMatrix, _score_array)
    y_predicted = _lr.predict(featureMatrix)
    # y_predicted.astype(int)

    print()
    # print(headerArray);
    # print(_lr.coef_)
    # print(_lr.intercept_)

    print(getprecisionWithTorlerate(y_predicted, _score_array, 0.5),
          getprecisionWithTorlerate(y_predicted, _score_array, 1.5),
          getprecisionWithTorlerate(y_predicted, _score_array, 2.5),
          spearmanr(y_predicted, _score_array),
          r2_score(_score_array, y_predicted))
def usLRtoPredict():
    examId_List = ["e1", "e2", "e3", "e4"]
    target_List = ["programStateRecord"]

    print('|数据名称|预测|r^2|spearman|')
    print('|-|-|-|')

    for eid in examId_List:
        for target in target_List:
            dataFileName = eid + "-" + target
            _data_matrix, _data_header, _score_array = getData("npsm//" +
                                                               dataFileName)

            _feature_matrix = getSerevalColumn(
                _data_matrix, [i for i in range(1, _data_header.__len__())])
            # _feature_matrix = getSerevalColumn(_data_matrix,[1,8]);
            # _feature_matrix = getSerevalColumn(_data_matrix,[1,4,8]);
            # _feature_matrix = getSerevalColumn(_data_matrix,[1,2,3,9]);
            _score_array = np.array(_score_array).reshape(
                _score_array.__len__(), 1)

            _lr = LinearRegression()
            _lr.fit(_feature_matrix, _score_array)
            y_predicted = _lr.predict(_feature_matrix)

            print("|", dataFileName, "|",
                  getprecisionWithTorlerate(_score_array, y_predicted,
                                            0.5), "|",
                  getprecisionWithTorlerate(_score_array, y_predicted,
                                            1.5), "|",
                  getprecisionWithTorlerate(_score_array, y_predicted, 2.5),
                  "|", r2_score(_score_array, y_predicted), "|",
                  spearmanr(_score_array, y_predicted), "|",
                  getprecisionWithTorlerate(_score_array, y_predicted, 5))
Exemplo n.º 3
0
def usLRtoPredictWithExpDef():
    examId_List = ["e1", "e2", "e3", "e4"]
    algorithm_List = ["EQ", "Watwin"]
    target_List = ["finalscore"]

    scoreMap = getGradeMap()
    print('|数据名称|预测准确率|')
    print('|-|-|')

    for al in algorithm_List:
        for eid in examId_List:
            for target in target_List:
                dataFileName = eid + "-" + al + "-" + target
                student_data = getStudentData(al + "//" + dataFileName)
                # scoreArray = getOneColumn(student_data, 2);
                scoreArray = []
                final_score_map = get_final_score_map()
                scoreArray = getOneColumn(student_data, 2)
                for index in range(scoreArray.__len__()):
                    scoreArray[index] = scoreMap[scoreArray[index]]

                scoreArray = np.array(scoreArray).reshape(
                    scoreArray.__len__(), 1)

                watwinArray = getOneColumn(student_data, 1)
                watwinArray = np.array(watwinArray).reshape(
                    watwinArray.__len__(), 1)

                _lr = LinearRegression(fit_intercept=True)
                _lr.fit(watwinArray, scoreArray)
                y_predicted = _lr.predict(watwinArray)

                print("|", dataFileName, "|",
                      getprecisionWithTorlerate(y_predicted, scoreArray, 0.5),
                      "|")
Exemplo n.º 4
0
def usLRtoPredict():
    examId_List = ["e1", "e2", "e3", "e4"]
    algorithm_List = ["EQ", "Watwin"]
    target_List = ["finalscore"]

    print('|数据名称|预测|5分|10分|')
    print('|-|-|-|')

    for al in algorithm_List:
        for eid in examId_List:
            for target in target_List:
                dataFileName = eid + "-" + al + "-" + target
                student_data = getStudentData(al + "//" + dataFileName)
                # scoreArray = getOneColumn(student_data, 2);
                scoreArray = []
                final_score_map = get_final_score_map()
                for _line in student_data:
                    scoreArray.append(final_score_map[str(_line[0])])

                scoreArray = np.array(scoreArray).reshape(
                    scoreArray.__len__(), 1)

                watwinArray = getOneColumn(student_data, 1)
                watwinArray = np.array(watwinArray).reshape(
                    watwinArray.__len__(), 1)
                _lr = LinearRegression(fit_intercept=True)
                _lr.fit(watwinArray, scoreArray)
                y_predicted = _lr.predict(watwinArray)
                print("|", dataFileName, "|",
                      getprecisionWithTorlerate(y_predicted, scoreArray,
                                                0.5), "|",
                      getprecisionWithTorlerate(y_predicted, scoreArray,
                                                1.5), "|",
                      getprecisionWithTorlerate(y_predicted, scoreArray, 2.5),
                      "|", r2_score(scoreArray, y_predicted), "|",
                      spearmanr(y_predicted, scoreArray))
def lr_precision(_lr, x_test, y_test):
    y_predict = _lr.predict(x_test)
    return getprecisionWithTorlerate(y_test, y_predict, 0.5)
Exemplo n.º 6
0
def getScore(_svr, x_test, y_test):
    y_predict = _svr.predict(x_test)
    return getprecisionWithTorlerate(y_test, y_predict, 0.5)
Exemplo n.º 7
0
def usLRtoPredictWithKFold():
    examId_List = ["e1", "e2", "e3", "e4"]
    algorithm_List = ["EQ", "Watwin"]
    target_List = ["finalscore"]

    print('|数据名称|预测|5分|10分|')
    print('|-|-|-|')

    for al in algorithm_List:
        for eid in examId_List:
            for target in target_List:
                dataFileName = eid + "-" + al + "-" + target
                student_data = getStudentData(al + "//" + dataFileName)
                # scoreArray = getOneColumn(student_data, 2);
                scoreArray = []
                final_score_map = get_final_score_map()
                for _line in student_data:
                    scoreArray.append(final_score_map[str(_line[0])])

                scoreArray = np.array(scoreArray).reshape(
                    scoreArray.__len__(), 1)

                watwinArray = getOneColumn(student_data, 1)
                watwinArray = np.array(watwinArray).reshape(
                    watwinArray.__len__(), 1)

                kf = KFold(n_splits=10, shuffle=True)
                accurate_array = []
                within_5_array = []
                r_2_array = []
                within_10_array = []

                for train_index_array, test_index_array in kf.split(
                        watwinArray):
                    X_train = []
                    X_test = []
                    y_train = []
                    y_test = []
                    for train_index in train_index_array:
                        X_train.append(watwinArray[train_index])
                        y_train.append(scoreArray[train_index])

                    for test_index in test_index_array:
                        X_test.append(watwinArray[test_index])
                        y_test.append(scoreArray[test_index])

                    _lr = LinearRegression(fit_intercept=True)
                    _lr.fit(X_train, y_train)
                    y_predicted = _lr.predict(X_test)

                    accurate_array.append(
                        getprecisionWithTorlerate(y_predicted, y_test, 0.5))
                    within_5_array.append(
                        getprecisionWithTorlerate(y_test, y_predicted, 1.5))
                    within_10_array.append(
                        getprecisionWithTorlerate(y_test, y_predicted, 2.5))
                    r_2_array.append(r2_score(y_test, y_predicted))

                print("|", dataFileName, "|",
                      np.array(accurate_array).mean(), "|",
                      np.array(within_5_array).mean(), "|",
                      np.array(within_10_array).mean(), "|",
                      np.array(r_2_array).mean())