#!user/bin/env python3
# -*- coding: utf-8 -*-

'''
Created on 20170518
@author: JohnHuiWB
'''

import sys
sys.path.append("../")
from SynXNLP.classify.Logistic import Logistic

def load_data_set():
    data = [
     [-0.017612, -14.053064, 0.0],
     [-1.395634, -4.662541, 1.0],
     [-0.752157, -6.53862, 0.0],
     [-1.322371, -7.152853, 0.0],
     [0.423363, -11.054677, 0.0],
     [0.406704, -7.067335, 1.0],
     [0.667394, -12.741452, 0.0],
     [-2.46015, -6.866805, 1.0],
     [0.569411, -9.548755, 0.0],
     [-0.026632, -10.427743, 0.0],
     [0.850433, -6.920334, 1.0],
     [1.347183, -13.1755, 0.0],
     [1.176813, -3.16702, 1.0],
     [-1.781871, -9.097953, 0.0],
     [-0.566606, -5.749003, 1.0],
     [0.931635, -1.589505, 1.0],
     [-0.024205, -6.151823, 1.0],
     [-0.036453, -2.690988, 1.0],
     [-0.196949, -0.444165, 1.0],
     [1.014459, -5.754399, 1.0],
     [1.985298, -3.230619, 1.0],
     [-1.693453, 0.55754, 1.0],
     [-0.576525, -11.778922, 0.0],
     [-0.346811, 1.67873, 1.0],
     [-2.124484, -2.672471, 1.0],
     [1.217916, -9.597015, 0.0],
     [-0.733928, -9.098687, 0.0],
     [-3.642001, 1.618087, 1.0],
     [0.315985, -3.523953, 1.0],
     [1.416614, -9.619232, 0.0],
     [-0.386323, -3.989286, 1.0],
     [0.556921, -8.294984, 1.0],
     [1.224863, -11.58736, 0.0],
     [-1.347803, 2.406051, 1.0],
     [1.196604, -4.951851, 1.0],
     [0.275221, -9.543647, 0.0],
     [0.470575, -9.332488, 0.0],
     [-1.889567, -9.542662, 0.0],
     [-1.527893, -12.150579, 0.0],
     [-1.185247, -11.309318, 0.0],
     [-0.445678, -3.297303, 1.0],
     [1.042222, -6.105155, 1.0],
     [-0.618787, -10.320986, 0.0],
     [1.152083, -0.548467, 1.0],
     [0.828534, -2.676045, 1.0],
     [-1.237728, -10.549033, 0.0],
     [-0.683565, 2.166125, 1.0],
     [0.229456, -5.921938, 1.0],
     [-0.959885, -11.555336, 0.0],
     [0.492911, -10.993324, 0.0],
     [0.184992, -8.721488, 0.0],
     [-0.355715, -10.325976, 0.0],
     [-0.397822, -8.058397, 0.0],
     [0.824839, -13.730343, 0.0],
     [1.507278, -5.027866, 1.0],
     [0.099671, -6.835839, 1.0],
     [-0.344008, -10.717485, 0.0],
     [1.785928, -7.718645, 1.0],
     [-0.918801, -11.560217, 0.0],
     [-0.364009, -4.7473, 1.0],
     [-0.841722, -4.119083, 1.0],
     [0.490426, -1.960539, 1.0],
     [-0.007194, -9.075792, 0.0],
     [0.356107, -12.447863, 0.0],
     [0.342578, -12.281162, 0.0],
     [-0.810823, 1.466018, 1.0],
     [2.530777, -6.476801, 1.0],
     [1.296683, -11.607559, 0.0],
     [0.475487, -12.040035, 0.0],
     [-0.783277, -11.009725, 0.0],
     [0.074798, -11.02365, 0.0],
     [-1.337472, -0.468339, 1.0],
     [-0.102781, -13.763651, 0.0],
     [-0.147324, -2.874846, 1.0],
     [0.518389, -9.887035, 0.0],
     [1.015399, -7.571882, 0.0],
     [-1.658086, 0.027255, 1.0],
     [1.319944, -2.171228, 1.0],
     [2.056216, -5.019981, 1.0],
     [-0.851633, -4.375691, 1.0],
     [-1.510047, -6.061992, 0.0],
     [-1.076637, 3.181888, 1.0],
     [1.821096, -10.28399, 0.0],
     [3.01015, -8.401766, 1.0],
     [-1.099458, -1.688274, 1.0],
     [-0.834872, 1.733869, 1.0],
     [-0.846637, -3.849075, 1.0],
     [1.400102, -12.628781, 0.0],
     [1.752842, -5.468166, 1.0],
     [0.078557, -0.059736, 1.0],
     [0.089392, 0.7153, 1.0],
     [1.825662, -12.693808, 0.0],
     [0.197445, -9.744638, 0.0],
     [0.126117, -0.922311, 1.0],
     [-0.679797, -1.22053, 1.0],
     [0.677983, -2.556666, 1.0],
     [0.761349, -10.693862, 0.0],
     [-2.168791, -0.143632, 1.0],
     [1.38861, 6.341997, 1.0],
     [0.317029, 10.739025, 1.0]]

    train_data = [[1.0, x1, x2] for x1, x2, label in data]
    labels = [label for x1, x2, label in data]
    return train_data, labels


def main():
    train_data, labels = load_data_set()
    C = Logistic()
    C.train(train_data, labels, iter = 500)
    
    TP = FP = TN = FN = 0
    # TP：真阳性，是相关项目中被正确识别为相关的。
    # TN：真阴性，是不相关项目中被正确识别为不相关的。
    # FP：假阳性，是不相关项目中被错误被识别为相关的。
    # FN：假阴性，是相关项目中被错误识别为不相关的。

    print('='*120)
    for i in range(len(labels)):
        result = C.predict(train_data[i])
        if int(labels[i]) == 1:
            if result == 1:
                #print('NO.%2d被正确识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                TP += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                FN += 1
        else:
            if result == 0:
                #print('NO.%2d被正确识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                TN += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                FP += 1

    print('='*120)
    print('weight:%s' % C.train_result())
    print("TP: %d\tFN: %d\tTN: %d\tFP: %d" % (TP, FN, TN, FP))
    print('Precision:  %f' % (TP/(TP+FP) * 100))
    print('Recall:  %f' % (TP/(TP+FN) * 100))
    print('F-Measure:  %f' % ((2 * TP/(TP+FP) * TP/(TP+FN) / (TP/(TP+FP) + TP/(TP+FN))) * 100))
    print('='*120)

    plot_graph(train_data, labels, C.train_result())

def plot_graph(data, labels, weights):
    import matplotlib.pyplot as plt
    import numpy as np
    data_arr = np.array(data)
    n = np.shape(data_arr)[0]
    xcord0 = []
    ycord0 = []
    xcord1 = []
    ycord1 = []
    for i in range(n):
        if labels[i] == 0:
            xcord0.append(data_arr[i, 1])
            ycord0.append(data_arr[i, 2])
        else:
            xcord1.append(data_arr[i, 1])
            ycord1.append(data_arr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord0, ycord0, s = 30, c = 'red', marker = 's')
    ax.scatter(xcord1, ycord1, s = 30, c = 'green')
    x = np.arange(-4.0, 10.0, 0.1)
    y = (-weights[0]-weights[1]*x)/weights[2]
    ax.plot(x, y)
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()


if __name__ == "__main__":
    main()

ba21bb5dabe4fa7777bdf909bd94eb0f5a2ceb79
<mask token>


def main():
    train_data, labels = load_data_set()
    C = Logistic()
    C.train(train_data, labels, iter=500)
    TP = FP = TN = FN = 0
    print('=' * 120)
    for i in range(len(labels)):
        result = C.predict(train_data[i])
        if int(labels[i]) == 1:
            if result == 1:
                TP += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1,
                    result, train_data[i][1], train_data[i][2]))
                FN += 1
        elif result == 0:
            TN += 1
        else:
            print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1, result,
                train_data[i][1], train_data[i][2]))
            FP += 1
    print('=' * 120)
    print('weight:%s' % C.train_result())
    print('TP: %d\tFN: %d\tTN: %d\tFP: %d' % (TP, FN, TN, FP))
    print('Precision:  %f' % (TP / (TP + FP) * 100))
    print('Recall:  %f' % (TP / (TP + FN) * 100))
    print('F-Measure:  %f' % (2 * TP / (TP + FP) * TP / (TP + FN) / (TP / (
        TP + FP) + TP / (TP + FN)) * 100))
    print('=' * 120)
    plot_graph(train_data, labels, C.train_result())


<mask token>

<mask token>


def main():
    train_data, labels = load_data_set()
    C = Logistic()
    C.train(train_data, labels, iter=500)
    TP = FP = TN = FN = 0
    print('=' * 120)
    for i in range(len(labels)):
        result = C.predict(train_data[i])
        if int(labels[i]) == 1:
            if result == 1:
                TP += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1,
                    result, train_data[i][1], train_data[i][2]))
                FN += 1
        elif result == 0:
            TN += 1
        else:
            print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1, result,
                train_data[i][1], train_data[i][2]))
            FP += 1
    print('=' * 120)
    print('weight:%s' % C.train_result())
    print('TP: %d\tFN: %d\tTN: %d\tFP: %d' % (TP, FN, TN, FP))
    print('Precision:  %f' % (TP / (TP + FP) * 100))
    print('Recall:  %f' % (TP / (TP + FN) * 100))
    print('F-Measure:  %f' % (2 * TP / (TP + FP) * TP / (TP + FN) / (TP / (
        TP + FP) + TP / (TP + FN)) * 100))
    print('=' * 120)
    plot_graph(train_data, labels, C.train_result())


def plot_graph(data, labels, weights):
    import matplotlib.pyplot as plt
    import numpy as np
    data_arr = np.array(data)
    n = np.shape(data_arr)[0]
    xcord0 = []
    ycord0 = []
    xcord1 = []
    ycord1 = []
    for i in range(n):
        if labels[i] == 0:
            xcord0.append(data_arr[i, 1])
            ycord0.append(data_arr[i, 2])
        else:
            xcord1.append(data_arr[i, 1])
            ycord1.append(data_arr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord0, ycord0, s=30, c='red', marker='s')
    ax.scatter(xcord1, ycord1, s=30, c='green')
    x = np.arange(-4.0, 10.0, 0.1)
    y = (-weights[0] - weights[1] * x) / weights[2]
    ax.plot(x, y)
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()


<mask token>

<mask token>


