美文网首页影像组学学习笔记
影像组学学习笔记(10)-T检验+lasso+随机森林

影像组学学习笔记(10)-T检验+lasso+随机森林

作者: 北欧森林 | 来源:发表于2020-11-24 04:52 被阅读0次

本笔记来源于B站Up主: 有Li 的影像组学系列教学视频
本节(10)主要介绍: T检验+lasso+随机森林

李博士借用和女朋友一起吃饭这个实例来说明:爱情和机器学习一样,复杂深奥、难以揣测。

import pandas as pd
import numpy as np
from sklearn.utils import shuffle
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LassoCV
from sklearn.model_selection import train_test_split, cross_val_score,KFold,RepeatedKFold,GridSearchCV
from scipy.stats import pearsonr, ttest_ind, levene
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
xlsx1_filePath = 'C:/Users/RONG/Desktop/PythonBasic/data_A.xlsx'
xlsx2_filePath = 'C:/Users/RONG/Desktop/PythonBasic/data_B.xlsx'
data_1 = pd.read_excel(xlsx1_filePath)
data_2 = pd.read_excel(xlsx2_filePath)
rows_1,__ = data_1.shape
rows_2,__ = data_2.shape
data_1.insert(0,'label',[0]*rows_1)
data_2.insert(0,'label',[1]*rows_2)
data = pd.concat([data_1,data_2])
data = shuffle(data)
data = data.fillna(0)
X = data[data.columns[1:]]
y = data['label']
colNames = X.columns
X = X.astype(np.float64)
X = StandardScaler().fit_transform(X)
X = pd.DataFrame(X)
X.columns = colNames
# t-test for feature selection
index = []
for colName in data.columns[1:]:
    if levene(data_1[colName],data_2[colName])[1] > 0.05:
        if ttest_ind(data_1[colName],data_2[colName])[1] < 0.05:
            index.append(colName)
    else:
        if ttest_ind(data_1[colName],data_2[colName],equal_var = False)[1] < 0.05:
            index.append(colName)
print(len(colName))
# to select the 'positive' features
if 'label' not in index:index = ['label']+index
data_1 = data_1[index]
data_2 = data_2[index]
data = pd.concat([data_1,data_2])
data = shuffle(data)
data.index = range(len(data))#re-label after mixure
X = data[data.columns[1:]]
y = data['label']
X = X.apply(pd.to_numeric,errors = 'ignore') # transform the type of the data 
colNames = X.columns # to read the feature's name
X = X.fillna(0)
X = X.astype(np.float64)
X = StandardScaler().fit_transform(X)
X = pd.DataFrame(X)
X.columns = colNames
# lasso for further feature selection
alphas = np.logspace(-3,1,30)
model_lassoCV = LassoCV(alphas = alphas, cv = 10, max_iter = 100000).fit(X,y)
print(model_lassoCV.alpha_)
coef = pd.Series(model_lassoCV.coef_,index = X.columns)
print('Lasso picked ' + str(sum(coef !=0))+' variables and eliminated the other ' + str(sum(coef == 0))
index = coef[coef != 0].index
X = X[index]
X.head()
print(coef[coef !=0])
# RandomFrorest
X_train, X_test,y_train,y_test = train_test_split(X,y,test_size = 0.3)
model_rf = RandomForestClassifier(n_estimators = 20).fit(X_train,y_train)
score_rf = model_rf.score(X_test,y_test)
print(score_rf)

相关文章

  • 影像组学学习笔记(10)-T检验+lasso+随机森林

    本笔记来源于B站Up主: 有Li[https://space.bilibili.com/542601735?fro...

  • 待学清单

    算法原理 回归模型(线性+逻辑) 固定、随机、混合效应模型 时间序列分析 决策树与随机森林 判断差异的方法:t检验...

  • 影像组学学习笔记(9)-T检验(T-test)理论及示例

    本笔记来源于B站Up主: 有Li[https://space.bilibili.com/542601735?fro...

  • 统计学梳理

    今天听师兄做的统计学的专题报告,总结一下学习内容。 一 均值检验 2组样本时: t检验:参数检验,适用于小样本,正...

  • 配对t检验说明

    配对t检验说明 配对样本t检验是统计学中参数检验的一种有效途径,可以用来检验两组具有相关性的样本数据是否源自同均值...

  • T检验靠谱吗?

    T检验靠谱吗? 对于实验生物学研究者来说,经常遇到的一个场景是,对照组测量3个值,实验组测量3个值,然后用T检验得...

  • R做方差齐次检验

    一组数据需要做t检验,了解了一下t检验分为:a.单样本t检验,b.独立双样本t检验和c.成对或非独立样本t检验,三...

  • 统计-T检验和ANOVA+代码实战

    细说T检验: A 配对T检验:针对一个样本,当你有实验前和实验后的两组数据时选择配对T检验 不配对T检验推荐选择d...

  • 何为决策树和随机森林?

    随机森林 定义:随机森林或随机决策森林是用于分类、回归和其他任务的集成学习方法。 名字由来:随机森林就是使用随机的...

  • 什么是假设检验

    什么是假设检验 假设检验: 参数检验:T检验, 对照组为122,实验组为234,检测实验组比对照组显著,认为各组结...

网友评论

    本文标题:影像组学学习笔记(10)-T检验+lasso+随机森林

    本文链接:https://www.haomeiwen.com/subject/dpcabktx.html