import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Arrays%20with%20NumPy%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20names%20every%20other%20cell%20uses.%0A%20%20%20%20import%20warnings%0A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%20%20%23%20NumPy%2C%20imported%20as%20np%20by%20everyone%20who%20uses%20it%0A%20%20%20%20import%20pandas%20as%20pd%20%20%23%20here%20only%20to%20hand%20over%20the%20survey's%20columns%20and%20the%20price%20grid%3B%20module%203%20teaches%20it%0A%20%20%20%20import%20seaborn%20as%20sns%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey%2C%20the%20prices%20and%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20attempt%2C%20check_cases%2C%20waiting_for%0A%0A%20%20%20%20%23%20The%20provinces%20of%20the%20farmgate%20price%20files%2C%20north%20to%20south%2C%20and%20their%2048%20months%2C%20January%202022%20to%20December%202025%0A%20%20%20%20PROVINCES%20%3D%20%5B%22Ilocos%20Norte%22%2C%20%22Isabela%22%2C%20%22Nueva%20Ecija%22%2C%20%22Camarines%20Sur%22%2C%20%22Iloilo%22%2C%20%22Bukidnon%22%2C%20%22Cotabato%22%5D%0A%20%20%20%20MONTHS%20%3D%20pd.period_range(%222022-01%22%2C%20%222025-12%22%2C%20freq%3D%22M%22)%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%20Arrays%20with%20NumPy%0A%0A%20%20%20%20**Where%20you%20are%3A**%20module%201%20opened%20the%20farm%20survey%20and%20drew%20a%20first%20chart%20of%20it.%20Before%20the%20tables%20of%20module%203%2C%20this%20module%20works%20with%20what%20every%20table%20is%20built%20on%3A%20the%20**array**%2C%20a%20block%20of%20values%20of%20one%20type%20that%20Python%20computes%20with%20all%20at%20once.%20**NumPy**%20(Numerical%20Python)%20is%20the%20library%20that%20provides%20it%3A%20pandas%20keeps%20its%20columns%20of%20numbers%20as%20NumPy%20arrays%2C%20and%20matplotlib%20and%20seaborn%20draw%20from%20them.%0A%0A%20%20%20%20In%20this%20module%20you%20will%3A%0A%0A%20%20%20%201.%20create%20arrays%20and%20read%20what%20they%20hold%3A%20their%20**dtype**%20(the%20type%20of%20every%20value)%20and%20their%20**shape**%20(how%20many%20values%2C%20in%20how%20many%20rows%20and%20columns)%3B%0A%20%20%20%202.%20select%20values%20by%20position%20and%20by%20condition%3A%20the%20plots%20recorded%20in%20square%20meters%2C%20the%20irrigated%20rice%3B%0A%20%20%20%203.%20compute%20on%20whole%20arrays%20without%20a%20loop%3A%20every%20plot's%20area%20in%20hectares%20and%20its%20yield%2C%20a%20price%20index%20for%20every%20province%20and%20month%3B%0A%20%20%20%204.%20summarize%20along%20rows%20or%20along%20columns%2C%20and%20handle%20a%20month%20of%20prices%20that%20never%20arrived.%0A%0A%20%20%20%20Each%20exercise%20is%20a%20function%3A%20write%20it%2C%20delete%20its%20%60raise%60%20line%2C%20run%20its%20cell%2C%20and%20read%20the%20check%20under%20it.%20The%20**Try%20it**%20cell%20after%20each%20check%20runs%20your%20function%20on%20the%20survey%20or%20the%20prices.%20If%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong%2C%20and%20usually%20where.%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%20course.quizzes_at(%222.0%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%20Creating%20arrays%3B%20dtype%20and%20shape%0A%0A%20%20%20%20The%20survey's%20plots%20table%20has%202%2C164%20rows%2C%20one%20per%20plot.%20Suppose%20their%20areas%20sat%20in%20a%20Python%20list%20and%20you%20needed%20them%20in%20hectares%2C%20or%20doubled.%20A%20list%20doesn't%20do%20arithmetic%20on%20its%20values%3A%20%60*%60%20repeats%20the%20list%2C%20and%20%60%2F%60%20is%20refused.%20The%20cell%20below%20tries%20both%20on%20two%20plots.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20explain_error(action%2C%20code%3A%20str)%3A%0A%20%20%20%20%22%22%22Run%20%60action%60%20(a%20function%20with%20no%20inputs)%20and%20show%20what%20%60code%60%20gives%20back%2C%20or%20the%20error%20it%20raises.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20action()%0A%20%20%20%20except%20Exception%20as%20error%3A%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60%7Bcode%7D%60%20raises%20**%7Btype(error).__name__%7D**%3A%20%60%7Berror%7D%60%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20return%20mo.md(f%22%60%7Bcode%7D%60%20gives%20%60%7Bresult!r%7D%60%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20two_areas%20%3D%20%5B0.439%2C%201940.0%5D%20%20%23%20two%20plots'%20areas%2C%20as%20a%20Python%20list%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20explain_error(lambda%3A%20two_areas%20*%202%2C%20%22two_areas%20*%202%22)%2C%0A%20%20%20%20%20%20%20%20explain_error(lambda%3A%20two_areas%20%2F%2010_000%2C%20%22two_areas%20%2F%2010_000%22)%2C%0A%20%20%20%20%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%20The%20first%20gives%20the%20list%20twice.%20The%20second%20raises%20a%20**TypeError**%3A%20read%20its%20message%20from%20the%20end%2C%20*'list'%20and%20'int'*.%20Python%20has%20no%20rule%20for%20dividing%20a%20list%20by%20a%20number%2C%20so%20it%20refuses%20rather%20than%20guess.%0A%0A%20%20%20%20A%20NumPy%20**array**%20has%20that%20rule.%20%60np.array(...)%60%20makes%20one%20from%20a%20list%2C%20and%20arithmetic%20on%20it%20applies%20to%20every%20**element**%20(every%20value%20in%20it)%20at%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%20four_areas%20%3D%20np.array(%5B0.439%2C%201940.0%2C%200.454%2C%200.942%5D)%20%20%23%20an%20array%2C%20made%20from%20a%20list%0A%20%20%20%20four_areas%20%2F%2010_000%20%20%23%20every%20element%20divided%2C%20with%20no%20loop%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%20That%20is%20the%20whole%20idea%20of%20NumPy%3A%20write%20the%20operation%20once%2C%20for%20the%20whole%20array.%20Section%203%20calls%20it%20**vectorized**%20arithmetic.%0A%0A%20%20%20%20%23%23%23%23%20dtype%3A%20one%20type%20for%20every%20element%0A%0A%20%20%20%20A%20list%20can%20mix%20numbers%20and%20text%3B%20an%20array%20cannot.%20Every%20element%20of%20an%20array%20has%20the%20same%20type%2C%20its%20**dtype**%20(data%20type)%2C%20and%20NumPy%20picks%20it%20from%20what%20you%20give%20it%3A%0A%0A%20%20%20%20%7C%20made%20with%20%7C%20%60.dtype%60%20%7C%20what%20happened%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60np.array(%5B1640%2C%20700%5D)%60%20%7C%20%60int64%60%20%7C%20whole%20numbers%20(each%20stored%20in%2064%20bits)%20%7C%0A%20%20%20%20%7C%20%60np.array(%5B0.439%2C%201940.0%5D)%60%20%7C%20%60float64%60%20%7C%20decimals%20(%22floating-point%20numbers%22)%20%7C%0A%20%20%20%20%7C%20%60np.array(%5B1%2C%202.5%5D)%60%20%7C%20%60float64%60%20%7C%20one%20decimal%20makes%20them%20all%20decimals%3A%20%601%60%20becomes%20%601.0%60%20%7C%0A%20%20%20%20%7C%20%60np.array(%5BTrue%2C%20False%5D)%60%20%7C%20%60bool%60%20%7C%20%60True%60%20or%20%60False%60%20%7C%0A%20%20%20%20%7C%20%60np.array(%5B%22Rice%22%2C%20%22Corn%22%5D)%60%20%7C%20%60%3CU4%60%20%7C%20text%20of%20up%20to%204%20characters%20(%60U%60%20for%20Unicode)%20%7C%0A%20%20%20%20%7C%20%60np.array(%5B1%2C%20%22Rice%22%5D)%60%20%7C%20%60%3CU21%60%20%7C%20one%20text%20makes%20them%20all%20text%3A%20the%20number%20%601%60%20became%20the%20text%20%60'1'%60%20%7C%0A%0A%20%20%20%20The%20last%20row%20is%20a%20trap%20worth%20remembering%3A%20an%20array%20of%20numbers%20with%20one%20stray%20text%20in%20it%20is%20an%20array%20of%20text%2C%20and%20its%20arithmetic%20stops%20working.%0A%0A%20%20%20%20%23%23%23%23%20shape%3A%20how%20many%2C%20in%20how%20many%20rows%20and%20columns%0A%0A%20%20%20%20An%20array%20can%20have%20rows%20and%20columns%2C%20like%20a%20table.%20Its%20**shape**%20says%20how%20many%20of%20each%3A%20%60(3%2C%202)%60%20is%203%20rows%20of%202.%20A%20row%20of%20values%20with%20no%20columns%2C%20as%20above%2C%20has%20a%20shape%20of%20one%20number%2C%20%60(4%2C)%60%3A%20the%20comma%20marks%20it%20as%20a%20tuple%20of%20one.%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%20harvests%20%3D%20np.array(%5B%5B1640%2C%20700%5D%2C%20%5B1990%2C%204000%5D%2C%20%5B1710%2C%201200%5D%5D)%20%20%23%20three%20households%2C%20two%20plots%20each%3A%20kg%20harvested%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60harvests%60%20is%20%60%7Bharvests.tolist()%7D%60.%0A%0A%20%20%20%20%7C%20attribute%20%7C%20gives%20%7C%20means%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60harvests.dtype%60%20%7C%20%60%7Bharvests.dtype%7D%60%20%7C%20every%20element%20is%20a%20whole%20number%20%7C%0A%20%20%20%20%7C%20%60harvests.shape%60%20%7C%20%60%7Bharvests.shape%7D%60%20%7C%203%20rows%2C%202%20columns%20%7C%0A%20%20%20%20%7C%20%60harvests.ndim%60%20%7C%20%60%7Bharvests.ndim%7D%60%20%7C%202%20dimensions%20(rows%20and%20columns)%3B%20a%20single%20row%20of%20values%20has%201%20%7C%0A%20%20%20%20%7C%20%60harvests.size%60%20%7C%20%60%7Bharvests.size%7D%60%20%7C%206%20elements%20in%20all%20%7C%0A%20%20%20%20%7C%20%60len(harvests)%60%20%7C%20%60%7Blen(harvests)%7D%60%20%7C%20the%20number%20of%20rows%3A%20the%20first%20number%20of%20the%20shape%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%23%20The%20survey%2C%20as%20arrays%0A%0A%20%20%20%20You%20won't%20type%20the%20survey's%202%2C164%20plots%20by%20hand.%20%60course.plots(clean%3DFalse)%60%20reads%20the%20plots%20table%2C%20every%20trap%20still%20in%20it%2C%20as%20a%20pandas%20**DataFrame**%20(module%203%20is%20all%20about%20those).%20%60.to_numpy()%60%20hands%20over%20one%20of%20its%20columns%20as%20a%20NumPy%20array%2C%20and%20that%20is%20all%20of%20pandas%20this%20module%20uses.%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%20plots%20%3D%20course.plots(clean%3DFalse)%20%20%23%20the%20plots%20table%2C%20as%20read%20from%20data%2Ffarm_survey%2Fplots.csv%0A%20%20%20%20area%20%3D%20plots%5B%22area%22%5D.to_numpy()%20%20%23%20each%20column%2C%20as%20a%20NumPy%20array%0A%20%20%20%20unit%20%3D%20plots%5B%22area_unit%22%5D.to_numpy()%0A%20%20%20%20production%20%3D%20plots%5B%22production_kg%22%5D.to_numpy()%0A%20%20%20%20crop%20%3D%20plots%5B%22crop%22%5D.to_numpy()%0A%20%20%20%20irrigated%20%3D%20plots%5B%22irrigated%22%5D.to_numpy()%0A%20%20%20%20fertilizer%20%3D%20plots%5B%22fertilizer_kg%22%5D.to_numpy()%0A%20%20%20%20return%20area%2C%20crop%2C%20fertilizer%2C%20irrigated%2C%20production%2C%20unit%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(area%2C%20crop%2C%20fertilizer%2C%20irrigated%2C%20production%2C%20unit)%3A%0A%20%20%20%20_arrays%20%3D%20%7B%22area%22%3A%20area%2C%20%22unit%22%3A%20unit%2C%20%22production%22%3A%20production%2C%20%22crop%22%3A%20crop%2C%20%22irrigated%22%3A%20irrigated%2C%20%22fertilizer%22%3A%20fertilizer%7D%0A%20%20%20%20_rows%20%3D%20%22%5Cn%20%20%20%20%22.join(f%22%7C%20%60%7Bname%7D%60%20%7C%20%60%7Ba.dtype%7D%60%20%7C%20%60%7Ba.shape%7D%60%20%7C%20%60%7Ba%5B%3A4%5D.tolist()%7D%60%20%7C%22%20for%20name%2C%20a%20in%20_arrays.items())%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20array%20%7C%20dtype%20%7C%20shape%20%7C%20its%20first%20four%20elements%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7B_rows%7D%0A%0A%20%20%20%20Every%20array%20has%202%2C164%20elements%2C%20one%20per%20row%20of%20the%20table%2C%20in%20the%20same%20order%3A%20element%200%20of%20each%20is%20the%20first%20plot.%20(Four%20plots%20were%20entered%20twice%3B%20module%205%20finds%20duplicates%20like%20these%2C%20and%20here%20they%20barely%20move%20any%20result.)%20Three%20things%20to%20read%20in%20this%20table%3A%0A%0A%20%20%20%20-%20%60production%60%20is%20%60int64%60%3A%20whole%20kilograms.%20%60area%60%20and%20%60fertilizer%60%20are%20%60float64%60.%0A%20%20%20%20-%20The%20text%20columns%20come%20out%20as%20%60object%60%3A%20each%20element%20is%20a%20Python%20value%20of%20any%20type%2C%20here%20a%20text%20(a%20%60str%60)%2C%20or%20the%20decimal%20%60nan%60%20(%22not%20a%20number%22)%20where%20the%20crop%20was%20not%20recorded.%20Comparing%20them%20with%20%60%3D%3D%60%20works%20the%20same%20as%20on%20%60%3CU%60%20text%20(section%202).%0A%20%20%20%20-%20%60fertilizer%60%20already%20shows%20a%20%60nan%60%3A%20fertilizer%20was%20not%20recorded%20for%20that%20plot.%20Section%204%20deals%20with%20missing%20numbers.%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%20A%20first%20look%20at%20the%20areas%2C%20as%20a%20histogram%20(matplotlib%2C%20as%20in%20module%201)%3A%20how%20many%20plots%20fall%20in%20each%20range%20of%20area.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(area)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%203)%2C%20layout%3D%22constrained%22)%0A%20%20%20%20_ax.hist(area%2C%20bins%3D30)%20%20%23%2030%20equal%20ranges%20from%20the%20smallest%20area%20to%20the%20largest%0A%20%20%20%20_ax.annotate(%22a%20few%20plots%20per%20bar%20out%20here%2C%20too%20few%20to%20see%22%2C%20xy%3D(3500%2C%2015)%2C%20xytext%3D(1900%2C%20900)%2C%20arrowprops%3D%7B%22arrowstyle%22%3A%20%22-%3E%22%7D)%0A%20%20%20%20_ax.set_xlabel(%22area%2C%20as%20recorded%22)%0A%20%20%20%20_ax.set_ylabel(%22plots%22)%0A%20%20%20%20_ax.set_title(%22Plot%20areas%2C%20as%20recorded%3A%20almost%20all%20in%20the%20first%20bar%22)%0A%20%20%20%20_ax%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%20Almost%20every%20plot%20is%20in%20the%20first%20bar%2C%20yet%20the%20axis%20runs%20out%20to%206%2C000%3A%20a%20few%20plots%20sit%20out%20there%2C%20between%20500%20and%206%2C000%2C%20in%20bars%20too%20low%20to%20see.%20No%20plot%20in%20this%20survey%20covers%206%2C000%20hectares%3A%20those%20areas%20are%20in%20**square%20meters**%2C%20as%20their%20%60area_unit%60%20says%20(%60unit%60%20above).%20One%20hectare%20is%2010%2C000%20square%20meters.%20Section%202%20finds%20these%20plots%2C%20and%20section%203%20converts%20them.%0A%0A%20%20%20%20%23%23%23%23%20Arrays%20without%20data%0A%0A%20%20%20%20Often%20you%20need%20an%20array%20before%20you%20have%20data%3A%20a%20range%20of%20numbers%2C%20evenly%20spaced%20class%20limits%2C%20a%20tally%20that%20starts%20at%20zero.%20NumPy%20makes%20these%20directly%3A%0A%0A%20%20%20%20%7C%20function%20%7C%20gives%20%7C%20example%20%7C%20result%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60np.arange(start%2C%20stop%2C%20step)%60%20%7C%20numbers%20from%20%60start%60%20up%20to%2C%20**not%20including**%2C%20%60stop%60%2C%20as%20%60range%60%20does%20%7C%20%60np.arange(1%2C%2013)%60%20%7C%20the%20month%20numbers%201%20to%2012%2C%20%60int64%60%20%7C%0A%20%20%20%20%7C%20%60np.linspace(start%2C%20stop%2C%20num)%60%20%7C%20%60num%60%20evenly%20spaced%20numbers%2C%20**including**%20both%20ends%20%7C%20%60np.linspace(0%2C%204%2C%209)%60%20%7C%20%600.0%2C%200.5%2C%20%E2%80%A6%2C%204.0%60%2C%20%60float64%60%20%7C%0A%20%20%20%20%7C%20%60np.zeros(shape)%60%20%7C%20zeros%20%7C%20%60np.zeros(7)%60%20%7C%20a%20tally%20of%207%20provinces%2C%20%60float64%60%20%7C%0A%20%20%20%20%7C%20%60np.ones(shape)%60%20%7C%20ones%20%7C%20%60np.ones((2%2C%203))%60%20%7C%202%20rows%20of%203%20ones%2C%20%60float64%60%20%7C%0A%0A%20%20%20%20%60zeros%60%20and%20%60ones%60%20give%20decimals%20unless%20you%20ask%20for%20another%20dtype%3A%20%60np.zeros(7%2C%20dtype%3Dint)%60.%20A%20shape%20of%20more%20than%20one%20number%20goes%20in%20brackets%3A%20%60np.ones((2%2C%203))%60.%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%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20code%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60np.arange(1%2C%2013)%60%20%7C%20%60%7Bnp.arange(1%2C%2013)%7D%60%20%7C%0A%20%20%20%20%7C%20%60np.arange(0%2C%201%2C%200.25)%60%20%7C%20%60%7Bnp.arange(0%2C%201%2C%200.25)%7D%60%20(stops%20before%201)%20%7C%0A%20%20%20%20%7C%20%60np.linspace(0%2C%201%2C%205)%60%20%7C%20%60%7Bnp.linspace(0%2C%201%2C%205)%7D%60%20(ends%20at%201)%20%7C%0A%20%20%20%20%7C%20%60np.zeros(3%2C%20dtype%3Dint)%60%20%7C%20%60%7Bnp.zeros(3%2C%20dtype%3Dint)%7D%60%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%23%20reshape%3A%20the%20same%20values%2C%20in%20rows%20and%20columns%0A%0A%20%20%20%20%60.reshape(rows%2C%20columns)%60%20lays%20an%20array's%20values%20out%20in%20a%20new%20shape%2C%20filling%20the%20first%20row%2C%20then%20the%20next.%20The%20values%20and%20their%20order%20don't%20change%2C%20so%20the%20new%20shape%20must%20hold%20exactly%20as%20many%3A%2012%20values%20fit%204%20rows%20of%203%2C%20or%202%20rows%20of%206%2C%20but%20not%205%20rows%20of%203.%20Give%20one%20number%20as%20%60-1%60%20and%20NumPy%20works%20it%20out%3A%20%60.reshape(-1%2C%203)%60%20is%20%22rows%20of%203%2C%20as%20many%20rows%20as%20it%20takes%22.%0A%0A%20%20%20%20Pick%20a%20shape%20for%20the%2012%20month%20numbers%20%60np.arange(1%2C%2013)%60%2C%20and%20read%20what%20comes%20back%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%20reshape_choice%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%7B%224%20rows%20of%203%20(quarters)%22%3A%20(4%2C%203)%2C%20%223%20rows%20of%204%22%3A%20(3%2C%204)%2C%20%222%20rows%20of%206%20(half-years)%22%3A%20(2%2C%206)%2C%20%221%20row%20of%2012%22%3A%20(1%2C%2012)%2C%20%2212%20rows%20of%201%22%3A%20(12%2C%201)%2C%20%225%20rows%20of%203%22%3A%20(5%2C%203)%7D%2C%0A%20%20%20%20%20%20%20%20value%3D%224%20rows%20of%203%20(quarters)%22%2C%20label%3D%22Reshape%20the%2012%20months%20into%22%2C%0A%20%20%20%20)%0A%20%20%20%20reshape_choice%0A%20%20%20%20return%20(reshape_choice%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(reshape_choice)%3A%0A%20%20%20%20_rows%2C%20_cols%20%3D%20reshape_choice.value%0A%20%20%20%20_code%20%3D%20f%22np.arange(1%2C%2013).reshape(%7B_rows%7D%2C%20%7B_cols%7D)%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_result%20%3D%20np.arange(1%2C%2013).reshape(_rows%2C%20_cols)%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.vstack(%5Bmo.md(f%22%60%7B_code%7D%60%20has%20shape%20%60%7B_result.shape%7D%60%3A%22)%2C%20mo.plain_text(repr(_result))%5D)%0A%20%20%20%20except%20ValueError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.callout(mo.md(f%22%60%7B_code%7D%60%20raises%20**ValueError**%3A%20%60%7B_error%7D%60%5Cn%5CnRead%20it%20from%20the%20end%3A%2012%20values%20cannot%20fill%20%7B_rows%7D%20%C3%97%20%7B_cols%7D%20%3D%20%7B_rows%20*%20_cols%7D%20places.%20NumPy%20refuses%20rather%20than%20drop%20or%20invent%20values.%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20_shown%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%20Exercise%202.1.%20Class%20limits%20for%20a%20histogram%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20histogram%20above%20let%20matplotlib%20choose%20its%20ranges.%20A%20report%20fixes%20its%20own%2C%20so%20charts%20from%20different%20years%20compare%3A%20plots%20of%200%20to%200.5%20hectares%2C%200.5%20to%201%2C%20and%20so%20on.%20A%20histogram%20takes%20these%20**class%20limits**%20(its%20%60bins%3D%60)%20as%20an%20array.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20lowest%20limit%2C%20the%20highest%20limit%20and%20the%20number%20of%20classes%20%E2%86%92%20an%20array%20of%20the%20limits%2C%20decimals%20(%60float64%60)%2C%20lowest%20first.%0A%0A%20%20%20%20%7C%20%60low%60%20%7C%20%60high%60%20%7C%20%60n_classes%60%20%7C%20limits%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%200%20%7C%204%20%7C%208%20%7C%20%60%5B0.0%2C%200.5%2C%201.0%2C%201.5%2C%202.0%2C%202.5%2C%203.0%2C%203.5%2C%204.0%5D%60%20%7C%0A%20%20%20%20%7C%200%20%7C%201%20%7C%204%20%7C%20%60%5B0.0%2C%200.25%2C%200.5%2C%200.75%2C%201.0%5D%60%20%7C%0A%0A%20%20%20%20Count%20them%3A%208%20classes%20need%20**9**%20limits%2C%20as%208%20sections%20of%20fence%20need%209%20posts.%0A%0A%20%20%20%20**Tools%3A**%20%60np.linspace(start%2C%20stop%2C%20num)%60%2C%20from%20*Arrays%20without%20data*%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20class_edges(low%3A%20float%2C%20high%3A%20float%2C%20n_classes%3A%20int)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22The%20limits%20of%20n_classes%20equal%20classes%20from%20low%20to%20high%2C%20both%20ends%20included%2C%20as%20decimals.%22%22%22%0A%20%20%20%20%23%20Exercise%202.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20work%20out%20how%20many%20limits%20n_classes%20classes%20need%0A%20%20%20%20%23%20Step%202%3A%20return%20that%20many%20evenly%20spaced%20numbers%20from%20low%20to%20high%2C%20both%20included%20(np.linspace)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.1%22)%0A%0A%0A%40app.function%0Adef%20with_kind(result)%3A%0A%20%20%20%20%22%22%22An%20array%20result%20with%20the%20kind%20of%20values%20it%20holds%2C%20for%20the%20checks%3A%20%5B1%2C%202%5D%20and%20%5B1.0%2C%202.0%5D%20are%20different%20answers.%22%22%22%0A%20%20%20%20if%20isinstance(result%2C%20np.ndarray)%3A%0A%20%20%20%20%20%20%20%20kinds%20%3D%20%7B%22b%22%3A%20%22True%2FFalse%22%2C%20%22i%22%3A%20%22whole%20numbers%22%2C%20%22u%22%3A%20%22whole%20numbers%22%2C%20%22f%22%3A%20%22decimals%22%7D%0A%20%20%20%20%20%20%20%20return%20%7B%22holds%22%3A%20kinds.get(result.dtype.kind%2C%20str(result.dtype))%2C%20%22array%22%3A%20result%7D%0A%20%20%20%20return%20result%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%22Exercise%202.1%22%2C%20class_edges%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%220%20to%204%20hectares%20in%208%20classes%22%2C%20(0%2C%204%2C%208))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%220%20to%201%20hectare%20in%204%20classes%22%2C%20(0%2C%201%2C%204))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%222%20to%206%20in%202%20classes%20(whole-number%20limits)%22%2C%20(2%2C%206%2C%202))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%220.5%20to%202%20in%203%20classes%22%2C%20(0.5%2C%202.0%2C%203))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20classes%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20the%20limits%20first%3A%208%20classes%20need%209.%20The%20highest%20limit%20is%20one%20of%20them%2C%20and%20every%20limit%20is%20a%20decimal.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20try_array(function%2C%20*args)%3A%0A%20%20%20%20%22%22%22A%20Try%20it%20cell's%20display%3A%20the%20array%20your%20function%20gives%20back%2C%20with%20its%20shape%20and%20dtype%2C%20or%20why%20it%20gave%20none.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20function(*args)%0A%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60%7Bfunction.__name__%7D%60%3A%20**not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%20NotImplementedError%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20return%20course.try_it(function%2C%20*args)%20%20%23%20the%20course's%20own%20explanation%20of%20the%20error%0A%20%20%20%20if%20not%20isinstance(result%2C%20np.ndarray)%3A%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20a%20**%7Btype(result).__name__%7D**%2C%20not%20a%20NumPy%20array%3A%20%60%7Bresult!r%3A.200%7D%60%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20an%20array%20of%20shape%20**%60%7Bresult.shape%7D%60**%2C%20dtype%20**%60%7Bresult.dtype%7D%60**%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.plain_text(repr(result))%2C%0A%20%20%20%20%5D)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20half-hectare%20classes%20from%200%20to%205%20hectares%2C%20for%20the%20chart%20below.%0A%20%20%20%20try_array(class_edges%2C%200%2C%205%2C%2010)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(area)%3A%0A%20%20%20%20_edges%20%3D%20attempt(class_edges%2C%200%2C%205%2C%2010)%0A%20%20%20%20mo.stop(_edges%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.1%22))%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%203)%2C%20layout%3D%22constrained%22)%0A%20%20%20%20_ax.hist(area%2C%20bins%3D_edges%2C%20edgecolor%3D%22white%22)%20%20%23%20areas%20outside%20the%20limits%20are%20left%20out%20of%20every%20bar%0A%20%20%20%20_ax.set_xlabel(%22area%20(ha)%22)%0A%20%20%20%20_ax.set_ylabel(%22plots%22)%0A%20%20%20%20_ax.set_title(%22Plots%20recorded%20in%20hectares%2C%20in%20half-hectare%20classes%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_ax%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Same%20areas%2C%20your%20limits.%20Every%20area%20in%20square%20meters%20is%20far%20above%205%2C%20outside%20every%20class%2C%20so%20%60hist%60%20leaves%20it%20out%3A%20this%20chart%20shows%20the%20plots%20recorded%20in%20hectares%20only.%20Most%20of%20them%20are%20under%20one%20hectare.%22)%2C%0A%20%20%20%20%5D)%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%20Exercise%202.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%208%20limits%20instead%20of%209%2C%20with%20widths%20such%20as%200.571%20%7C%20One%20limit%20too%20few%3A%20as%20many%20limits%20as%20classes.%20%7C%20%60n_classes%20%2B%201%60%20%7C%0A%20%20%20%20%7C%20The%20last%20limit%20(4.0)%20is%20missing%20%7C%20%60np.arange%60%20stops%20before%20its%20%60stop%60.%20%7C%20%60np.linspace%60%2C%20which%20includes%20both%20ends%20%7C%0A%20%20%20%20%7C%20*holds%20whole%20numbers*%20for%202%20to%206%20%7C%20%60np.arange%60%20with%20whole%20numbers%20gives%20%60int64%60.%20%7C%20%60np.linspace%60%2C%20which%20always%20gives%20decimals%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20'float'%20object%20cannot%20be%20interpreted%20as%20an%20integer*%20%7C%20The%20number%20of%20limits%20passed%20to%20%60linspace%60%20is%20a%20decimal%2C%20as%20after%20a%20%60%2F%60.%20%7C%20%60num%60%20must%20be%20a%20whole%20number%3A%20%60n_classes%20%2B%201%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%23%20The%20farmgate%20prices%2C%20as%20a%20grid%0A%0A%20%20%20%20The%20prices%20come%20as%20one%20file%20per%20month%20(module%205%20reads%20them).%20Here%20they%20arrive%20ready%20as%20a%20**grid**%3A%20one%20row%20per%20province%2C%20one%20column%20per%20month.%20The%20function%20below%20builds%20it%20with%20pandas%3B%20you%20don't%20need%20to%20follow%20its%20lines%20yet%2C%20only%20what%20it%20gives%20back.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20price_grid(commodity%3A%20str)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22One%20commodity's%20farmgate%20prices%20(PHP%2Fkg)%3A%20a%20row%20per%20province%20(PROVINCES)%2C%20a%20column%20per%20month%20(MONTHS)%2C%20nan%20where%20a%20month%20has%20no%20price.%22%22%22%0A%20%20%20%20prices%20%3D%20course.prices()%20%20%23%20every%20monthly%20price%20file%20in%20one%20long%20table%20(module%205%20builds%20this)%0A%20%20%20%20one%20%3D%20prices%5Bprices%5B%22commodity%22%5D%20%3D%3D%20commodity%5D%0A%20%20%20%20table%20%3D%20one.pivot_table(index%3D%22province%22%2C%20columns%3D%22month%22%2C%20values%3D%22price%22)%20%20%23%20provinces%20down%2C%20months%20across%20(module%204)%0A%20%20%20%20table%20%3D%20table.reindex(index%3DPROVINCES%2C%20columns%3DMONTHS)%20%20%23%20every%20province%20and%20month%20in%20order%3B%20a%20month%20with%20no%20file%20gets%20nan%0A%20%20%20%20return%20table.to_numpy()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20palay%20%3D%20price_grid(%22Palay%22)%20%20%23%20palay%3A%20rice%20as%20harvested%2C%20before%20milling%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60palay%60%20has%20shape%20%60%7Bpalay.shape%7D%60%3A%20**%7Bpalay.shape%5B0%5D%7D%20rows**%20(provinces%2C%20%60PROVINCES%60)%20and%20**%7Bpalay.shape%5B1%5D%7D%20columns**%20(months%2C%20January%202022%20to%20December%202025%2C%20%60MONTHS%60).%20Its%20dtype%20is%20%60%7Bpalay.dtype%7D%60%3A%20prices%20in%20pesos%20per%20kilogram.%0A%0A%20%20%20%20One%20column%20is%20all%20%60nan%60%3A%20September%202023%2C%20whose%20file%20never%20arrived.%20That%20month%20is%20still%20in%20the%20grid%2C%20as%20a%20column%20of%20%22no%20value%20here%22%2C%20so%20every%20later%20month%20stays%20in%20its%20place.%20Section%204%20deals%20with%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%20(palay%2C)%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%20Exercise%202.2.%20One%20row%20per%20year%0A%0A%20%20%20%20**Why%20it%20matters%3A**%2048%20monthly%20prices%20in%20one%20long%20row%20are%20hard%20to%20compare.%20Laid%20out%20as%20one%20row%20per%20year%2C%20January%20to%20December%2C%20each%20column%20is%20one%20calendar%20month%20across%20the%20years%2C%20and%20section%204%20averages%20a%20whole%20year%20in%20one%20step.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%201-D%20array%20of%20monthly%20values%2C%20starting%20in%20January%2C%20whose%20length%20is%20a%20whole%20number%20of%20years%20%E2%86%92%20the%20same%20values%2C%20one%20row%20per%20year%20and%2012%20columns.%0A%0A%20%20%20%20%7C%20input%20%7C%20output%20shape%20%7C%20output%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60np.arange(1%2C%2025)%60%20(24%20months)%20%7C%20%60(2%2C%2012)%60%20%7C%20row%200%20is%20%601%20%E2%80%A6%2012%60%2C%20row%201%20is%20%6013%20%E2%80%A6%2024%60%20%7C%0A%20%20%20%20%7C%2048%20monthly%20prices%20of%20one%20province%20%7C%20%60(4%2C%2012)%60%20%7C%20row%200%20is%202022%2C%20row%203%20is%202025%20%7C%0A%0A%20%20%20%20Your%20function%20must%20work%20for%20any%20number%20of%20whole%20years%2C%20not%20only%20four.%0A%0A%20%20%20%20**Tools%3A**%20%60.reshape(...)%60%20with%20%60-1%60%2C%20from%20*reshape*%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20by_year(monthly%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22Monthly%20values%20starting%20in%20January%2C%20laid%20out%20as%20one%20row%20per%20year%20of%2012%20months.%22%22%22%0A%20%20%20%20%23%20Exercise%202.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20row%20is%2012%20months%3B%20let%20NumPy%20work%20out%20how%20many%20rows%20(-1)%0A%20%20%20%20%23%20Step%202%3A%20return%20the%20reshaped%20array%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(palay)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.2%22%2C%20by_year%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20month%20numbers%20of%20one%20year%2C%20np.arange(1%2C%2013)%22%2C%20(np.arange(1%2C%2013)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20month%20numbers%20of%20three%20years%2C%20np.arange(1%2C%2037)%22%2C%20(np.arange(1%2C%2037)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22Nueva%20Ecija's%2048%20monthly%20palay%20prices%2C%20palay%5B2%5D%22%2C%20(palay%5B2%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22series%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Rows%20are%20years%20and%20columns%20are%20months%2C%20whatever%20the%20number%20of%20years%3A%20give%2012%20as%20the%20number%20of%20columns%20and%20let%20NumPy%20count%20the%20rows.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20%23%20Try%20it%3A%20Nueva%20Ecija's%20palay%20prices%2C%20one%20row%20per%20year.%20palay%5B2%5D%20is%20row%202%20of%20the%20grid%3B%20section%202%20explains%20positions.%0A%20%20%20%20try_array(by_year%2C%20palay%5B2%5D)%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%20Exercise%202.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*ValueError%3A%20cannot%20reshape%20array%20of%20size%2012%20into%20shape%20(4%2C12)*%20%7C%20The%20number%20of%20years%20is%20fixed%20at%204.%20%7C%20%60-1%60%20for%20the%20rows%20%7C%0A%20%20%20%20%7C%20Shape%20%60(12%2C%201)%60%20or%20%60(12%2C%203)%60%20%7C%20Rows%20and%20columns%20swapped%3A%2012%20rows.%20%7C%20%60reshape(-1%2C%2012)%60%3A%2012%20columns%20%7C%0A%20%20%20%20%7C%20Shape%20%60(3%2C%2012)%60%2C%20but%20the%20values%20are%20out%20of%20order%20%7C%20The%20array%20was%20transposed%20(%60.T%60)%20after%20reshaping%20to%2012%20rows.%20%7C%20rows%20fill%20first%3A%20reshape%20straight%20to%2012%20columns%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%202%20%C2%B7%20Indexing%2C%20slicing%20and%20boolean%20masks%0A%0A%20%20%20%20Section%201%20left%20two%20questions.%20*Which*%20plots%20are%20recorded%20in%20square%20meters%3F%20And%20for%20a%20later%20question%3A%20which%20plots%20grow%20irrigated%20rice%3F%20In%20Python%20Essentials%20you%20would%20loop%20over%20every%20plot%20with%20an%20%60if%60.%20Arrays%20answer%20both%20in%20one%20line%2C%20by%20**condition**.%20First%2C%20the%20simpler%20kind%20of%20selection%3A%20by%20**position**.%0A%0A%20%20%20%20%23%23%23%23%20By%20position%0A%0A%20%20%20%20Positions%20count%20from%200%2C%20as%20in%20a%20list.%20A%20**slice**%20%60start%3Astop%60%20takes%20from%20%60start%60%20up%20to%2C%20not%20including%2C%20%60stop%60%3B%20a%20negative%20position%20counts%20from%20the%20end.%0A%0A%20%20%20%20%7C%20code%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60production%5B0%5D%60%20%7C%20the%20first%20plot's%20harvest%20%7C%0A%20%20%20%20%7C%20%60production%5B-1%5D%60%20%7C%20the%20last%20plot's%20%7C%0A%20%20%20%20%7C%20%60production%5B%3A5%5D%60%20%7C%20the%20first%20five%20(positions%200%20to%204)%20%7C%0A%20%20%20%20%7C%20%60production%5B10%3A13%5D%60%20%7C%20positions%2010%2C%2011%20and%2012%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(production)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60production%5B0%5D%60%20is%20%60%7Bproduction%5B0%5D%7D%60%2C%20%60production%5B-1%5D%60%20is%20%60%7Bproduction%5B-1%5D%7D%60%2C%20%60production%5B%3A5%5D%60%20is%20%60%7Bproduction%5B%3A5%5D%7D%60%2C%20and%20%60production%5B10%3A13%5D%60%20is%20%60%7Bproduction%5B10%3A13%5D%7D%60.%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%20A%20grid%20takes%20two%20positions%2C%20**row%20first%2C%20then%20column**%2C%20separated%20by%20a%20comma%3A%20%60palay%5Brow%2C%20column%5D%60.%20A%20%60%3A%60%20alone%20means%20%22all%20of%20them%22.%0A%0A%20%20%20%20%7C%20code%20%7C%20gives%20%7C%20shape%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60palay%5B2%2C%200%5D%60%20%7C%20Nueva%20Ecija%20(row%202)%2C%20January%202022%20(column%200)%20%7C%20one%20number%20%7C%0A%20%20%20%20%7C%20%60palay%5B2%5D%60%2C%20the%20same%20as%20%60palay%5B2%2C%20%3A%5D%60%20%7C%20Nueva%20Ecija%2C%20every%20month%20%7C%20%60(48%2C)%60%20%7C%0A%20%20%20%20%7C%20%60palay%5B%3A%2C%200%5D%60%20%7C%20every%20province%2C%20January%202022%20%7C%20%60(7%2C)%60%20%7C%0A%20%20%20%20%7C%20%60palay%5B%3A%2C%20-12%3A%5D%60%20%7C%20every%20province%2C%20the%20last%2012%20months%20(2025)%20%7C%20%60(7%2C%2012)%60%20%7C%0A%20%20%20%20%7C%20%60palay%5B%3A3%2C%20%3A6%5D%60%20%7C%20the%20first%20three%20provinces%2C%20the%20first%20six%20months%20%7C%20%60(3%2C%206)%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60palay%5B2%2C%200%5D%60%20is%20%60%7Bpalay%5B2%2C%200%5D%7D%60%20pesos%20per%20kilogram.%20%60palay%5B%3A%2C%200%5D%60%20is%20%60%7Bpalay%5B%3A%2C%200%5D%7D%60%3A%20January%202022%20in%20each%20of%20the%20%7Blen(PROVINCES)%7D%20provinces%2C%20north%20to%20south.%20%60palay%5B%3A%2C%20-12%3A%5D.shape%60%20is%20%60%7Bpalay%5B%3A%2C%20-12%3A%5D.shape%7D%60.%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%20counts%20%3D%20np.arange(5)%20%20%23%20%5B0%201%202%203%204%5D%0A%20%20%20%20first_three%20%3D%20counts%5B%3A3%5D%20%20%23%20a%20slice%3A%20a%20window%20onto%20counts%2C%20not%20a%20copy%0A%20%20%20%20first_three%5B0%5D%20%3D%2099%20%20%23%20changes%20counts%20too%0A%20%20%20%20counts%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**A%20slice%20shares%20its%20values%20with%20the%20array%20it%20came%20from.**%20%60first_three%60%20holds%20no%20values%20of%20its%20own%3A%20it%20is%20a%20window%20onto%20%60counts%60%2C%20so%20changing%20it%20changed%20%60counts%60.%20NumPy%20does%20this%20so%20that%20slicing%20a%20large%20array%20costs%20nothing.%20When%20you%20mean%20to%20change%20values%20without%20touching%20the%20original%2C%20take%20a%20copy%3A%20%60counts%5B%3A3%5D.copy()%60.%0A%0A%20%20%20%20The%20survey's%20number%20arrays%20protect%20themselves%3A%20%60area%5B0%5D%20%3D%201%60%20raises%20**ValueError**%3A%20*assignment%20destination%20is%20read-only*.%20That%20is%20pandas%203%20guarding%20its%20table%20(below).%20You%20don't%20need%20to%20change%20them%20in%20this%20module%3A%20every%20exercise%20builds%20a%20new%20array.%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**If%20you%20know%20pandas%202.**%20In%20pandas%202%2C%20%60.to_numpy()%60%20on%20a%20column%20of%20numbers%20usually%20gave%20an%20array%20you%20could%20write%20into%2C%20and%20writing%20into%20it%20quietly%20changed%20the%20DataFrame%20as%20well.%20pandas%203%20copies%20on%20write%3A%20a%20DataFrame%20changes%20only%20through%20its%20own%20methods%2C%20so%20the%20array%20it%20hands%20over%20from%20a%20number%20column%20is%20**read-only**.%20Take%20%60.copy()%60%20of%20it%20when%20you%20need%20to%20change%20values.%20Module%203%20covers%20copy-on-write%20in%20full.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%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%23%20By%20condition%3A%20boolean%20masks%0A%0A%20%20%20%20Compare%20an%20array%20with%20a%20value%2C%20and%20you%20get%20an%20array%20of%20%60True%60%20and%20%60False%60%20of%20the%20same%20shape%2C%20one%20answer%20per%20element.%20This%20is%20a%20**boolean%20mask**%20(boolean%3A%20true%20or%20false).%20Put%20a%20mask%20in%20the%20square%20brackets%2C%20and%20you%20get%20the%20elements%20where%20it%20is%20%60True%60%3A%0A%0A%20%20%20%20%7C%20code%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60unit%20%3D%3D%20%22sqm%22%60%20%7C%202%2C164%20answers%3A%20is%20this%20plot%20recorded%20in%20square%20meters%3F%20%7C%0A%20%20%20%20%7C%20%60area%5Bunit%20%3D%3D%20%22sqm%22%5D%60%20%7C%20the%20areas%20of%20the%20plots%20where%20the%20answer%20is%20%60True%60%20%7C%0A%20%20%20%20%7C%20%60(unit%20%3D%3D%20%22sqm%22).sum()%60%20%7C%20how%20many%3A%20%60True%60%20counts%20as%201%20and%20%60False%60%20as%200%20%7C%0A%20%20%20%20%7C%20%60(unit%20%3D%3D%20%22sqm%22).mean()%60%20%7C%20the%20share%20of%20plots%2C%20between%200%20and%201%20%7C%0A%0A%20%20%20%20The%20mask%20and%20the%20array%20must%20be%20the%20same%20length%2C%20and%20they%20line%20up%20by%20position%3A%20the%20mask%20from%20%60unit%60%20selects%20from%20%60area%60%20because%20element%200%20of%20both%20is%20the%20same%20plot.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(area%2C%20unit)%3A%0A%20%20%20%20in_sqm%20%3D%20unit%20%3D%3D%20%22sqm%22%20%20%23%20one%20True%20or%20False%20per%20plot%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60in_sqm%5B%3A8%5D%60%20is%20%60%7Bin_sqm%5B%3A8%5D%7D%60%3A%20the%20second%20plot%20is%20in%20square%20meters.%0A%0A%20%20%20%20%60in_sqm.sum()%60%20is%20**%7Bin_sqm.sum()%7D**%20plots%2C%20%60in_sqm.mean()%60%20is%20**%7Bin_sqm.mean()%3A.3f%7D**%20of%20all%20plots%2C%20and%20%60area%5Bin_sqm%5D%5B%3A5%5D%60%20are%20their%20first%20areas%3A%20%60%7Barea%5Bin_sqm%5D%5B%3A5%5D%7D%60.%20Section%201's%20histogram%20had%20found%20them%3B%20now%20they%20have%20names.%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%23%20Combining%20conditions%3A%20%26%2C%20%7C%20and%20~%0A%0A%20%20%20%20-%20%60a%20%26%20b%60%20is%20**and**%2C%20both%20true%3A%20%60(crop%20%3D%3D%20%22Rice%22)%20%26%20(irrigated%20%3D%3D%20%22Yes%22)%60%20is%20irrigated%20rice.%0A%20%20%20%20-%20%60a%20%7C%20b%60%20is%20**or**%2C%20at%20least%20one%20true%3A%20%60(crop%20%3D%3D%20%22Rice%22)%20%7C%20(crop%20%3D%3D%20%22Corn%22)%60%20is%20rice%20or%20corn.%0A%20%20%20%20-%20%60~a%60%20is%20**not**%2C%20the%20opposite%3A%20%60~(irrigated%20%3D%3D%20%22Yes%22)%60%20is%20not%20irrigated.%0A%0A%20%20%20%20Two%20rules%2C%20both%20because%20Python%20reads%20these%20operators%20before%20%60%3D%3D%60%3A%0A%0A%20%20%20%201.%20**Put%20every%20comparison%20in%20brackets.**%20Without%20them%2C%20%60crop%20%3D%3D%20%22Rice%22%20%26%20irrigated%20%3D%3D%20%22Yes%22%60%20is%20read%20as%20%60crop%20%3D%3D%20(%22Rice%22%20%26%20irrigated)%20%3D%3D%20%22Yes%22%60%2C%20and%20Python%20tries%20to%20%22and%22%20a%20word%20with%20an%20array.%0A%20%20%20%202.%20**With%20arrays%2C%20use%20%60%26%60%2C%20%60%7C%60%20and%20%60~%60%2C%20not%20%60and%60%2C%20%60or%60%20and%20%60not%60.**%20The%20words%20ask%20one%20question%20of%20a%20whole%20array%20(is%20this%20array%20true%3F)%2C%20and%20an%20array%20of%202%2C164%20answers%20has%20no%20single%20answer.%0A%0A%20%20%20%20Pick%20a%20condition%20and%20read%20what%20comes%20back.%20The%20errors%20are%20worth%20reading%3A%20you%20will%20meet%20both.%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%20condition%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20%26%20(irrigated%20%3D%3D%20%22Yes%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20%7C%20(crop%20%3D%3D%20%22Corn%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'~(irrigated%20%3D%3D%20%22Yes%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20%26%20~(irrigated%20%3D%3D%20%22Yes%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'~(crop%20%3D%3D%20%22Rice%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'crop%20%3D%3D%20%22Rice%22%20%26%20irrigated%20%3D%3D%20%22Yes%22'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20and%20(irrigated%20%3D%3D%20%22Yes%22)'%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20value%3D'(crop%20%3D%3D%20%22Rice%22)%20%26%20(irrigated%20%3D%3D%20%22Yes%22)'%2C%20label%3D%22Condition%22%2C%0A%20%20%20%20)%0A%20%20%20%20condition%0A%20%20%20%20return%20(condition%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(condition%2C%20crop%2C%20irrigated)%3A%0A%20%20%20%20_conditions%20%3D%20%7B%0A%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20%26%20(irrigated%20%3D%3D%20%22Yes%22)'%3A%20(lambda%3A%20(crop%20%3D%3D%20%22Rice%22)%20%26%20(irrigated%20%3D%3D%20%22Yes%22)%2C%20%22Both%20must%20hold%3A%20rice%2C%20and%20irrigated.%22)%2C%0A%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20%7C%20(crop%20%3D%3D%20%22Corn%22)'%3A%20(lambda%3A%20(crop%20%3D%3D%20%22Rice%22)%20%7C%20(crop%20%3D%3D%20%22Corn%22)%2C%20%22Either%20will%20do.%20%60np.isin(crop%2C%20%5B%5C%22Rice%5C%22%2C%20%5C%22Corn%5C%22%5D)%60%20gives%20the%20same%20mask%2C%20and%20reads%20better%20for%20longer%20lists.%22)%2C%0A%20%20%20%20%20%20%20%20'~(irrigated%20%3D%3D%20%22Yes%22)'%3A%20(lambda%3A%20~(irrigated%20%3D%3D%20%22Yes%22)%2C%20%22The%20opposite%20of%20irrigated%3A%20here%20the%20same%20plots%20as%20%60irrigated%20%3D%3D%20%5C%22No%5C%22%60%2C%20since%20every%20plot%20says%20Yes%20or%20No.%22)%2C%0A%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20%26%20~(irrigated%20%3D%3D%20%22Yes%22)'%3A%20(lambda%3A%20(crop%20%3D%3D%20%22Rice%22)%20%26%20~(irrigated%20%3D%3D%20%22Yes%22)%2C%20%22Rain-fed%20rice%3A%20%60~%60%20turns%20one%20mask%20around%20before%20%60%26%60%20combines%20it.%22)%2C%0A%20%20%20%20%20%20%20%20'~(crop%20%3D%3D%20%22Rice%22)'%3A%20(lambda%3A%20~(crop%20%3D%3D%20%22Rice%22)%2C%20%22Careful%3A%20%5C%22not%20rice%5C%22%20includes%20the%2014%20plots%20with%20no%20crop%20recorded%20(%60nan%60%20is%20not%20equal%20to%20%5C%22Rice%5C%22).%20Not%20rice%20is%20not%20the%20same%20as%20another%20crop.%22)%2C%0A%20%20%20%20%20%20%20%20'crop%20%3D%3D%20%22Rice%22%20%26%20irrigated%20%3D%3D%20%22Yes%22'%3A%20(lambda%3A%20crop%20%3D%3D%20%22Rice%22%20%26%20irrigated%20%3D%3D%20%22Yes%22%2C%20%22Read%20from%20the%20end%3A%20*for%20%26%3A%20'str'%20and%20'str'*.%20Without%20brackets%2C%20%60%26%60%20came%20first%20and%20was%20given%20the%20text%20%5C%22Rice%5C%22%20and%20the%20texts%20of%20%60irrigated%60.%20The%20fix%3A%20a%20bracket%20around%20each%20comparison.%22)%2C%0A%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20and%20(irrigated%20%3D%3D%20%22Yes%22)'%3A%20(lambda%3A%20(crop%20%3D%3D%20%22Rice%22)%20and%20(irrigated%20%3D%3D%20%22Yes%22)%2C%20%22Read%20from%20the%20end%3A%20*the%20truth%20value%20of%20an%20array%20with%20more%20than%20one%20element%20is%20ambiguous*.%20%60and%60%20needs%20one%20True%20or%20False%2C%20and%20an%20array%20of%202%2C164%20has%20none%3A%20some%20are%20True%20and%20some%20are%20False.%20The%20fix%3A%20%60%26%60%2C%20which%20combines%20the%20two%20masks%20element%20by%20element.%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20_make%2C%20_note%20%3D%20_conditions%5Bcondition.value%5D%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_mask%20%3D%20_make()%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.callout(mo.md(f%22%60%7Bcondition.value%7D%60%20gives%20a%20mask%3B%20**%7B_mask.sum()%3A%2C%7D**%20of%20%7B_mask.size%3A%2C%7D%20plots%20are%20%60True%60.%5Cn%5Cn%7B_note%7D%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20except%20Exception%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.callout(mo.md(f%22%60%7Bcondition.value%7D%60%20raises%20**%7Btype(_error).__name__%7D**%3A%20%60%7B_error%7D%60%5Cn%5Cn%7B_note%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20_shown%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%20Exercise%202.3.%20Irrigated%20rice%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20irrigation%20is%20the%20largest%20difference%20between%20rice%20plots.%20Section%203%20compares%20the%20yields%20of%20irrigated%20and%20rain-fed%20rice%2C%20and%20both%20groups%20start%20from%20this%20mask.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots'%20%60crop%60%20and%20%60irrigated%60%20arrays%20%E2%86%92%20a%20boolean%20mask%2C%20%60True%60%20for%20each%20plot%20that%20grows%20rice%20**and**%20is%20irrigated.%0A%0A%20%20%20%20%7C%20crop%20%7C%20irrigated%20%7C%20in%20the%20mask%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Rice%20%7C%20Yes%20%7C%20%60True%60%20%7C%0A%20%20%20%20%7C%20Rice%20%7C%20No%20%7C%20%60False%60%20%7C%0A%20%20%20%20%7C%20Corn%20%7C%20Yes%20%7C%20%60False%60%20%7C%0A%20%20%20%20%7C%20(not%20recorded)%20%7C%20Yes%20%7C%20%60False%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60%3D%3D%60%2C%20%60%26%60%20and%20brackets%2C%20from%20*Combining%20conditions*%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20irrigated_rice(crop%3A%20np.ndarray%2C%20irrigated%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22True%20for%20each%20plot%20that%20grows%20rice%20and%20is%20irrigated.%22%22%22%0A%20%20%20%20%23%20Exercise%202.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20mask%20for%20the%20rice%20plots%2C%20and%20a%20mask%20for%20the%20irrigated%20plots%0A%20%20%20%20%23%20Step%202%3A%20return%20both%20combined%2C%20so%20a%20plot%20must%20be%20in%20both%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.3%22)%0A%0A%0A%40app.function%0Adef%20small_plots()%20-%3E%20tuple%5Bnp.ndarray%2C%20np.ndarray%5D%3A%0A%20%20%20%20%22%22%22Six%20hand-made%20plots%2C%20used%20by%20the%20check%20of%20Exercise%202.3%3A%20one%20of%20each%20kind%20the%20mask%20must%20tell%20apart.%22%22%22%0A%20%20%20%20crop%20%3D%20np.array(%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Corn%22%2C%20%22Corn%22%2C%20np.nan%2C%20%22Rice%22%5D%2C%20dtype%3Dobject)%20%20%23%20object%2C%20as%20.to_numpy()%20gives%0A%20%20%20%20irrigated%20%3D%20np.array(%5B%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22Yes%22%5D%2C%20dtype%3Dobject)%0A%20%20%20%20return%20crop%2C%20irrigated%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(crop%2C%20irrigated)%3A%0A%20%20%20%20_crop%2C%20_irrigated%20%3D%20small_plots()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.3%22%2C%20irrigated_rice%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22six%20plots%3A%20irrigated%20rice%2C%20rain-fed%20rice%2C%20irrigated%20corn%2C%20rain-fed%20corn%2C%20irrigated%20with%20no%20crop%20recorded%2C%20irrigated%20rice%22%2C%20(_crop%2C%20_irrigated))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20rice%20plots%2C%20none%20irrigated%22%2C%20(np.array(%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%5D%2C%20dtype%3Dobject)%2C%20np.array(%5B%22No%22%2C%20%22No%22%2C%20%22No%22%5D%2C%20dtype%3Dobject)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20plots%20typed%20as%20np.array(%5B...%5D)%20text%20(%3CU%20dtype)%2C%20not%20object%22%2C%20(np.array(%5B%22Rice%22%2C%20%22Corn%22%5D)%2C%20np.array(%5B%22Yes%22%2C%20%22Yes%22%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20plot%20in%20the%20survey%22%2C%20(crop%2C%20irrigated))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20plots%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20hint%3D%22A%20plot%20is%20in%20the%20mask%20only%20when%20both%20conditions%20hold%2C%20and%20each%20comparison%20needs%20brackets%20of%20its%20own.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(crop%2C%20irrigated)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20mask%20for%20the%20whole%20survey.%20Its%20sum%20is%20the%20number%20of%20irrigated%20rice%20plots.%0A%20%20%20%20try_array(irrigated_rice%2C%20crop%2C%20irrigated)%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%20Exercise%202.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%20*ValueError%3A%20The%20truth%20value%20of%20an%20array%20with%20more%20than%20one%20element%20is%20ambiguous*%20%7C%20%60and%60%20instead%20of%20%60%26%60.%20%7C%20%60%26%60%20between%20the%20two%20masks%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20unsupported%20operand%20type(s)%20for%20%26%3A%20'str'%20and%20'str'*%20%7C%20A%20comparison%20without%20brackets%3A%20%60%26%60%20ran%20before%20%60%3D%3D%60.%20%7C%20%60(crop%20%3D%3D%20%22Rice%22)%20%26%20(irrigated%20%3D%3D%20%22Yes%22)%60%20%7C%0A%20%20%20%20%7C%20Irrigated%20corn%20is%20%60True%60%20%7C%20The%20%22or%22%20operator%20instead%20of%20%60%26%60%20(and).%20%7C%20%60%26%60%20%7C%0A%20%20%20%20%7C%20Every%20plot%20is%20%60False%60%20%7C%20A%20spelling%20the%20data%20doesn't%20use%2C%20such%20as%20%60%22rice%22%60%20or%20%60%22yes%22%60.%20%7C%20the%20exact%20text%3A%20%60%22Rice%22%60%2C%20%60%22Yes%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%20Exercise%202.4.%20One%20year%20of%20prices%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20reports%20compare%20years%3A%202025%20against%202024.%20In%20the%20price%20grid%20a%20year%20is%20twelve%20columns%20side%20by%20side%2C%20and%20taking%20them%20is%20a%20slice%20by%20position.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20grid%20with%20one%20row%20per%20province%20and%20one%20column%20per%20month%2C%20starting%20in%20**January%202022**%2C%20and%20a%20year%20%E2%86%92%20the%20columns%20of%20that%20year%2C%20every%20row%3A%20shape%20%60(rows%2C%2012)%60.%0A%0A%20%20%20%20The%20year's%20first%20column%20is%2012%20for%20each%20year%20since%202022%3A%0A%0A%20%20%20%20%7C%20year%20%7C%20columns%20%7C%20slice%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%202022%20%7C%200%20to%2011%20%7C%20%600%3A12%60%20%7C%0A%20%20%20%20%7C%202023%20%7C%2012%20to%2023%20%7C%20%6012%3A24%60%20%7C%0A%20%20%20%20%7C%202025%20%7C%2036%20to%2047%20%7C%20%6036%3A48%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20grid%20slice%20%60grid%5B%3A%2C%20start%3Astop%5D%60%2C%20from%20*By%20position*%20above.%20Work%20out%20%60start%60%20and%20%60stop%60%20from%20%60year%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20prices_in_year(grid%3A%20np.ndarray%2C%20year%3A%20int)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22The%2012%20columns%20of%20one%20year%20from%20a%20grid%20of%20months%20that%20starts%20in%20January%202022%3A%20every%20row%2C%20shape%20(rows%2C%2012).%22%22%22%0A%20%20%20%20%23%20Exercise%202.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20year's%20first%20column%3A%2012%20columns%20for%20each%20year%20since%202022%0A%20%20%20%20%23%20Step%202%3A%20return%20every%20row%20(%3A)%20and%20the%2012%20columns%20from%20there%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.4%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(palay)%3A%0A%20%20%20%20_small%20%3D%20np.arange(72).reshape(2%2C%2036)%20%20%23%202%20rows%2C%2036%20months%3A%20January%202022%20to%20December%202024%2C%20numbered%200%20to%2071%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.4%22%2C%20prices_in_year%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20grid%20of%202%20rows%20and%2036%20months%20(2022%20to%202024)%2C%20year%202022%22%2C%20(_small%2C%202022))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20grid%2C%20year%202023%22%2C%20(_small%2C%202023))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20grid%2C%20year%202024%2C%20its%20last%2012%20columns%22%2C%20(_small%2C%202024))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20palay%20grid%2C%20year%202023%20(with%20its%20missing%20month)%22%2C%20(palay%2C%202023))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20palay%20grid%2C%20year%202025%22%2C%20(palay%2C%202025))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22grids%20and%20years%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Every%20row%2C%2012%20columns.%20The%20year's%20first%20column%20is%20(year%20%E2%88%92%202022)%20%C3%97%2012%2C%20and%20a%20slice's%20stop%20is%20not%20included.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20%23%20Try%20it%3A%202023%20in%20every%20province.%20Column%208%20of%20the%20result%20is%20September%202023%2C%20the%20month%20with%20no%20file.%0A%20%20%20%20try_array(prices_in_year%2C%20palay%2C%202023)%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%20Exercise%202.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%20Shape%20%60(2%2C%2036)%60%2C%20%60(0%2C%2036)%60%20or%20%60(0%2C%2048)%60%20%7C%20The%20slice%20took%20rows%2C%20not%20columns.%20%7C%20%60grid%5B%3A%2C%20start%3Astop%5D%60%20%7C%0A%20%20%20%20%7C%20Shape%20%60(2%2C%2011)%60%20%7C%20The%20stop%20is%20one%20short%3A%20it%20is%20not%20included.%20%7C%20%60stop%20%3D%20start%20%2B%2012%60%20%7C%0A%20%20%20%20%7C%20The%20values%20are%20one%20month%20late%20%7C%20The%20start%20counts%20from%201%2C%20or%20from%202021.%20%7C%202022%20starts%20at%20column%200%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%203%20%C2%B7%20Vectorized%20arithmetic%20and%20broadcasting%0A%0A%20%20%20%20A%20plot's%20**yield**%20is%20its%20harvest%20per%20hectare%3A%20production%20in%20tonnes%20divided%20by%20area%20in%20hectares.%20In%20Python%20Essentials%20that%20is%20a%20loop%20over%202%2C164%20plots%2C%20appending%20one%20result%20at%20a%20time.%20With%20arrays%20it%20is%20one%20line%2C%20%60production%20%2F%201000%20%2F%20area_ha%60%3A%20arithmetic%20between%20two%20arrays%20of%20the%20same%20shape%20works%20**element%20by%20element**%20(element%200%20with%20element%200%2C%20and%20so%20on)%2C%20and%20arithmetic%20with%20a%20single%20number%20applies%20it%20to%20every%20element.%20This%20is%20**vectorized**%20arithmetic%3A%20the%20loop%20still%20happens%2C%20inside%20NumPy%2C%20many%20times%20faster%20than%20a%20Python%20loop.%0A%0A%20%20%20%20The%20one%20line%20needs%20%60area_ha%60%2C%20every%20area%20in%20hectares%2C%20and%20that%20is%20the%20first%20thing%20to%20fix.%0A%0A%20%20%20%20%23%23%23%23%20Which%20dtype%20comes%20out%0A%0A%20%20%20%20%7C%20operation%20%7C%20example%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%2F%60%20divides%2C%20always%20to%20decimals%20%7C%20%60production%5B%3A5%5D%20%2F%201000%60%20%7C%20%60float64%60%2C%20tonnes%20with%20their%20decimals%20%7C%0A%20%20%20%20%7C%20%60%2F%2F%60%20divides%20and%20drops%20the%20remainder%20%7C%20%60production%5B%3A5%5D%20%2F%2F%201000%60%20%7C%20%60int64%60%3A%201%2C640%20kg%20becomes%201%20tonne%2C%20700%20kg%20becomes%200%20%7C%0A%20%20%20%20%7C%20whole%20numbers%20and%20decimals%20together%20%7C%20%60production%20*%201.0%60%20%7C%20%60float64%60%20%7C%0A%20%20%20%20%7C%20comparisons%20%7C%20%60production%20%3E%201000%60%20%7C%20%60bool%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(production)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60production%5B%3A5%5D%60%20is%20%60%7Bproduction%5B%3A5%5D%7D%60.%20Divided%20with%20%60%2F%201000%60%3A%20%60%7Bproduction%5B%3A5%5D%20%2F%201000%7D%60.%20With%20%60%2F%2F%201000%60%3A%20%60%7Bproduction%5B%3A5%5D%20%2F%2F%201000%7D%60%3A%20the%20second%20plot's%20700%20kg%20became%20nothing.%20Use%20%60%2F%2F%60%20only%20when%20dropping%20the%20remainder%20is%20what%20you%20mean.%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%23%20np.where%3A%20one%20value%20or%20another%2C%20element%20by%20element%0A%0A%20%20%20%20%60np.where(condition%2C%20if_true%2C%20if_false)%60%20builds%20a%20new%20array%3A%20where%20the%20mask%20is%20%60True%60%20it%20takes%20the%20element%20of%20%60if_true%60%2C%20elsewhere%20the%20element%20of%20%60if_false%60.%20All%20three%20line%20up%20by%20position.%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_sizes%20%3D%20np.array(%5B3%2C%2012%2C%207%5D)%0A%20%20%20%20np.where(_sizes%20%3E%205%2C%20%22large%22%2C%20%22small%22)%20%20%23%20one%20answer%20per%20element%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%20Exercise%202.5.%20Every%20area%20in%20hectares%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20section%201's%20histogram%20showed%20151%20plots%20recorded%20in%20square%20meters.%20Left%20as%20they%20are%2C%20a%20plot%20of%201%2C940%20square%20meters%20counts%20as%201%2C940%20hectares%2C%20and%20its%20yield%20comes%20out%2010%2C000%20times%20too%20small.%20Every%20total%2C%20mean%20and%20yield%20of%20area%20needs%20one%20unit.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots'%20%60area%60%20and%20%60unit%60%20arrays%20%E2%86%92%20every%20area%20in%20hectares%2C%20as%20decimals.%20One%20hectare%20is%2010%2C000%20square%20meters.%0A%0A%20%20%20%20%7C%20area%20%7C%20unit%20%7C%20area%20in%20hectares%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%200.5%20%7C%20ha%20%7C%200.5%20%7C%0A%20%20%20%20%7C%202500%20%7C%20sqm%20%7C%200.25%20%7C%0A%20%20%20%20%7C%2010000%20%7C%20sqm%20%7C%201.0%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20mask%20(section%202)%2C%20%60%2F%60%20(above)%20and%20%60np.where%60.%20Return%20a%20new%20array%3B%20don't%20change%20%60area%60%20itself.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20area_in_hectares(area%3A%20np.ndarray%2C%20unit%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22Every%20area%20in%20hectares%3A%20areas%20recorded%20in%20square%20meters%20divided%20by%2010%2C000%2C%20the%20others%20as%20they%20are.%22%22%22%0A%20%20%20%20%23%20Exercise%202.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20mask%20of%20the%20plots%20recorded%20in%20square%20meters%0A%20%20%20%20%23%20Step%202%3A%20return%2C%20with%20np.where%2C%20area%20%2F%2010_000%20where%20the%20mask%20is%20True%20and%20area%20where%20it%20is%20False%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.5%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(area%2C%20unit)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.5%22%2C%20area_in_hectares%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22five%20plots%3A%200.5%20ha%2C%202500%20sqm%2C%201.2%20ha%2C%2010000%20sqm%2C%203%20ha%22%2C%20(np.array(%5B0.5%2C%202500.0%2C%201.2%2C%2010000.0%2C%203.0%5D)%2C%20np.array(%5B%22ha%22%2C%20%22sqm%22%2C%20%22ha%22%2C%20%22sqm%22%2C%20%22ha%22%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20plots%20all%20in%20hectares%3A%20nothing%20to%20convert%22%2C%20(np.array(%5B0.25%2C%201.0%2C%202.75%5D)%2C%20np.array(%5B%22ha%22%2C%20%22ha%22%2C%20%22ha%22%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22areas%20typed%20as%20whole%20numbers%3A%205000%20sqm%20and%202%20ha%22%2C%20(np.array(%5B5000%2C%202%5D)%2C%20np.array(%5B%22sqm%22%2C%20%22ha%22%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20plot%20in%20the%20survey%22%2C%20(area%2C%20unit))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20plots%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Divide%20by%2010%2C000%20only%20where%20the%20unit%20is%20sqm%2C%20with%20%2F%20(not%20%2F%2F)%2C%20and%20return%20a%20new%20array%20of%20decimals.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(area%2C%20unit)%3A%0A%20%20%20%20%23%20Try%20it%3A%20every%20plot's%20area%20in%20hectares.%20The%20second%20plot%20was%201%2C940%20square%20meters.%0A%20%20%20%20try_array(area_in_hectares%2C%20area%2C%20unit)%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%20Exercise%202.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%202500%20is%20still%202500%20%7C%20The%20square%20meters%20were%20not%20converted.%20%7C%20%60np.where(unit%20%3D%3D%20%22sqm%22%2C%20...)%60%20%7C%0A%20%20%20%20%7C%202500%20became%202.5%20%7C%20Divided%20by%201%2C000.%20%7C%2010%2C000%20square%20meters%20in%20a%20hectare%20%7C%0A%20%20%20%20%7C%200.5%20ha%20became%200.00005%20%7C%20The%20mask%20picks%20the%20hectare%20plots.%20%7C%20%60unit%20%3D%3D%20%22sqm%22%60%20%7C%0A%20%20%20%20%7C%202500%20became%200%2C%20or%205000%20became%200%20%7C%20%60%2F%2F%60%20drops%20the%20remainder%2C%20or%20a%20whole-number%20array%20was%20assigned%20into.%20%7C%20%60%2F%60%20and%20%60np.where%60%2C%20which%20return%20decimals%20%7C%0A%20%20%20%20%7C%20*holds%20whole%20numbers*%20%7C%20The%20result%20kept%20the%20input's%20whole-number%20dtype.%20%7C%20%60area%20%2F%2010_000%60%20gives%20decimals%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20assignment%20destination%20is%20read-only*%20%7C%20Assigning%20into%20%60area%5B...%5D%60%3A%20the%20survey's%20arrays%20can't%20be%20changed%20(section%202).%20%7C%20%60np.where%60%2C%20which%20builds%20a%20new%20array%20%7C%0A%20%20%20%20%7C%20*UFuncTypeError%3A%20Cannot%20cast%20ufunc%20'divide'%20output%20from%20dtype('float64')%20to%20dtype('int64')*%20%7C%20%60%2F%3D%60%20into%20a%20whole-number%20array%2C%20which%20can't%20hold%20decimals.%20%7C%20%60np.where%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%0Adef%20_(area%2C%20crop%2C%20irrigated%2C%20production%2C%20unit)%3A%0A%20%20%20%20area_ha%20%3D%20attempt(area_in_hectares%2C%20area%2C%20unit)%0A%20%20%20%20mo.stop(area_ha%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.5%22))%0A%20%20%20%20yields%20%3D%20production%20%2F%201000%20%2F%20area_ha%20%20%23%20tonnes%20per%20hectare%2C%20every%20plot%20at%20once%0A%20%20%20%20rice%20%3D%20crop%20%3D%3D%20%22Rice%22%0A%20%20%20%20_watered%20%3D%20irrigated%20%3D%3D%20%22Yes%22%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20With%20%60area_ha%60%2C%20every%20yield%20is%20one%20line%3A%20%60yields%20%3D%20production%20%2F%201000%20%2F%20area_ha%60%2C%20shape%20%60%7Byields.shape%7D%60.%0A%0A%20%20%20%20%7C%20rice%20plots%20%7C%20plots%20%7C%20mean%20yield%20(t%2Fha)%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20all%20%7C%20%7Brice.sum()%3A%2C%7D%20%7C%20%7Byields%5Brice%5D.mean()%3A.2f%7D%20%7C%0A%20%20%20%20%7C%20irrigated%20%7C%20%7B(rice%20%26%20_watered).sum()%3A%2C%7D%20%7C%20%7Byields%5Brice%20%26%20_watered%5D.mean()%3A.2f%7D%20%7C%0A%20%20%20%20%7C%20rain-fed%20%7C%20%7B(rice%20%26%20~_watered).sum()%3A%2C%7D%20%7C%20%7Byields%5Brice%20%26%20~_watered%5D.mean()%3A.2f%7D%20%7C%0A%20%20%20%20%7C%20all%2C%20with%20square%20meters%20left%20unconverted%20%7C%20%7Brice.sum()%3A%2C%7D%20%7C%20%7B(production%20%2F%201000%20%2F%20area)%5Brice%5D.mean()%3A.2f%7D%20%7C%0A%0A%20%20%20%20The%20last%20row%20is%20the%20mistake%20Exercise%202.5%20prevents%3A%20the%20plots%20in%20square%20meters%20get%20yields%20near%20zero%20and%20drag%20the%20mean%20down.%20These%20are%20means%20of%20plots%2C%20each%20plot%20counted%20once%3B%20module%204%20weights%20them%20by%20the%20households%20each%20one%20stands%20for.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%20rice%2C%20yields%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%20yields%20of%20rice%20plots%2C%20irrigated%20and%20rain-fed%2C%20as%20a%20histogram.%20This%20section's%20charts%20use%20**seaborn**%2C%20which%20draws%20one%20color%20per%20group%20when%20given%20%60hue%3D%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(irrigated%2C%20rice%2C%20yields)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%203.2)%2C%20layout%3D%22constrained%22)%0A%20%20%20%20sns.histplot(x%3Dyields%5Brice%5D%2C%20hue%3Dirrigated%5Brice%5D%2C%20hue_order%3D%5B%22Yes%22%2C%20%22No%22%5D%2C%20bins%3D30%2C%20element%3D%22step%22%2C%20ax%3D_ax)%0A%20%20%20%20_ax.set_xlabel(%22yield%20(t%2Fha)%22)%0A%20%20%20%20_ax.set_ylabel(%22rice%20plots%22)%0A%20%20%20%20_ax.set_title(%22Rice%20yields%3A%20irrigated%20plots%20sit%20to%20the%20right%22)%0A%20%20%20%20_ax.get_legend().set_title(%22irrigated%22)%0A%20%20%20%20_ax%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%23%20Broadcasting%3A%20arrays%20of%20different%20shapes%0A%0A%20%20%20%20Vectorized%20arithmetic%20so%20far%20paired%20arrays%20of%20the%20same%20shape%2C%20or%20an%20array%20with%20one%20number.%20Prices%20raise%20a%20new%20need%3A%20compare%20every%20province%20with%20that%20month's%20average%20across%20provinces%2C%20or%20apply%20one%20factor%20per%20province%20to%20all%2048%20of%20its%20months.%20That%20is%20the%20grid%2C%20shape%20%60(7%2C%2048)%60%2C%20with%20an%20array%20of%20another%20shape.%20NumPy's%20rule%20for%20it%20is%20called%20**broadcasting**%3A%0A%0A%20%20%20%201.%20Line%20the%20two%20shapes%20up%20**from%20the%20right**.%0A%20%20%20%202.%20Each%20pair%20of%20numbers%20must%20be%20**equal**%2C%20or%20one%20of%20them%20must%20be%20**1**%20(or%20missing%2C%20on%20the%20left).%0A%20%20%20%203.%20A%201%20is%20stretched%20to%20match%3A%20the%20same%20values%20are%20used%20for%20every%20row%2C%20or%20every%20column.%0A%0A%20%20%20%20%7C%20grid%20%7C%20the%20other%20%7C%20lined%20up%20from%20the%20right%20%7C%20result%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60(7%2C%2048)%60%20%7C%20one%20number%20%7C%20nothing%20to%20compare%20%7C%20%60(7%2C%2048)%60%3A%20the%20number%20used%20everywhere%20%7C%0A%20%20%20%20%7C%20%60(7%2C%2048)%60%20%7C%20%60(48%2C)%60%2C%20one%20per%20month%20%7C%2048%20with%2048%3B%207%20with%20nothing%20%7C%20%60(7%2C%2048)%60%3A%20the%20same%2048%20values%20for%20every%20province%20%7C%0A%20%20%20%20%7C%20%60(7%2C%2048)%60%20%7C%20%60(7%2C%201)%60%2C%20one%20per%20province%2C%20as%20a%20column%20%7C%2048%20with%201%3B%207%20with%207%20%7C%20%60(7%2C%2048)%60%3A%20each%20province's%20value%20for%20all%20its%20months%20%7C%0A%20%20%20%20%7C%20%60(7%2C%2048)%60%20%7C%20%60(7%2C)%60%2C%20one%20per%20province%20%7C%20**48%20with%207**%20%7C%20ValueError%20%7C%0A%0A%20%20%20%20The%20last%20row%20is%20the%20error%20everyone%20meets%3A%20seven%20values%20per%20province%2C%20given%20as%20a%20row%2C%20are%20lined%20up%20with%20the%2048%20months.%20Make%20them%20a%20column%2C%20shape%20%60(7%2C%201)%60%2C%20with%20%60.reshape(-1%2C%201)%60%20or%20%60%5B%3A%2C%20None%5D%60.%0A%0A%20%20%20%20Pick%20what%20to%20add%20to%20%60palay%60%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%20other%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22one%20number%3A%201.0%22%3A%20%22number%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22one%20value%20per%20month%3A%20np.ones(48)%22%3A%20%22months%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22one%20value%20per%20province%3A%20np.ones(7)%22%3A%20%22provinces_row%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22one%20value%20per%20province%2C%20as%20a%20column%3A%20np.ones(7).reshape(-1%2C%201)%22%3A%20%22provinces_column%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22twelve%20values%3A%20np.ones(12)%22%3A%20%22twelve%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20value%3D%22one%20value%20per%20month%3A%20np.ones(48)%22%2C%20label%3D%22palay%20%2B%20%22%2C%0A%20%20%20%20)%0A%20%20%20%20other%0A%20%20%20%20return%20(other%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(other%2C%20palay)%3A%0A%20%20%20%20_choices%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22number%22%3A%20(1.0%2C%20%22A%20single%20number%20goes%20to%20every%20element.%22)%2C%0A%20%20%20%20%20%20%20%20%22months%22%3A%20(np.ones(48)%2C%20%22Lined%20up%20from%20the%20right%2C%2048%20meets%2048.%20The%20grid's%207%20has%20nothing%20to%20meet%2C%20so%20the%2048%20values%20are%20reused%20for%20each%20of%20the%207%20rows.%22)%2C%0A%20%20%20%20%20%20%20%20%22provinces_row%22%3A%20(np.ones(7)%2C%20%22Read%20the%20end%20of%20the%20message%3A%20*shapes%20(7%2C48)%20(7%2C)*.%20From%20the%20right%2C%2048%20meets%207%3A%20neither%20is%201%2C%20so%20NumPy%20cannot%20pair%20them.%20It%20never%20guesses%20that%20the%207%20was%20meant%20for%20the%20rows.%22)%2C%0A%20%20%20%20%20%20%20%20%22provinces_column%22%3A%20(np.ones(7).reshape(-1%2C%201)%2C%20%22As%20a%20column%2C%20shape%20(7%2C%201)%3A%2048%20meets%201%2C%20which%20is%20stretched%20across%20the%20months%2C%20and%207%20meets%207.%20Each%20province's%20value%20goes%20to%20all%2048%20of%20its%20months.%22)%2C%0A%20%20%20%20%20%20%20%20%22twelve%22%3A%20(np.ones(12)%2C%20%22Twelve%20values%20have%20no%20place%20in%2048%20months%3A%2048%20meets%2012.%20To%20add%20one%20value%20per%20calendar%20month%2C%20reshape%20the%20months%20into%20years%20first%20(Exercise%202.2).%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20_value%2C%20_note%20%3D%20_choices%5Bother.value%5D%0A%20%20%20%20_shape%20%3D%20np.shape(_value)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_result%20%3D%20palay%20%2B%20_value%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.callout(mo.md(f%22%60palay%60%20%60(7%2C%2048)%60%20%2B%20an%20array%20of%20shape%20%60%7B_shape%7D%60%20gives%20shape%20**%60%7B_result.shape%7D%60**.%5Cn%5Cn%7B_note%7D%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20except%20ValueError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.callout(mo.md(f%22%60palay%60%20%60(7%2C%2048)%60%20%2B%20an%20array%20of%20shape%20%60%7B_shape%7D%60%20raises%20**ValueError**%3A%20%60%7B_error%7D%60%5Cn%5Cn%7B_note%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20_shown%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%20Exercise%202.6.%20A%20price%20index%20for%20every%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20provinces%20start%20from%20different%20prices%2C%20so%20their%20raw%20prices%20are%20hard%20to%20compare.%20A%20**price%20index**%20sets%20each%20province's%20first%20month%20to%20100%3A%20an%20index%20of%20125%20means%20prices%20rose%2025%25%20since%20then%2C%20whatever%20the%20starting%20price.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20grid%20with%20one%20row%20per%20province%20and%20one%20column%20per%20month%20%E2%86%92%20the%20same%20shape%2C%20each%20value%20divided%20by%20**its%20own%20row's%20first%20value**%2C%20times%20100%2C%20as%20decimals.%20A%20missing%20price%20stays%20%60nan%60.%0A%0A%20%20%20%20%7C%20%7C%20Jan%20%7C%20Feb%20%7C%20Mar%20%7C%20%7C%20index%20%7C%20Jan%20%7C%20Feb%20%7C%20Mar%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20province%20A%20%7C%2010%20%7C%2012%20%7C%2015%20%7C%20%E2%86%92%20%7C%20province%20A%20%7C%20100%20%7C%20120%20%7C%20150%20%7C%0A%20%20%20%20%7C%20province%20B%20%7C%2020%20%7C%2020%20%7C%2025%20%7C%20%E2%86%92%20%7C%20province%20B%20%7C%20100%20%7C%20100%20%7C%20125%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20slice%20that%20keeps%20the%20first%20column%20**as%20a%20column**%2C%20shape%20%60(rows%2C%201)%60%3A%20%60grid%5B%3A%2C%20%3A1%5D%60%20(section%202)%2C%20and%20broadcasting%20(above).%20%60grid%5B%3A%2C%200%5D%60%20gives%20a%20row%20of%20shape%20%60(rows%2C)%60%3B%20read%20the%20table%20above%20to%20see%20why%20that%20fails.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20price_index(grid%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22Each%20row's%20values%20as%20an%20index%3A%20100%20times%20each%20value%20divided%20by%20its%20row's%20first%20value.%22%22%22%0A%20%20%20%20%23%20Exercise%202.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20row's%20first%20value%2C%20kept%20as%20a%20column%20of%20shape%20(rows%2C%201)%0A%20%20%20%20%23%20Step%202%3A%20return%20100%20*%20the%20grid%20%2F%20that%20column%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.6%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(palay)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.6%22%2C%20price_index%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20provinces%2C%20three%20months%2C%20whole-number%20prices%22%2C%20(np.array(%5B%5B10%2C%2012%2C%2015%5D%2C%20%5B20%2C%2020%2C%2025%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20provinces%2C%20three%20months%2C%20one%20price%20missing%20(as%20many%20rows%20as%20columns)%22%2C%20(np.array(%5B%5B10.0%2C%2011.0%2C%2012.0%5D%2C%20%5B20.0%2C%2022.0%2C%2024.0%5D%2C%20%5B5.0%2C%206.0%2C%20np.nan%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20palay%20grid%3A%207%20provinces%2C%2048%20months%22%2C%20(palay%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22price%20grids%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Each%20row%20is%20divided%20by%20its%20own%20first%20value%3A%20keep%20the%20first%20column%20as%20a%20column%2C%20shape%20(rows%2C%201)%2C%20so%20it%20broadcasts%20across%20the%20months.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20palay%20index.%20The%20last%20column%20is%20December%202025%20against%20January%202022%2C%20province%20by%20province.%0A%20%20%20%20try_array(price_index%2C%20palay)%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%20Exercise%202.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%20*ValueError%3A%20operands%20could%20not%20be%20broadcast%20together%20with%20shapes%20(2%2C3)%20(2%2C)*%20%7C%20%60grid%5B%3A%2C%200%5D%60%20is%20a%20row%2C%20lined%20up%20with%20the%20months.%20%7C%20%60grid%5B%3A%2C%20%3A1%5D%60%2C%20a%20column%20%7C%0A%20%20%20%20%7C%20Right%20for%20the%20first%20case%2C%20wrong%20for%20the%20square%20one%20%7C%20%60grid%5B%3A%2C%200%5D%60%20again%3A%20with%20as%20many%20rows%20as%20columns%20it%20broadcasts%2C%20along%20the%20wrong%20direction.%20%7C%20%60grid%5B%3A%2C%20%3A1%5D%60%20%7C%0A%20%20%20%20%7C%20Province%20B's%20January%20is%20200%2C%20not%20100%20%7C%20Divided%20by%20the%20first%20row%2C%20%60grid%5B0%5D%60%2C%20not%20each%20row's%20first%20value.%20%7C%20%60grid%5B%3A%2C%20%3A1%5D%60%20%7C%0A%20%20%20%20%7C%20Values%20such%20as%201.2%20instead%20of%20120%20%7C%20The%20%60100%20*%60%20is%20missing.%20%7C%20%60100%20*%20grid%20%2F%20...%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%204%20%C2%B7%20Aggregating%20along%20an%20axis%3B%20NaN%0A%0A%20%20%20%20Two%20questions%20about%20the%20palay%20grid%3A%20what%20is%20each%20**province's**%20average%20price%20over%20the%20four%20years%2C%20and%20what%20is%20each%20**month's**%20average%20across%20the%20provinces%3F%20Both%20are%20a%20mean%20of%20the%20same%20grid%2C%20taken%20in%20different%20directions.%20And%20both%20meet%20September%202023%2C%20the%20month%20with%20no%20prices.%0A%0A%20%20%20%20%23%23%23%23%20axis%3A%20which%20direction%20to%20summarize%0A%0A%20%20%20%20%60.sum()%60%2C%20%60.mean()%60%2C%20%60.max()%60%20and%20%60.min()%60%20summarize%20every%20element%20into%20one%20number.%20Given%20%60axis%3D%60%2C%20they%20summarize%20in%20one%20direction%20only%2C%20and%20**that%20direction%20disappears**%20from%20the%20shape%3A%0A%0A%20%20%20%20%7C%20code%20%7C%20summarizes%20%7C%20result's%20shape%20%7C%20one%20value%20per%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60palay.mean()%60%20%7C%20everything%20%7C%20one%20number%20%7C%20grid%20%7C%0A%20%20%20%20%7C%20%60palay.mean(axis%3D0)%60%20%7C%20down%20the%20rows%3A%20the%207%20provinces%20of%20each%20month%20%7C%20%60(48%2C)%60%20%7C%20month%20%7C%0A%20%20%20%20%7C%20%60palay.mean(axis%3D1)%60%20%7C%20across%20the%20columns%3A%20the%2048%20months%20of%20each%20province%20%7C%20%60(7%2C)%60%20%7C%20province%20%7C%0A%0A%20%20%20%20%60axis%3D0%60%20is%20the%20first%20number%20of%20the%20shape%20(the%20rows)%2C%20%60axis%3D1%60%20the%20second%20(the%20columns).%20If%20you%20can't%20remember%20which%2C%20look%20at%20the%20shape%20you%20want%20and%20remove%20the%20other.%0A%0A%20%20%20%20%23%23%23%23%20NaN%3A%20a%20missing%20number%0A%0A%20%20%20%20%60nan%60%20(not%20a%20number)%20is%20the%20decimal%20value%20NumPy%20uses%20for%20a%20number%20that%20is%20missing.%20It%20spreads%3A%20any%20arithmetic%20with%20%60nan%60%20gives%20%60nan%60%2C%20so%20one%20missing%20month%20makes%20a%20whole%20province's%20mean%20%60nan%60.%20It%20is%20not%20even%20equal%20to%20itself%20(%60np.nan%20%3D%3D%20np.nan%60%20is%20%60False%60)%2C%20so%20find%20it%20with%20%60np.isnan(...)%60%2C%20which%20gives%20a%20mask.%0A%0A%20%20%20%20For%20each%20summary%20there%20is%20a%20version%20that%20leaves%20%60nan%60%20out%3A%0A%0A%20%20%20%20%7C%20spreads%20nan%20%7C%20leaves%20nan%20out%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60np.mean(x)%60%2C%20%60x.mean()%60%20%7C%20%60np.nanmean(x)%60%20%7C%0A%20%20%20%20%7C%20%60np.sum(x)%60%20%7C%20%60np.nansum(x)%60%20%7C%0A%20%20%20%20%7C%20%60np.max(x)%60%2C%20%60np.min(x)%60%20%7C%20%60np.nanmax(x)%60%2C%20%60np.nanmin(x)%60%20%7C%0A%0A%20%20%20%20Choose%20a%20function%20and%20an%20axis%2C%20and%20read%20the%20result%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%20summary_function%20%3D%20mo.ui.radio(options%3D%5B%22np.mean%22%2C%20%22np.nanmean%22%5D%2C%20value%3D%22np.mean%22%2C%20label%3D%22Function%22%2C%20inline%3DTrue)%0A%20%20%20%20summary_axis%20%3D%20mo.ui.dropdown(options%3D%7B%22none%3A%20every%20element%22%3A%20None%2C%20%22axis%3D0%3A%20down%20the%20rows%22%3A%200%2C%20%22axis%3D1%3A%20across%20the%20columns%22%3A%201%7D%2C%20value%3D%22axis%3D1%3A%20across%20the%20columns%22%2C%20label%3D%22Axis%22)%0A%20%20%20%20mo.hstack(%5Bsummary_function%2C%20summary_axis%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20summary_axis%2C%20summary_function%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(palay%2C%20summary_axis%2C%20summary_function)%3A%0A%20%20%20%20_fn%20%3D%20np.mean%20if%20summary_function.value%20%3D%3D%20%22np.mean%22%20else%20np.nanmean%0A%20%20%20%20_axis%20%3D%20summary_axis.value%0A%20%20%20%20_code%20%3D%20f%22%7Bsummary_function.value%7D(palay%7B''%20if%20_axis%20is%20None%20else%20f'%2C%20axis%3D%7B_axis%7D'%7D)%22%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20_caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20_result%20%3D%20_fn(palay%2C%20axis%3D_axis)%0A%20%20%20%20_notes%20%3D%20%7B%0A%20%20%20%20%20%20%20%20(%22np.mean%22%2C%20None)%3A%20%22One%20number%2C%20and%20it%20is%20%60nan%60%3A%20the%20missing%20month%20spread%20into%20the%20whole%20mean.%22%2C%0A%20%20%20%20%20%20%20%20(%22np.mean%22%2C%200)%3A%20%22One%20value%20per%20month.%20Every%20month%20has%20its%20mean%20except%20September%202023%20(position%2020)%2C%20whose%20seven%20prices%20are%20all%20%60nan%60.%22%2C%0A%20%20%20%20%20%20%20%20(%22np.mean%22%2C%201)%3A%20%22One%20value%20per%20province%2C%20and%20every%20one%20is%20%60nan%60%3A%20each%20province's%2048%20months%20include%20September%202023.%22%2C%0A%20%20%20%20%20%20%20%20(%22np.nanmean%22%2C%20None)%3A%20%22One%20number%3A%20the%20average%20of%20the%20329%20prices%20there%20are.%22%2C%0A%20%20%20%20%20%20%20%20(%22np.nanmean%22%2C%200)%3A%20%22One%20value%20per%20month%3B%20September%202023%20is%20still%20%60nan%60%2C%20because%20it%20has%20no%20price%20at%20all%20to%20average%2C%20and%20NumPy%20warns%20about%20it%20(below).%22%2C%0A%20%20%20%20%20%20%20%20(%22np.nanmean%22%2C%201)%3A%20%22One%20value%20per%20province%2C%20each%20the%20average%20of%20its%2047%20months%20with%20a%20price.%20This%20is%20the%20answer%20the%20question%20wanted.%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20_parts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%7B_code%7D%60%20has%20shape%20%60%7Bnp.shape(_result)%7D%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.plain_text(np.array2string(np.asarray(_result)%2C%20precision%3D2%2C%20max_line_width%3D100))%2C%0A%20%20%20%20%20%20%20%20mo.md(_notes%5B(summary_function.value%2C%20_axis)%5D)%2C%0A%20%20%20%20%5D%0A%20%20%20%20if%20_caught%3A%0A%20%20%20%20%20%20%20%20_parts.append(mo.callout(mo.md(f%22NumPy%20warned%3A%20**RuntimeWarning%3A%20%7B_caught%5B0%5D.message%7D**.%20A%20warning%20is%20not%20an%20error%3A%20the%20result%20is%20there.%20Here%20it%20says%20one%20column%20had%20nothing%20to%20average%2C%20so%20its%20mean%20is%20%60nan%60%2C%20which%20is%20the%20honest%20answer%20for%20a%20month%20with%20no%20data.%22)%2C%20kind%3D%22warn%22))%0A%20%20%20%20mo.vstack(_parts)%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%20same%20holds%20for%20a%20single%20row%20of%20values.%20Fertilizer%20was%20not%20recorded%20for%20some%20plots%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fertilizer)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20code%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60fertilizer.mean()%60%20%7C%20%60%7Bfertilizer.mean()%7D%60%20%7C%0A%20%20%20%20%7C%20%60np.nanmean(fertilizer)%60%20%7C%20%60%7Bnp.nanmean(fertilizer)%3A.1f%7D%60%20kg%20per%20plot%2C%20over%20the%20plots%20where%20it%20was%20recorded%20%7C%0A%20%20%20%20%7C%20%60np.isnan(fertilizer).sum()%60%20%7C%20%60%7Bnp.isnan(fertilizer).sum()%7D%60%20plots%20without%20it%3A%20a%20mask%2C%20summed%20%7C%0A%20%20%20%20%7C%20%60(~np.isnan(fertilizer)).mean()%60%20%7C%20%60%7B(~np.isnan(fertilizer)).mean()%3A.3f%7D%60%3A%20the%20share%20of%20plots%20with%20it%20recorded%20%7C%0A%0A%20%20%20%20Leaving%20%60nan%60%20out%20is%20a%20decision%2C%20not%20a%20fix%3A%20the%20mean%20is%20now%20the%20mean%20of%20the%20plots%20that%20recorded%20fertilizer%2C%20which%20may%20differ%20from%20the%20plots%20that%20didn't.%20Module%205%20looks%20at%20when%20to%20leave%20missing%20values%20out%20and%20when%20not%20to.%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**keepdims%3DTrue%20keeps%20the%20summarized%20direction%2C%20with%20a%20size%20of%201.**%20%60np.nanmean(palay%2C%20axis%3D1)%60%20has%20shape%20%60(7%2C)%60%3B%20with%20%60keepdims%3DTrue%60%20it%20has%20shape%20%60(7%2C%201)%60%2C%20a%20column%2C%20which%20broadcasts%20back%20against%20the%20grid%20(section%203).%20So%20%60palay%20-%20np.nanmean(palay%2C%20axis%3D1%2C%20keepdims%3DTrue)%60%20is%20every%20price's%20distance%20from%20its%20province's%20own%20average.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%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%20Exercise%202.7.%20Months%20with%20a%20price%2C%20for%20each%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20before%20averaging%2C%20say%20how%20much%20there%20is%20to%20average.%20A%20province%20mean%20over%2047%20months%20is%20not%20the%20same%20claim%20as%20one%20over%2048%2C%20and%20a%20report%20states%20how%20many%20values%20each%20mean%20rests%20on.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20grid%20with%20one%20row%20per%20province%20and%20one%20column%20per%20month%20%E2%86%92%20for%20each%20row%2C%20the%20number%20of%20values%20that%20are%20**not**%20%60nan%60%2C%20as%20whole%20numbers.%0A%0A%20%20%20%20%7C%20%7C%20Jan%20%7C%20Feb%20%7C%20Mar%20%7C%20Apr%20%7C%20%7C%20months%20with%20a%20price%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20province%20A%20%7C%2018.2%20%7C%20%60nan%60%20%7C%2018.9%20%7C%2019.1%20%7C%20%E2%86%92%20%7C%203%20%7C%0A%20%20%20%20%7C%20province%20B%20%7C%20%60nan%60%20%7C%20%60nan%60%20%7C%2020.4%20%7C%2020.0%20%7C%20%E2%86%92%20%7C%202%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60np.isnan%60%20and%20%60~%60%20(a%20mask%2C%20then%20turned%20around)%2C%20and%20%60.sum(axis%3D...)%60%2C%20which%20counts%20the%20%60True%60%20in%20each%20row.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20months_reported(grid%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22For%20each%20row%20of%20the%20grid%2C%20how%20many%20of%20its%20values%20are%20not%20nan.%22%22%22%0A%20%20%20%20%23%20Exercise%202.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20mask%20that%20is%20True%20where%20the%20grid%20has%20a%20value%20(not%20nan)%0A%20%20%20%20%23%20Step%202%3A%20return%20its%20sum%20across%20the%20columns%2C%20one%20count%20per%20row%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.7%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(palay)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.7%22%2C%20months_reported%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20provinces%2C%20four%20months%2C%20three%20values%20missing%22%2C%20(np.array(%5B%5B18.2%2C%20np.nan%2C%2018.9%2C%2019.1%5D%2C%20%5Bnp.nan%2C%20np.nan%2C%2020.4%2C%2020.0%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20provinces%2C%20three%20months%2C%20missing%20values%20in%20a%20triangle%20(as%20many%20rows%20as%20columns)%22%2C%20(np.array(%5B%5B1.0%2C%20np.nan%2C%20np.nan%5D%2C%20%5B1.0%2C%202.0%2C%20np.nan%5D%2C%20%5B1.0%2C%202.0%2C%203.0%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20grid%20with%20nothing%20missing%22%2C%20(np.array(%5B%5B17.5%2C%2017.8%5D%2C%20%5B18.1%2C%2018.0%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20palay%20grid%3A%207%20provinces%2C%2048%20months%22%2C%20(palay%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22price%20grids%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20the%20values%20that%20are%20there%2C%20not%20the%20missing%20ones%2C%20across%20each%20row%20(axis%3D1)%3B%20a%20count%20is%20a%20whole%20number.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20%23%20Try%20it%3A%20months%20with%20a%20palay%20price%2C%20province%20by%20province%2C%20in%20the%20order%20of%20PROVINCES.%0A%20%20%20%20try_array(months_reported%2C%20palay)%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%20Exercise%202.7%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%5B1%2C%202%5D%60%20instead%20of%20%60%5B3%2C%202%5D%60%20%7C%20Counted%20the%20missing%20values%2C%20not%20the%20ones%20there.%20%7C%20%60~np.isnan(grid)%60%20%7C%0A%20%20%20%20%7C%20%60%5B3%2C%202%2C%201%5D%60%20instead%20of%20%60%5B1%2C%202%2C%203%5D%60%2C%20or%20one%20count%20per%20month%20%7C%20Summed%20down%20the%20rows.%20%7C%20%60axis%3D1%60%20%7C%0A%20%20%20%20%7C%20%60%5B4%2C%204%5D%60%2C%20or%20%60%5B48%2C%2048%2C%20...%5D%60%20%7C%20Every%20value%20counted%3A%20%60count_nonzero%60%2C%20%60len%60%2C%20or%20%60grid%20!%3D%20np.nan%60.%20%7C%20%60np.isnan%60%2C%20which%20finds%20%60nan%60%20%7C%0A%20%20%20%20%7C%20*holds%20decimals*%20%7C%20The%20count%20was%20turned%20into%20decimals%2C%20as%20with%20%60*%201.0%60.%20%7C%20a%20summed%20mask%20is%20already%20whole%20numbers%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%20Exercise%202.8.%20The%20monthly%20average%20across%20provinces%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20single%20national%20line%20shows%20the%20trend%20that%20seven%20lines%20hide%3A%20the%20monthly%20average%20of%20the%20provinces.%20A%20month%20where%20one%20province%20didn't%20report%20still%20has%20an%20average%20of%20the%20others%3B%20the%20month%20with%20no%20file%20at%20all%20has%20none%2C%20and%20the%20chart%20should%20show%20a%20gap%20there%2C%20not%20an%20invented%20value.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20grid%20with%20one%20row%20per%20province%20and%20one%20column%20per%20month%20%E2%86%92%20for%20each%20month%2C%20the%20mean%20of%20the%20values%20that%20are%20not%20%60nan%60.%20A%20month%20with%20no%20value%20at%20all%20gives%20%60nan%60.%0A%0A%20%20%20%20%7C%20%7C%20Jan%20%7C%20Feb%20%7C%20Mar%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20province%20A%20%7C%2010%20%7C%20%60nan%60%20%7C%2012%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20province%20B%20%7C%2014%20%7C%2016%20%7C%20%60nan%60%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20**average**%20%7C%20**12**%20%7C%20**16**%20%7C%20**12**%20%7C%20%E2%86%90%20%7C%20each%20month's%20mean%20of%20what%20is%20there%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20%60nan%60-aware%20mean%20along%20the%20right%20axis%2C%20from%20the%20tables%20above.%20NumPy%20warns%20*Mean%20of%20empty%20slice*%20for%20a%20month%20with%20no%20value%3B%20that%20is%20expected.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20monthly_average(grid%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22For%20each%20column%20of%20the%20grid%2C%20the%20mean%20of%20its%20values%20that%20are%20not%20nan%20(nan%20if%20it%20has%20none).%22%22%22%0A%20%20%20%20%23%20Exercise%202.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20choose%20the%20axis%3A%20one%20result%20per%20month%20means%20summarizing%20down%20the%20rows%0A%20%20%20%20%23%20Step%202%3A%20return%20the%20mean%20along%20that%20axis%2C%20leaving%20nan%20out%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.8%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(palay)%3A%0A%20%20%20%20with%20warnings.catch_warnings()%3A%20%20%23%20the%20month%20with%20no%20prices%20warns%20%22Mean%20of%20empty%20slice%22%3A%20expected%20here%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22ignore%22%2C%20RuntimeWarning)%0A%20%20%20%20%20%20%20%20_shown%20%3D%20check_cases(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Exercise%202.8%22%2C%20monthly_average%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20provinces%2C%20three%20months%2C%20one%20price%20missing%20in%20February%20and%20one%20in%20March%22%2C%20(np.array(%5B%5B10.0%2C%20np.nan%2C%2012.0%5D%2C%20%5B14.0%2C%2016.0%2C%20np.nan%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20provinces%2C%20three%20months%2C%20nothing%20missing%20(as%20many%20rows%20as%20columns)%22%2C%20(np.array(%5B%5B10.0%2C%2020.0%2C%2030.0%5D%2C%20%5B12.0%2C%2022.0%2C%2032.0%5D%2C%20%5B14.0%2C%2024.0%2C%2034.0%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20provinces%2C%20three%20months%2C%20the%20middle%20month%20missing%20in%20both%22%2C%20(np.array(%5B%5B10.0%2C%20np.nan%2C%2012.0%5D%2C%20%5B14.0%2C%20np.nan%2C%2016.0%5D%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20palay%20grid%3A%207%20provinces%2C%2048%20months%22%2C%20(palay%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22price%20grids%22%2C%20summarize%3Dwith_kind%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22One%20value%20per%20month%3A%20summarize%20down%20the%20rows%20(axis%3D0)%2C%20with%20the%20mean%20that%20leaves%20nan%20out.%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_shown%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20palay%20average%20of%20the%20seven%20provinces%2C%20month%20by%20month.%20Position%2020%20is%20September%202023.%0A%20%20%20%20try_array(monthly_average%2C%20palay)%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%20Exercise%202.8%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%60nan%60%20for%20February%20and%20March%20in%20the%20first%20case%20%7C%20The%20mean%20spreads%20%60nan%60.%20%7C%20%60np.nanmean%60%20%7C%0A%20%20%20%20%7C%20Two%20values%20instead%20of%20three%2C%20or%207%20instead%20of%2048%20%7C%20Averaged%20across%20the%20columns%3A%20one%20per%20province.%20%7C%20%60axis%3D0%60%20%7C%0A%20%20%20%20%7C%20Right%20for%20the%20first%20case%2C%20wrong%20for%20the%20square%20one%20%7C%20%60axis%3D1%60%20again%3A%20with%20as%20many%20rows%20as%20columns%2C%20the%20shape%20is%20right%20and%20the%20values%20are%20not.%20%7C%20%60axis%3D0%60%20%7C%0A%20%20%20%20%7C%20One%20number%20%7C%20No%20axis%3A%20the%20mean%20of%20every%20value.%20%7C%20%60axis%3D0%60%20%7C%0A%20%20%20%20%7C%20%600%60%20for%20the%20month%20with%20no%20prices%20%7C%20%60np.nansum%60%20was%20used%3A%20the%20sum%20of%20no%20values%20is%200%2C%20not%20%60nan%60.%20%7C%20%60np.nanmean%60%2C%20which%20gives%20%60nan%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%20Your%20monthly%20average%2C%20as%20a%20line.%20This%20section's%20chart%20uses%20**Altair**%2C%20which%20reads%20a%20table%3A%20the%20two%20arrays%20become%20its%20two%20columns.%20Altair%20leaves%20a%20gap%20where%20a%20value%20is%20%60nan%60%2C%20so%20the%20month%20with%20no%20file%20shows%20as%20a%20break%20in%20the%20line%2C%20not%20as%20a%20dip%20or%20an%20invented%20value.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20with%20warnings.catch_warnings()%3A%20%20%23%20the%20month%20with%20no%20prices%20warns%20%22Mean%20of%20empty%20slice%22%3A%20expected%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22ignore%22%2C%20RuntimeWarning)%0A%20%20%20%20%20%20%20%20_average%20%3D%20attempt(monthly_average%2C%20palay)%0A%20%20%20%20mo.stop(_average%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.8%22))%0A%20%20%20%20_table%20%3D%20pd.DataFrame(%7B%22month%22%3A%20MONTHS.to_timestamp()%2C%20%22price%22%3A%20_average%7D)%20%20%23%20two%20arrays%2C%20as%20a%20table's%20two%20columns%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20alt.Chart(_table%2C%20title%3D%22Palay%3A%20average%20farmgate%20price%20of%20seven%20provinces%2C%20by%20month%22)%0A%20%20%20%20%20%20%20%20.mark_line()%0A%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dalt.X(%22month%3AT%22%2C%20title%3DNone)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.Y(%22price%3AQ%22%2C%20title%3D%22PHP%20per%20kg%22%2C%20scale%3Dalt.Scale(zero%3DFalse))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tooltip%3D%5Balt.Tooltip(%22month%3AT%22%2C%20format%3D%22%25B%20%25Y%22)%2C%20alt.Tooltip(%22price%3AQ%22%2C%20format%3D%22.2f%22)%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.properties(width%3D560%2C%20height%3D220)%0A%20%20%20%20)%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%20What%20you%20can%20do%20now%0A%0A%20%20%20%20-%20**Make%20an%20array%20and%20read%20what%20it%20holds%3A**%20%60np.array%60%2C%20%60np.arange%60%2C%20%60np.linspace%60%2C%20%60np.zeros%60%2C%20%60np.ones%60%2C%20%60.dtype%60%2C%20%60.shape%60%2C%20%60.reshape%60.%0A%20%20%20%20-%20**Select%20by%20position%20and%20by%20condition%3A**%20%60a%5Bi%5D%60%2C%20%60a%5Bstart%3Astop%5D%60%2C%20%60grid%5Brow%2C%20column%5D%60%2C%20masks%20combined%20with%20%60%26%60%2C%20%60%7C%60%20and%20%60~%60%2C%20each%20comparison%20in%20brackets.%0A%20%20%20%20-%20**Compute%20on%20whole%20arrays%3A**%20arithmetic%20between%20arrays%20and%20with%20numbers%2C%20%60%2F%60%20and%20%60%2F%2F%60%2C%20%60np.where%60%2C%20broadcasting%20and%20its%20shape%20errors.%0A%20%20%20%20-%20**Summarize%2C%20and%20handle%20what%20is%20missing%3A**%20%60.sum()%60%20and%20%60.mean()%60%20with%20%60axis%3D%60%2C%20%60np.isnan%60%2C%20%60np.nanmean%60%20and%20the%20other%20%60nan%60%20functions.%0A%0A%20%20%20%20Every%20one%20of%20these%20carries%20into%20the%20next%20module.%20A%20pandas%20column%20is%20an%20array%20with%20a%20name%20and%20an%20**index**%20(a%20label%20for%20each%20row)%2C%20and%20a%20DataFrame%20is%20columns%20side%20by%20side.%20Masks%2C%20%60%26%60%20with%20brackets%2C%20%60axis%3D%60%20and%20%60nan%60%20work%20there%20as%20they%20do%20here.%20Next%3A%20%5Bmodule%203%2C%20DataFrames%5D(%2F%3Ffile%3D20_dataframes.py).%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%20course.quizzes_at(%222.4%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
5f89346f1371d3d1d3308d648ef72c80