Repository navigation
Expand file tree
/
Copy pathsolubility.py
More file actions
71 lines (50 loc) · 2.02 KB
/
Copy pathsolubility.py
File metadata and controls
71 lines (50 loc) · 2.02 KB
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from rdkit import Chem
from rdkit.Chem import Descriptors
def plot_pca(T, labels):
plt.figure(figsize=(5, 4))
for i, (label, color) in enumerate(zip(sorted(set(labels)), ('green', 'gray', 'red'))):
plt.scatter(T[labels == label, 0], T[labels == label, 1], label=label,
marker=str(i+1), color=color, linewidth=1.0)
plt.axline([0, 0], [1, 0], linestyle='--', linewidth=0.8, color='gray')
plt.axline([0, 0], [0, 1], linestyle='--', linewidth=0.8, color='gray')
plt.xlim(-10, 25)
plt.ylim(-20, 15)
plt.legend()
plt.savefig('figure/solubility_PCA.png', dpi=100)
def pca(X, columns):
N, n = X.shape
# Mean centering.
X = X - np.mean(X, axis=0)
# Standardization.
X = X / np.std(X, axis=0)
U, S, Vt = np.linalg.svd(X)
T = U[:, :n] * S
importance = pd.DataFrame(zip(columns, Vt.T[:, 1]), columns=['feature', 'value'])
return T, importance
def load_dataset():
descriptors, y, labels = [], [], []
for mol in Chem.SDMolSupplier('data/solubility.train.sdf'):
descriptors.append(Descriptors.CalcMolDescriptors(mol))
y.append(np.float32(mol.GetProp('SOL')))
labels.append(mol.GetProp('SOL_classification'))
data = []
for descriptor in descriptors:
data.append(descriptor.values())
X = pd.DataFrame(data, columns=descriptor.keys())
labels = np.asarray(labels)
return X, y, labels
def main():
X, y, labels = load_dataset()
print(pd.DataFrame(labels, columns=['label']).groupby('label').size())
print('Exclude {} features of the NaN values.'.format((X.isnull().sum(axis=0) > 0).sum()))
X = X.loc[:, X.isnull().sum(axis=0) == 0]
print('Exclude {} features of zero variance.'.format((X.var(axis=0) == 0).sum()))
X = X.loc[:, X.var(axis=0) != 0]
T, importance = pca(X.values, X.columns)
print(importance.sort_values('value', ascending=False))
plot_pca(T, labels)
if __name__ == '__main__':
main()