Skip to content

Instantly share code, notes, and snippets.

@dkoslicki
Created July 20, 2020 21:28
Show Gist options
  • Save dkoslicki/847070280b463df46218994b2cfb695c to your computer and use it in GitHub Desktop.
Save dkoslicki/847070280b463df46218994b2cfb695c to your computer and use it in GitHub Desktop.
pred = predictor(model_file=pkl_file) # your model file
single_X = [[0.024110625,-0.014302074,0.03327463,0.037940405,-0.008836642,0.016498972,-0.035753097,-0.018181683,-0.04282986,-0.00093017286,-0.020855421,0.09168679,-0.026489392,-0.000757988,0.015053533,-0.03811925,0.105790354,-0.15019746,0.005389204,0.065862186,-0.059054427,0.09367167,-0.07321083,-0.029161578,0.019454233,-0.0025663017,0.13445973,0.034153406,-0.045934483,-0.019593718,0.044405438,-0.0064168656,0.024581399,-0.02436311,-0.02830375,0.038942236,-0.025031557,-0.04817994,0.08156777,-0.006039464,0.03207281,0.010570812,-0.044887736,0.04389168,0.0083243875,0.02332488,0.079191886,-0.015065394,0.059166152,-0.00917501,-0.02219141,0.047573287,-0.0142929265,-0.04038168,-0.004716043,0.029420398,-0.009532481,0.014363899,-0.040293276,-0.026997436,0.117409654,-0.018956954,-0.052647393,0.004364208,0.062688805,-0.0042845616,-0.06180385,0.030208366,0.028081495,0.023202026,-0.009765206,0.021606075,0.004227908,0.043646853,0.011311999,0.06761212,-0.03812019,0.050944738,0.025982138,0.048860647,0.028527997,0.017820543,-0.04389491,-0.010474947,-0.08245314,-0.04667332,-0.06164875,0.03886953,0.010487614,0.049546346,-0.034748133,0.075719714,0.007027092,0.013887945,-0.10373934,0.011361224,-0.008949787,0.0072890674,0.019102214,0.0474463,0.041876446,-0.05403571,-0.01992158,0.0021828208,-0.027132075,0.036031574,-0.066363454,0.015064542,-0.0027122926,-0.08071291,-0.04548319,0.014086138,-0.04447227,0.0019236135,-0.03350557,-0.045608353,0.041940987,0.015760036,-0.003007987,-0.026847243,-0.032526977,-0.03949789,-0.0060131014,-0.00970389,-0.05600266,0.0056613833,0.04824599,-0.092988715,-0.020533709,-0.04972502,-0.022495395,0.09017643,-0.10370153,0.035397265,-0.016887268,-0.012451917,-0.031235987,0.03473209,-0.0076955585,-0.022342468,0.0075491145,-0.004613826,-0.08329673,-0.026921524,-0.010647891,0.04842147,-0.0035890266,0.0305396,0.054524314,0.06524237,0.0043049124,-0.11452096,-0.08674276,-0.019455874,0.061068997,-0.006677321,0.026959153,0.022146035,-0.039174415,-0.07541643,0.016754644,-0.027966497,-0.0036841112,0.011827213,-0.0066287993,-0.008551931,0.013653803,-0.033946767,-0.021464108,-0.003623524,-0.023265729,-0.028787417,-0.03862123,-0.026362307,0.0075541837,0.061165754,-0.068612196,0.03627297,-0.005428982,-0.008757867,-0.007163035,0.041081067,-0.046250496,-0.09233998,0.084832065,-0.043613825,-0.047719292,-0.010363735,-0.032827288,-0.04197799,-0.06509741,0.043590706,-0.088228166,0.044177983,0.043620165,-0.015278521,-0.016344396,0.016725054,0.06975445,0.012534944,0.0056598987,-0.031868257,-0.06565744,0.0638258,-0.063247286,0.028269045,-0.027077923,-0.025841348,-0.017955074,-0.04629228,-0.056770366,-0.11283211,-0.041298077,-0.00027958836,0.055364046,0.0055209263,-0.05714987,-0.045626935,0.064740345,0.033170972,-0.0036321655,0.010127464,0.074391626,-0.044319637,0.011666187,0.0036749807,-0.009732942,-0.06664975,0.05174389,-0.02686999,-0.0025608235,-0.11980411,0.035624914,0.0720005,-0.07571991,-0.059911106,0.0007879732,0.038824853,-0.014986784,0.023223938,0.035218026,-0.038038462,0.05704521,-0.060068037,0.06460279,0.0675934,0.003989461,0.06819226,0.036054507,-0.0029095397,0.01777339,-0.028364113,-0.003566817,-0.033329237,-0.0354291,0.047265753,0.0008325964,-0.07312996,-0.018961234,-0.0006309382,0.001421919,0.03379338,-0.08524401,-0.109695435,0.038095344,-0.06308378,0.022901576,-0.00012904222,0.0029712385,0.068708844,0.015504749,-0.015925195,0.01890484,0.018041112,0.027146941,-0.0457287,-0.03187146,-0.026066715,0.010592819,0.0012404931,0.010864983,-0.030190473,0.027138148,-0.026868073,-0.048654642,-0.008129084,-0.017117355,0.04975107,0.013257576,0.029260973,-0.04497425,0.08518692,0.0064053666,-0.00021626659,0.021683238,0.05256915,-0.0005742155,0.008878452,-0.054198176,-0.023786956,-0.009413838,0.03674504,-0.0238031,0.0035283875,-0.010769862,0.027812334,-0.019243257,0.051627677,-0.02784635,0.055182464,-0.012185149,0.01051869,-0.021426275,0.026292684,0.017904961,0.008839672,0.010610331,-0.009420034,-0.06812929,0.0013296901,0.0052254237,0.06479965,0.04297067,0.0010702406,-0.01146825,0.024291692,0.037616633,0.028202247,-0.014899986,0.007367879,0.05735716,0.022026882,-0.057384294,0.04933936,-0.017486587,-0.0478373,0.044427503,0.0031798233,-5.116575e-05,0.047288977,-0.02218265,0.055824395,-0.044196784,-0.030144723,0.0002506226,0.07417111,0.0026395405,-0.0074372916,-0.04123362,-0.02924693,-0.04299769,-0.0019482938,0.0594726,0.041352686,-0.053971447,-0.012445598,-0.0224251,-0.03669321,-0.035859115,0.04067296,-0.049495023,-0.057491526,-0.014044943,0.0726841,-0.014004656,0.03830568,0.040366303,-0.021766905,0.0675638,-0.0043169116,0.031177124,0.04453131,0.012451151,-0.01637466,0.025402384,0.083997205,-0.014311952,-0.07785351,0.030339492,-0.06240542,0.018029658,-0.0016018794,-0.050012033,-0.0018489552,0.07544807,-0.09431414,-0.01879602,0.037744362,0.048737664,0.00013057602,-0.06641705,-0.023597706,0.07961141,-0.051947795,-0.018308735,-0.028413448,0.027071344,0.082473435,-0.0028986998,0.00019564363,-0.062596545,0.000168347,0.013316728,-0.043191303,-0.009365904,0.0041995756,-0.007856454,0.014618495,0.028015392,0.006085821,0.018153096,0.08523722,0.0071213837,-0.060388286,0.0027143627,0.043190766,0.03564747,-0.07553264,0.024804773,-0.061379172,0.0154607305,0.012664495,-0.10599732,0.04011271,-0.010598285,0.01834385,-0.023352357,0.0032048093,-0.010565198,0.045424365,-0.057503812,-0.06078194,0.03670302,0.017871292,0.0051311636,0.02985192,-0.0726099,0.05171096,0.04691152,0.016545637,-0.022176988,0.0004439917,0.03820156,-0.0008640815,0.0055647013,-0.03752301,0.066199735,-0.0321968,-0.032663614,-0.06880989,0.037826754,0.011322294,0.008054823,0.0026846742,-0.041504245,-0.054153945,-0.025700549,-0.02666842,0.04474876,-0.031888854,0.02721114,0.016231151,-0.009572623,0.027519964,-0.01946024,-0.021713097,0.02273508,-0.05460976,-0.02438252,0.00088928494,0.04947093,-0.05875815,-0.0007115496,0.021068783,-0.058321666,-0.020203501,-0.017874338,-0.03189875,-0.027890319,0.0748221,0.004971471,-0.062478494,0.08015344,0.1312422,0.0016234467,-0.010355278,0.018427923,-0.11378872,-0.08634725,0.007288925,0.031788018,0.016112342,0.103131995,0.014198542,0.0021118629,-0.022385426,0.13056318,-0.030023802,0.09612468,0.022595443,-0.08058487,-0.075224414,0.055973962,0.06108778,0.0047430545,-0.032326955,0.06986624,0.043596294,-0.039373387,-0.02542065,0.031101117,-0.03973313]]
pred.prob(single_X)
# Out[29]: array([[0.548, 0.452]])
np.mean([x.predict_proba(single_X)[0,0] for x in pred.model.estimators_])
#Out[30]: 0.548
np.mean([x.predict_proba(single_X)[0,1] for x in pred.model.estimators_])
#Out[28]: 0.452
# note that the pred.model.estimators_ are the individual decision trees, so they return one-hot prediction vectors, and
# the RF just takes the mean of these:
# Add them up:
# https://github.com/scikit-learn/scikit-learn/blob/fd237278e895b42abe8d8d09105cbb82dc2cbba7/sklearn/ensemble/_forest.py#L470-L472
# divide by number of estimators/decision trees:
# https://github.com/scikit-learn/scikit-learn/blob/fd237278e/sklearn/ensemble/_forest.py#L688-L689
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment