import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Debugging%20drill%3A%20a%20detector%20that%20flags%20the%20wrong%20rows%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%2C%20and%20builds%20the%20tables%20every%20other%20cell%20uses%20the%20way%20module%209%20builds%20them.%0A%20%20%20%20import%20io%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20pyod.models.ecod%20import%20ECOD%0A%20%20%20%20from%20sklearn.ensemble%20import%20IsolationForest%0A%20%20%20%20from%20sklearn.neighbors%20import%20LocalOutlierFactor%0A%20%20%20%20from%20sklearn.preprocessing%20import%20RobustScaler%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20PATHS%2C%20UNIT_KEY%2C%20check_cases%2C%20try_it%0A%0A%20%20%20%20%23%20The%20features%20every%20detector%20here%20reads%3A%20one%20number%20each%20per%20fishing%20unit%20(module%209%2C%20section%201)%0A%20%20%20%20FEATURES%20%3D%20%5B%22n_boats%22%2C%20%22largest_tonnage%22%2C%20%22longest_boat_m%22%2C%20%22sum_tonnage%22%2C%20%22sum_engine_hp%22%2C%20%22n_species%22%2C%20%22n_gears%22%2C%20%22n_landings%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22n_crew%22%2C%20%22hh_size%22%2C%20%22age_of_operator%22%2C%20%22hh_interview_minutes%22%5D%0A%20%20%20%20HOUSEHOLD%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20a%20household%20is%20its%20region%20and%20its%20id%3A%20ids%20repeat%20across%20regions%0A%20%20%20%20CHUNK_ROWS%20%3D%2050_000%20%20%23%20the%20rows%20scored%20at%20a%20time%2C%20as%20module%209%2C%20section%205%20scores%20the%20large%20table%0A%20%20%20%20SCRATCH%20%3D%20PATHS.root%20%2F%20%22scratch%22%20%20%23%20where%20this%20drill%20writes%20its%20files%3B%20the%20files%20in%20data%2F%20are%20only%20ever%20read%0A%20%20%20%20SCRATCH.mkdir(exist_ok%3DTrue)%0A%0A%20%20%20%20%23%20The%20survey%2C%20clean%20as%20module%202%20teaches%3A%20every%20region's%20rows%2C%20removed%20households%20and%20voided%20rows%20left%20out%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20boats%20%3D%20course.child(%22boat%22)%0A%20%20%20%20catch%20%3D%20course.child(%22catch%22)%0A%20%20%20%20gears%20%3D%20course.child(%22gear%22)%0A%20%20%20%20landings%20%3D%20course.child(%22landing%22)%0A%20%20%20%20crew%20%3D%20course.child(%22crew%22)%0A%20%20%20%20members%20%3D%20course.members()%0A%20%20%20%20households%20%3D%20course.households()%0A%20%20%20%20planted%20%3D%20course.planted_defects()%20%20%23%20every%20defect%20the%20course%20planted%3B%20a%20real%20survey%20has%20no%20such%20list%0A%0A%20%20%20%20%23%20Module%209's%20functions%2C%20copied%20so%20this%20drill%20runs%20on%20its%20own%0A%20%20%20%20def%20unit_ids(table%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%20%20%20%20%22%22%22One%20text%20id%20per%20fishing%20unit%2C%20from%20its%20region%2C%20household%20and%20key%20parts%3A%20%2201-5012-1-1%22.%22%22%22%0A%20%20%20%20%20%20%20%20parts%20%3D%20%5Btable%5B%22region_code%22%5D.astype(str).str.zfill(2)%2C%20*(table%5Bc%5D.astype(int).astype(str)%20for%20c%20in%20UNIT_KEY%5B1%3A%5D)%5D%0A%20%20%20%20%20%20%20%20out%20%3D%20parts%5B0%5D%0A%20%20%20%20%20%20%20%20for%20part%20in%20parts%5B1%3A%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20out%20%3D%20out%20%2B%20%22-%22%20%2B%20part%0A%20%20%20%20%20%20%20%20return%20out.rename(%22unit_id%22)%0A%0A%20%20%20%20def%20boat_features(boats%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20%22%22%22One%20row%20per%20unit%20that%20has%20boats%3A%20n_boats%2C%20largest_tonnage%2C%20longest_boat_m%2C%20sum_tonnage%20and%20sum_engine_hp%2C%20keyed%20on%20UNIT_KEY.%22%22%22%0A%20%20%20%20%20%20%20%20measured%20%3D%20boats%5B%22boat_measured_in%22%5D%20%20%23%201%20%3D%20from%20the%20papers%2C%20no%20length%3B%202%20%3D%20measured%20in%20meters%3B%203%20%3D%20measured%20in%20feet%0A%20%20%20%20%20%20%20%20length_m%20%3D%20np.select(%5Bmeasured%20%3D%3D%202%2C%20measured%20%3D%3D%203%5D%2C%20%5Bboats%5B%22boat_length%22%5D%2C%20boats%5B%22boat_length%22%5D%20%2F%203.281%5D%2C%20default%3Dnp.nan)%0A%20%20%20%20%20%20%20%20return%20(boats%5BUNIT_KEY%5D.assign(gt%3Dboats%5B%22boat_tonnage%22%5D%2C%20length_m%3Dlength_m%2C%20hp%3Dboats%5B%22boat_engine_hp%22%5D.fillna(0))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.groupby(UNIT_KEY%2C%20as_index%3DFalse)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.agg(n_boats%3D(%22gt%22%2C%20%22size%22)%2C%20largest_tonnage%3D(%22gt%22%2C%20%22max%22)%2C%20longest_boat_m%3D(%22length_m%22%2C%20%22max%22)%2C%20sum_tonnage%3D(%22gt%22%2C%20%22sum%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sum_engine_hp%3D(%22hp%22%2C%20%22sum%22)))%0A%0A%20%20%20%20def%20survey_features()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20%22%22%22Module%209's%20features%20table%3A%20one%20row%20per%20fishing%20unit%20of%20the%20clean%20survey%2C%20with%20unit_id%2C%20its%20key%20and%20scale%0A%20%20%20%20%20%20%20%20(unit_scale)%2C%20the%20FEATURES%20and%2C%20beside%20them%2C%20not%20features%2C%20the%20household's%20interviewer%2C%20stratum%20and%20final%20weight.%22%22%22%0A%20%20%20%20%20%20%20%20%23%201.%20The%20units%2C%20each%20once%3A%20module%204%20found%20units%20entered%20twice%0A%20%20%20%20%20%20%20%20out%20%3D%20units%5B%5B*UNIT_KEY%2C%20%22unit_scale%22%5D%5D.drop_duplicates(UNIT_KEY)%0A%20%20%20%20%20%20%20%20%23%202.%20The%20interview%20and%20the%20design%20from%20the%20household%2C%20joined%20on%20the%20household%20(region%20and%20id)%0A%20%20%20%20%20%20%20%20out%20%3D%20out.merge(households%5B%5B*HOUSEHOLD%2C%20%22hh_interview_minutes%22%2C%20%22hh_interviewer%22%2C%20%22sel_stratum%22%2C%20%22sel_final_weight%22%5D%5D%2C%20on%3DHOUSEHOLD%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%20%20%20%20%23%203.%20The%20child%20tables'%20features%3A%20boats%2C%20then%20a%20count%20of%20distinct%20species%2C%20and%20of%20gears%2C%20landing%20sites%20and%20crew%20per%20unit%0A%20%20%20%20%20%20%20%20counts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20boat_features(boats)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20catch.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_species%3D(%22catch_species%22%2C%20%22nunique%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20gears.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_gears%3D(%22gear_type%22%2C%20%22size%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20landings.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_landings%3D(%22land_line%22%2C%20%22size%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20crew.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_crew%3D(%22crew_line%22%2C%20%22size%22))%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20for%20part%20in%20counts%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20out%20%3D%20out.merge(part%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20%20%20%20%20%23%20A%20unit%20with%20no%20row%20in%20a%20child%20table%20has%20none%20of%20it%3A%20a%20missing%20count%20is%20a%20count%20of%200%0A%20%20%20%20%20%20%20%20zero%20%3D%20%5B%22n_boats%22%2C%20%22largest_tonnage%22%2C%20%22sum_tonnage%22%2C%20%22sum_engine_hp%22%2C%20%22n_species%22%2C%20%22n_gears%22%2C%20%22n_landings%22%2C%20%22n_crew%22%5D%0A%20%20%20%20%20%20%20%20out%5Bzero%5D%20%3D%20out%5Bzero%5D.fillna(0)%0A%20%20%20%20%20%20%20%20%23%204.%20The%20household's%20member%20rows%2C%20and%20the%20operator%3A%20the%20member%20whose%20roster%20line%20is%20operator_line%0A%20%20%20%20%20%20%20%20size%20%3D%20members.groupby(HOUSEHOLD%2C%20as_index%3DFalse).agg(hh_size%3D(%22mem_line%22%2C%20%22size%22))%0A%20%20%20%20%20%20%20%20operator%20%3D%20members%5B%5B*HOUSEHOLD%2C%20%22mem_line%22%2C%20%22mem_age%22%5D%5D.rename(columns%3D%7B%22mem_line%22%3A%20%22operator_line%22%2C%20%22mem_age%22%3A%20%22age_of_operator%22%7D)%0A%20%20%20%20%20%20%20%20out%20%3D%20out.merge(size%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22).merge(operator%2C%20on%3D%5B*HOUSEHOLD%2C%20%22operator_line%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%20%20%20%20%23%205.%20A%20length%20for%20every%20unit%3A%200%20with%20no%20boat%3B%20the%20scale's%20median%20where%20no%20boat%20was%20measured%20(sized%20from%20its%20papers)%0A%20%20%20%20%20%20%20%20no_length%20%3D%20out%5B%22longest_boat_m%22%5D.isna()%20%26%20(out%5B%22n_boats%22%5D%20%3E%200)%0A%20%20%20%20%20%20%20%20out%5B%22longest_boat_m%22%5D%20%3D%20out%5B%22longest_boat_m%22%5D.fillna(out.groupby(%22unit_scale%22)%5B%22longest_boat_m%22%5D.transform(%22median%22)).where(out%5B%22n_boats%22%5D%20%3E%200%2C%200.0)%0A%20%20%20%20%20%20%20%20out%5B%22length_imputed%22%5D%20%3D%20no_length%0A%20%20%20%20%20%20%20%20whole%20%3D%20%5B%22n_boats%22%2C%20%22n_species%22%2C%20%22n_gears%22%2C%20%22n_landings%22%2C%20%22n_crew%22%2C%20%22hh_size%22%2C%20%22age_of_operator%22%5D%0A%20%20%20%20%20%20%20%20out%5Bwhole%5D%20%3D%20out%5Bwhole%5D.astype(int)%0A%20%20%20%20%20%20%20%20out.insert(0%2C%20%22unit_id%22%2C%20unit_ids(out))%0A%20%20%20%20%20%20%20%20return%20out.reset_index(drop%3DTrue)%0A%0A%20%20%20%20def%20neighbor_matrix(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20%22%22%22The%20features%20for%20distances%20and%20tails%3A%20the%20twelve%20scaled%20by%20their%20medians%20and%20IQRs%2C%20and%20large_scale%20as%200%20or%201.%22%22%22%0A%20%20%20%20%20%20%20%20scaled%20%3D%20RobustScaler().set_output(transform%3D%22pandas%22).fit_transform(table%5BFEATURES%5D)%0A%20%20%20%20%20%20%20%20return%20scaled.assign(large_scale%3D(table%5B%22unit_scale%22%5D%20%3D%3D%202).astype(int).to_numpy())%0A%0A%20%20%20%20def%20shown_by_features()%20-%3E%20dict%3A%0A%20%20%20%20%20%20%20%20%22%22%22The%20planted%20kinds%20that%20change%20at%20least%20one%20of%20a%20unit's%20features%2C%20and%20which%20feature.%22%22%22%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22age_out_of_range%22%3A%20%22age_of_operator%3A%20the%20head%2C%20who%20runs%20the%20household's%20first%20unit%2C%20is%20over%20110%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22operator_too_young%22%3A%20%22age_of_operator%3A%20the%20operator%20is%20under%2010%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22scale_contradicts_boats%22%3A%20%22largest_tonnage%3A%20a%20boat%20of%205.0%20gross%20tons%20or%20more%20in%20a%20small-scale%20unit%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22no_engine_large_boat%22%3A%20%22largest_tonnage%3A%20an%20outrigger%20boat%20with%20no%20engine%2C%20of%205.0%20gross%20tons%20or%20more%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stored_tonnage_wrong%22%3A%20%22largest_tonnage%2C%20sum_tonnage%3A%20the%20stored%20tonnage%20is%20the%20dimensions%20multiplied%20without%20the%20factor%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22feet_recorded_as_meters%22%3A%20%22largest_tonnage%2C%20longest_boat_m%3A%20feet%20recorded%20as%20meters%2C%20a%20small%20boat%20of%20many%20tons%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22made_up_interview%22%3A%20%22hh_interview_minutes%2C%20age_of_operator%3A%208%20to%2013%20minutes%2C%20ages%20rounded%20to%20fives%2C%20the%20same%20boat%20and%20catch%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22unit_without_catch%22%3A%20%22n_species%3A%20no%20catch%20rows%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22catch_rows_all_astray%22%3A%20%22n_species%3A%20every%20catch%20row%20keyed%20to%20another%20unit%2C%20so%20none%20matched%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20def%20truth_units(features%3A%20pd.DataFrame)%20-%3E%20set%3A%0A%20%20%20%20%20%20%20%20%22%22%22The%20unit_ids%20of%20the%20units%20a%20planted%20defect%20of%20a%20kind%20in%20shown_by_features()%20changed.%20A%20defect%20recorded%20on%20a%20unit's%0A%20%20%20%20%20%20%20%20key%20names%20that%20unit%3B%20one%20recorded%20on%20a%20member%20names%20every%20unit%20of%20the%20household.%22%22%22%0A%20%20%20%20%20%20%20%20kinds%20%3D%20planted%5Bplanted%5B%22kind%22%5D.isin(shown_by_features())%5D%0A%20%20%20%20%20%20%20%20on_unit%20%3D%20kinds%5Bkinds%5B%22operator_line%22%5D.notna()%5D%0A%20%20%20%20%20%20%20%20on_household%20%3D%20kinds%5Bkinds%5B%22operator_line%22%5D.isna()%5D%0A%20%20%20%20%20%20%20%20named%20%3D%20set(unit_ids(on_unit))%0A%20%20%20%20%20%20%20%20in_household%20%3D%20set(features.merge(on_household%5BHOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD)%5B%22unit_id%22%5D)%0A%20%20%20%20%20%20%20%20return%20(named%20%7C%20in_household)%20%26%20set(features%5B%22unit_id%22%5D)%0A%0A%20%20%20%20def%20furthest(ids%3A%20list)%20-%3E%20pd.Series%3A%0A%20%20%20%20%20%20%20%20%22%22%22For%20each%20listed%20small-scale%20unit%2C%20the%20column%20where%20it%20sits%20furthest%20from%20the%20median%2C%20in%20IQRs%20(Drill%20D2.2).%22%22%22%0A%20%20%20%20%20%20%20%20distance%20%3D%20neighbor_matrix(small)%5BFEATURES%5D.abs().set_index(small%5B%22unit_id%22%5D)%0A%20%20%20%20%20%20%20%20return%20distance.idxmax(axis%3D1).loc%5Bids%5D%0A%0A%20%20%20%20features%20%3D%20survey_features()%0A%20%20%20%20truth%20%3D%20truth_units(features)%0A%20%20%20%20small%20%3D%20features%5Bfeatures%5B%22unit_scale%22%5D%20%3D%3D%201%5D.reset_index(drop%3DTrue)%20%20%23%20every%20boat%20under%205%20gross%20tons%2C%20or%20no%20boat%0A%20%20%20%20large%20%3D%20features%5Bfeatures%5B%22unit_scale%22%5D%20%3D%3D%202%5D.reset_index(drop%3DTrue)%20%20%23%20a%20boat%20of%205%20gross%20tons%20or%20more%0A%0A%20%20%20%20%23%20The%20small-scale%20units%20as%20the%20unit%20table%20holds%20them%3A%20the%20ones%20entered%20twice%20have%20their%20copy%20at%20the%20end%20(Drill%20D2.4)%0A%20%20%20%20_twice%20%3D%20units.loc%5Bunits.duplicated(UNIT_KEY)%2C%20UNIT_KEY%5D%0A%20%20%20%20small_as_entered%20%3D%20pd.concat(%5Bsmall%2C%20small.merge(_twice%2C%20on%3DUNIT_KEY)%5D%2C%20ignore_index%3DTrue)%0A%0A%20%20%20%20%23%20The%20small-scale%20units'%20features%20exported%20as%20CSV%2C%20with%20each%20household's%20hh_code%2C%20as%20another%20survey%20team%20could%20send%20them%20(Drill%20D2.5)%0A%20%20%20%20_exported%20%3D%20small.merge(households%5B%5B*HOUSEHOLD%2C%20%22hh_code%22%5D%5D%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%5B%5B%22unit_id%22%2C%20%22hh_code%22%2C%20%22hh_interviewer%22%2C%20*FEATURES%5D%5D%0A%20%20%20%20SMALL_CSV%20%3D%20SCRATCH%20%2F%20%22d2_small_scale_features.csv%22%0A%20%20%20%20_exported.to_csv(SMALL_CSV%2C%20index%3DFalse)%0A%20%20%20%20%23%20The%20same%20units%20and%20answers%2C%20their%20households%20numbered%20in%20the%20opposite%20order%2C%20as%20another%20listing%20could%20number%20them%0A%20%20%20%20RENUMBERED_CSV%20%3D%20SCRATCH%20%2F%20%22d2_small_scale_features_renumbered.csv%22%0A%20%20%20%20_exported.assign(hh_code%3D_exported%5B%22hh_code%22%5D.to_numpy()%5B%3A%3A-1%5D).to_csv(RENUMBERED_CSV%2C%20index%3DFalse)%0A%0A%20%20%20%20%23%20The%20large%20table%20(module%209%2C%20section%205)%3A%20240%2C000%20units%20with%20the%20same%20features%2C%20and%20its%20planted%20defects%0A%20%20%20%20all_regions%20%3D%20pd.read_parquet(PATHS.all_regions)%0A%20%20%20%20all_regions%5B%22unit_id%22%5D%20%3D%20unit_ids(all_regions)%0A%20%20%20%20all_regions_defects%20%3D%20pd.read_csv(PATHS.all_regions_defects)%0A%20%20%20%20all_regions_small%20%3D%20all_regions%5Ball_regions%5B%22unit_scale%22%5D%20%3D%3D%201%5D.reset_index(drop%3DTrue)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Debugging%20drill%3A%20a%20detector%20that%20flags%20the%20wrong%20rows%0A%0A%20%20%20%20Six%20functions%20from%20a%20detection%20pipeline%20on%20the%20survey%2C%20each%20with%20**one**%20mistake%20in%20it%2C%20of%20the%20kinds%20module%209%20warned%20about.%20None%20of%20them%20stops%20with%20an%20error.%20Each%20fits%20its%20detector%20and%20hands%20back%20a%20review%20list%20that%20looks%20like%20any%20other%3A%20the%20right%20type%2C%20about%20the%20right%20length%2C%20ids%20a%20supervisor%20would%20open%20one%20by%20one.%20Each%20list%20is%20wrong%20all%20the%20same%2C%20because%20the%20detector%20read%20a%20column%20it%20should%20not%20have%2C%20read%20the%20right%20columns%20on%20the%20wrong%20scale%2C%20flagged%20the%20wrong%20share%2C%20or%20was%20handed%20the%20wrong%20rows.%20Each%20check%20below%20starts%20**red**%3A%20that%20is%20the%20point.%20For%20each%20function%3A%0A%0A%20%20%20%201.%20**predict**%3A%20read%20the%20task%20and%20the%20function%2C%20and%20write%20down%20what%20you%20expect%20it%20to%20give%3B%0A%20%20%20%202.%20**run**%20it%20with%20its%20**Try%20it**%20cell%2C%20and%20compare%20what%20it%20gives%20with%20what%20you%20predicted%3B%0A%20%20%20%203.%20**trace**%3A%20find%20the%20line%20that%20makes%20the%20difference%2C%20with%20the%20cell%20the%20drill%20gives%20you%20for%20it%3B%0A%20%20%20%204.%20**fix**%20it%2C%20run%20the%20function's%20cell%20again%2C%20and%20watch%20the%20check%20turn%20green%3B%0A%20%20%20%205.%20**say**%20what%20the%20mistake%20was%2C%20in%20one%20sentence%3A%20to%20your%20table%20in%20a%20training%2C%20or%20in%20a%20comment%20above%20your%20fix.%0A%0A%20%20%20%20With%20no%20error%20to%20read%20from%20its%20last%20line%2C%20the%20tools%20are%20the%20ones%20module%209%20judged%20detectors%20with%3A%20count%20what%20the%20list%20holds%2C%20set%20it%20beside%20what%20it%20should%20hold%2C%20and%20change%20one%20thing%20at%20a%20time.%20Every%20detector%20here%20that%20draws%20at%20random%20fixes%20its%20draws%20with%20%60random_state%3D0%60%2C%20and%20LOF%20and%20ECOD%20draw%20nothing%2C%20so%20a%20list%20that%20changes%20changed%20for%20a%20reason.%20The%20hints%20say%20what%20you%20see%2C%20never%20what%20to%20change.%20Everything%20you%20need%20was%20taught%20in%20module%209%2C%20and%20in%20modules%202%20and%204%20before%20it%3B%20each%20task%20links%20to%20the%20section.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20A%20detector%20that%20flags%20the%20wrong%20rows%3A%20six%20bugs%20to%20find%20and%20fix%0A%0A%20%20%20%20The%20setup%20cell%20at%20the%20top%20has%20built%20the%20survey's%20features%20the%20way%20module%209%20does%2C%20and%20kept%20a%20few%20tables%20and%20files%20for%20the%20drill%3A%0A%0A%20%20%20%20%7C%20name%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60features%60%20%7C%20the%20survey's%20fishing%20units%2C%20one%20row%20each%3A%20%60unit_id%60%2C%20the%20key%2C%20the%20scale%20%60unit_scale%60%2C%20the%20twelve%20%60FEATURES%60%2C%20and%20beside%20them%2C%20not%20features%2C%20the%20household's%20%60hh_interviewer%60%2C%20%60sel_stratum%60%20and%20final%20weight%20%60sel_final_weight%60%20%7C%0A%20%20%20%20%7C%20%60small%60%2C%20%60large%60%20%7C%20%60features%60%20split%20by%20scale%3A%20every%20boat%20under%205%20gross%20tons%20or%20no%20boat%20(%60unit_scale%60%201)%2C%20and%20a%20boat%20of%205%20gross%20tons%20or%20more%20(%60unit_scale%60%202)%20%7C%0A%20%20%20%20%7C%20%60truth%60%20%7C%20the%20%60unit_id%60s%20of%20the%20units%20carrying%20a%20planted%20defect%20their%20features%20can%20show%2C%20module%209's%20truth%20%7C%0A%20%20%20%20%7C%20%60small_as_entered%60%20%7C%20%60small%60%20as%20the%20unit%20table%20holds%20it%3A%20the%20units%20entered%20twice%20are%20there%20twice%20%7C%0A%20%20%20%20%7C%20%60SMALL_CSV%60%2C%20%60RENUMBERED_CSV%60%20%7C%20the%20small-scale%20units'%20features%20exported%20as%20CSV%20files%20into%20%60scratch%2F%60%2C%20the%20second%20with%20the%20households%20numbered%20another%20way%20%7C%0A%20%20%20%20%7C%20%60all_regions%60%2C%20%60all_regions_small%60%2C%20%60all_regions_defects%60%20%7C%20module%209's%20large%20table%20of%20240%2C000%20units%2C%20its%20small-scale%20units%2C%20and%20its%20planted%20defects%20%7C%0A%20%20%20%20%7C%20%60neighbor_matrix(table)%60%20%7C%20module%209's%20matrix%20for%20distances%20and%20tails%3A%20the%20twelve%20features%20scaled%20by%20their%20medians%20and%20IQRs%20%7C%0A%20%20%20%20%7C%20%60furthest(ids)%60%20%7C%20for%20each%20listed%20small-scale%20unit%2C%20the%20column%20where%20it%20sits%20furthest%20from%20the%20median%2C%20in%20IQRs%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D2.1%20%C2%B7%20A%20weight%20read%20as%20an%20answer%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20detector%20flags%20what%20is%20rare%20in%20the%20columns%20it%20reads.%20A%20household's%20weight%20says%20how%20it%20was%20sampled%2C%20not%20how%20it%20fishes.%20Read%20as%20an%20answer%2C%20it%20sends%20the%20supervisor%20to%20households%20for%20the%20stratum%20they%20were%20drawn%20from.%0A%0A%20%20%20%20%60forest_review(table%2C%20k)%60%20should%20give%20the%20%60k%60%20units%20an%20Isolation%20Forest%20(200%20trees%2C%20%60random_state%3D0%60)%20finds%20most%20unusual%20in%20what%20they%20answered%3A%20the%20%60k%60%20lowest%20%60score_samples%60%20on%20the%20twelve%20%60FEATURES%60%2C%20as%20their%20%60unit_id%60s%2C%20sorted.%20Its%20author%20added%20the%20weight%2C%20reasoning%20that%20a%20household%20standing%20for%20sixteen%20units%20matters%20more%20than%20one%20standing%20for%20one.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20units%20and%20%60k%60%20%E2%86%92%20the%20%60unit_id%60s%20of%20the%20%60k%60%20most%20unusual%2C%20sorted.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60large.set_index(%22unit_id%22).loc%5Bforest_review(large%2C%2020)%2C%20%22sel_stratum%22%5D.value_counts().to_dict()%60%20%7C%20%60%7B'F'%3A%2014%2C%20'C'%3A%204%2C%20'B'%3A%202%7D%60%3A%20the%20area%20strata%20hold%2037%25%20of%20the%20large-scale%20units%2C%20and%206%20of%20the%2020%20on%20the%20list%20%7C%0A%20%20%20%20%7C%20%60forest_review(drill_strata()%2C%203)%60%20%7C%20%60%5B'T03'%2C%20'T06'%2C%20'T10'%5D%60%3A%20%60T06%60%2C%20the%20one%20unit%20with%20unusual%20answers%2C%20and%20two%20others%2C%20none%20from%20stratum%20A%20%7C%0A%0A%20%20%20%20Tools%3A%20what%20to%20leave%20out%20(%5Bmodule%209%2C%20section%201%5D(%2F%3Ffile%3D80_anomalies_ml.py)).%0A%0A%20%20%20%20**Predict.**%20The%20large-scale%20units%20of%20stratum%20F%20weigh%20about%201%2C%20and%20those%20of%20the%20area%20strata%20about%203%2C%205%20or%2016.%20Given%20that%20column%2C%20which%20units%20will%20the%20forest%20find%20easiest%20to%20isolate%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20forest_review(table%3A%20pd.DataFrame%2C%20k%3A%20int)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20unit_ids%20of%20the%20k%20units%20an%20Isolation%20Forest%20finds%20most%20unusual%20in%20their%20answers%2C%20sorted.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.1%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20X%20%3D%20table%5B%5B*FEATURES%2C%20%22sel_final_weight%22%5D%5D%20%20%23%20the%20answers%2C%20and%20the%20weight%3A%20how%20many%20units%20each%20one%20stands%20for%0A%20%20%20%20scores%20%3D%20IsolationForest(n_estimators%3D200%2C%20random_state%3D0).fit(X).score_samples(X)%0A%20%20%20%20return%20sorted(table%5B%22unit_id%22%5D.iloc%5Bnp.argsort(scores%2C%20kind%3D%22stable%22)%5B%3Ak%5D%5D)%0A%0A%0A%40app.function%0Adef%20drill_units(n%3A%20int%2C%20seed%3A%20int)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22n%20ordinary%20small-scale%20units%2C%20T01%20to%20Tn%2C%20all%20in%20stratum%20C%20(weights%20about%2016)%2C%20drawn%20with%20this%20seed.%20The%20drills'%0A%20%20%20%20small%20tables%20start%20from%20it%20and%20change%20a%20few%20rows.%22%22%22%0A%20%20%20%20rng%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20gt%20%3D%20rng.uniform(0.3%2C%201.5%2C%20n).round(2)%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22unit_id%22%3A%20%5Bf%22T%7Bi%3A02d%7D%22%20for%20i%20in%20range(1%2C%20n%20%2B%201)%5D%2C%20%22unit_scale%22%3A%201%2C%20%22sel_stratum%22%3A%20%22C%22%2C%20%22sel_final_weight%22%3A%20rng.uniform(14.5%2C%2018%2C%20n).round(2)%2C%0A%20%20%20%20%20%20%20%20%22n_boats%22%3A%201%2C%20%22largest_tonnage%22%3A%20gt%2C%20%22longest_boat_m%22%3A%20(gt%20*%204%20%2B%203).round(1)%2C%20%22sum_tonnage%22%3A%20gt%2C%20%22sum_engine_hp%22%3A%20rng.integers(0%2C%203%2C%20n)%20*%208.0%2C%0A%20%20%20%20%20%20%20%20%22n_species%22%3A%20rng.integers(3%2C%207%2C%20n)%2C%20%22n_gears%22%3A%20rng.integers(1%2C%203%2C%20n)%2C%20%22n_landings%22%3A%20rng.integers(1%2C%203%2C%20n)%2C%20%22n_crew%22%3A%20rng.integers(0%2C%203%2C%20n)%2C%0A%20%20%20%20%20%20%20%20%22hh_size%22%3A%20rng.integers(2%2C%208%2C%20n)%2C%20%22age_of_operator%22%3A%20rng.integers(30%2C%2061%2C%20n)%2C%20%22hh_interview_minutes%22%3A%20rng.integers(35%2C%2061%2C%20n)%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20drill_strata()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Forty%20units%3A%20T37%20to%20T40%20in%20stratum%20A%20(weight%202.8)%2C%20the%20rest%20in%20stratum%20C%2C%20and%20T06%20with%20nine%20species%2C%20three%20gears%0A%20%20%20%20and%20a%20boat%20of%202.9%20tons.%20Used%20by%20the%20check%20of%20Drill%20D2.1.%22%22%22%0A%20%20%20%20table%20%3D%20drill_units(40%2C%201)%0A%20%20%20%20table.loc%5B36%3A%2C%20%5B%22sel_stratum%22%2C%20%22sel_final_weight%22%5D%5D%20%3D%20%5B%22A%22%2C%202.8%5D%0A%20%20%20%20table.loc%5B5%2C%20%5B%22n_species%22%2C%20%22n_gears%22%2C%20%22largest_tonnage%22%2C%20%22sum_tonnage%22%5D%5D%20%3D%20%5B9%2C%203%2C%202.9%2C%202.9%5D%0A%20%20%20%20return%20table%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Drill%20D2.1%22%2C%20forest_review%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20large-scale%20units%2C%20k%20%3D%2020%22%2C%20(large%2C%2020))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20large-scale%20units%2C%20k%20%3D%2040%22%2C%20(large%2C%2040))%2C%0A%20%20%20%20%20%20%20%20%20(%22forty%20units%2C%20four%20of%20them%20in%20stratum%20A%2C%20one%20with%20unusual%20answers%2C%20k%20%3D%203%22%2C%20(drill_strata()%2C%203))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20ids%3A%20sorted(map(str%2C%20ids))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20the%20strata%20of%20the%20units%20on%20the%20list%2C%20and%20compare%20with%20the%20strata%20of%20the%20table.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20twenty%20most%20unusual%20large-scale%20units%0A%20%20%20%20try_it(forest_review%2C%20large%2C%2020)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Trace.**%20The%20list%20should%20look%20like%20the%20table%20it%20came%20from%2C%20unless%20the%20answers%20say%20otherwise.%20Set%20the%20strata%20of%20the%20twenty%20listed%20units%20beside%20the%20strata%20of%20all%20large-scale%20units%2C%20with%20each%20stratum's%20median%20weight%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Trace%3A%20the%20large-scale%20units%20by%20stratum%2C%20their%20median%20weight%2C%20and%20how%20many%20of%20them%20the%20function%20lists%0A%20%20%20%20_listed%20%3D%20large%5B%22unit_id%22%5D.isin(forest_review(large%2C%2020))%0A%20%20%20%20large.assign(listed%3D_listed).groupby(%22sel_stratum%22).agg(%0A%20%20%20%20%20%20%20%20units%3D(%22unit_id%22%2C%20%22size%22)%2C%20median_weight%3D(%22sel_final_weight%22%2C%20%22median%22)%2C%20listed%3D(%22listed%22%2C%20%22sum%22)).round(2)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20area%20strata%20hold%20over%20a%20third%20of%20the%20large-scale%20units%2C%20and%20their%20weights%20sit%20near%203%2C%205%20and%2016%2C%20far%20from%20stratum%20F's%20near%201.%20A%20forest%20cuts%20a%20column%20at%20a%20random%20value%20between%20its%20smallest%20and%20largest.%20Any%20cut%20on%20the%20weight%20that%20falls%20between%20stratum%20F%20and%20an%20area%20stratum%20splits%20that%20stratum's%20households%20off%20from%20the%20rest%2C%20so%20they%2C%20the%20smaller%20groups%2C%20need%20fewer%20cuts%20to%20stand%20alone.%20They%20look%20rare%20because%20of%20how%20they%20were%20drawn.%20Module%209%2C%20section%201%20ran%20the%20same%20comparison%20and%20left%20the%20weight%20out%3B%20module%208%20checks%20weights%20against%20their%20own%20stratum.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20area%20strata%20are%20more%20than%20half%20of%20the%20list%20%7C%20The%20weight%20is%20one%20of%20the%20columns%20the%20forest%20reads.%20%7C%20the%20columns%20in%20%60X%60%20%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20a%20column%20%7C%20A%20column%20was%20dropped%20by%20name%2C%20and%20the%20small%20table%20has%20fewer%20columns%20than%20%60large%60.%20%7C%20name%20the%20features%20instead%20%7C%0A%20%20%20%20%7C%20A%20few%20ids%20differ%2C%20the%20same%20each%20run%20%7C%20The%20columns%20are%20in%20another%20order%3A%20the%20forest%20picks%20a%20column%20by%20its%20position.%20%7C%20%60FEATURES%60%20in%20its%20own%20order%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D2.2%20%C2%B7%20Distances%20on%20unscaled%20features%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20LOF%20finds%20rows%20whose%20neighborhood%20is%20sparse%2C%20and%20a%20neighborhood%20is%20measured%20in%20distances.%20Unscaled%2C%20a%20column%20measured%20in%20tens%20of%20units%20outweighs%20one%20measured%20in%20ones%2C%20and%20the%20detector%20reads%20only%20the%20wide%20columns.%0A%0A%20%20%20%20%60lof_review(table%2C%20k)%60%20should%20give%20the%20%60k%60%20units%20with%20the%20lowest%20LOF%20factor%20(%60negative_outlier_factor_%60%2C%2035%20neighbors)%20on%20the%20features%20scaled%20by%20their%20medians%20and%20IQRs%2C%20which%20is%20%60neighbor_matrix(table)%60%3A%20their%20%60unit_id%60s%2C%20sorted.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20units%20and%20%60k%60%20%E2%86%92%20the%20%60unit_id%60s%20of%20the%20%60k%60%20most%20unusual%2C%20sorted.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60lof_review(drill_no_catch()%2C%203)%60%20%7C%20%60%5B'T08'%2C%20'T10'%2C%20'T67'%5D%60%3A%20%60T08%60%20landed%20no%20catch%20at%20all%20%7C%0A%20%20%20%20%7C%20%60int(furthest(lof_review(small%2C%20117)).isin(%5B%22age_of_operator%22%2C%20%22hh_interview_minutes%22%5D).sum())%60%20%7C%20%6023%60%3A%20of%20the%20117%20listed%2C%2023%20sit%20furthest%20out%20in%20the%20operator's%20age%20or%20the%20interview's%20length%2C%20and%20the%20rest%20in%20the%20other%20ten%20columns%20%7C%0A%0A%20%20%20%20Tools%3A%20scaling%20(%5Bmodule%209%2C%20section%201%5D(%2F%3Ffile%3D80_anomalies_ml.py))%2C%20LOF%20(%5Bmodule%209%2C%20section%202%5D(%2F%3Ffile%3D80_anomalies_ml.py)).%0A%0A%20%20%20%20**Predict.**%20In%20%60drill_no_catch()%60%2C%20the%20operators'%20ages%20run%20from%2018%20to%2079%20and%20the%20interviews%20from%2015%20to%2099%20minutes%3B%20every%20unit%20lands%203%20to%206%20species%20except%20%60T08%60%2C%20which%20lands%20none.%20Which%20of%20these%20differences%20decides%20who%20is%20far%20from%20whom%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20lof_review(table%3A%20pd.DataFrame%2C%20k%3A%20int)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20unit_ids%20of%20the%20k%20units%20with%20the%20lowest%20LOF%20factor%20(35%20neighbors)%2C%20on%20the%20features%20scaled%2C%20sorted.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.2%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20X%20%3D%20table%5BFEATURES%5D%20%20%23%20the%20twelve%20answers%2C%20as%20the%20Isolation%20Forest%20reads%20them%0A%20%20%20%20factors%20%3D%20LocalOutlierFactor(n_neighbors%3D35).fit(X).negative_outlier_factor_%0A%20%20%20%20return%20sorted(table%5B%22unit_id%22%5D.iloc%5Bnp.argsort(factors%2C%20kind%3D%22stable%22)%5B%3Ak%5D%5D)%0A%0A%0A%40app.function%0Adef%20drill_no_catch()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Eighty%20units%20whose%20operators%20are%2018%20to%2079%20and%20whose%20interviews%20took%2015%20to%2099%20minutes%2C%20and%20T08%2C%20which%20landed%20no%0A%20%20%20%20species.%20Used%20by%20the%20check%20of%20Drill%20D2.2.%22%22%22%0A%20%20%20%20table%20%3D%20drill_units(80%2C%203)%0A%20%20%20%20rng%20%3D%20np.random.default_rng(13)%0A%20%20%20%20table%5B%22age_of_operator%22%5D%20%3D%20rng.integers(18%2C%2080%2C%2080)%0A%20%20%20%20table%5B%22hh_interview_minutes%22%5D%20%3D%20rng.integers(15%2C%20100%2C%2080)%0A%20%20%20%20table.loc%5B7%2C%20%22n_species%22%5D%20%3D%200%0A%20%20%20%20return%20table%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Drill%20D2.2%22%2C%20lof_review%2C%0A%20%20%20%20%20%20%20%20%5B(%22eighty%20units%2C%20one%20with%20no%20catch%2C%20k%20%3D%203%22%2C%20(drill_no_catch()%2C%203))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20small-scale%20units%2C%20k%20%3D%20117%22%2C%20(small%2C%20117))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20large-scale%20units%2C%20k%20%3D%2020%22%2C%20(large%2C%2020))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20ids%3A%20sorted(map(str%2C%20ids))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22For%20each%20unit%20on%20the%20list%2C%20find%20the%20column%20where%20it%20is%20furthest%20from%20the%20median%2C%20in%20IQRs.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20three%20most%20unusual%20of%20the%20eighty%20units%3B%20T08%20landed%20no%20catch%0A%20%20%20%20try_it(lof_review%2C%20drill_no_catch()%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Trace.**%20A%20distance%20is%20a%20sum%20over%20columns%2C%20so%20look%20at%20how%20wide%20each%20column%20is%2C%20and%20at%20the%20column%20that%20puts%20each%20listed%20unit%20furthest%20from%20the%20median.%20The%20table%20below%20counts%20that%20column%20for%20the%20117%20units%20the%20function%20lists%20on%20the%20survey%2C%20beside%20the%20117%20that%20LOF%20lists%20on%20the%20scaled%20features%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Trace%3A%20each%20column's%20spread%2C%20and%20for%20each%20listed%20unit%20the%20column%20where%20it%20sits%20furthest%20from%20the%20median%2C%20in%20IQRs%0A%20%20%20%20_spread%20%3D%20(small%5BFEATURES%5D.quantile(0.75)%20-%20small%5BFEATURES%5D.quantile(0.25)).rename(%22IQR%22)%0A%0A%20%20%20%20def%20_count(ids%3A%20list)%20-%3E%20pd.Series%3A%0A%20%20%20%20%20%20%20%20return%20furthest(ids).value_counts()%0A%0A%20%20%20%20_raw%20%3D%20LocalOutlierFactor(n_neighbors%3D35).fit(small%5BFEATURES%5D).negative_outlier_factor_%0A%20%20%20%20_scaled%20%3D%20LocalOutlierFactor(n_neighbors%3D35).fit(neighbor_matrix(small)).negative_outlier_factor_%0A%20%20%20%20_lists%20%3D%20%7B%22the%20function's%20list%22%3A%20course.attempt(lof_review%2C%20small%2C%20117)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22unscaled%22%3A%20small%5B%22unit_id%22%5D.iloc%5Bnp.argsort(_raw%2C%20kind%3D%22stable%22)%5B%3A117%5D%5D.tolist()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22scaled%22%3A%20small%5B%22unit_id%22%5D.iloc%5Bnp.argsort(_scaled%2C%20kind%3D%22stable%22)%5B%3A117%5D%5D.tolist()%7D%0A%20%20%20%20_table%20%3D%20pd.DataFrame(%7B%22IQR%22%3A%20_spread.round(2)%2C%20**%7B_name%3A%20_count(_ids)%20for%20_name%2C%20_ids%20in%20_lists.items()%20if%20isinstance(_ids%2C%20list)%7D%7D).fillna(0)%0A%20%20%20%20_found%20%3D%20%7B_name%3A%20len(set(_ids)%20%26%20truth)%20for%20_name%2C%20_ids%20in%20_lists.items()%20if%20isinstance(_ids%2C%20list)%7D%0A%20%20%20%20mo.vstack(%5B_table.astype(%7Bc%3A%20int%20for%20c%20in%20_table.columns%20if%20c%20!%3D%20%22IQR%22%7D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22Planted%20defects%20on%20each%20list%3A%20%22%20%2B%20%22%2C%20%22.join(f%22%7B_name%7D%20**%7B_n%7D**%22%20for%20_name%2C%20_n%20in%20_found.items())%20%2B%20%22.%22)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Unscaled%2C%20about%20half%20the%20listed%20units%20are%20there%20for%20the%20operator's%20age%20or%20the%20interview's%20length%2C%20two%20of%20the%20widest%20columns%20in%20the%20IQR%20column%3B%20scaled%2C%20the%20list%20draws%20on%20all%20twelve%20columns%20more%20evenly.%20Both%20lists%20hold%20the%20same%20number%20of%20planted%20defects%20on%20this%20survey%2C%20and%20that%20is%20the%20trap%20in%20reading%20one%20figure%3A%20many%20planted%20defects%20here%20are%20short%20interviews%20and%20impossible%20ages%2C%20so%20a%20detector%20of%20the%20wide%20columns%20does%20well%20on%20them%20and%20is%20blind%20to%20a%20defect%20in%20a%20narrow%20column%2C%20as%20%60T08%60%20showed.%20Module%209%2C%20section%201%20scales%20before%20any%20distance%3B%20its%20Exercise%209.1%20wrote%20the%20scaler%20by%20hand.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60T08%60%20missing%20from%20the%20small%20table's%20list%20%7C%20The%20features%20are%20not%20scaled%3A%20age%20and%20minutes%20decide%20every%20distance.%20%7C%20%60neighbor_matrix(table)%60%20%7C%0A%20%20%20%20%7C%20Close%2C%20but%20a%20few%20ids%20differ%20%7C%20%60StandardScaler%60%20(mean%20and%20standard%20deviation)%20in%20place%20of%20median%20and%20IQR.%20%7C%20%60neighbor_matrix%60%2C%20which%20uses%20%60RobustScaler%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20...%20has%20no%20attribute%20'negative_outlier_factor_'*%20%7C%20%60.fit%60%20was%20replaced%20by%20%60.fit_predict%60%20and%20its%20result%20used%20as%20the%20detector.%20%7C%20keep%20%60.fit(X)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D2.3%20%C2%B7%20Contamination%20from%20another%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20contamination%20is%20the%20share%20of%20rows%20a%20detector%20flags.%20Set%20to%20the%20share%20found%20in%20another%20table%2C%20it%20flags%20as%20many%20rows%20as%20that%20table%20had%2C%20and%20the%20review%20either%20misses%20most%20of%20this%20table's%20anomalies%20or%20wastes%20its%20afternoon.%0A%0A%20%20%20%20%60flags_at_rate(table%2C%20truth)%60%20should%20give%20the%20%60unit_id%60s%20an%20Isolation%20Forest%20(200%20trees%2C%20%60random_state%3D0%60)%20flags%20on%20the%20twelve%20%60FEATURES%60%2C%20sorted%2C%20with%20%60contamination%60%20set%20to%20**this%20table's**%20anomaly%20rate%3A%20the%20share%20of%20its%20units%20whose%20%60unit_id%60%20is%20in%20%60truth%60.%20The%20course%20knows%20that%20share%20for%20the%20survey%20from%20its%20planted%20list%3B%20a%20survey%20team%20would%20take%20it%20from%20a%20reinterview%20sample%20or%20last%20round's%20audit%20of%20the%20same%20kind%20of%20table.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20units%20and%20a%20set%20of%20%60unit_id%60s%20known%20to%20be%20wrong%20%E2%86%92%20the%20%60unit_id%60s%20flagged%2C%20sorted.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60round(float(small%5B%22unit_id%22%5D.isin(truth).mean())%2C%204)%60%20%7C%20%600.0244%60%3A%20the%20small-scale%20units'%20anomaly%20rate%20%7C%0A%20%20%20%20%7C%20%60len(flags_at_rate(small%2C%20truth))%60%20%7C%20%6057%60%20of%202%2C335%3A%202.44%25%20of%20them%20%7C%0A%0A%20%20%20%20Tools%3A%20contamination%20(%5Bmodule%209%2C%20section%202%5D(%2F%3Ffile%3D80_anomalies_ml.py)).%0A%0A%20%20%20%20**Predict.**%20How%20many%20of%20the%202%2C335%20small-scale%20units%20will%20the%20function%20flag%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20flags_at_rate(table%3A%20pd.DataFrame%2C%20truth%3A%20set)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20unit_ids%20an%20Isolation%20Forest%20flags%2C%20contamination%20set%20to%20the%20table's%20own%20anomaly%20rate%2C%20sorted.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.3%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20contamination%20%3D%20len(all_regions_defects)%20%2F%20len(all_regions)%20%20%23%20the%20anomaly%20rate%3A%20anomalies%20over%20units%0A%20%20%20%20forest%20%3D%20IsolationForest(n_estimators%3D200%2C%20contamination%3Dcontamination%2C%20random_state%3D0).fit(table%5BFEATURES%5D)%0A%20%20%20%20return%20sorted(table%5B%22unit_id%22%5D%5Bforest.predict(table%5BFEATURES%5D)%20%3D%3D%20-1%5D)%0A%0A%0A%40app.function%0Adef%20drill_rate()%20-%3E%20tuple%5Bpd.DataFrame%2C%20set%5D%3A%0A%20%20%20%20%22%22%22Forty%20units%2C%20three%20of%20them%20(T04%2C%20T18%2C%20T30)%20with%20a%20boat%20of%202.95%20tons%20and%20an%20interview%20of%2011%20to%2013%20minutes%2C%20and%0A%20%20%20%20the%20set%20of%20those%20three%3A%20the%20table's%20anomalies.%20Used%20by%20the%20check%20of%20Drill%20D2.3.%22%22%22%0A%20%20%20%20table%20%3D%20drill_units(40%2C%203)%0A%20%20%20%20table.loc%5B%5B3%2C%2017%2C%2029%5D%2C%20%5B%22largest_tonnage%22%2C%20%22sum_tonnage%22%2C%20%22hh_interview_minutes%22%5D%5D%20%3D%20%5B%5B2.95%2C%202.95%2C%2012%5D%2C%20%5B2.95%2C%202.95%2C%2011%5D%2C%20%5B2.95%2C%202.95%2C%2013%5D%5D%0A%20%20%20%20return%20table%2C%20%7B%22T04%22%2C%20%22T18%22%2C%20%22T30%22%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Drill%20D2.3%22%2C%20flags_at_rate%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small-scale%20units%20and%20the%20survey's%20truth%22%2C%20(small%2C%20truth))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20large-scale%20units%20and%20the%20survey's%20truth%22%2C%20(large%2C%20truth))%2C%0A%20%20%20%20%20%20%20%20%20(%22forty%20units%2C%20three%20of%20them%20known%20anomalies%22%2C%20drill_rate())%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20ids%3A%20sorted(map(str%2C%20ids))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20the%20flags%2C%20and%20divide%20by%20the%20units%20in%20the%20table%3A%20compare%20with%20the%20share%20of%20them%20in%20truth.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20flag%20the%20small-scale%20units%20at%20their%20own%20anomaly%20rate%2C%20and%20count%20the%20flags%0A%20%20%20%20try_it(flags_at_rate%2C%20small%2C%20truth)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Trace.**%20The%20forest%20flags%20the%20share%20it%20is%20given%2C%20so%20compare%20the%20share%20the%20function%20gave%20it%20with%20each%20table's%20own%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Trace%3A%20the%20large%20table's%20anomaly%20rate%2C%20and%20each%20survey%20table's%2C%20with%20the%20flags%20each%20gives%0A%20%20%20%20_rows%20%3D%20%5B%7B%22table%22%3A%20%22all_regions%22%2C%20%22units%22%3A%20len(all_regions)%2C%20%22anomalies%22%3A%20len(all_regions_defects)%7D%5D%0A%20%20%20%20for%20_name%2C%20_table%20in%20%5B(%22small%22%2C%20small)%2C%20(%22large%22%2C%20large)%5D%3A%0A%20%20%20%20%20%20%20%20_rows.append(%7B%22table%22%3A%20_name%2C%20%22units%22%3A%20len(_table)%2C%20%22anomalies%22%3A%20int(_table%5B%22unit_id%22%5D.isin(truth).sum())%7D)%0A%20%20%20%20_rates%20%3D%20pd.DataFrame(_rows)%0A%20%20%20%20_rates%5B%22rate%22%5D%20%3D%20(_rates%5B%22anomalies%22%5D%20%2F%20_rates%5B%22units%22%5D).round(4)%0A%20%20%20%20_flagged%20%3D%20%7B_name%3A%20course.attempt(flags_at_rate%2C%20_table%2C%20truth)%20for%20_name%2C%20_table%20in%20%5B(%22small%22%2C%20small)%2C%20(%22large%22%2C%20large)%5D%7D%0A%20%20%20%20_rates%5B%22the%20function%20flags%22%5D%20%3D%20_rates%5B%22table%22%5D.map(%7B_name%3A%20len(_ids)%20for%20_name%2C%20_ids%20in%20_flagged.items()%20if%20isinstance(_ids%2C%20list)%7D)%0A%20%20%20%20_rates%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20function%20flagged%20the%20survey%20at%20the%20large%20table's%20rate.%20That%20table%20was%20planted%20with%20its%20own%20mix%20of%20anomalies%2C%20in%20its%20own%20share%2C%20and%20so%20is%20every%20round%20and%20every%20table%20a%20survey%20team%20scores%3A%20a%20rate%20travels%20only%20between%20tables%20of%20the%20same%20kind.%20Module%209%2C%20section%202%20sets%20the%20cut%20and%20shows%20that%20it%20moves%20no%20score.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2011%20small-scale%20units%20flagged%2C%201%20large-scale%20%7C%20The%20large%20table's%20rate%2C%200.47%25%2C%20used%20for%20the%20survey.%20%7C%20where%20%60contamination%60%20comes%20from%20%7C%0A%20%20%20%20%7C%20The%20small-scale%20count%20right%2C%20the%20large-scale%20one%20wrong%20%7C%20One%20rate%20computed%20on%20all%20of%20%60features%60%20and%20used%20for%20both%20tables.%20%7C%20the%20rate%20of%20%60table%60%2C%20the%20argument%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20'contamination'%20parameter%20...%20must%20be%20...*%20%7C%20A%20percentage%2C%20%602.44%60%2C%20where%20a%20share%2C%20%600.0244%60%2C%20belongs%3B%20or%20a%20rate%20of%200%20from%20ids%20that%20never%20match.%20%7C%20%60.mean()%60%20of%20%60isin(truth)%60%20%7C%0A%20%20%20%20%7C%20About%20two-fifths%20of%20the%20large-scale%20units%20flagged%20%7C%20%60len(truth)%20%2F%20len(table)%60%3A%20%60truth%60%20holds%20the%20anomalies%20of%20both%20scales%2C%20so%20its%20length%20is%20not%20this%20table's%20count.%20%7C%20the%20share%20of%20%60table%60's%20own%20%60unit_id%60s%20that%20are%20in%20%60truth%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D2.4%20%C2%B7%20Duplicates%20fitted%20twice%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20unit%20entered%20twice%20is%20one%20unit.%20Fitted%20twice%2C%20its%20two%20copies%20cannot%20be%20cut%20apart%2C%20so%20it%20looks%20more%20ordinary%20than%20it%20is%3B%20listed%20twice%2C%20it%20takes%20two%20places%20on%20the%20review%20list.%0A%0A%20%20%20%20Module%204%20found%20units%20entered%20twice%2C%20and%20module%209%20built%20its%20features%20from%20each%20unit%20once.%20%60small_as_entered%60%20holds%20the%20small-scale%20units%20as%20the%20unit%20table%20does%2C%20with%20the%20copies.%20%60flags_once(table%2C%20contamination)%60%20should%20fit%20an%20Isolation%20Forest%20(200%20trees%2C%20%60random_state%3D0%60)%20on%20the%20twelve%20%60FEATURES%60%20of%20each%20unit%20**once**%2C%20and%20give%20the%20%60unit_id%60s%20it%20flags%2C%20sorted%2C%20each%20once.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20units%2C%20perhaps%20with%20some%20entered%20twice%2C%20and%20a%20contamination%20%E2%86%92%20the%20%60unit_id%60s%20flagged%2C%20sorted%2C%20each%20once.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60len(flags_once(small_as_entered%2C%200.05))%60%20%7C%20%60117%60%2C%20each%20unit%20once%20%7C%0A%20%20%20%20%7C%20%60flags_once(small_as_entered%2C%200.05)%20%3D%3D%20flags_once(small%2C%200.05)%60%20%7C%20%60True%60%3A%20a%20copy%20changes%20nothing%20%7C%0A%20%20%20%20%7C%20%60flags_once(drill_twice()%2C%200.05)%60%20%7C%20%60%5B'T11'%2C%20'T21'%5D%60%3A%20two%20units%20with%20unusual%20answers%20%7C%0A%0A%20%20%20%20Tools%3A%20duplicates%20by%20key%20(%5Bmodule%204%2C%20section%205%5D(%2F%3Ffile%3D30_edit_rules.py))%2C%20each%20unit%20once%20(%5Bmodule%209%2C%20section%201%5D(%2F%3Ffile%3D80_anomalies_ml.py)).%0A%0A%20%20%20%20**Predict.**%20%60drill_twice()%60%20has%2041%20rows%3A%20forty%20units%20and%20a%20copy%20of%20%60T11%60.%20At%20a%20contamination%20of%200.05%2C%20how%20many%20rows%20will%20the%20forest%20flag%2C%20and%20which%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20flags_once(table%3A%20pd.DataFrame%2C%20contamination%3A%20float)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20unit_ids%20an%20Isolation%20Forest%20flags%2C%20fitted%20on%20each%20unit%20once%2C%20sorted%2C%20each%20once.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.4%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20rows%20%3D%20table%20%20%23%20one%20row%20per%20unit%0A%20%20%20%20forest%20%3D%20IsolationForest(n_estimators%3D200%2C%20contamination%3Dcontamination%2C%20random_state%3D0).fit(rows%5BFEATURES%5D)%0A%20%20%20%20return%20sorted(rows%5B%22unit_id%22%5D%5Bforest.predict(rows%5BFEATURES%5D)%20%3D%3D%20-1%5D)%0A%0A%0A%40app.function%0Adef%20drill_twice()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Forty%20units%2C%20T11%20with%20a%20boat%20of%202.9%20tons%20and%20nine%20species%20and%20T21%20with%20an%20operator%20of%2016%20and%20a%2012-minute%0A%20%20%20%20interview%2C%20and%20a%20copy%20of%20T11%20at%20the%20end.%20Used%20by%20the%20check%20of%20Drill%20D2.4.%22%22%22%0A%20%20%20%20table%20%3D%20drill_units(40%2C%204)%0A%20%20%20%20table.loc%5B10%2C%20%5B%22largest_tonnage%22%2C%20%22sum_tonnage%22%2C%20%22longest_boat_m%22%2C%20%22n_species%22%5D%5D%20%3D%20%5B2.9%2C%202.9%2C%2014.0%2C%209%5D%0A%20%20%20%20table.loc%5B20%2C%20%5B%22age_of_operator%22%2C%20%22hh_interview_minutes%22%5D%5D%20%3D%20%5B16%2C%2012%5D%0A%20%20%20%20return%20pd.concat(%5Btable%2C%20table.loc%5B%5B10%5D%5D%5D%2C%20ignore_index%3DTrue)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Drill%20D2.4%22%2C%20flags_once%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small-scale%20units%20as%20entered%2C%20contamination%200.05%22%2C%20(small_as_entered%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20small-scale%20units%2C%20each%20once%2C%20contamination%200.05%22%2C%20(small%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20(%22forty%20units%20and%20a%20copy%20of%20T11%2C%20contamination%200.05%22%2C%20(drill_twice()%2C%200.05))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20ids%3A%20list(map(str%2C%20ids))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20the%20rows%20the%20forest%20was%20fitted%20on%2C%20and%20the%20different%20unit_ids%20among%20them.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20forty%20units%20and%20a%20copy%20of%20T11%0A%20%20%20%20try_it(flags_once%2C%20drill_twice()%2C%200.05)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Trace.**%20Count%20the%20rows%20of%20each%20table%20and%20the%20different%20%60unit_id%60s%20in%20it%2C%20and%20look%20at%20the%20units%20whose%20%60unit_id%60%20is%20there%20more%20than%20once%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Trace%3A%20rows%20against%20distinct%20unit_ids%2C%20and%20the%20units%20listed%20twice%0A%20%20%20%20_counts%20%3D%20pd.DataFrame(%5B%7B%22table%22%3A%20_name%2C%20%22rows%22%3A%20len(_table)%2C%20%22different%20unit_ids%22%3A%20_table%5B%22unit_id%22%5D.nunique()%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20_name%2C%20_table%20in%20%5B(%22small%22%2C%20small)%2C%20(%22small_as_entered%22%2C%20small_as_entered)%2C%20(%22drill_twice()%22%2C%20drill_twice())%5D%5D)%0A%20%20%20%20_twice_listed%20%3D%20small_as_entered.loc%5Bsmall_as_entered%5B%22unit_id%22%5D.duplicated(keep%3DFalse)%2C%20%5B%22unit_id%22%2C%20*FEATURES%5D%5D%0A%20%20%20%20mo.vstack(%5B_counts%2C%20mo.md(%22The%20units%20entered%20twice%2C%20every%20row%20of%20them%3A%22)%2C%20_twice_listed%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Two%20rows%20more%20than%20units%2C%20and%20each%20of%20the%20two%20is%20a%20row-for-row%20copy.%20An%20Isolation%20Forest%20isolates%20a%20row%20by%20cutting%20until%20it%20stands%20alone%2C%20and%20no%20cut%20separates%20two%20identical%20rows%2C%20so%20a%20copied%20unit%20needs%20more%20cuts%20and%20scores%20as%20more%20ordinary%3B%20a%20list%20made%20from%20rows%20names%20it%20once%20per%20copy.%20The%20planted%20copies%20here%20are%20ordinary%20units%2C%20so%20on%20the%20survey%20the%20harm%20shows%20as%20flags%20that%20move.%20Module%204%2C%20section%205%20finds%20duplicates%20by%20key%3B%20module%209%2C%20section%201%20drops%20them%20in%20the%20first%20step%20of%20its%20features.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%5B'T11'%2C%20'T11'%5D%60%20%7C%20Both%20copies%20of%20%60T11%60%20fitted%20and%20listed.%20%7C%20one%20row%20per%20unit%20before%20%60fit%60%20%7C%0A%20%20%20%20%7C%20%60%5B'T11'%5D%60%20alone%2C%20and%20a%20few%20survey%20ids%20differ%20%7C%20Duplicates%20dropped%20from%20the%20list%20after%20fitting%3A%20the%20forest%20still%20learned%20from%20the%20copies.%20%7C%20drop%20them%20before%20%60fit%60%20%7C%0A%20%20%20%20%7C%20Fewer%20ids%20than%20expected%20%7C%20%60keep%3DFalse%60%20dropped%20both%20copies%2C%20and%20the%20unit%20with%20them.%20%7C%20the%20default%2C%20%60keep%3D%22first%22%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D2.5%20%C2%B7%20An%20id%20read%20as%20a%20number%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20number%20that%20names%20a%20row%20says%20nothing%20about%20it%2C%20but%20a%20detector%20cuts%20on%20it%20like%20any%20other%20column.%20The%20flags%20then%20depend%20on%20how%20the%20households%20happened%20to%20be%20numbered%2C%20and%20a%20new%20listing%20changes%20the%20review.%0A%0A%20%20%20%20Another%20survey%20team%20sends%20features%20as%20CSV%20files%2C%20one%20row%20per%20unit%3A%20%60unit_id%60%2C%20%60hh_code%60%20(the%20household's%2013-digit%20code)%2C%20%60hh_interviewer%60%2C%20then%20the%20twelve%20features.%20%60flags_from_file(path)%60%20should%20read%20such%20a%20file%20and%20give%20the%20%60unit_id%60s%20an%20Isolation%20Forest%20(200%20trees%2C%20%60contamination%3D0.05%60%2C%20%60random_state%3D0%60)%20flags%20on%20the%20twelve%20features%2C%20sorted.%20The%20setup%20wrote%20two%20files%20into%20%60scratch%2F%60%3A%20%60SMALL_CSV%60%2C%20the%20survey's%20small-scale%20units%2C%20and%20%60RENUMBERED_CSV%60%2C%20the%20same%20units%20and%20answers%20with%20the%20households%20numbered%20in%20the%20opposite%20order.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20a%20features%20file%20%E2%86%92%20the%20%60unit_id%60s%20flagged%2C%20sorted.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60flags_from_file(SMALL_CSV)%20%3D%3D%20flags_from_file(RENUMBERED_CSV)%60%20%7C%20%60True%60%3A%20the%20same%20answers%2C%20so%20the%20same%20flags%20%7C%0A%20%20%20%20%7C%20%60flags_from_file(SMALL_CSV)%20%3D%3D%20flags_once(small%2C%200.05)%60%20%7C%20%60True%60%3A%20the%20same%20twelve%20answers%20and%20the%20same%20forest%20as%20Drill%20D2.4's%20%7C%0A%0A%20%20%20%20Tools%3A%20codes%20kept%20whole%20(%5Bmodule%202%2C%20section%203%5D(%2F%3Ffile%3D10_flat_table.py))%2C%20what%20to%20leave%20out%20(%5Bmodule%209%2C%20section%201%5D(%2F%3Ffile%3D80_anomalies_ml.py)).%0A%0A%20%20%20%20**Predict.**%20The%20function%20keeps%20the%20columns%20read%20as%20numbers%2C%20on%20the%20reasoning%20that%20ids%20are%20text.%20Which%20columns%20will%20%60read_csv%60%20read%20as%20numbers%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20flags_from_file(path)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20unit_ids%20an%20Isolation%20Forest%20flags%20on%20the%20answers%20in%20a%20features%20file%2C%20sorted.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.5%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20table%20%3D%20pd.read_csv(path)%0A%20%20%20%20X%20%3D%20table.select_dtypes(%22number%22)%20%20%23%20the%20answers%3A%20every%20column%20read%20as%20a%20number%2C%20since%20the%20ids%20are%20text%0A%20%20%20%20forest%20%3D%20IsolationForest(n_estimators%3D200%2C%20contamination%3D0.05%2C%20random_state%3D0).fit(X)%0A%20%20%20%20return%20sorted(table%5B%22unit_id%22%5D%5Bforest.predict(X)%20%3D%3D%20-1%5D)%0A%0A%0A%40app.function%0Adef%20drill_features_csv()%20-%3E%20io.StringIO%3A%0A%20%20%20%20%22%22%22A%20small%20features%20file%2C%20as%20text%3A%20twenty%20units%20of%20one%20barangay%20in%20region%2001%20but%20the%20twelfth%2C%20T12%2C%20in%20region%2007%2C%20and%0A%20%20%20%20T03%20with%20nine%20species%20where%20the%20others%20land%20three%20to%20six.%20Used%20by%20the%20check%20of%20Drill%20D2.5.%22%22%22%0A%20%20%20%20table%20%3D%20drill_units(20%2C%209)%0A%20%20%20%20table.loc%5B2%2C%20%22n_species%22%5D%20%3D%209%0A%20%20%20%20codes%20%3D%20%5Bf%220103310008%7Bi%3A03d%7D%22%20for%20i%20in%20range(1%2C%2021)%5D%0A%20%20%20%20codes%5B11%5D%20%3D%20%220701212015001%22%0A%20%20%20%20table.insert(1%2C%20%22hh_code%22%2C%20codes)%0A%20%20%20%20table.insert(2%2C%20%22hh_interviewer%22%2C%20%22INT004%22)%0A%20%20%20%20return%20io.StringIO(table%5B%5B%22unit_id%22%2C%20%22hh_code%22%2C%20%22hh_interviewer%22%2C%20*FEATURES%5D%5D.to_csv(index%3DFalse))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Drill%20D2.5%22%2C%20flags_from_file%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small-scale%20units'%20file%22%2C%20(SMALL_CSV%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20same%20units%2C%20the%20households%20numbered%20in%20the%20opposite%20order%22%2C%20(RENUMBERED_CSV%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22twenty%20units%2C%20one%20in%20region%2007%2C%20one%20with%20nine%20species%22%2C%20(drill_features_csv()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%22%2C%20summarize%3Dlambda%20ids%3A%20sorted(map(str%2C%20ids))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Compare%20the%20flags%20for%20the%20two%20survey%20files%3A%20their%20answers%20are%20the%20same.%20Then%20look%20at%20the%20columns%20the%20forest%20was%20fitted%20on.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20flags%20from%20the%20small-scale%20units'%20file%0A%20%20%20%20try_it(flags_from_file%2C%20SMALL_CSV)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Trace.**%20The%20two%20survey%20files%20hold%20the%20same%20answers%2C%20so%20any%20flag%20that%20differs%20between%20them%20comes%20from%20a%20column%20that%20is%20not%20an%20answer.%20Look%20at%20the%20columns%20%60read_csv%60%20reads%20as%20numbers%2C%20and%20compare%20the%20two%20files'%20flags%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Trace%3A%20the%20columns%20read_csv%20reads%20as%20numbers%2C%20and%20the%20flags%20of%20the%20two%20files%20side%20by%20side%0A%20%20%20%20_numbers%20%3D%20pd.read_csv(SMALL_CSV).select_dtypes(%22number%22).columns.tolist()%0A%20%20%20%20_first%20%3D%20course.attempt(flags_from_file%2C%20SMALL_CSV)%0A%20%20%20%20_second%20%3D%20course.attempt(flags_from_file%2C%20RENUMBERED_CSV)%0A%20%20%20%20mo.md(f%22Read%20as%20numbers%3A%20%60%7B'%2C%20'.join(_numbers)%7D%60.%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20f%22Flags%20from%20the%20file%3A%20**%7Blen(_first)%7D**%3B%20from%20the%20renumbered%20file%3A%20**%7Blen(_second)%7D**%3B%20flagged%20in%20one%20file%20and%20not%20the%20other%3A%20**%7Blen(set(_first)%20%5E%20set(_second))%7D**.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60hh_code%60%20is%20among%20the%20numbers%3A%20thirteen%20digits%20and%20nothing%20else%2C%20so%20pandas%20made%20it%20an%20%60int64%60%2C%20and%20the%20function%20took%20every%20number%20for%20an%20answer.%20Numbered%20the%20other%20way%2C%20the%20same%20households%20fall%20elsewhere%20in%20that%20column's%20range%2C%20and%20the%20forest%20isolates%20others.%20Module%202%2C%20section%203%20names%20the%20type%20of%20a%20code%20column%20when%20it%20is%20read%3B%20module%209%2C%20section%201%20lists%20the%20ids%20among%20the%20columns%20a%20detector%20leaves%20out.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.5%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Different%20flags%20for%20the%20two%20survey%20files%20%7C%20%60hh_code%60%20read%20as%20a%20number%2C%20so%20the%20forest%20reads%20it.%20%7C%20%60read_csv%60's%20%60dtype%3D%60%20for%20%60hh_code%60%20%7C%0A%20%20%20%20%7C%20The%20same%20flags%20for%20both%20survey%20files%2C%20but%20not%20the%20right%20ones%20%7C%20The%20columns%20are%20in%20another%20order%2C%20or%20one%20is%20missing%3A%20the%20forest%20picks%20a%20column%20by%20its%20position.%20%7C%20the%20twelve%20features%2C%20in%20the%20file's%20order%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20could%20not%20convert%20string%20to%20float*%2C%20naming%20an%20interviewer%20such%20as%20%60'INT004'%60%20%7C%20%60hh_interviewer%60%20went%20into%20the%20forest%3A%20every%20column%2C%20not%20the%20numbers.%20%7C%20%60select_dtypes(%22number%22)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'unit_id'*%20%7C%20The%20file%20was%20read%20with%20%60index_col%3D0%60%2C%20which%20makes%20%60unit_id%60%20the%20index.%20%7C%20read%20it%20without%20%60index_col%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D2.6%20%C2%B7%20ECOD%20scored%20chunk%20by%20chunk%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20review%20list%20from%20a%20large%20table%20has%20to%20rest%20on%20scores%20that%20do%20not%20depend%20on%20how%20the%20table%20was%20split%20to%20fit%20in%20memory.%20For%20ECOD%20they%20do%3A%20module%209%2C%20section%205%20showed%20its%20scores%20are%20not%20exact%20in%20chunks.%0A%0A%20%20%20%20%60ecod_review(table%2C%20k)%60%20should%20fit%20ECOD%20on%20the%20table's%20features%20scaled%20(%60neighbor_matrix(table)%5BFEATURES%5D%60)%20and%20give%20the%20%60k%60%20units%20with%20the%20highest%20scores%2C%20highest%20first%2C%20as%20a%20table%20of%20%60unit_id%60%20and%20%60ecod_score%60.%20Each%20score%20must%20be%20**exact**%3A%20the%20score%20ECOD%20gives%20the%20row%20as%20one%20of%20the%20rows%20it%20was%20fitted%20on.%20The%20function%20scores%20in%20chunks%20of%20%60CHUNK_ROWS%60%20(50%2C000)%20rows%2C%20as%20module%209%20scored%20the%20large%20table%20with%20its%20Isolation%20Forests.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20units%20and%20%60k%60%20%E2%86%92%20a%20table%20of%20%60k%60%20rows%3A%20%60unit_id%60%20and%20%60ecod_score%60%2C%20highest%20score%20first.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60ecod_review(small%2C%20117)%60%20%7C%20the%20survey's%20117%20highest%20scores%2C%20the%20same%20as%20the%20function%20gives%20now%20%7C%0A%20%20%20%20%7C%20%60ecod_review(all_regions_small%2C%20600)%5B%22ecod_score%22%5D.round(2).head(3).tolist()%60%20%7C%20%60%5B66.67%2C%2060.91%2C%2058.8%5D%60%20%7C%0A%0A%20%20%20%20Tools%3A%20ECOD%20(%5Bmodule%209%2C%20section%202%5D(%2F%3Ffile%3D80_anomalies_ml.py))%2C%20scoring%20in%20chunks%20(%5Bmodule%209%2C%20section%205%5D(%2F%3Ffile%3D80_anomalies_ml.py)).%0A%0A%20%20%20%20**Predict.**%20Run%20**Try%20it**%20below%2C%20on%20the%20survey's%20small-scale%20units.%20Is%20anything%20wrong%20with%20what%20it%20gives%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20ecod_review(table%3A%20pd.DataFrame%2C%20k%3A%20int)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20k%20units%20with%20the%20highest%20exact%20ECOD%20scores%20on%20the%20features%20scaled%2C%20highest%20first%3A%20unit_id%20and%20ecod_score.%22%22%22%0A%20%20%20%20X%20%3D%20neighbor_matrix(table)%5BFEATURES%5D.to_numpy()%0A%20%20%20%20detector%20%3D%20ECOD().fit(X)%0A%20%20%20%20%23%20Drill%20D2.6%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20scores%20%3D%20np.concatenate(%5Bdetector.decision_function(X%5Bstart%3Astart%20%2B%20CHUNK_ROWS%5D)%20for%20start%20in%20range(0%2C%20len(X)%2C%20CHUNK_ROWS)%5D)%20%20%23%20in%20chunks%2C%20as%20module%209%20scores%20a%20large%20table%0A%20%20%20%20top%20%3D%20np.argsort(-scores%2C%20kind%3D%22stable%22)%5B%3Ak%5D%0A%20%20%20%20return%20pd.DataFrame(%7B%22unit_id%22%3A%20table%5B%22unit_id%22%5D.to_numpy()%5Btop%5D%2C%20%22ecod_score%22%3A%20scores%5Btop%5D%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Drill%20D2.6%22%2C%20ecod_review%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20small-scale%20units%2C%20k%20%3D%20117%22%2C%20(small%2C%20117))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20large%20table's%20small-scale%20units%2C%20k%20%3D%20600%22%2C%20(all_regions_small%2C%20600))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20large%20table's%20first%20120%2C000%20small-scale%20units%2C%20k%20%3D%20600%22%2C%20(all_regions_small.head(120_000)%2C%20600))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20top%3A%20top.round(%7B%22ecod_score%22%3A%204%7D).reset_index(drop%3DTrue)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20names%20the%20first%20table%20that%20goes%20wrong.%20Compare%20its%20scores%20with%20the%20scores%20ECOD%20gave%20its%20rows%20when%20it%20was%20fitted.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20117%20most%20unusual%20small-scale%20units%20by%20ECOD%0A%20%20%20%20try_it(ecod_review%2C%20small%2C%20117)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**It%20looks%20right.**%20On%20the%20survey%20the%20list%20is%20the%20one%20the%20task%20asks%20for%2C%20and%20its%20scores%20are%20exact.%20Its%20check%20is%20red%20all%20the%20same%3A%20the%20check%20also%20runs%20the%20function%20on%20the%20large%20table.%20Fit%20ECOD%20on%20the%20large%20table's%20small-scale%20units%2C%20and%20set%20each%20row's%20score%20from%20the%20fit%20beside%20the%20score%20the%20chunks%20give%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Trace%3A%20each%20row's%20score%20from%20the%20fit%2C%20and%20from%20the%20fitted%20detector%20in%20chunks%20of%20CHUNK_ROWS%0A%20%20%20%20_X%20%3D%20neighbor_matrix(all_regions_small)%5BFEATURES%5D.to_numpy()%0A%20%20%20%20_detector%20%3D%20ECOD().fit(_X)%0A%20%20%20%20_fitted%20%3D%20_detector.decision_scores_%0A%20%20%20%20_chunked%20%3D%20np.concatenate(%5B_detector.decision_function(_X%5B_start%3A_start%20%2B%20CHUNK_ROWS%5D)%20for%20_start%20in%20range(0%2C%20len(_X)%2C%20CHUNK_ROWS)%5D)%0A%20%20%20%20_alone%20%3D%20_detector.decision_function(_X%5B%3A1%5D)%5B0%5D%0A%20%20%20%20_top_fitted%20%3D%20set(np.argsort(-_fitted%2C%20kind%3D%22stable%22)%5B%3A600%5D)%0A%20%20%20%20_top_chunked%20%3D%20set(np.argsort(-_chunked%2C%20kind%3D%22stable%22)%5B%3A600%5D)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20-%20**%7Blen(_X)%3A%2C%7D%20rows**%20in%20**%7B-(-len(_X)%20%2F%2F%20CHUNK_ROWS)%7D%20chunks**.%0A%20%20%20%20-%20Rows%20whose%20score%20moved%20by%20more%20than%200.01%3A%20**%7Bint((np.abs(_fitted%20-%20_chunked)%20%3E%200.01).sum())%3A%2C%7D**%3B%20the%20largest%20move%3A%20**%7Bnp.abs(_fitted%20-%20_chunked).max()%3A.3f%7D**.%0A%20%20%20%20-%20The%20first%20unit's%20score%3A%20**%7B_fitted%5B0%5D%3A.3f%7D**%20from%20the%20fit%2C%20**%7B_chunked%5B0%5D%3A.3f%7D**%20in%20its%20chunk%2C%20**%7B_alone%3A.3f%7D**%20scored%20alone.%0A%20%20%20%20-%20Of%20the%20600%20highest%20scores%2C%20**%7Blen(_top_fitted%20-%20_top_chunked)%7D**%20belong%20to%20other%20units%20in%20chunks.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Trace.**%20One%20unit%2C%20three%20scores%3A%20its%20answers%20did%20not%20change%2C%20only%20the%20rows%20it%20was%20scored%20with.%20ECOD's%20score%20is%20built%20from%20how%20far%20into%20each%20column's%20tails%20a%20value%20sits%2C%20and%20%60decision_function%60%20works%20the%20tails%20out%20again%20on%20the%20fitted%20rows%20plus%20the%20rows%20it%20is%20given.%20A%20detector%20whose%20score%20for%20a%20row%20depends%20on%20the%20other%20rows%20in%20the%20call%20cannot%20be%20scored%20in%20chunks%20and%20give%20the%20same%20answer.%20Module%209%2C%20section%205%20measured%20the%20same%20thing%20for%20a%20sample%20and%20three%20chunks.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Right%20on%20the%20survey%2C%20wrong%20on%20the%20large%20table%20%7C%20The%20rows%20were%20scored%20in%20chunks%3A%20on%20the%20survey%20there%20is%20only%20one.%20%7C%20%60detector.decision_scores_%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'ECOD'%20object%20has%20no%20attribute%20'decision_scores_'*%20%7C%20A%20new%20%60ECOD()%60%20that%20was%20never%20fitted.%20%7C%20%60detector%60%2C%20the%20one%20fitted%20above%20%7C%0A%20%20%20%20%7C%20The%20lowest%20scores%2C%20not%20the%20highest%20%7C%20%60np.argsort(scores)%60%20sorts%20up%3B%20ECOD%20scores%20the%20unusual%20**high**.%20%7C%20%60np.argsort(-scores%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20*MemoryError*%20or%20a%20long%20wait%20%7C%20ECOD%20refitted%20inside%20the%20loop%2C%20once%20per%20chunk.%20%7C%20fit%20once%2C%20outside%20any%20loop%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**What%20the%20six%20mistakes%20have%20in%20common.**%20None%20raised%20an%20error%2C%20and%20each%20gave%20back%20a%20list%20that%20looked%20like%20a%20review%20list.%20Each%20detector%20did%20what%20it%20was%20told%3A%20it%20found%20what%20was%20rare%20in%20the%20columns%20it%20read%2C%20at%20the%20share%20it%20was%20given%2C%20among%20the%20rows%20it%20was%20handed.%20Three%20mistakes%20were%20in%20what%20it%20read%20(a%20design%20weight%2C%20columns%20on%20unequal%20scales%2C%20an%20id%20read%20as%20a%20number)%2C%20one%20in%20the%20share%20it%20was%20given%2C%20and%20two%20in%20the%20rows%20it%20was%20handed%20(a%20unit%20twice%2C%20rows%20scored%20in%20groups).%20A%20recall%20figure%20did%20not%20find%20them%3B%20on%20the%20survey%2C%20the%20unscaled%20detector%20even%20scored%20better.%20What%20found%20each%20one%20was%20a%20comparison%20the%20list%20should%20survive%3A%20its%20strata%20against%20the%20table's%2C%20the%20column%20behind%20each%20flag%2C%20its%20length%20against%20the%20table's%20own%20rate%2C%20the%20same%20answers%20renumbered%2C%20a%20table%20larger%20than%20one%20chunk.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20keep%20a%20detector%20honest%20%7C%20do%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20choose%20what%20it%20reads%20%7C%20name%20the%20features%3A%20%60table%5BFEATURES%5D%60%3B%20weights%2C%20strata%20and%20ids%20stay%20beside%20them%20%7C%20the%20strata%20of%20the%20flagged%20rows%20against%20the%20table's%20%7C%0A%20%20%20%20%7C%20measure%20distances%20%7C%20scale%20first%2C%20by%20median%20and%20IQR%3A%20%60RobustScaler%60%2C%20%60neighbor_matrix%60%20%7C%20the%20column%20that%20puts%20each%20flagged%20row%20on%20the%20list%20%7C%0A%20%20%20%20%7C%20set%20contamination%20%7C%20the%20share%20expected%20in%20**this**%20table%20%7C%20the%20flags%20against%20that%20share%20of%20its%20rows%20%7C%0A%20%20%20%20%7C%20fit%20and%20list%20each%20unit%20once%20%7C%20%60drop_duplicates(%22unit_id%22)%60%20before%20%60fit%60%20%7C%20rows%20against%20distinct%20ids%20%7C%0A%20%20%20%20%7C%20read%20a%20features%20file%20%7C%20ids%20as%20text%3A%20%60dtype%3D%7B%22hh_code%22%3A%20str%7D%60%20%7C%20the%20same%20flags%20with%20the%20ids%20renumbered%20%7C%0A%20%20%20%20%7C%20score%20with%20ECOD%20%7C%20fit%20on%20the%20whole%20table%20and%20read%20%60decision_scores_%60%20%7C%20the%20scores%20on%20a%20table%20larger%20than%20one%20chunk%20%7C%0A%0A%20%20%20%20Next%2C%20the%20stretch%20exercises%20S3%20(%6081_stretch_detection.py%60)%20take%20detection%20a%20step%20past%20modules%208%20and%209.%20The%20checks%20you%20used%20here%20are%20also%20the%20ones%20to%20ask%20of%20an%20agent's%20code%2C%20as%20the%20stretch%20S4%20(%6086_stretch_agents.py%60)%20shows.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
638afe4c4100adff1e96bb122855730e