-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHyperFunctions.py
166 lines (158 loc) · 5.65 KB
/
HyperFunctions.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
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
"""
@author: Sonic
"""
import scipy.io as sio
import numpy as np
import matplotlib.pyplot as plt
def featureNormalize(X,type):
if type==1:
mu = np.mean(X,0)
X_norm = X-mu
sigma = np.std(X_norm,0)
X_norm = X_norm/sigma
return X_norm
elif type==2:
minX = np.min(X,0)
maxX = np.max(X,0)
X_norm = X-minX
X_norm = X_norm/(maxX-minX)
elif type==3:
maxX = np.max(X,0)
X_norm = 2*X-maxX
X_norm = X_norm/maxX
return X_norm
# def DrawResult(labels,imageID):
# #ID=1:MUUFL
# #ID=2:Indian Pines
# # ID=3: pavia
# #ID=7:Houston
# #ID=8:Houston 18
# global palette
# global row
# global col
# num_class = int(labels.max())
# if imageID == 1:
# row = 326
# col = 220
# palette = np.array([[255,0,0],
# [0,255,0],
# [0,0,255],
# [255,255,0],
# [0,255,255],
# [255,0,255],
# [176,48,96],
# [46,139,87],
# [160,32,240],
# [255,127,80],
# [127,255,212]])
# palette = palette*1.0/255
# elif imageID == 2:
# row = 145
# col = 145
# palette = np.array([[255,0,0],
# [0,255,0],
# [0,0,255],
# [255,255,0],
# [0,255,255],
# [255,0,255],
# [176,48,96],
# [46,139,87],
# [160,32,240],
# [255,127,80],
# [127,255,212],
# [218,112,214],
# [160,82,45],
# [127,255,0],
# [216,191,216],
# [238,0,0]])
# palette = palette*1.0/255
# elif imageID == 3:
# row = 610
# col = 340
# palette = np.array([[255,0,0],
# [0,255,0],
# [0,0,255],
# [255,255,0],
# [0,255,255],
# [255,0,255],
# [176,48,96],
# [46,139,87],
# [160,32,240]])
# palette = palette*1.0/255
# elif imageID == 4:
# row = 166
# col = 600
# palette = np.array([[255,0,0],
# [0,255,0],
# [0,0,255],
# [255,255,0],
# [0,255,255],
# [255,0,255]])
# palette = palette*1.0/255
# elif imageID == 7:
# row = 349
# col = 1905
# palette = np.array([[0, 205, 0],
# [127, 255, 0],
# [46, 139, 87],
# [0, 139, 0],
# [160, 82, 45],
# [0, 255, 255],
# [255, 255, 255],
# [216, 191, 216],
# [255, 0, 0],
# [139, 0, 0],
# [0, 0, 0],
# [255, 255, 0],
# [238, 154, 0],
# [85, 26, 139],
# [255, 127, 80]])
# palette = palette*1.0/255
# elif imageID == 8:
# row = 601
# col = 1192
# palette = np.array([[0, 205, 0],
# [127, 255, 0],
# [46, 139, 87],
# [0, 139, 0],
# [160, 82, 45],
# [0, 255, 255],
# [255, 255, 255],
# [216, 191, 216],
# [255, 0, 0],
# [139, 0, 0],
# [0, 0, 0],
# [255, 255, 0],
# [238, 154, 0],
# [85, 26, 139],
# [255, 127, 80],
# [127,127,127],
# [85,85,85],
# [26,26,26],
# [46,46,46],
# [210,210,210]])
# palette = palette*1.0/255
# X_result = np.zeros((labels.shape[0],3))
# for i in range(1,num_class+1):
# X_result[np.where(labels==i),0] = palette[i-1,0]
# X_result[np.where(labels==i),1] = palette[i-1,1]
# X_result[np.where(labels==i),2] = palette[i-1,2]
# X_result = np.reshape(X_result,(row,col,3))
# plt.axis ( "off" )
# plt.imshow(X_result)
# return X_result
def CalAccuracy(predict,label):
n = label.shape[0]
OA = np.sum(predict==label)*1.0/n
correct_sum = np.zeros((max(label)+1))
reali = np.zeros((max(label)+1))
predicti = np.zeros((max(label)+1))
producerA = np.zeros((max(label)+1))
for i in range(0,max(label)+1):
correct_sum[i] = np.sum(label[np.where(predict==i)]==i)
reali[i] = np.sum(label==i)
predicti[i] = np.sum(predict==i)
producerA[i] = correct_sum[i] / reali[i]
Kappa = (n*np.sum(correct_sum) - np.sum(reali * predicti)) *1.0/ (n*n - np.sum(reali * predicti))
return OA,Kappa,producerA