def load_data_set():
    data = [[-0.017612, -14.053064, 0.0], [-1.395634, -4.662541, 1.0], [-
        0.752157, -6.53862, 0.0], [-1.322371, -7.152853, 0.0], [0.423363, -
        11.054677, 0.0], [0.406704, -7.067335, 1.0], [0.667394, -12.741452,
        0.0], [-2.46015, -6.866805, 1.0], [0.569411, -9.548755, 0.0], [-
        0.026632, -10.427743, 0.0], [0.850433, -6.920334, 1.0], [1.347183, 
        -13.1755, 0.0], [1.176813, -3.16702, 1.0], [-1.781871, -9.097953, 
        0.0], [-0.566606, -5.749003, 1.0], [0.931635, -1.589505, 1.0], [-
        0.024205, -6.151823, 1.0], [-0.036453, -2.690988, 1.0], [-0.196949,
        -0.444165, 1.0], [1.014459, -5.754399, 1.0], [1.985298, -3.230619, 
        1.0], [-1.693453, 0.55754, 1.0], [-0.576525, -11.778922, 0.0], [-
        0.346811, 1.67873, 1.0], [-2.124484, -2.672471, 1.0], [1.217916, -
        9.597015, 0.0], [-0.733928, -9.098687, 0.0], [-3.642001, 1.618087, 
        1.0], [0.315985, -3.523953, 1.0], [1.416614, -9.619232, 0.0], [-
        0.386323, -3.989286, 1.0], [0.556921, -8.294984, 1.0], [1.224863, -
        11.58736, 0.0], [-1.347803, 2.406051, 1.0], [1.196604, -4.951851, 
        1.0], [0.275221, -9.543647, 0.0], [0.470575, -9.332488, 0.0], [-
        1.889567, -9.542662, 0.0], [-1.527893, -12.150579, 0.0], [-1.185247,
        -11.309318, 0.0], [-0.445678, -3.297303, 1.0], [1.042222, -6.105155,
        1.0], [-0.618787, -10.320986, 0.0], [1.152083, -0.548467, 1.0], [
        0.828534, -2.676045, 1.0], [-1.237728, -10.549033, 0.0], [-0.683565,
        2.166125, 1.0], [0.229456, -5.921938, 1.0], [-0.959885, -11.555336,
        0.0], [0.492911, -10.993324, 0.0], [0.184992, -8.721488, 0.0], [-
        0.355715, -10.325976, 0.0], [-0.397822, -8.058397, 0.0], [0.824839,
        -13.730343, 0.0], [1.507278, -5.027866, 1.0], [0.099671, -6.835839,
        1.0], [-0.344008, -10.717485, 0.0], [1.785928, -7.718645, 1.0], [-
        0.918801, -11.560217, 0.0], [-0.364009, -4.7473, 1.0], [-0.841722, 
        -4.119083, 1.0], [0.490426, -1.960539, 1.0], [-0.007194, -9.075792,
        0.0], [0.356107, -12.447863, 0.0], [0.342578, -12.281162, 0.0], [-
        0.810823, 1.466018, 1.0], [2.530777, -6.476801, 1.0], [1.296683, -
        11.607559, 0.0], [0.475487, -12.040035, 0.0], [-0.783277, -
        11.009725, 0.0], [0.074798, -11.02365, 0.0], [-1.337472, -0.468339,
        1.0], [-0.102781, -13.763651, 0.0], [-0.147324, -2.874846, 1.0], [
        0.518389, -9.887035, 0.0], [1.015399, -7.571882, 0.0], [-1.658086, 
        0.027255, 1.0], [1.319944, -2.171228, 1.0], [2.056216, -5.019981, 
        1.0], [-0.851633, -4.375691, 1.0], [-1.510047, -6.061992, 0.0], [-
        1.076637, 3.181888, 1.0], [1.821096, -10.28399, 0.0], [3.01015, -
        8.401766, 1.0], [-1.099458, -1.688274, 1.0], [-0.834872, 1.733869, 
        1.0], [-0.846637, -3.849075, 1.0], [1.400102, -12.628781, 0.0], [
        1.752842, -5.468166, 1.0], [0.078557, -0.059736, 1.0], [0.089392, 
        0.7153, 1.0], [1.825662, -12.693808, 0.0], [0.197445, -9.744638, 
        0.0], [0.126117, -0.922311, 1.0], [-0.679797, -1.22053, 1.0], [
        0.677983, -2.556666, 1.0], [0.761349, -10.693862, 0.0], [-2.168791,
        -0.143632, 1.0], [1.38861, 6.341997, 1.0], [0.317029, 10.739025, 1.0]]
    train_data = [[1.0, x1, x2] for x1, x2, label in data]
    labels = [label for x1, x2, label in data]
    return train_data, labels


