Skip to content

Instantly share code, notes, and snippets.

@guomaimang
Created March 29, 2023 14:19
Show Gist options
  • Save guomaimang/475c96545090ec3d6a14c38faf51d3f1 to your computer and use it in GitHub Desktop.
Save guomaimang/475c96545090ec3d6a14c38faf51d3f1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Problem 2 (5 points)
Load dataset in problem2data.txt by using “numpy.loadtxt()” in Python, which contains two synthetic classes separated by a non-linear function y = f (x), with additive noise. The last column contains “0” or “1”, indicating the corresponding classes. You are asked to investigate the extent to which polynomial functions can be used to build a logistic regression classifier.
(a) Build a logistic regression classifier by using a polynomial function of order 1 (e.g., θ0 + θ1x1 + θ2x2). Apply 5-fold-cross-validation to generate training and test sets. Use the training set to train the model. Compute its five F1 scores on test set (one F1 score for each fold). Compute its five F1 scores on training set. You will need “f1 score” from “sklearn.metrics”.
(b) Repeat part (a) for polynomials of order 2 to 12. You will need “PolynomialFeatures” from “sklearn.preprocessing”.
(c) Repeatparts(a-b)50times,andestimatetheaverageF1scoreforeachpolynomialorder(average across its 5 × 50 runs, both for training and test sets). Generate a plot that shows the F1 scores versus the polynomial order.
(d) Discuss how the F1 scores of the model changes as a function of the polynomial order.
(e) Use Jupyter Notebook to perform implementation. You are required to submit your “.ipynb” file. You could use “LogisticRegression” from “sklearn.linear model”. Do not add any regulariza- tions.
0.613314 1.138547 1
0.262046 0.245954 1
0.809557 1.481712 0
-0.718433 1.017201 0
1.029742 -0.930264 1
0.774779 0.421600 1
-0.199856 0.834872 1
1.375865 1.069579 0
1.989108 -1.011556 0
-0.824581 1.162131 0
0.317651 1.015422 1
-0.290987 -1.444732 0
-0.425702 0.733502 0
-0.272108 1.815222 0
0.549337 1.057739 1
-0.848813 -2.976883 0
0.012111 -0.225729 1
0.390161 -0.183677 1
-1.926063 -1.008605 0
-1.099828 -0.694339 0
0.508255 0.578926 1
0.850737 0.651793 1
-0.033053 -0.130284 1
0.787215 -0.762899 1
-0.038519 1.611504 0
-0.472445 -0.386231 1
1.258378 0.419605 0
-0.717852 0.125713 1
-0.582736 -1.876984 0
0.325972 -0.606360 0
0.186885 -1.774063 0
0.060160 0.531081 1
2.282959 -0.799219 0
-0.042758 -0.569428 1
-0.349675 1.026485 1
-0.836310 0.314065 0
1.402300 1.353519 1
1.790429 0.093447 0
-0.657885 -0.469742 1
0.081076 0.675757 1
-0.427873 0.780149 1
0.305725 -0.249730 1
0.114417 -0.474510 1
1.405881 -1.613601 0
-0.342941 1.171695 1
-0.536916 0.809782 1
0.866872 0.879536 1
-0.178839 0.178593 1
-0.386451 -0.272887 1
-0.444673 1.325140 0
-0.427720 -1.325383 0
1.493518 -0.397801 0
1.598455 0.863339 0
-0.589265 -0.558992 1
0.498690 -0.975839 1
-0.066833 0.651329 0
-0.882276 0.851463 1
0.568176 0.578583 1
1.481210 1.119399 0
-0.386217 0.287276 1
-1.646837 0.488304 1
-0.947643 1.407636 0
0.973617 0.301670 1
-1.399947 0.226901 1
-0.705675 0.249466 1
0.670474 0.164554 1
-0.892780 -0.749184 1
-0.988287 0.419198 1
-0.062565 0.483585 1
-1.008232 -0.774024 0
-0.968482 0.425189 1
-0.536537 0.992213 1
-1.036762 -0.766457 0
1.139686 0.584417 0
-1.699671 0.282727 0
0.780049 -0.016618 1
0.356973 0.184010 1
0.194489 -1.518220 1
0.748962 0.249237 1
-1.808371 -0.601036 0
0.505844 0.260151 1
-1.937025 0.613470 0
-0.732923 -0.615180 0
-1.204355 -0.206517 1
-1.041579 -1.662270 0
2.304348 -1.274444 0
-0.047930 -0.364013 1
1.000271 0.460434 1
0.736371 0.165175 1
0.881891 0.263887 1
0.800148 -0.591255 1
0.550951 -0.729138 1
0.564236 -0.195021 1
2.241986 0.143139 0
-0.546970 -1.456324 1
1.650634 1.065971 0
-0.226952 -1.960053 0
-1.801285 1.479390 0
0.066124 -0.832030 0
-1.316995 0.549992 0
1.618044 -1.098968 0
-0.026781 -1.629827 1
0.259780 -0.193447 1
0.283243 1.838626 0
-1.250759 0.136003 1
-0.193607 2.211414 0
-0.697837 0.923998 1
1.580176 -0.488059 0
1.905097 -1.539129 0
-1.432117 0.587596 0
-0.819680 1.206287 1
-0.721205 0.013837 1
-1.238450 -1.027095 1
0.569622 -0.305500 1
1.227231 0.055767 1
-0.243673 -1.467144 0
-0.056698 -0.758902 1
1.134139 0.579336 0
0.750128 1.588325 0
-0.011491 -0.357357 1
0.157716 -0.592261 1
0.975409 1.604715 0
-0.033511 -1.884186 0
0.803727 0.195732 1
-1.494797 0.260357 0
0.829612 0.760352 1
0.903573 0.330151 1
-0.224743 0.888610 1
0.491423 1.846284 0
-0.059776 1.107025 0
1.285392 -1.675366 1
-0.967001 1.301542 0
0.057726 -0.516695 1
-0.265231 2.024662 0
-0.491739 -0.326646 1
-0.290933 0.900442 1
0.142664 -0.940222 1
0.610646 -0.961925 1
-1.069967 0.005860 1
1.084554 -0.225489 0
-1.262739 -0.967477 0
-2.202103 1.031785 0
1.519489 -0.850220 0
1.059279 0.262240 1
0.198421 -1.417318 0
-0.030526 0.577294 1
0.141733 -1.767388 0
-1.338873 0.954409 0
-1.386694 0.177172 0
1.323029 0.583639 0
1.595514 0.168128 0
-0.772088 2.087584 0
-0.147904 0.200109 1
0.794853 1.690710 0
-0.550274 1.128170 1
-0.054331 -0.944408 1
1.401778 -1.437623 0
-0.128683 -1.199107 0
-1.085437 0.693021 0
0.423416 0.916563 1
1.157933 0.022142 0
-0.188484 1.413747 1
-0.867737 -0.356470 1
0.466978 0.185830 1
0.275667 -0.638717 1
1.083973 1.292849 1
-1.383083 0.241033 0
-1.334178 1.011217 0
0.938851 0.068744 1
0.633656 0.028857 1
1.719417 1.020423 0
-1.534748 1.313091 0
-1.160450 -0.036652 1
-0.343292 1.447463 0
-0.189255 -1.731416 0
-0.469645 -1.886983 0
-1.222660 -1.629282 0
-0.232676 2.104827 0
1.470893 -2.330178 0
-1.548380 0.755267 0
-0.398179 -0.069614 1
-0.534760 -0.676713 1
-0.821939 -0.073104 1
-2.020913 0.076581 0
-0.315938 -0.176290 1
1.626721 1.288116 1
-1.182973 0.978748 0
-1.695017 0.487101 0
0.690161 0.190217 0
-0.023415 -0.103744 1
1.353762 -0.847091 0
0.904993 -0.433884 0
1.550321 -0.245616 1
-0.051601 0.532306 1
0.180012 -0.400915 0
-0.582564 -0.284964 1
-0.364865 -1.796093 0
0.753614 -0.837184 1
1.508759 -1.343763 0
-0.729070 -0.444445 1
-0.901131 0.248629 1
0.696850 -0.174010 1
-0.420171 0.698103 1
1.717383 -1.087999 0
-1.102521 1.250952 0
0.491983 0.222628 1
-1.446794 -0.381357 0
-1.342031 0.427488 0
-0.528468 -1.395423 0
-0.463651 -0.685014 1
1.269173 -0.870687 0
1.616297 0.826770 0
-0.784221 -0.602980 1
0.323480 -0.320430 0
-1.690844 -0.873425 0
-1.453109 -0.929109 0
-1.407910 0.873465 0
-0.105939 1.820689 0
-1.550853 1.953872 0
0.825880 1.156696 0
1.801087 1.347545 0
1.591594 0.872608 0
-0.372558 -1.472208 1
1.841180 -0.002700 0
-0.317781 1.043911 1
-1.823326 0.193140 0
0.777251 0.096300 1
-0.896354 0.032070 1
0.653807 -1.056308 1
-0.396580 -1.382814 1
-0.414860 0.043164 1
-0.416350 1.278864 0
0.587992 1.159943 1
1.181592 0.009188 1
-0.549632 -1.755756 1
0.275653 1.016104 0
-1.533291 -0.717830 0
0.014515 0.970354 1
0.865654 -1.479030 1
-1.393019 -0.555454 0
-0.453115 -2.174316 0
0.700745 -0.583529 0
1.432943 -1.634038 0
0.032781 -1.045794 0
-0.223411 -1.083523 1
1.366038 -0.391939 1
-1.865088 -0.266794 0
0.473467 0.847037 1
-0.285082 -0.083150 1
0.405214 -0.633274 1
-0.759647 0.282055 1
-0.060479 -0.055749 1
0.762395 0.412079 0
-1.352862 -1.800141 0
0.320452 -0.999484 1
0.425554 -0.448655 1
-0.446631 -0.178530 1
0.294769 -0.578956 1
-1.107063 0.849657 1
-0.727018 -0.776311 0
-0.506762 -0.878681 1
-0.819493 -0.053662 1
1.478921 -1.345741 0
0.868603 -0.352663 1
1.419151 -1.829074 0
-1.495157 1.170090 0
-1.123626 -0.304015 1
0.937412 1.240584 0
1.404310 -1.065227 0
0.848379 -0.333967 1
1.664446 -0.232740 1
-1.035274 -1.568599 0
-0.003818 -0.230050 1
-1.327906 -0.861318 0
0.632168 -0.004161 1
0.354735 1.408302 0
-2.431032 0.283745 0
-1.368860 0.902849 1
0.905052 -1.060739 0
-1.702759 0.127006 0
-1.293567 -2.213517 0
-0.598508 1.565842 0
0.555133 -0.717331 1
-1.443826 1.725842 0
2.562633 0.333265 0
1.076085 2.284061 0
-1.305517 0.805411 0
0.375260 -1.303618 1
0.197543 -0.523846 1
2.061584 -0.716461 0
1.275767 1.595834 0
0.416572 0.752419 1
-0.671428 0.410974 1
0.180958 1.308233 0
1.354086 0.108842 1
0.652330 -0.641753 1
-0.518464 -0.943119 1
0.847956 1.105545 0
-0.220733 -0.875584 1
-0.057265 1.004688 1
1.341052 1.293779 0
0.753888 0.114469 0
0.242388 0.964681 0
0.138125 -1.220668 1
-0.784929 0.975558 1
1.513990 0.064605 0
1.158068 -0.838852 1
0.301329 2.311115 0
-0.438949 0.631913 1
-0.106826 -1.409216 0
1.453599 -1.240063 0
-0.456405 -0.125271 1
0.518014 1.152157 1
1.849083 0.966669 0
1.554057 0.259868 0
0.093039 0.469581 1
0.658387 -1.684434 0
-1.404011 -0.398327 0
1.716883 0.869810 0
0.377063 -1.678740 0
0.158166 -1.491699 0
-1.731936 -1.268462 0
0.747261 1.391268 1
-2.064615 -0.277745 0
0.945036 -1.809102 0
-0.346379 -0.520447 1
0.250938 -0.150477 1
0.719655 -0.036619 1
1.562449 0.092212 0
0.425155 -0.636461 0
0.029309 1.531372 0
0.701972 -0.701907 1
1.329626 -0.624012 1
-1.427677 0.351236 1
-1.049503 -0.235406 1
-1.272584 -1.332112 0
-0.710129 0.986293 0
-1.464080 1.076835 0
0.881803 0.075523 1
-1.753538 0.948819 0
-1.008525 0.500687 1
-0.175808 -1.442803 0
0.053602 -0.581460 1
0.978815 1.536600 0
0.111467 -1.536273 0
-0.516901 1.455998 1
0.216782 -2.061335 0
0.733313 -1.623603 0
-0.616086 0.453617 1
-0.007185 -0.972491 0
-0.369403 -0.412201 1
-1.493300 0.376542 0
0.326352 -1.174460 0
-1.684900 -0.623278 0
-0.616161 -0.042049 1
-0.833410 0.477024 1
-0.094719 0.741931 1
-0.730714 2.299293 0
-2.034351 1.114754 0
-0.043821 -1.246739 0
-1.633289 -0.874058 0
-0.544968 -1.171923 0
0.723487 -1.678109 0
-1.547766 -1.163376 0
-0.479259 0.823491 1
-0.915526 -1.026850 1
0.348547 0.549239 0
0.595157 -0.301924 1
-0.624280 1.555556 0
0.495535 -1.905047 0
1.810173 -0.150213 0
-0.654424 1.752815 0
0.194886 0.893076 1
-0.445449 0.024576 1
-0.772282 -0.170362 1
0.468692 1.317051 0
0.839897 -0.492119 1
1.309667 0.468775 0
-0.141550 -0.699748 1
-1.340752 0.150726 1
0.369285 0.882737 0
-0.160929 -0.854501 0
-0.744320 1.709049 0
1.013220 -0.534619 1
0.432823 -0.416634 1
-1.211977 -1.288574 0
-0.135305 -0.875032 1
2.139802 -0.532939 0
0.476824 1.116217 0
-0.124238 0.286110 1
0.836918 -1.217247 1
-0.675714 -0.187682 1
1.117730 0.352988 1
-0.217155 0.755173 1
-1.103079 1.063341 1
0.142455 0.635044 1
-0.348947 -0.343763 0
-0.597154 -0.440448 0
-0.010096 0.307627 1
1.231562 0.758073 1
0.577225 -1.043268 0
0.458278 2.033535 0
-0.882106 0.287630 1
1.742536 -0.569830 0
1.444839 -0.246868 1
-0.247111 1.074756 0
1.671405 -0.621897 0
-2.431672 -0.664778 0
-0.465113 -0.312862 1
1.046766 0.920431 1
0.820692 0.895364 1
-2.047267 -0.519237 0
-1.173254 -1.670673 0
-0.495140 1.277688 0
-0.394906 -1.256389 1
-1.558798 -1.089759 0
-0.096177 0.554349 1
-0.252144 0.377909 1
0.474246 1.029804 1
-1.445488 -0.217804 0
0.512017 -1.180014 0
-0.445507 0.079108 1
0.689839 0.204562 1
-1.007628 -0.073571 0
-0.870789 -1.428898 0
1.378152 0.709524 1
-0.418085 -0.647960 1
1.251540 -0.803086 0
-1.638293 0.707260 1
1.298283 1.269640 0
0.595977 -1.674164 0
-0.664050 0.740811 0
0.994038 0.823883 0
-0.321838 1.767136 1
-0.875613 0.390224 1
-0.837781 0.475573 0
-0.525564 0.917389 1
-0.937473 1.663801 1
1.709189 2.006025 0
-0.040300 1.828893 0
0.043507 0.757025 0
0.762153 0.552114 1
1.066754 0.066290 0
1.093466 -0.452004 1
1.049337 1.359941 0
-2.207476 -1.201178 1
-0.514644 -0.787856 1
0.892531 -0.462921 1
0.605638 0.645604 0
-1.988550 -0.472402 0
-0.794120 -0.374479 1
-1.617307 -0.606368 0
0.452580 -1.440601 1
0.973746 -1.186408 0
0.124749 -0.189088 1
0.616990 -1.580109 0
0.340519 0.962051 1
-1.017410 0.305002 1
0.366321 0.095228 0
0.329893 1.464727 1
0.634794 0.496731 0
0.724411 0.984778 1
-0.586836 0.515333 1
1.177428 0.348781 0
0.375147 -0.331895 1
-1.251358 -0.557798 0
-0.911512 2.300462 0
-0.663156 -0.597960 1
0.940920 0.415351 0
1.616829 -1.166078 0
-0.108880 1.345235 0
0.295136 0.893782 0
-0.998981 -0.923879 1
-0.574138 -0.920751 1
-1.656047 -0.512795 0
1.361980 -0.615184 1
-0.571196 -0.134894 1
0.904397 -0.621630 1
-0.874430 0.034924 1
-0.825504 -0.060562 1
0.452563 0.361893 1
-0.091334 1.005199 1
-0.439472 -2.164088 0
-0.029154 0.640696 1
-0.956310 -0.812389 1
1.457361 -0.219097 0
0.235592 1.388073 0
0.314694 -0.909554 1
-1.631890 -0.413033 0
-0.653021 -1.163834 0
0.145379 -1.379731 1
-0.425382 0.679273 1
-1.523212 0.646107 0
0.613313 -1.190313 0
-0.376459 0.615096 1
1.011127 -0.327964 1
0.169213 0.829311 0
-0.614421 -0.347707 1
1.531586 -0.408730 0
1.317895 1.429511 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment