实验二 如何将随机森林算法应用于激酶抑制剂分类任务

发布于:2025-03-31 ⋅ 阅读:(21) ⋅ 点赞:(0)

目录

一、问题描述

二、解决方案

三、实验步骤

四、核心代码

五、结果分析


一、问题描述

       激酶抑制剂是一种抑制激酶活性的化合物。蛋白激酶用于调节细胞的多个功能,自2001年以来已有76种激酶抑制剂获批上市,例如伊马替尼(Imatinib)已被成功应用于癌症治疗。这里使用的数据集为包含2013个激酶抑制剂的四分类数据,每个数据包含5个特征。

二、解决方案

       采用随机森林算法对激酶抑制剂进行分类。随机森林是一种集成学习方法,主要用于分类和回归任务。它通过构建多个决策树并将它们的预测结果结合起来,从而提高模型的准确性和稳健性。这里使用Python的sklearn库来构建和训练随机森林模型,并对模型的结果进行分析。

三、实验步骤

1. 数据准备

数据收集:收集激酶抑制剂相关的数据集,包括化合物的化学结构特征、活性数据(如IC50值或其他抑制活性指标)以及靶标信息。

数据预处理:

数据清洗:去除缺失值或异常值。

特征提取:使用描述符(如分子指纹、物理化学性质等)来表示化合物。

标签编码:将激酶抑制剂和非抑制剂进行编码(如0和1)。

2. 特征选择

选择特征:使用统计测试(如卡方检验、相关系数)或特征选择算法(如递归特征消除)来选择最具预测性的特征。

降维:如果特征维度较高,可以考虑使用PCA(主成分分析)等降维技术来简化模型。

3. 模型构建

构建随机森林模型:

模型初始化:选择合适的随机森林参数,如树的数量(n_estimators)、最大深度(max_depth)等。

训练模型:使用训练集数据来训练随机森林模型。

4. 模型评估

评估指标:

使用交叉验证(如k折交叉验证)来评估模型的性能。

选择适当的评估指标,如准确率、召回率、F1-score和ROC-AUC曲线。

5. 超参数调优

使用网格搜索(Grid Search)或随机搜索(Random Search)对随机森林的超参数进行调优,以获得最佳模型性能。

6. 结果分析

特征重要性:分析随机森林模型的特征重要性,识别对分类结果贡献最大的特征。

可视化:使用图形工具(如特征重要性条形图)进行可视化,以帮助理解模型。

7. 预测与应用

使用训练好的模型对新化合物进行预测,分类它们为激酶抑制剂或非抑制剂。

将模型集成到药物研发流程中,支持决策制定。

8. 持续优化

根据新数据和实验结果,定期更新和优化模型,以提高预测准确性和适用性。

四、核心代码

#读取数据
df = pd.read_csv("kinase_selected.csv", delimiter = ',',header=0,index_col=0)
label = pd.read_csv('label_kinase_selected.csv', sep=',', )
#查看是否有缺失值
print(df.isnull().any())
#输出结果
# Unnamed: 0 False
# r_desc_Average_connectivity_index_chi-2 False
# r_desc_Maximal_electrotopological_negative_variation False
# r_desc_Second_Mohar False
# r_desc_Second_Zagreb_index_by_valence_vertex_degrees False
# r_qp_ACxDN^.5/SA False
feature_list = ['feature'+str(i) for i in range(5)]
feature_list = pd.array(feature_list)
# 显示统计数据
#print(df.describe())
#输出效果如图 5-7 所示
# 相关性矩阵
corr = df.corr()
sns.heatmap(corr,xticklabels=feature_list,yticklabels=feature_list)
#输出如图 5-8 所示
print(corr)
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder, StandardScaler
le = preprocessing.LabelEncoder()
y=label.iloc[:,-1]
# print(y.shape)
# 产生 X, y,即特征值与目标值
dataset = df.values
X = dataset[:,:]
# lable encoder
y=le.fit_transform(y)
# 将数据分为训练和测试数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123, stratify=y)
# 实例化
dtree = tree.DecisionTreeClassifier(
criterion='entropy',
max_depth=5, # 定义树的深度, 可以用来防止过拟合
min_weight_fraction_leaf=0.005 # 定义叶子节点最少需要包含多少个样本(使用百分比表达), 防止过拟合
)
# 训练
dtree = dtree.fit(X_train,y_train)
#输出结果
print("决策树: train score: " , dtree.score(X_train, y_train))
print("决策树: test score: " , dtree.score(X_test, y_test))
# 获取特征重要性
importances = dtree.feature_importances_
# 获取特征名称
feat_names = df.columns
# 排序
indices = np.argsort(importances)[::-1]
# 绘图
plt.figure(figsize=(10,6))
plt.title("Feature importances by Decision Tree")
plt.bar(range(len(indices)), importances[indices], color='lightblue', align="center" )
# plt.step(range(len(indices)), np.cumsum(importances[indices]), where='mid',label='Cumulative')
plt.xticks(range(len(indices)), feature_list[indices],fontsize=14)
plt.xlim([-1, len(indices)])
#输出结果
plt.show()
# 实例化随机森林
rf = RandomForestClassifier(
n_estimators=100,
criterion='entropy',
max_depth=7, # 定义树的深度, 可以用来防止过拟合
min_samples_split=10, # 定义至少多少个样本的情况下才继续分叉
#min_weight_fraction_leaf=0.02 # 定义叶子节点最少需要包含多少个样本(使用百分比表达), 防止过拟合
)
# 模型训练
rf.fit(X_train, y_train)
#输出结果
print("随机森林: train score: " , rf.score(X_train, y_train))
print("随机森林: test score: " , rf.score(X_test, y_test))
# 特征的重要程度
importances = rf.feature_importances_
# 特征名称
feat_names = df.columns
# 排序
indices = np.argsort(importances)[::-1]
# 绘图
plt.figure(figsize=(10,6))
plt.title(" Feature importances by RandomForest" )
plt.bar(range(len(indices)), importances[indices], color='lightblue', align="center" )
# plt.step(range(len(indices)), np.cumsum(importances[indices]), where='mid', label='Cumulative')
plt.xticks(range(len(indices)), feature_list[indices],fontsize=14)
plt.xlim([-1, len(indices)])
#输出结果
plt.show()
#保存模型
joblib.dump(dtree, "train_model_dtree.m" )
#调用模型
clf = joblib.load("train_model_dtree.m" )
#保存模型
joblib.dump(rf, "train_model_rf.m" )
#调用模型
clf = joblib.load("train_model_rf.m" )

#实例化随机森林模型
RF = RandomForestClassifier(n_estimators=100,
        criterion=" squared_error" ,
        max_depth=None,
        min_samples_split=2,
        min_samples_leaf=1,
        min_weight_fraction_leaf=0.0,
        max_features=" auto" ,
        max_leaf_nodes=None,
        bootstrap=True,
        oob_score=False,
        n_jobs=None,
        verbose=0,
        max_samples=None,
        )

五、结果分析

对运行结果的解释: