def cv_warning(model, data, tournament='bernie', kfold=5, nsamples=100): "Hold out a sample of eras not rows when doing cross validation." data = data['train'] results_cve = pd.DataFrame() results_cv = pd.DataFrame() for i in range(nsamples): # cv across eras cve = nx.CVSplitter(data, kfold=kfold, seed=i) prediction = nx.run(model, cve, tournament, verbosity=0) df = prediction.performance(data, tournament) results_cve = results_cve.append(df, ignore_index=True) # cv ignoring eras but y balanced cv = nx.IgnoreEraCVSplitter(data, tournament=tournament, kfold=kfold, seed=i) prediction = nx.run(model, cv, tournament, verbosity=0) df = prediction.performance(data, tournament) results_cv = results_cv.append(df, ignore_index=True) # display results rcve = results_cve.mean(axis=0) rcv = results_cv.mean(axis=0) rcve.name = 'cve' rcv.name = 'cv' r = pd.concat([rcve, rcv], axis=1) print("\n{} runs".format(i + 1)) print(r)
def test_splitter_overlap(): "prediction data should not overlap" d = nx.play_data() splitters = [ nx.TournamentSplitter(d), nx.ValidationSplitter(d), nx.CheatSplitter(d), nx.CVSplitter(d), nx.IgnoreEraCVSplitter(d), nx.SplitSplitter(d, fit_fraction=0.5) ] for splitter in splitters: predict_ids = [] for dfit, dpredict in splitter: predict_ids.extend(dpredict.ids.tolist()) ok_(len(predict_ids) == len(set(predict_ids)), "ids overlap")
def test_splitter_reset(): "splitter reset should not change results" d = nx.play_data() splitters = [ nx.TournamentSplitter(d), nx.ValidationSplitter(d), nx.CheatSplitter(d), nx.CVSplitter(d), nx.IgnoreEraCVSplitter(d), nx.SplitSplitter(d, fit_fraction=0.5) ] for splitter in splitters: ftups = [[], []] ptups = [[], []] for i in range(2): for dfit, dpredict in splitter: ftups[i].append(dfit) ptups[i].append(dpredict) splitter.reset() ok_(ftups[0] == ftups[1], "splitter reset changed fit split") ok_(ptups[0] == ptups[1], "splitter reset changed predict split")
def cv_warning(data, nsamples=100): model = nx.logistic() for i in range(nsamples): report = nx.Report() # cv across eras cve = nx.CVSplitter(data, seed=i) prediction = nx.run(model, cve, verbosity=0) report.append_prediction(prediction, 'cve') # cv ignoring eras but y balanced cv = nx.IgnoreEraCVSplitter(data, seed=i) prediction = nx.run(model, cv, verbosity=0) report.append_prediction(prediction, 'cv') # save performance results df = report.performance_df(data) cols = df.columns.tolist() cols[-1] = 'cv_type' df.columns = cols if i == 0: results = df else: results = results.append(df, ignore_index=True) # display results rcve = results[results.cv_type == 'cve'].mean(axis=0) rcv = results[results.cv_type == 'cv'].mean(axis=0) rcve.name = 'cve' rcv.name = 'cv' r = pd.concat([rcve, rcv], axis=1) print("\n{} runs".format(i+1)) print(r)