def main():
    train_data, labels = load_data_set()
    C = Logistic()
    C.train(train_data, labels, iter=500)
    TP = FP = TN = FN = 0
    print('=' * 120)
    for i in range(len(labels)):
        result = C.predict(train_data[i])
        if int(labels[i]) == 1:
            if result == 1:
                TP += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1,
                    result, train_data[i][1], train_data[i][2]))
                FN += 1
        elif result == 0:
            TN += 1
        else:
            print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1, result,
                train_data[i][1], train_data[i][2]))
            FP += 1
    print('=' * 120)
    print('weight:%s' % C.train_result())
    print('TP: %d\tFN: %d\tTN: %d\tFP: %d' % (TP, FN, TN, FP))
    print('Precision:  %f' % (TP / (TP + FP) * 100))
    print('Recall:  %f' % (TP / (TP + FN) * 100))
    print('F-Measure:  %f' % (2 * TP / (TP + FP) * TP / (TP + FN) / (TP / (
        TP + FP) + TP / (TP + FN)) * 100))
    print('=' * 120)
    plot_graph(train_data, labels, C.train_result())


def plot_graph(data, labels, weights):
    import matplotlib.pyplot as plt
    import numpy as np
    data_arr = np.array(data)
    n = np.shape(data_arr)[0]
    xcord0 = []
    ycord0 = []
    xcord1 = []
    ycord1 = []
    for i in range(n):
        if labels[i] == 0:
            xcord0.append(data_arr[i, 1])
            ycord0.append(data_arr[i, 2])
        else:
            xcord1.append(data_arr[i, 1])
            ycord1.append(data_arr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord0, ycord0, s=30, c='red', marker='s')
    ax.scatter(xcord1, ycord1, s=30, c='green')
    x = np.arange(-4.0, 10.0, 0.1)
    y = (-weights[0] - weights[1] * x) / weights[2]
    ax.plot(x, y)
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()


<mask token>

<mask token>
import sys
sys.path.append('../')
from SynXNLP.classify.Logistic import Logistic


def load_data_set():
    data = [[-0.017612, -14.053064, 0.0], [-1.395634, -4.662541, 1.0], [-
        0.752157, -6.53862, 0.0], [-1.322371, -7.152853, 0.0], [0.423363, -
        11.054677, 0.0], [0.406704, -7.067335, 1.0], [0.667394, -12.741452,
        0.0], [-2.46015, -6.866805, 1.0], [0.569411, -9.548755, 0.0], [-
        0.026632, -10.427743, 0.0], [0.850433, -6.920334, 1.0], [1.347183, 
        -13.1755, 0.0], [1.176813, -3.16702, 1.0], [-1.781871, -9.097953, 
        0.0], [-0.566606, -5.749003, 1.0], [0.931635, -1.589505, 1.0], [-
        0.024205, -6.151823, 1.0], [-0.036453, -2.690988, 1.0], [-0.196949,
        -0.444165, 1.0], [1.014459, -5.754399, 1.0], [1.985298, -3.230619, 
        1.0], [-1.693453, 0.55754, 1.0], [-0.576525, -11.778922, 0.0], [-
        0.346811, 1.67873, 1.0], [-2.124484, -2.672471, 1.0], [1.217916, -
        9.597015, 0.0], [-0.733928, -9.098687, 0.0], [-3.642001, 1.618087, 
        1.0], [0.315985, -3.523953, 1.0], [1.416614, -9.619232, 0.0], [-
        0.386323, -3.989286, 1.0], [0.556921, -8.294984, 1.0], [1.224863, -
        11.58736, 0.0], [-1.347803, 2.406051, 1.0], [1.196604, -4.951851, 
        1.0], [0.275221, -9.543647, 0.0], [0.470575, -9.332488, 0.0], [-
        1.889567, -9.542662, 0.0], [-1.527893, -12.150579, 0.0], [-1.185247,
        -11.309318, 0.0], [-0.445678, -3.297303, 1.0], [1.042222, -6.105155,
        1.0], [-0.618787, -10.320986, 0.0], [1.152083, -0.548467, 1.0], [
        0.828534, -2.676045, 1.0], [-1.237728, -10.549033, 0.0], [-0.683565,
        2.166125, 1.0], [0.229456, -5.921938, 1.0], [-0.959885, -11.555336,
        0.0], [0.492911, -10.993324, 0.0], [0.184992, -8.721488, 0.0], [-
        0.355715, -10.325976, 0.0], [-0.397822, -8.058397, 0.0], [0.824839,
        -13.730343, 0.0], [1.507278, -5.027866, 1.0], [0.099671, -6.835839,
        1.0], [-0.344008, -10.717485, 0.0], [1.785928, -7.718645, 1.0], [-
        0.918801, -11.560217, 0.0], [-0.364009, -4.7473, 1.0], [-0.841722, 
        -4.119083, 1.0], [0.490426, -1.960539, 1.0], [-0.007194, -9.075792,
        0.0], [0.356107, -12.447863, 0.0], [0.342578, -12.281162, 0.0], [-
        0.810823, 1.466018, 1.0], [2.530777, -6.476801, 1.0], [1.296683, -
        11.607559, 0.0], [0.475487, -12.040035, 0.0], [-0.783277, -
        11.009725, 0.0], [0.074798, -11.02365, 0.0], [-1.337472, -0.468339,
        1.0], [-0.102781, -13.763651, 0.0], [-0.147324, -2.874846, 1.0], [
        0.518389, -9.887035, 0.0], [1.015399, -7.571882, 0.0], [-1.658086, 
        0.027255, 1.0], [1.319944, -2.171228, 1.0], [2.056216, -5.019981, 
        1.0], [-0.851633, -4.375691, 1.0], [-1.510047, -6.061992, 0.0], [-
        1.076637, 3.181888, 1.0], [1.821096, -10.28399, 0.0], [3.01015, -
        8.401766, 1.0], [-1.099458, -1.688274, 1.0], [-0.834872, 1.733869, 
        1.0], [-0.846637, -3.849075, 1.0], [1.400102, -12.628781, 0.0], [
        1.752842, -5.468166, 1.0], [0.078557, -0.059736, 1.0], [0.089392, 
        0.7153, 1.0], [1.825662, -12.693808, 0.0], [0.197445, -9.744638, 
        0.0], [0.126117, -0.922311, 1.0], [-0.679797, -1.22053, 1.0], [
        0.677983, -2.556666, 1.0], [0.761349, -10.693862, 0.0], [-2.168791,
        -0.143632, 1.0], [1.38861, 6.341997, 1.0], [0.317029, 10.739025, 1.0]]
    train_data = [[1.0, x1, x2] for x1, x2, label in data]
    labels = [label for x1, x2, label in data]
    return train_data, labels


def main():
    train_data, labels = load_data_set()
    C = Logistic()
    C.train(train_data, labels, iter=500)
    TP = FP = TN = FN = 0
    print('=' * 120)
    for i in range(len(labels)):
        result = C.predict(train_data[i])
        if int(labels[i]) == 1:
            if result == 1:
                TP += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1,
                    result, train_data[i][1], train_data[i][2]))
                FN += 1
        elif result == 0:
            TN += 1
        else:
            print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i + 1, result,
                train_data[i][1], train_data[i][2]))
            FP += 1
    print('=' * 120)
    print('weight:%s' % C.train_result())
    print('TP: %d\tFN: %d\tTN: %d\tFP: %d' % (TP, FN, TN, FP))
    print('Precision:  %f' % (TP / (TP + FP) * 100))
    print('Recall:  %f' % (TP / (TP + FN) * 100))
    print('F-Measure:  %f' % (2 * TP / (TP + FP) * TP / (TP + FN) / (TP / (
        TP + FP) + TP / (TP + FN)) * 100))
    print('=' * 120)
    plot_graph(train_data, labels, C.train_result())


def plot_graph(data, labels, weights):
    import matplotlib.pyplot as plt
    import numpy as np
    data_arr = np.array(data)
    n = np.shape(data_arr)[0]
    xcord0 = []
    ycord0 = []
    xcord1 = []
    ycord1 = []
    for i in range(n):
        if labels[i] == 0:
            xcord0.append(data_arr[i, 1])
            ycord0.append(data_arr[i, 2])
        else:
            xcord1.append(data_arr[i, 1])
            ycord1.append(data_arr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord0, ycord0, s=30, c='red', marker='s')
    ax.scatter(xcord1, ycord1, s=30, c='green')
    x = np.arange(-4.0, 10.0, 0.1)
    y = (-weights[0] - weights[1] * x) / weights[2]
    ax.plot(x, y)
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()


