-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (135 loc) · 4.64 KB
/
main.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import glob
import numpy as np
import math
from numba import jit
#global
_feature = 196
_max = 180
_outputfolder = "out"
_maharanobisuvalue = 1701
_hiragana = ["あ","い","う","え","お","か","き","く","け","こ","さ","し","す","せ","そ","た","ち","つ","て","と","な","に","ぬ","ね","の","は","ひ","ふ","へ","ほ","ま","み","む","め","も","や","ゆ","よ","ら","り","る","れ","ろ","わ","を","ん"]
###ファルダを作成
def makeFolder():
try:
print("/" + _outputfolder + "を生成します")
os.mkdir(_outputfolder)
except:
print("/" + _outputfolder + "はすでに存在します.")
###ファイル読み込み
def openData():
lists = []
files = []
for file in glob.glob('data/*.txt'):
lines = open(file, 'r').readlines()
floatData = [float(d.strip("\n")) for d in lines]
splitData = [a for a in zip(*[iter(floatData)] * _feature)]
lists.append(splitData)
files.append(file)
return (lists,files)
###平均値
def average(data):
list = [0 for i in range(_feature)]
for i in range(0, _feature):
for j in range(0, _feature):
list[j] += data[i][j]
list = [d / _feature for d in list]
return list
###分散共分散行列を計算
def varianceMatrix(data, ave):
matrix = [[0 for i in range(_max)] for j in range(_max)]
for i in range(0, _max):
for j in range(0, _max):
total = 0
for a in range(0, _max):
total += data[a][i] * data[a][j]
matrix[i][j] = (total / _max) - (ave[i] * ave[j])
return matrix
###二次元配列を書き出すメソッド
def writeVector(headStr, file, matrixList):
outfile = file.replace("data/c", _outputfolder+"/"+headStr)
with open(outfile, "w") as fp:
for i in range(0, len(matrixList)):
for j in range(0, len(matrixList[0])):
fp.write(str(matrixList[i][j])+"\n")
fp.write("\n")
print("write "+outfile)
###リストを書き出すメソッド
def writeValue(headStr, file, valueList):
outfile = file.replace("data/c", _outputfolder+"/"+headStr)
with open(outfile, "w") as fp:
for i in range(0, len(valueList)):
fp.write(str(valueList[i])+"\n")
print("write "+outfile)
###ヤコビ行列のソート
@jit
def sortJacobi(values, vectors):
mergeMatrix = np.c_[values.T, vectors]
eigenValue, eigenVector = np.hsplit(mergeMatrix[mergeMatrix[:, 0].argsort()], [1])
return (eigenValue[:, 0].tolist(), eigenVector.tolist())
###マハラノビス距離
@jit
def maharanobisu(x, m, e, l):
d = 0
x2 = np.array(x)
for n in range(180):
a = x2-m
aa = np.array(a).T
b = np.dot(aa, e[n])
d += (b*b)/(l[n] + _maharanobisuvalue)
return d
###認識
# @jit
def recognize(dataList, aveList, eigenValues, eigenVectors):
probability = [0] * 46
for strNum, data in enumerate(dataList):
p = 0
for i in range(179, 199):
dmin = maharanobisu(data[i], aveList[0], eigenVectors[0], eigenValues[0])
nummin = 0
for j in range(46):
d = maharanobisu(data[i], aveList[j], eigenVectors[j], eigenValues[j])
if dmin > d:
nummin = j
dmin = d
if nummin == strNum:
p += 1
print(_hiragana[strNum]+"の認識率 : "+str(p/20))
probability.append(p / 20)
return sum(probability)/46
def main():
#フォルダの作成
makeFolder()
#ファイル読み込み
dataList,files = openData()
#初期化
eigenValues = []
eigenVectors = []
aveList = []
print("c+番号.txt: 分散・共分散行列")
print("v+番号.txt: 固有ベクトル")
print("d+番号.txt: 固有値")
#計算
for file, data in zip(files, dataList):
###平均
ave = np.array(average(data))
###分散共分散行列
matrix = np.cov(data, rowvar=0, bias=1)
# matrix = varianceMatrix(data, ave)
###固有値・固有ベクトル
va, ve = np.linalg.eig(matrix)
eigenValue, eigenVector = sortJacobi(va, ve)
# eigenValue, eigenVector = jacobi(matrix)
###append
eigenValues.append(eigenValue)
eigenVectors.append(eigenVector)
aveList.append(ave)
###output
writeVector("c", file, matrix)
writeVector("v", file, eigenVector)
writeValue("d", file, eigenValue)
#認識率
per = recognize(dataList, aveList, eigenValues, eigenVectors)
print("全体の認識率 : " + str(per))
if __name__ == '__main__':
main()