Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Python code for MVAdiscbh #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions QID-1208-MVAdiscbh/MVAdiscbh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pandas as pd
import numpy as np
from scipy.cluster import hierarchy
import random
import matplotlib.pyplot as plt

data = pd.read_csv("bostonh.dat", sep = "\s+", header=None)

# transform data

xt = data
for i in [0, 2, 4, 5, 7, 8, 9, 13]:
xt.iloc[:, i] = np.log(data.iloc[:, i])

xt.iloc[:, 1] = data.iloc[:, 1]/10
xt.iloc[:, 6] = (data.iloc[:, 6]**(2.5))/10000
xt.iloc[:, 10] = np.exp(0.4 * data.iloc[:, 10])/1000
xt.iloc[:, 11] = data.iloc[:, 11]/100
xt.iloc[:, 12] = np.sqrt(data.iloc[:, 12])
data = xt.drop(3, axis = 1)

da = (data - np.mean(data))/np.std(data, ddof = 1)
d = np.zeros([len(da),len(da)])


for i in range(0, len(da)):
for j in range(0, len(da)):
d[i, j] = np.linalg.norm(da.iloc[i, :] - da.iloc[j, :])

ddd = d[1:, :-1][:, 0]
for i in range(1, len(da)-1):
ddd = np.concatenate((ddd, d[1:, :-1][i:, i]))

w = hierarchy.linkage(ddd, 'ward')
tree = hierarchy.cut_tree(w, n_clusters = 2)

da["tree"] = tree

t1 = da[da["tree"] == 0].iloc[:, :-1]
t2 = da[da["tree"] == 1].iloc[:, :-1]

m1 = t1.mean() # mean of first cluster
m2 = t2.mean() # mean of second cluster
m = (m1 + m2)/2
s = ((len(t1) - 1) * t1.cov() + (len(t2) - 1) * t2.cov())/(len(xt) - 2)
alpha = np.linalg.inv(s) @ (m1 - m2)

mis1 = ((t1 - m) @ alpha < 0).sum()
mis2 = ((t2 - m) @ alpha > 0).sum()

corr1 = ((t1 - m) @ alpha > 0).sum()
corr2 = ((t2 - m) @ alpha < 0).sum()

aper = (mis1 + mis2)/len(xt)
alph = (da.iloc[:, :-1] - np.tile(np.array(m), (len(da), 1))) @ alpha

random.seed(1)

p = tree.flatten() + (0.05*np.random.normal(size = len(tree)))

dav = pd.DataFrame(data = {"tree": tree.flatten(), "p": p, "alph": alph})

fig, ax = plt.subplots(figsize = (10, 10))
ax.scatter(dav[dav["tree"] == 0]["alph"], dav[dav["tree"] == 0]["p"], c = "r")
ax.scatter(dav[dav["tree"] == 1]["alph"], dav[dav["tree"] == 1]["p"], c = "k",
marker = "^")
ax.axvline(0, c = "k")
plt.title("Discrimination scores", fontsize = 16)
plt.show()

Binary file added QID-1208-MVAdiscbh/MVAdiscbh_python.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions QID-1208-MVAdiscbh/Metainfo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ See also: MVAaer, MVAaper, MVAaerbh, MVAdisfbank, MVAdisnorm

Author: Zografia Anastasiadou
Author[SAS]: Svetlana Bykovskaya
Author[Python]: 'Matthias Fengler, Liudmila Gorkun-Voevoda'

Submitted: Thu, August 04 2011 by Awdesch Melzer
Submitted[SAS]: Wen, April 6 2016 by Svetlana Bykovskaya
Submitted[Python]: Tue, February 22 2022 by Liudmila Gorkun-Voevoda

Datafile: bostonh.dat

Expand Down
285 changes: 0 additions & 285 deletions QID-1208-MVAdiscbh/README.md

This file was deleted.