if __name__ == '__main__':
    main()

#!user/bin/env python3
# -*- coding: utf-8 -*-

'''
Created on 20170518
@author: JohnHuiWB
'''

import sys
sys.path.append("../")
from SynXNLP.classify.Logistic import Logistic

def load_data_set():
    data = [
     [-0.017612, -14.053064, 0.0],
     [-1.395634, -4.662541, 1.0],
     [-0.752157, -6.53862, 0.0],
     [-1.322371, -7.152853, 0.0],
     [0.423363, -11.054677, 0.0],
     [0.406704, -7.067335, 1.0],
     [0.667394, -12.741452, 0.0],
     [-2.46015, -6.866805, 1.0],
     [0.569411, -9.548755, 0.0],
     [-0.026632, -10.427743, 0.0],
     [0.850433, -6.920334, 1.0],
     [1.347183, -13.1755, 0.0],
     [1.176813, -3.16702, 1.0],
     [-1.781871, -9.097953, 0.0],
     [-0.566606, -5.749003, 1.0],
     [0.931635, -1.589505, 1.0],
     [-0.024205, -6.151823, 1.0],
     [-0.036453, -2.690988, 1.0],
     [-0.196949, -0.444165, 1.0],
     [1.014459, -5.754399, 1.0],
     [1.985298, -3.230619, 1.0],
     [-1.693453, 0.55754, 1.0],
     [-0.576525, -11.778922, 0.0],
     [-0.346811, 1.67873, 1.0],
     [-2.124484, -2.672471, 1.0],
     [1.217916, -9.597015, 0.0],
     [-0.733928, -9.098687, 0.0],
     [-3.642001, 1.618087, 1.0],
     [0.315985, -3.523953, 1.0],
     [1.416614, -9.619232, 0.0],
     [-0.386323, -3.989286, 1.0],
     [0.556921, -8.294984, 1.0],
     [1.224863, -11.58736, 0.0],
     [-1.347803, 2.406051, 1.0],
     [1.196604, -4.951851, 1.0],
     [0.275221, -9.543647, 0.0],
     [0.470575, -9.332488, 0.0],
     [-1.889567, -9.542662, 0.0],
     [-1.527893, -12.150579, 0.0],
     [-1.185247, -11.309318, 0.0],
     [-0.445678, -3.297303, 1.0],
     [1.042222, -6.105155, 1.0],
     [-0.618787, -10.320986, 0.0],
     [1.152083, -0.548467, 1.0],
     [0.828534, -2.676045, 1.0],
     [-1.237728, -10.549033, 0.0],
     [-0.683565, 2.166125, 1.0],
     [0.229456, -5.921938, 1.0],
     [-0.959885, -11.555336, 0.0],
     [0.492911, -10.993324, 0.0],
     [0.184992, -8.721488, 0.0],
     [-0.355715, -10.325976, 0.0],
     [-0.397822, -8.058397, 0.0],
     [0.824839, -13.730343, 0.0],
     [1.507278, -5.027866, 1.0],
     [0.099671, -6.835839, 1.0],
     [-0.344008, -10.717485, 0.0],
     [1.785928, -7.718645, 1.0],
     [-0.918801, -11.560217, 0.0],
     [-0.364009, -4.7473, 1.0],
     [-0.841722, -4.119083, 1.0],
     [0.490426, -1.960539, 1.0],
     [-0.007194, -9.075792, 0.0],
     [0.356107, -12.447863, 0.0],
     [0.342578, -12.281162, 0.0],
     [-0.810823, 1.466018, 1.0],
     [2.530777, -6.476801, 1.0],
     [1.296683, -11.607559, 0.0],
     [0.475487, -12.040035, 0.0],
     [-0.783277, -11.009725, 0.0],
     [0.074798, -11.02365, 0.0],
     [-1.337472, -0.468339, 1.0],
     [-0.102781, -13.763651, 0.0],
     [-0.147324, -2.874846, 1.0],
     [0.518389, -9.887035, 0.0],
     [1.015399, -7.571882, 0.0],
     [-1.658086, 0.027255, 1.0],
     [1.319944, -2.171228, 1.0],
     [2.056216, -5.019981, 1.0],
     [-0.851633, -4.375691, 1.0],
     [-1.510047, -6.061992, 0.0],
     [-1.076637, 3.181888, 1.0],
     [1.821096, -10.28399, 0.0],
     [3.01015, -8.401766, 1.0],
     [-1.099458, -1.688274, 1.0],
     [-0.834872, 1.733869, 1.0],
     [-0.846637, -3.849075, 1.0],
     [1.400102, -12.628781, 0.0],
     [1.752842, -5.468166, 1.0],
     [0.078557, -0.059736, 1.0],
     [0.089392, 0.7153, 1.0],
     [1.825662, -12.693808, 0.0],
     [0.197445, -9.744638, 0.0],
     [0.126117, -0.922311, 1.0],
     [-0.679797, -1.22053, 1.0],
     [0.677983, -2.556666, 1.0],
     [0.761349, -10.693862, 0.0],
     [-2.168791, -0.143632, 1.0],
     [1.38861, 6.341997, 1.0],
     [0.317029, 10.739025, 1.0]]

    train_data = [[1.0, x1, x2] for x1, x2, label in data]
    labels = [label for x1, x2, label in data]
    return train_data, labels


def main():
    train_data, labels = load_data_set()
    C = Logistic()
    C.train(train_data, labels, iter = 500)
    
    TP = FP = TN = FN = 0
    # TP：真阳性，是相关项目中被正确识别为相关的。
    # TN：真阴性，是不相关项目中被正确识别为不相关的。
    # FP：假阳性，是不相关项目中被错误被识别为相关的。
    # FN：假阴性，是相关项目中被错误识别为不相关的。

    print('='*120)
    for i in range(len(labels)):
        result = C.predict(train_data[i])
        if int(labels[i]) == 1:
            if result == 1:
                #print('NO.%2d被正确识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                TP += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                FN += 1
        else:
            if result == 0:
                #print('NO.%2d被正确识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                TN += 1
            else:
                print('NO.%2d被错误识别 label:%d Position:(%s,%s)' % (i+1, result, train_data[i][1], train_data[i][2]))
                FP += 1

    print('='*120)
    print('weight:%s' % C.train_result())
    print("TP: %d\tFN: %d\tTN: %d\tFP: %d" % (TP, FN, TN, FP))
    print('Precision:  %f' % (TP/(TP+FP) * 100))
    print('Recall:  %f' % (TP/(TP+FN) * 100))
    print('F-Measure:  %f' % ((2 * TP/(TP+FP) * TP/(TP+FN) / (TP/(TP+FP) + TP/(TP+FN))) * 100))
    print('='*120)

    plot_graph(train_data, labels, C.train_result())

def plot_graph(data, labels, weights):
    import matplotlib.pyplot as plt
    import numpy as np
    data_arr = np.array(data)
    n = np.shape(data_arr)[0]
    xcord0 = []
    ycord0 = []
    xcord1 = []
    ycord1 = []
    for i in range(n):
        if labels[i] == 0:
            xcord0.append(data_arr[i, 1])
            ycord0.append(data_arr[i, 2])
        else:
            xcord1.append(data_arr[i, 1])
            ycord1.append(data_arr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord0, ycord0, s = 30, c = 'red', marker = 's')
    ax.scatter(xcord1, ycord1, s = 30, c = 'green')
    x = np.arange(-4.0, 10.0, 0.1)
    y = (-weights[0]-weights[1]*x)/weights[2]
    ax.plot(x, y)
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()


if __name__ == "__main__":
    main()
