Skip to content

Instantly share code, notes, and snippets.

@chengluyu
Created January 11, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chengluyu/111a3a959b610f2af05e67bf056a32b9 to your computer and use it in GitHub Desktop.
Save chengluyu/111a3a959b610f2af05e67bf056a32b9 to your computer and use it in GitHub Desktop.
Zhang Ziqi
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