-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadFileToArray.py
More file actions
86 lines (65 loc) · 2.34 KB
/
readFileToArray.py
File metadata and controls
86 lines (65 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from distutils.text_file import TextFile
from config import *
import matplotlib.pyplot as plt
import numpy as np
import random
def subArrayColumn(data):
size = len(data)
a = [0]*size
b = [0]*size
c = [0]*size
for ix in range(size):
a[ix] = data[ix][0]
b[ix] = data[ix][1]
c[ix] = data[ix][2]
return a,b,c
def getTwoDatasetForPlot(data):
size = len(data)
result_positive = []
result_negative = []
for ix in range(size):
if data[ix][2] == 1:
result_positive.append(data[ix])
else:
result_negative.append(data[ix])
return result_positive, result_negative
def addNoiseDiffRou(data, rou_plus, rou_minus):
arr = data[:]
arr_plus = []
arr_minus = []
for a in arr:
if a[2] == 1:
arr_plus.append(a)
else:
arr_minus.append(a)
noise_index_plus = random.sample(range(0, len(arr_plus)-1), int(rou_plus*len(arr_plus)))
for ix in range(len(noise_index_plus)):
arr_plus[ix][2] = -1
noise_index_minus = random.sample(range(0, len(arr_minus)-1), int(rou_minus*len(arr_minus)))
for ix in range(len(noise_index_minus)):
arr_minus[ix][2] = 1
arr = np.vstack((arr_plus, arr_minus))
return arr
def myPlotScatter(data):
positive_arr, negative_arr = getTwoDatasetForPlot(data)
graph_positive_x1, graph_positive_x2, positive_label = subArrayColumn(positive_arr)
graph_negative_x1, graph_negative_x2, negative_label = subArrayColumn(negative_arr)
plt.scatter(graph_positive_x1,graph_positive_x2, marker='o', c='b', s=10, label='x_2')
plt.scatter(graph_negative_x1,graph_negative_x2, marker='x', c='r', s=10, label='x_1')
plt.title('Dataset')
plt.xlabel('x_1')
plt.ylabel('x_2')
def readTextFile(file_name):
t_file = open(file_name)
lines = t_file.readlines()
result = []
for line in lines:
_temp = []
for _item in line.split():
_temp.append(float(_item))
result.append(_temp)
return result
if __name__ == "__main__":
data = readTextFile('noise_output_data_100_samples_and_rou_0.4.txt')
myPlotScatter(data)
plt.show()