Created
January 11, 2018 14:33
-
-
Save chengluyu/111a3a959b610f2af05e67bf056a32b9 to your computer and use it in GitHub Desktop.
Zhang Ziqi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
def process_number(s): | |
s = s.replace(',', '') | |
if s[-1] == '\n': | |
s = s[:-1] | |
if s[-1] == '%': | |
return float(s[:-1]) * 0.01 | |
return float(s) | |
def read_data(): | |
rows = [] | |
with open('data.txt') as f: | |
for line in f.readlines(): | |
rows.append([process_number(s) for s in line.split('\t')]) | |
return np.array(rows).T | |
if __name__ == '__main__': | |
np.set_printoptions(precision=3) | |
data = read_data() | |
x = data[1:] | |
print('Data', x) | |
mu = (data - np.min(data, axis=0)[np.newaxis, :]) / (np.max(data, axis=0) - np.min(data, axis=0))[np.newaxis, :] | |
mu0 = mu[0] | |
mu = mu[1:] | |
p = x / np.sum(x, axis=0) | |
K = 1 / np.log(x.shape[0]) | |
E = - K * np.sum(p * np.log(p), axis=0) | |
print('Entropy of each index:\n', E) | |
w = E / np.sum(E) | |
print('Weight of each index:\n', w) | |
pH = 1 - np.sum(w * np.abs(mu - mu0[np.newaxis, :]), axis=1) | |
W = pH / np.sum(pH) | |
print(list(zip(*sorted(list(enumerate(W)), key=lambda x: x[1])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment