<jupyter_start><jupyter_text>LDA and KNN classification: Problem solving In this session, you'll work through a complete example using a new dataset, `binary`.  Load the dataframeThe `binary.csv` dataset contains 4 variables:- `admit`: the admittance status (0=not admitted, 1=admitted)- `gre`: the student's GRE score- `gpa`: the student's GPA- `rank`: rank of the institution (1=highest to 4=lowest prestige)Start by importing `pandas`.<jupyter_code>import pandas as pd

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="/%IFN5#t2uAm}E`8:KV:">pd</variable></variables><block type="importAs" id="_@5-r*j4E`}d?=DXLinf" x="16" y="10"><field name="libraryName">pandas</field><field name="libraryAlias" id="/%IFN5#t2uAm}E`8:KV:">pd</field></block></xml><jupyter_output><empty_output><jupyter_text>Load a dataframe with `binary.csv` and display the dataframe.<jupyter_code>dataframe = pd.read_csv('datasets/binary.csv')

dataframe

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="o69xDDO)6.5D2/bJqST?">dataframe</variable><variable id="/%IFN5#t2uAm}E`8:KV:">pd</variable></variables><block type="variables_set" id="gM*jw`FfIR3)8=g0iEB7" x="11" y="186"><field name="VAR" id="o69xDDO)6.5D2/bJqST?">dataframe</field><value name="VALUE"><block type="varDoMethod" id="ny0sjvqTnn2B]K2za7Li"><field name="VAR" id="/%IFN5#t2uAm}E`8:KV:">pd</field><field name="MEMBER">read_csv</field><data>pd:read_csv</data><value name="INPUT"><block type="text" id="dfrpI5b@DHr+DQ:|@vpv"><field name="TEXT">datasets/binary.csv</field></block></value></block></value></block><block type="variables_get" id="dn{+Q#DO%lN;G_tFGJ#B" x="8" y="304"><field name="VAR" id="o69xDDO)6.5D2/bJqST?">dataframe</field></block></xml><jupyter_output><empty_output><jupyter_text>Prepare the train/test dataTo train the classifiers, you need to split the dataframe into training data and testing data.Start by creating a dataframe `Y` that just has `admit` in it, and then display `Y` so you can be sure it worked.<jupyter_code>Y = dataframe[['admit']]

Y

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="@83Gxqb{/h|%%9Yr?2q!">Y</variable><variable id="o69xDDO)6.5D2/bJqST?">dataframe</variable></variables><block type="variables_set" id="46~F|Y~BiXd|Vok0}p,p" x="33" y="69"><field name="VAR" id="@83Gxqb{/h|%%9Yr?2q!">Y</field><value name="VALUE"><block type="indexer" id="p,##xUo8Nj3`vpMVBjr8"><field name="VAR" id="o69xDDO)6.5D2/bJqST?">dataframe</field><value name="INDEX"><block type="lists_create_with" id="a.wA7Xdx%GbQ)t#XDKvV"><mutation items="1"></mutation><value name="ADD0"><block type="text" id="aZG|GLqavO~,RXBOcuJ+"><field name="TEXT">admit</field></block></value></block></value></block></value></block><block type="variables_get" id="W]bu*LAe{2;n#?+7WQ-[" x="30" y="219"><field name="VAR" id="@83Gxqb{/h|%%9Yr?2q!">Y</field></block></xml><jupyter_output><empty_output><jupyter_text>Next do the same thing for `X` using the other columns in the dataframe.<jupyter_code>X = dataframe[['gre', 'gpa', 'rank']]

X

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="py,.kr!f9i=.v7I5_d+^">X</variable><variable id="o69xDDO)6.5D2/bJqST?">dataframe</variable></variables><block type="variables_set" id="46~F|Y~BiXd|Vok0}p,p" x="33" y="69"><field name="VAR" id="py,.kr!f9i=.v7I5_d+^">X</field><value name="VALUE"><block type="indexer" id="p,##xUo8Nj3`vpMVBjr8"><field name="VAR" id="o69xDDO)6.5D2/bJqST?">dataframe</field><value name="INDEX"><block type="lists_create_with" id="a.wA7Xdx%GbQ)t#XDKvV"><mutation items="3"></mutation><value name="ADD0"><block type="text" id="aZG|GLqavO~,RXBOcuJ+"><field name="TEXT">gre</field></block></value><value name="ADD1"><block type="text" id="b/mW9(0w,rS.ny;Y(]5A"><field name="TEXT">gpa</field></block></value><value name="ADD2"><block type="text" id="4yc?!4pnBI1/rSBQ41!t"><field name="TEXT">rank</field></block></value></block></value></block></value></block><block type="variables_get" id="W]bu*LAe{2;n#?+7WQ-[" x="30" y="219"><field name="VAR" id="py,.kr!f9i=.v7I5_d+^">X</field></block></xml><jupyter_output><empty_output><jupyter_text>To split the data into training and testing data, import `model_selection`.<jupyter_code>import sklearn.model_selection as model_selection

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="uASGz64Zb$AOvQyV4pRj">model_selection</variable></variables><block type="importAs" id="sN1YO5FEzpHyxb31@j,Z" x="16" y="10"><field name="libraryName">sklearn.model_selection</field><field name="libraryAlias" id="uASGz64Zb$AOvQyV4pRj">model_selection</field></block></xml><jupyter_output><empty_output><jupyter_text>Now split the data into training and testing data, using `test_size` at one of the following 3 values depending on your birthday:If your birthday is in:- Jan, Feb, Mar, Apr, use `0.2` - May, Jun, Jul, Aug, use `0.4`- Sep, Oct, Nov, Dec, use `0.6`So depending on your birthday, we'll use 20, 40, or 60% of the data for testing.<jupyter_code>splits = model_selection.train_test_split(X, Y, test_size=0.2)

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="_ut$e0PL4OMi4o1MXTpw">splits</variable><variable id="uASGz64Zb$AOvQyV4pRj">model_selection</variable><variable id="py,.kr!f9i=.v7I5_d+^">X</variable><variable id="@83Gxqb{/h|%%9Yr?2q!">Y</variable></variables><block type="variables_set" id="oTGRJ#{R!U^we@Bl@pkT" x="31" y="224"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field><value name="VALUE"><block type="varDoMethod" id="f?j@ker(a#hJv;Nh)IGX"><field name="VAR" id="uASGz64Zb$AOvQyV4pRj">model_selection</field><field name="MEMBER">train_test_split</field><data>model_selection:train_test_split</data><value name="INPUT"><block type="lists_create_with" id="er6r2]}|nA;1;}VsM5I7"><mutation items="3"></mutation><value name="ADD0"><block type="variables_get" id=".mm}`*H4)i%Eq5z={e-$"><field name="VAR" id="py,.kr!f9i=.v7I5_d+^">X</field></block></value><value name="ADD1"><block type="variables_get" id="I3dOV;CPBf^~E%BvgthZ"><field name="VAR" id="@83Gxqb{/h|%%9Yr?2q!">Y</field></block></value><value name="ADD2"><block type="dummyOutputCodeBlock" id="@Hg?ib/!8fH$;f3pWJy2"><field name="CODE">test_size=0.2</field></block></value></block></value></block></value></block></xml><jupyter_output><empty_output><jupyter_text>Train LDAFirst, import `discriminant_analysis`<jupyter_code>import sklearn.discriminant_analysis as da

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="t#|IDvUc9z_$x]MRbwQb">da</variable></variables><block type="importAs" id=":PRm%S%Z/GaCW^6TfKmk" x="143" y="258"><field name="libraryName">sklearn.discriminant_analysis</field><field name="libraryAlias" id="t#|IDvUc9z_$x]MRbwQb">da</field></block></xml><jupyter_output><empty_output><jupyter_text>Next define the LDA model, e.g. using `create`.<jupyter_code>lda = da.LinearDiscriminantAnalysis(n_components=1)

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="U%}a9ac|TF?I@w$@u{j!">lda</variable><variable id="t#|IDvUc9z_$x]MRbwQb">da</variable></variables><block type="variables_set" id="YKGK8q}oHC2L?9R.[4t!" x="-58" y="266"><field name="VAR" id="U%}a9ac|TF?I@w$@u{j!">lda</field><value name="VALUE"><block type="varCreateObject" id="}QuzQLcR5iE|*j7`Cf;6"><field name="VAR" id="t#|IDvUc9z_$x]MRbwQb">da</field><field name="MEMBER">LinearDiscriminantAnalysis</field><data>da:LinearDiscriminantAnalysis</data><value name="INPUT"><block type="lists_create_with" id="Yjx!q**Yny(IJC9]60c#"><mutation items="1"></mutation><value name="ADD0"><block type="dummyOutputCodeBlock" id="4JrrM7#xYy=;S5F{s5Dk"><field name="CODE">n_components=1</field></block></value></block></value></block></value></block></xml><jupyter_output><empty_output><jupyter_text>Now perform LDA.Because we need `ravel` to reformat the data for `sklearn`, go ahead and import `numpy`.<jupyter_code>import numpy as np

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="Zhzp)s*VL?V@ES3(j:*b">np</variable></variables><block type="importAs" id="6{E|A)Ya_2{hXV^n,QJP" x="16" y="10"><field name="libraryName">numpy</field><field name="libraryAlias" id="Zhzp)s*VL?V@ES3(j:*b">np</field></block></xml><jupyter_output><empty_output><jupyter_text>Train LDA and do the predictions in one cell.Save the predictions in the variable `ldaPredictions`, and then show the predictions to make sure it worked.<jupyter_code>lda.fit(splits[0], np.ravel(splits[2]))

