基因筛选与分析: Lasso回归、重要基因可视化和聚类分析
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from sklearn.linear_model import Lasso from sklearn.cluster import KMeans
读取Excel文件
df = pd.read_excel('C:\Users\lenovo\Desktop\HIV\PAH三个数据集\193lasso.xlsx', header=0)
删除包含缺失值的行
#df.dropna(inplace=True)
提取特征和标签
X = df.iloc[:, 1:] y = df.iloc[:, 0]
使用Lasso回归算法
lasso = Lasso(alpha=0.01) lasso.fit(X, y)
输出关键性作用较大的基因
coef = pd.Series(lasso.coef_, index=X.columns) important_genes = coef[coef != 0].sort_values(ascending=False) print('Important Genes:') print(important_genes)
绘制基因重要性的条形图
plt.figure(figsize=(10,8)) sns.barplot(x=important_genes.values, y=important_genes.index, palette='Blues_d') plt.title('Important Genes') plt.xlabel('Coefficient') plt.ylabel('Gene') plt.show()
输出所挑选的基因及其表达量和患者状态
selected_genes_df = df.loc[:, important_genes.index.tolist()] selected_genes_df.insert(0, 'state', df['state'])
绘制散点图
plt.figure(figsize=(10,8)) sns.scatterplot(x='state', y=important_genes.index[0], data=selected_genes_df) plt.title('Scatter Plot of Gene and Patient State') plt.xlabel('Patient State') plt.ylabel('Gene Expression') plt.show()
绘制箱线图
plt.figure(figsize=(10,8)) sns.boxplot(x='state', y=important_genes.index[0], data=selected_genes_df) plt.title('Boxplot of Gene and Patient State') plt.xlabel('Patient State') plt.ylabel('Gene Expression') plt.show()
进行聚类分析,绘制基因表达量热力图
kmeans = KMeans(n_clusters=2) kmeans.fit(selected_genes_df.iloc[:, 1:]) selected_genes_df['cluster'] = kmeans.labels_
plt.figure(figsize=(10,8)) sns.clustermap(selected_genes_df.iloc[:, 1:].T, cmap='coolwarm', col_cluster=False) plt.title('Heatmap of Gene Expression') plt.xlabel('Patient') plt.ylabel('Gene') plt.show()
原文地址: https://www.cveoy.top/t/topic/nfoL 著作权归作者所有。请勿转载和采集!