In [ ]:
In [1]:
from time import time
import psycopg2
from collections import Counter
import pandas as pd
import numpy as np
from sklearn.decomposition import NMF
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
np.set_printoptions(suppress=True,precision=10)
In [2]:
import sys
import os
sys.path.append(os.path.abspath("/home/scidb/HeartRatePatterns/Python"))
from LogisticRegresion import ajustLogisticRegression
In [3]:
def selectMatrix(dbname="mimic") :
conn = psycopg2.connect("dbname="+dbname)
cur = conn.cursor()
select_stament = ("SELECT m.subject_id,m.word,m.counting,s.isalive "
" FROM matrix m LEFT JOIN subjectwords s ON m.subject_id=s.subject_id "
# " WHERE m.word in (select word from wordspearson where p1>0.01 order by p1 limit 400) "
)
cur.execute(select_stament)
select = []
for row in cur :
select.append((row))
cur.close()
conn.close()
return select
In [4]:
def convertMatrix() :
labels = ['subject_id', 'Word', 'Counting','isAlive']
df = pd.DataFrame.from_records(selectMatrix(), columns=labels)
print(len(df))
table = pd.pivot_table(df,index=["subject_id","isAlive"],columns=["Word"],values=["Counting"],
aggfunc={"Counting":[np.sum]},fill_value=0)
table.columns = [value[2] for value in table.columns.values]
return table
In [5]:
t0=time()
table = convertMatrix()
print("converMatrix done in %0.3fs." % (time() - t0))
print(table.shape)
Out[5]:
In [6]:
survived = table.index.labels[1].tolist()
patients = table.values
In [7]:
print(table.shape)
print(len(survived))
print(patients.shape)
In [8]:
nmf = NMF(n_components=2, random_state=1,alpha=.1, l1_ratio=.5)
patients_trainnmf = nmf.fit_transform(patients)
In [9]:
components = nmf.components_
components.shape
Out[9]:
In [10]:
patients_trainnmf.shape
Out[10]:
In [11]:
def survivered(survived_train):
bal = []
for survived in survived_train:
if survived==0:
bal.append('green')
else:
bal.append('red')
return bal
plt.scatter(x=components[0],y=components[1],c='blue')
plt.scatter(x=patients_trainnmf[:,0],y=patients_trainnmf[:,1],c=survivered(survived))
plt.show()
In [12]:
nmf = NMF(n_components=3, random_state=1,alpha=.1, l1_ratio=.5)
patients_trainnmf = nmf.fit_transform(patients)
components = nmf.components_
fig = plt.figure()
ax2 = fig.add_subplot(111, projection='3d')
ax2.scatter(components[0],-components[1],components[2],c='blue',marker='o')
ax2.scatter(patients_trainnmf[:, 0], -patients_trainnmf[:, 1],patients_trainnmf[:, 2], marker='.', lw=0, alpha=0.7,c=survivered(survived), edgecolor='k')
plt.show()
In [ ]: