<reponame>bradduy/computer_vision<filename>Data Science/PCA.py
import pandas as pd
import numpy as np
import random as rd
from sklearn.decomposition import PCA
from sklearn import preprocessing
import matplotlib.pyplot as plt

#PPREPARE DATA ####################################################################
genes = ['gene' + str(i) for i in range(1, 101)]
wt = ['wt' + str(i) for i in range(1,6)]
ko = ['ko' + str(i) for i in range(1,6)]

data = pd.DataFrame(columns=[*wt, *ko], index=genes)

for gene in data.index:
    data.loc[gene, 'wt1':'wt5'] = np.random.poisson(lam=rd.randrange(10, 1000), size=5)
    data.loc[gene, 'ko1':'ko5'] = np.random.poisson(lam=rd.randrange(10, 1000), size=5)

print(data.head())
"""
       wt1   wt2   wt3   wt4  wt5  ko1  ko2  ko3  ko4  ko5
gene1  832   820   822   816  858  890  954  894  945  950
gene2  898  1001  1006   998  943  209  234  203  236  211
gene3  422   387   426   422  431  894  796  876  820  810
gene4  950   922   936  1000  939  977  952  970  946  961
gene5  845   874   927   850  900  451  489  482  452  498
"""
print(data.shap)
"""(100, 10)
100 rows x 10 columns
"""

# CENTERING ####################################################################
scaled_data = preprocessing.scale(data.T)
pca = PCA()
pca.fit(scaled_data)
pca_data = pca.transform(scaled_data)

####################################################################
per_var = np.round(pca.explained_variance_ratio_ * 100, decimals=1)
labels = ['PC' + str(x) for x in range(1, len(per_var)+1)]

plt.bar(x=range(1, len(per_var)+1), height=per_var, tick_label=labels)
plt.ylabel('Percentage of Explained Variance')
plt.xlabel('Principal Component')
plt.title('Scree Plot')
plt.show()

pca_df = pd.DataFrame(pca_data, index=[*wt, *ko], columns=labels)
plt.scatter(pca_df.PC1, pca_df.PC2)
plt.title('My PCA Graph')
plt.xlabel('PC1 - {0}%'.format(per_var[0]))
plt.ylabel('PC2 - {0}%'.format(per_var[1]))

for sample in pca_df.index:
    plt.annotate(sample, (pca_df.PC1.loc[sample], pca_df.PC2.loc[sample]))
plt.show()

####################################################################

loading_scores = pd.Series(pca.components_[0], index=genes)
sorted_loading_scores = loading_scores.abs().sort_values(ascending=False)

top_10_genes = sorted_loading_scores[0:10].index.values

print(loading_scores[top_10_genes])

Science/PCA.py
import pandas as pd
import numpy as np
import random as rd
from sklearn.decomposition import PCA
from sklearn import preprocessing
import matplotlib.pyplot as plt

#PPREPARE DATA ####################################################################
genes = ['gene' + str(i) for i in range(1, 101)]
wt = ['wt' + str(i) for i in range(1,6)]
ko = ['ko' + str(i) for i in range(1,6)]

data = pd.DataFrame(columns=[*wt, *ko], index=genes)

for gene in data.index:
    data.loc[gene, 'wt1':'wt5'] = np.random.poisson(lam=rd.randrange(10, 1000), size=5)
    data.loc[gene, 'ko1':'ko5'] = np.random.poisson(lam=rd.randrange(10, 1000), size=5)

print(data.head())
"""
       wt1   wt2   wt3   wt4  wt5  ko1  ko2  ko3  ko4  ko5
gene1  832   820   822   816  858  890  954  894  945  950
gene2  898  1001  1006   998  943  209  234  203  236  211
gene3  422   387   426   422  431  894  796  876  820  810
gene4  950   922   936  1000  939  977  952  970  946  961
gene5  845   874   927   850  900  451  489  482  452  498
"""
print(data.shap)
"""(100, 10)
100 rows x 10 columns
"""

# CENTERING ####################################################################
scaled_data = preprocessing.scale(data.T)
pca = PCA()
pca.fit(scaled_data)
pca_data = pca.transform(scaled_data)

####################################################################
per_var = np.round(pca.explained_variance_ratio_ * 100, decimals=1)
labels = ['PC' + str(x) for x in range(1, len(per_var)+1)]

plt.bar(x=range(1, len(per_var)+1), height=per_var, tick_label=labels)
plt.ylabel('Percentage of Explained Variance')
plt.xlabel('Principal Component')
plt.title('Scree Plot')
plt.show()

pca_df = pd.DataFrame(pca_data, index=[*wt, *ko], columns=labels)
plt.scatter(pca_df.PC1, pca_df.PC2)
plt.title('My PCA Graph')
plt.xlabel('PC1 - {0}%'.format(per_var[0]))
plt.ylabel('PC2 - {0}%'.format(per_var[1]))

for sample in pca_df.index:
    plt.annotate(sample, (pca_df.PC1.loc[sample], pca_df.PC2.loc[sample]))
plt.show()

####################################################################

loading_scores = pd.Series(pca.components_[0], index=genes)
sorted_loading_scores = loading_scores.abs().sort_values(ascending=False)

top_10_genes = sorted_loading_scores[0:10].index.values

print(loading_scores[top_10_genes])
#PPREPARE DATA #################################################################### wt1   wt2   wt3   wt4  wt5  ko1  ko2  ko3  ko4  ko5 gene1  832   820   822   816  858  890  954  894  945  950 gene2  898  1001  1006   998  943  209  234  203  236  211 gene3  422   387   426   422  431  894  796  876  820  810 gene4  950   922   936  1000  939  977  952  970  946  961 gene5  845   874   927   850  900  451  489  482  452  498 (100, 10) 100 rows x 10 columns # CENTERING #################################################################### #################################################################### ####################################################################