ldaPredictions = lda.predict(splits[1])

ldaPredictions

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="U%}a9ac|TF?I@w$@u{j!">lda</variable><variable id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</variable><variable id="Zhzp)s*VL?V@ES3(j:*b">np</variable><variable id="_ut$e0PL4OMi4o1MXTpw">splits</variable></variables><block type="varDoMethod" id="Z)$q-zn.KTC{+^l-wH6u" x="259" y="169"><field name="VAR" id="U%}a9ac|TF?I@w$@u{j!">lda</field><field name="MEMBER">fit</field><data>lda:fit</data><value name="INPUT"><block type="lists_create_with" id="e_B;36VOJ^lH70V=aWY}"><mutation items="2"></mutation><value name="ADD0"><block type="lists_getIndex" id="C#,#1*rEm+]qEx?L1x[L"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="b_Sz{9#d7d=ystO|k?l_"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id=":s{r1~S,,@.CSh#9`$R;"><field name="NUM">1</field></block></value></block></value><value name="ADD1"><block type="varDoMethod" id="zYBlZ,!^P^%Ib5R@Ch.H"><field name="VAR" id="Zhzp)s*VL?V@ES3(j:*b">np</field><field name="MEMBER">ravel</field><data>np:ravel</data><value name="INPUT"><block type="lists_create_with" id="9s({WSn={~Ink.5O+6Cc"><mutation items="1"></mutation><value name="ADD0"><block type="lists_getIndex" id="^)UBt0jM;BnGmWeG7pw*"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="4Vo!*g]qQ=D}XtD2i39/"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="RysCD3.C27sBxztz(T}2"><field name="NUM">3</field></block></value></block></value></block></value></block></value></block></value></block><block type="variables_set" id="qn$V)Zc+x1,^]#ZWBu0;" x="259" y="295"><field name="VAR" id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</field><value name="VALUE"><block type="varDoMethod" id="rq%)Cuo$S!Rv]]83[(OQ"><field name="VAR" id="U%}a9ac|TF?I@w$@u{j!">lda</field><field name="MEMBER">predict</field><data>lda:predict</data><value name="INPUT"><block type="lists_create_with" id="g!3rY4P1(W4]{2oU]dDb"><mutation items="1"></mutation><value name="ADD0"><block type="lists_getIndex" id="70_kQu*_lQfPL$)[E*]S"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="f8W4Kf.xz`3No$qQ@|16"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="}:Kw6eCPs%6nW8FY_E!N"><field name="NUM">2</field></block></value></block></value></block></value></block></value></block><block type="variables_get" id="l~QDDPzM@1QK#7dp~N~H" x="264" y="396"><field name="VAR" id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</field></block></xml><jupyter_output><empty_output><jupyter_text>You should see a mix of `1` and `0` in the predictions. **QUESTION:**Do you think `0` or `1` is more common in this dataset?What could you do with the dataframe to check? **ANSWER: (click here to edit)***`0` looks more common. An easy way to check would be to use `describe` on the dataframe.*  KNNFirst import `neighbors`.<jupyter_code>import sklearn.neighbors as neighbors

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="~%[Y}a{Syr+LrQ[I8?d(">neighbors</variable></variables><block type="importAs" id="ayw$B{(evWs,ynm/fP%g" x="123" y="313"><field name="libraryName">sklearn.neighbors</field><field name="libraryAlias" id="~%[Y}a{Syr+LrQ[I8?d(">neighbors</field></block></xml><jupyter_output><empty_output><jupyter_text>Next define the KNN model, e.g. using `create`.<jupyter_code>knn = neighbors.KNeighborsClassifier(n_neighbors=5)

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="DOr#Z1+,_{KG18zR#G)4">knn</variable><variable id="~%[Y}a{Syr+LrQ[I8?d(">neighbors</variable></variables><block type="variables_set" id="`z.|tb|jX$Y2a5;iN{pz" x="-7" y="283"><field name="VAR" id="DOr#Z1+,_{KG18zR#G)4">knn</field><value name="VALUE"><block type="varCreateObject" id="9i7]OHHOnhf2~MjH^n+y"><field name="VAR" id="~%[Y}a{Syr+LrQ[I8?d(">neighbors</field><field name="MEMBER">KNeighborsClassifier</field><data>neighbors:KNeighborsClassifier</data><value name="INPUT"><block type="lists_create_with" id="`.s=9G`sj;R8,?u+OR9Y"><mutation items="1"></mutation><value name="ADD0"><block type="dummyOutputCodeBlock" id="R*)t!8POzxw3}@cy,{:]"><field name="CODE">n_neighbors=5</field></block></value></block></value></block></value></block></xml><jupyter_output><empty_output><jupyter_text>Now train KNN and do the predictions in one cell.Save the predictions in the variable `knnPredictions`, and then show the predictions to make sure it worked.<jupyter_code>knn.fit(splits[0], np.ravel(splits[2]))

knnPredictions = knn.predict(splits[1])

knnPredictions

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="DOr#Z1+,_{KG18zR#G)4">knn</variable><variable id="vM0j[9C:2`MqdV2+),oH">knnPredictions</variable><variable id="Zhzp)s*VL?V@ES3(j:*b">np</variable><variable id="_ut$e0PL4OMi4o1MXTpw">splits</variable></variables><block type="varDoMethod" id="Z)$q-zn.KTC{+^l-wH6u" x="259" y="169"><field name="VAR" id="DOr#Z1+,_{KG18zR#G)4">knn</field><field name="MEMBER">fit</field><data>knn:fit</data><value name="INPUT"><block type="lists_create_with" id="e_B;36VOJ^lH70V=aWY}"><mutation items="2"></mutation><value name="ADD0"><block type="lists_getIndex" id="C#,#1*rEm+]qEx?L1x[L"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="b_Sz{9#d7d=ystO|k?l_"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id=":s{r1~S,,@.CSh#9`$R;"><field name="NUM">1</field></block></value></block></value><value name="ADD1"><block type="varDoMethod" id="zYBlZ,!^P^%Ib5R@Ch.H"><field name="VAR" id="Zhzp)s*VL?V@ES3(j:*b">np</field><field name="MEMBER">ravel</field><data>np:ravel</data><value name="INPUT"><block type="lists_create_with" id="9s({WSn={~Ink.5O+6Cc"><mutation items="1"></mutation><value name="ADD0"><block type="lists_getIndex" id="^)UBt0jM;BnGmWeG7pw*"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="4Vo!*g]qQ=D}XtD2i39/"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="RysCD3.C27sBxztz(T}2"><field name="NUM">3</field></block></value></block></value></block></value></block></value></block></value></block><block type="variables_set" id="qn$V)Zc+x1,^]#ZWBu0;" x="259" y="295"><field name="VAR" id="vM0j[9C:2`MqdV2+),oH">knnPredictions</field><value name="VALUE"><block type="varDoMethod" id="rq%)Cuo$S!Rv]]83[(OQ"><field name="VAR" id="DOr#Z1+,_{KG18zR#G)4">knn</field><field name="MEMBER">predict</field><data>knn:predict</data><value name="INPUT"><block type="lists_create_with" id="g!3rY4P1(W4]{2oU]dDb"><mutation items="1"></mutation><value name="ADD0"><block type="lists_getIndex" id="70_kQu*_lQfPL$)[E*]S"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="f8W4Kf.xz`3No$qQ@|16"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="}:Kw6eCPs%6nW8FY_E!N"><field name="NUM">2</field></block></value></block></value></block></value></block></value></block><block type="variables_get" id="l~QDDPzM@1QK#7dp~N~H" x="264" y="396"><field name="VAR" id="vM0j[9C:2`MqdV2+),oH">knnPredictions</field></block></xml><jupyter_output><empty_output><jupyter_text>Classifier evaluationTo see if the models are any good, do some evaluations.First import `metrics`.<jupyter_code>import sklearn.metrics as metrics

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="C8HQ^p):qDkQA/nhxe{{">metrics</variable></variables><block type="importAs" id="|1fEPj_#9:@0Qa@y|1F." x="135" y="207"><field name="libraryName">sklearn.metrics</field><field name="libraryAlias" id="C8HQ^p):qDkQA/nhxe{{">metrics</field></block></xml><jupyter_output><empty_output><jupyter_text>AccuracyCalculate the LDA accuracy.<jupyter_code>metrics.accuracy_score(splits[3], ldaPredictions)

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="C8HQ^p):qDkQA/nhxe{{">metrics</variable><variable id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</variable><variable id="_ut$e0PL4OMi4o1MXTpw">splits</variable></variables><block type="varDoMethod" id="l,28Y;cf6Q.r-rTBWP4}" x="8" y="188"><field name="VAR" id="C8HQ^p):qDkQA/nhxe{{">metrics</field><field name="MEMBER">accuracy_score</field><data>metrics:accuracy_score</data><value name="INPUT"><block type="lists_create_with" id="`V^tbb51h?lxzH^Z,L0+"><mutation items="2"></mutation><value name="ADD0"><block type="lists_getIndex" id="6Nb!QUCs+zcSbgi2=8Jb"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="0SesC-O%M@-rqWgnWJ[q"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="ZQBTZmCAMzn[;:irh:{@"><field name="NUM">4</field></block></value></block></value><value name="ADD1"><block type="variables_get" id=":5r(/A;QY|,.0/gzcHi."><field name="VAR" id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</field></block></value></block></value></block></xml><jupyter_output><empty_output><jupyter_text>And calculate the KNN accuracy.<jupyter_code>metrics.accuracy_score(splits[3], knnPredictions)

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="C8HQ^p):qDkQA/nhxe{{">metrics</variable><variable id="vM0j[9C:2`MqdV2+),oH">knnPredictions</variable><variable id="_ut$e0PL4OMi4o1MXTpw">splits</variable></variables><block type="varDoMethod" id="l,28Y;cf6Q.r-rTBWP4}" x="8" y="188"><field name="VAR" id="C8HQ^p):qDkQA/nhxe{{">metrics</field><field name="MEMBER">accuracy_score</field><data>metrics:accuracy_score</data><value name="INPUT"><block type="lists_create_with" id="`V^tbb51h?lxzH^Z,L0+"><mutation items="2"></mutation><value name="ADD0"><block type="lists_getIndex" id="6Nb!QUCs+zcSbgi2=8Jb"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="0SesC-O%M@-rqWgnWJ[q"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="ZQBTZmCAMzn[;:irh:{@"><field name="NUM">4</field></block></value></block></value><value name="ADD1"><block type="variables_get" id=":5r(/A;QY|,.0/gzcHi."><field name="VAR" id="vM0j[9C:2`MqdV2+),oH">knnPredictions</field></block></value></block></value></block></xml><jupyter_output><empty_output><jupyter_text>**QUESTION:**Which has more accuracy for you, LDA or KNN?Why do you think that might be? **ANSWER: (click here to edit)***LDA is higher, but just by a bit. I would expect LDA to do better on linear problems in general; since LDA does better here, this could be a linear problem.*  Recall/Precision per classGet the LDA `classification_report`.<jupyter_code>print(metrics.classification_report(splits[3], ldaPredictions))

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="C8HQ^p):qDkQA/nhxe{{">metrics</variable><variable id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</variable><variable id="_ut$e0PL4OMi4o1MXTpw">splits</variable></variables><block type="text_print" id="Sx0uF}9IfzkucZiyR^1:" x="196" y="360"><value name="TEXT"><shadow type="text" id="a=J{E!^,yE0MlS-rEKnT"><field name="TEXT">abc</field></shadow><block type="varDoMethod" id="l,28Y;cf6Q.r-rTBWP4}"><field name="VAR" id="C8HQ^p):qDkQA/nhxe{{">metrics</field><field name="MEMBER">classification_report</field><data>metrics:classification_report</data><value name="INPUT"><block type="lists_create_with" id="`V^tbb51h?lxzH^Z,L0+"><mutation items="2"></mutation><value name="ADD0"><block type="lists_getIndex" id="6Nb!QUCs+zcSbgi2=8Jb"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="0SesC-O%M@-rqWgnWJ[q"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="ZQBTZmCAMzn[;:irh:{@"><field name="NUM">4</field></block></value></block></value><value name="ADD1"><block type="variables_get" id=":5r(/A;QY|,.0/gzcHi."><field name="VAR" id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</field></block></value></block></value></block></value></block></xml><jupyter_output>precision    recall  f1-score   support

           0       0.76      0.91      0.83        57
           1       0.58      0.30      0.40        23

    accuracy                           0.74        80
   macro avg       0.67      0.61      0.62        80
weighted avg       0.71      0.74      0.71        80<jupyter_text>And get the KNN `classification_report`.<jupyter_code>print(metrics.classification_report(splits[3], knnPredictions))

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="C8HQ^p):qDkQA/nhxe{{">metrics</variable><variable id="vM0j[9C:2`MqdV2+),oH">knnPredictions</variable><variable id="_ut$e0PL4OMi4o1MXTpw">splits</variable></variables><block type="text_print" id="Sx0uF}9IfzkucZiyR^1:" x="196" y="360"><value name="TEXT"><shadow type="text" id="a=J{E!^,yE0MlS-rEKnT"><field name="TEXT">abc</field></shadow><block type="varDoMethod" id="l,28Y;cf6Q.r-rTBWP4}"><field name="VAR" id="C8HQ^p):qDkQA/nhxe{{">metrics</field><field name="MEMBER">classification_report</field><data>metrics:classification_report</data><value name="INPUT"><block type="lists_create_with" id="`V^tbb51h?lxzH^Z,L0+"><mutation items="2"></mutation><value name="ADD0"><block type="lists_getIndex" id="6Nb!QUCs+zcSbgi2=8Jb"><mutation statement="false" at="true"></mutation><field name="MODE">GET</field><field name="WHERE">FROM_START</field><value name="VALUE"><block type="variables_get" id="0SesC-O%M@-rqWgnWJ[q"><field name="VAR" id="_ut$e0PL4OMi4o1MXTpw">splits</field></block></value><value name="AT"><block type="math_number" id="ZQBTZmCAMzn[;:irh:{@"><field name="NUM">4</field></block></value></block></value><value name="ADD1"><block type="variables_get" id=":5r(/A;QY|,.0/gzcHi."><field name="VAR" id="vM0j[9C:2`MqdV2+),oH">knnPredictions</field></block></value></block></value></block></value></block></xml><jupyter_output>precision    recall  f1-score   support

           0       0.77      0.84      0.81        57
           1       0.50      0.39      0.44        23

    accuracy                           0.71        80
   macro avg       0.64      0.62      0.62        80
weighted avg       0.70      0.71      0.70        80<jupyter_text>**QUESTION:**Is there a particular class that LDA, KNN, or both do worse on?Why do you think that might be? **ANSWER: (click here to edit)***About 69% of the `admit`s are `0`, so the classifiers have a harder time with the `1`s because they are less common. This is called imbalanced classes and is problem often seen in the real world.*  Comparing classifiersThere are several ways to compare classifiers, including using accuracy and the classification report.But it is also interesting to ask how much the classifiers *agree* with each other on both correct and incorrect answers.The easiest way to do this is to use `accuracy` again, but use `knnPredictions` and `ldaPredictions` instead of `Ytest`.Try it.<jupyter_code>metrics.accuracy_score(knnPredictions, ldaPredictions)

#<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="C8HQ^p):qDkQA/nhxe{{">metrics</variable><variable id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</variable><variable id="vM0j[9C:2`MqdV2+),oH">knnPredictions</variable></variables><block type="varDoMethod" id="l,28Y;cf6Q.r-rTBWP4}" x="8" y="188"><field name="VAR" id="C8HQ^p):qDkQA/nhxe{{">metrics</field><field name="MEMBER">accuracy_score</field><data>metrics:accuracy_score</data><value name="INPUT"><block type="lists_create_with" id="`V^tbb51h?lxzH^Z,L0+"><mutation items="2"></mutation><value name="ADD0"><block type="variables_get" id="H]m:.G;[cFC!Mlp9-+xa"><field name="VAR" id="vM0j[9C:2`MqdV2+),oH">knnPredictions</field></block></value><value name="ADD1"><block type="variables_get" id=":5r(/A;QY|,.0/gzcHi."><field name="VAR" id="oi3{Kz:y:^!,i1^jb/Ue">ldaPredictions</field></block></value></block></value></block></xml><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22260072829789734, 'fraction_numerical': 0.033243274991189946, 'mean_word_length': 9.817450232952138, 'pattern_counts': {'":': 8, '<': 901, '<?xml version=': 0, '>': 901, 'https://': 19, 'lorem ipsum': 0, 'www.': 0, 'xml': 76}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '196357', 'license': 'Apache-2.0', 'n_tokens_mistral': 10229, 'n_tokens_neox': 9820, 'n_words': 1229, 'path': 'KNN-classification-PS.ipynb', 'repo_name': 'memphis-iis/datawhys-intern-solutions-2020', 'size': '25539'}