首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用fastai实现sklearns StratifiedKfold?

如何用fastai实现sklearns StratifiedKfold?
EN

Stack Overflow用户
提问于 2019-09-04 11:12:30
回答 2查看 1K关注 0票数 2

我参加了APTOS 2019年的kaggle竞赛,并试图在5个折叠上进行集成,但我在正确实现StratifiedKFold方面遇到了问题。

我试着在谷歌上搜索快速讨论,但我看不到任何解决方案。我正在使用fastai库,并且有一个预先训练好的模型。

代码语言:javascript
复制
def get_df():
    base_image_dir = os.path.join('..', 'input/aptos2019-blindness- 
    detection/')
    train_dir = os.path.join(base_image_dir,'train_images/')
    df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
    df['path'] = df['id_code'].map(lambda x: 
    os.path.join(train_dir,'{}.png'.format(x)))
    df = df.drop(columns=['id_code'])
    df = df.sample(frac=1).reset_index(drop=True) #shuffle dataframe
    test_df = pd.read_csv('../input/aptos2019-blindness- 
    detection/sample_submission.csv')
    return df, test_df

df, test_df = get_df()

random_state = np.random.seed(2019)
skf = StratifiedKFold(n_splits=5, random_state=random_state, shuffle=True)

X = df['path']
y = df['diagnosis']

#getting the splits
for train_index, test_index in skf.split(X, y):
   print('##')
   X_train, X_test = X[train_index], X[test_index]
   y_train, y_test = y[train_index], y[test_index]
   train = X_train, y_train
   test = X_test, y_test
   train_list = [list(x) for x in train]
   test_list  = [list(x) for x in test]


data = (ImageList.from_df(df=df,path='./',cols='path') 
    .split_by_rand_pct(0.2) 
    .label_from_df(cols='diagnosis',label_cls=FloatList) 
    .transform(tfms,size=sz,resize_method=ResizeMethod.SQUISH,padding_mode='zeros') 
    .databunch(bs=bs,num_workers=4) 
    .normalize(imagenet_stats)  
   )

learn = Learner(data, 
            md_ef, 
            metrics = [qk], 
            model_dir="models").to_fp16()
learn.data.add_test(ImageList.from_df(test_df,
                             '../input/aptos2019-blindness-detection',
                                  folder='test_images',
                                  suffix='.png'))

我想使用我从skf.split得到的折叠来训练我的模型,但我不确定如何做到这一点。

EN

回答 2

Stack Overflow用户

发布于 2019-10-31 16:22:42

有两种方法可以做到这一点。

  1. 将'split_by_idxs‘与索引

一起使用

代码语言:javascript
复制
    data = (ImageList.from_df(df=df,path='./',cols='path')
        .split_by_idxs(train_idx=train_index, valid_idx=test_index) 
        .label_from_df(cols='diagnosis',label_cls=FloatList) 
        .transform(tfms,size=sz,resize_method=ResizeMethod.SQUISH,padding_mode='zeros') 
        .databunch(bs=bs,num_workers=4) 
        .normalize(imagenet_stats)  
       )

使用'split_by_list'的

代码语言:javascript
复制
   il = ImageList.from_df(df=df,path='./',cols='path')

   data = (il.split_by_list(train=il[train_index], valid=il[test_index]) 
       .label_from_df(cols='diagnosis',label_cls=FloatList) 
       .transform(tfms,size=sz,resize_method=ResizeMethod.SQUISH,padding_mode='zeros') 
       .databunch(bs=bs,num_workers=4) 
       .normalize(imagenet_stats)  
      )
票数 2
EN

Stack Overflow用户

发布于 2019-09-04 11:31:30

这是一段代码。希望这能有所帮助。

代码语言:javascript
复制
# creating a KFold object with 5 splits 
folds = KFold(n_splits = 5, shuffle = True, random_state = 10)

# specify range of hyperparameters
# Set the parameters by cross-validation
hyper_params = [ {'gamma': [1e-2, 1e-3, 1e-4],
                     'C': [5,10]}]


# specify model
model = SVC(kernel="rbf")

# set up GridSearchCV()
model_cv = GridSearchCV(estimator = model, 
                        param_grid = hyper_params, 
                        scoring= 'accuracy', 
                        cv = folds, 
                        verbose = 1,
                        return_train_score=True)      

# fit the model
model_cv.fit(X_train, y_train)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57781005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档