<jupyter_start><jupyter_text>This file records the results without the feature selection procedure.<jupyter_code>from sklearn.mixture import GaussianMixture
from sklearn.decomposition import PCA
from sklearn.cluster import k_means, dbscan
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#import seaborn as sns
# read data
X = pd.read_csv('selectedX.csv',index_col=0)
# GaussianMixture model
gm = GaussianMixture(n_components=3, verbose=1)
gm.fit(X)
# get and save the GM groups
Xpre = gm.predict(X)
# save
pd.DataFrame(Xpre,columns=['GMgroup']).to_csv('GaussianMixtureResult.csv',index=False)<jupyter_output><empty_output><jupyter_text>The visualized GM groups (visualized by PCA)<jupyter_code># Use PCA to see the effect
Xpre = pd.read_csv('GaussianMixtureResult.csv').as_matrix().reshape([-1])
pca = PCA(svd_solver='full')
X1 = pca.fit_transform(X)
plt.scatter(X1[:,0],X1[:,1],c=Xpre)
# use less features (2 PCA features here)
from matplotlib.patches import Ellipse
gm1 = GaussianMixture(n_components=3, verbose=1)
n_pc = 2
gm1.fit(X1[:,:n_pc])
Xpre1 = gm1.predict(X1[:,:n_pc])
plt.scatter(X1[:,0],X1[:,1],c=Xpre1)
gm1.predict_proba(X1[:,:n_pc])
def draw_ellipse(position, covariance, ax=None, **kwargs):
    """Draw an ellipse with a given position and covariance"""
    ax = ax or plt.gca()
    # Convert covariance to principal axes
    if covariance.shape == (2, 2):
        U, s, Vt = np.linalg.svd(covariance)
        angle = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
        width, height = 2 * np.sqrt(s)
    else:
        angle = 0
        width, height = 2 * np.sqrt(covariance)
    # Draw the Ellipse
    for nsig in range(1, 4):
        ax.add_patch(Ellipse(position, nsig * width, nsig * height,
                             angle,color='black', **kwargs))
for pos, covar, w in zip(gm1.means_, gm1.covariances_, gm1.weights_):
    draw_ellipse(pos, covar, alpha=w * 0.1 / gm1.weights_.max())<jupyter_output>Initialization 0
  Iteration 0
Initialization converged: True<jupyter_text>The Subgroups of the original paper (visualized by PCA)<jupyter_code># compare with the "Subgroup"
Y = pd.read_csv('Patient_data_NEJM.csv', index_col=0)
Y = Y.sort_index()
assert all(Y.index==X.index) # make sure that the index is the same
Y['GMgroup'] = Xpre
Y[['Subgroup','GMgroup']].head()
plt.scatter(X1[:,0],X1[:,1],c=pd.factorize(Y['Subgroup'])[0])<jupyter_output><empty_output><jupyter_text>The Subgroups of K-means (visualized by PCA)<jupyter_code>kmeans_result = k_means(X,3)
plt.scatter(X1[:,0],X1[:,1],c=kmeans_result[1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1433552886478109, 'fraction_numerical': 0.02247191011235955, 'mean_word_length': 5.25181598062954, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '38927', 'license': 'MIT', 'n_tokens_mistral': 1029, 'n_tokens_neox': 972, 'n_words': 218, 'path': 'backup/3.2_GaussianMixture.ipynb', 'repo_name': 'yanglyuxun/multivariate_gene_analysis', 'size': '2581'}