機器學習
This commit is contained in:
commit
e21eb40033
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
66
.idea/inspectionProfiles/Project_Default.xml
Normal file
66
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,66 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="HttpUrlsUsage" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="ignoredUrls">
|
||||
<list>
|
||||
<option value="http://0.0.0.0" />
|
||||
<option value="http://127.0.0.1" />
|
||||
<option value="http://activemq.apache.org/schema/" />
|
||||
<option value="http://cxf.apache.org/schemas/" />
|
||||
<option value="http://java.sun.com/" />
|
||||
<option value="http://javafx.com/fxml" />
|
||||
<option value="http://javafx.com/javafx/" />
|
||||
<option value="http://json-schema.org/draft" />
|
||||
<option value="http://localhost" />
|
||||
<option value="http://maven.apache.org/POM/" />
|
||||
<option value="http://maven.apache.org/xsd/" />
|
||||
<option value="http://primefaces.org/ui" />
|
||||
<option value="http://schema.cloudfoundry.org/spring/" />
|
||||
<option value="http://schemas.xmlsoap.org/" />
|
||||
<option value="http://tiles.apache.org/" />
|
||||
<option value="http://www.dda5.com" />
|
||||
<option value="http://www.ibm.com/webservices/xsd" />
|
||||
<option value="http://www.jboss.com/xml/ns/" />
|
||||
<option value="http://www.jboss.org/j2ee/schema/" />
|
||||
<option value="http://www.springframework.org/schema/" />
|
||||
<option value="http://www.springframework.org/security/tags" />
|
||||
<option value="http://www.springframework.org/tags" />
|
||||
<option value="http://www.thymeleaf.org" />
|
||||
<option value="http://www.w3.org/" />
|
||||
<option value="http://xmlns.jcp.org/" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredPackages">
|
||||
<value>
|
||||
<list size="22">
|
||||
<item index="0" class="java.lang.String" itemvalue="clyent" />
|
||||
<item index="1" class="java.lang.String" itemvalue="protobuf" />
|
||||
<item index="2" class="java.lang.String" itemvalue="python-lsp-jsonrpc" />
|
||||
<item index="3" class="java.lang.String" itemvalue="atomicwrites" />
|
||||
<item index="4" class="java.lang.String" itemvalue="jsonpointer" />
|
||||
<item index="5" class="java.lang.String" itemvalue="et-xmlfile" />
|
||||
<item index="6" class="java.lang.String" itemvalue="PyQtWebEngine" />
|
||||
<item index="7" class="java.lang.String" itemvalue="pyasn1-modules" />
|
||||
<item index="8" class="java.lang.String" itemvalue="fonttools" />
|
||||
<item index="9" class="java.lang.String" itemvalue="patsy" />
|
||||
<item index="10" class="java.lang.String" itemvalue="pyls-spyder" />
|
||||
<item index="11" class="java.lang.String" itemvalue="appdirs" />
|
||||
<item index="12" class="java.lang.String" itemvalue="conda-repo-cli" />
|
||||
<item index="13" class="java.lang.String" itemvalue="munkres" />
|
||||
<item index="14" class="java.lang.String" itemvalue="backports.weakref" />
|
||||
<item index="15" class="java.lang.String" itemvalue="conda-verify" />
|
||||
<item index="16" class="java.lang.String" itemvalue="PyQt5" />
|
||||
<item index="17" class="java.lang.String" itemvalue="PyDispatcher" />
|
||||
<item index="18" class="java.lang.String" itemvalue="ply" />
|
||||
<item index="19" class="java.lang.String" itemvalue="webencodings" />
|
||||
<item index="20" class="java.lang.String" itemvalue="inflection" />
|
||||
<item index="21" class="java.lang.String" itemvalue="openpyxl" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
10
.idea/machineLeaningAtoZ.iml
Normal file
10
.idea/machineLeaningAtoZ.iml
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
7
.idea/misc.xml
Normal file
7
.idea/misc.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.9 (machineLeaningAtoZ)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (machineLeaningAtoZ)" project-jdk-type="Python SDK" />
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/machineLeaningAtoZ.iml" filepath="$PROJECT_DIR$/.idea/machineLeaningAtoZ.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/other.xml
Normal file
6
.idea/other.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PySciProjectComponent">
|
||||
<option name="PY_INTERACTIVE_PLOTS_SUGGESTED" value="true" />
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
401
section_10_model_selection/Social_Network_Ads.csv
Normal file
401
section_10_model_selection/Social_Network_Ads.csv
Normal file
@ -0,0 +1,401 @@
|
||||
User ID,Gender,Age,EstimatedSalary,Purchased
|
||||
15624510,Male,19,19000,0
|
||||
15810944,Male,35,20000,0
|
||||
15668575,Female,26,43000,0
|
||||
15603246,Female,27,57000,0
|
||||
15804002,Male,19,76000,0
|
||||
15728773,Male,27,58000,0
|
||||
15598044,Female,27,84000,0
|
||||
15694829,Female,32,150000,1
|
||||
15600575,Male,25,33000,0
|
||||
15727311,Female,35,65000,0
|
||||
15570769,Female,26,80000,0
|
||||
15606274,Female,26,52000,0
|
||||
15746139,Male,20,86000,0
|
||||
15704987,Male,32,18000,0
|
||||
15628972,Male,18,82000,0
|
||||
15697686,Male,29,80000,0
|
||||
15733883,Male,47,25000,1
|
||||
15617482,Male,45,26000,1
|
||||
15704583,Male,46,28000,1
|
||||
15621083,Female,48,29000,1
|
||||
15649487,Male,45,22000,1
|
||||
15736760,Female,47,49000,1
|
||||
15714658,Male,48,41000,1
|
||||
15599081,Female,45,22000,1
|
||||
15705113,Male,46,23000,1
|
||||
15631159,Male,47,20000,1
|
||||
15792818,Male,49,28000,1
|
||||
15633531,Female,47,30000,1
|
||||
15744529,Male,29,43000,0
|
||||
15669656,Male,31,18000,0
|
||||
15581198,Male,31,74000,0
|
||||
15729054,Female,27,137000,1
|
||||
15573452,Female,21,16000,0
|
||||
15776733,Female,28,44000,0
|
||||
15724858,Male,27,90000,0
|
||||
15713144,Male,35,27000,0
|
||||
15690188,Female,33,28000,0
|
||||
15689425,Male,30,49000,0
|
||||
15671766,Female,26,72000,0
|
||||
15782806,Female,27,31000,0
|
||||
15764419,Female,27,17000,0
|
||||
15591915,Female,33,51000,0
|
||||
15772798,Male,35,108000,0
|
||||
15792008,Male,30,15000,0
|
||||
15715541,Female,28,84000,0
|
||||
15639277,Male,23,20000,0
|
||||
15798850,Male,25,79000,0
|
||||
15776348,Female,27,54000,0
|
||||
15727696,Male,30,135000,1
|
||||
15793813,Female,31,89000,0
|
||||
15694395,Female,24,32000,0
|
||||
15764195,Female,18,44000,0
|
||||
15744919,Female,29,83000,0
|
||||
15671655,Female,35,23000,0
|
||||
15654901,Female,27,58000,0
|
||||
15649136,Female,24,55000,0
|
||||
15775562,Female,23,48000,0
|
||||
15807481,Male,28,79000,0
|
||||
15642885,Male,22,18000,0
|
||||
15789109,Female,32,117000,0
|
||||
15814004,Male,27,20000,0
|
||||
15673619,Male,25,87000,0
|
||||
15595135,Female,23,66000,0
|
||||
15583681,Male,32,120000,1
|
||||
15605000,Female,59,83000,0
|
||||
15718071,Male,24,58000,0
|
||||
15679760,Male,24,19000,0
|
||||
15654574,Female,23,82000,0
|
||||
15577178,Female,22,63000,0
|
||||
15595324,Female,31,68000,0
|
||||
15756932,Male,25,80000,0
|
||||
15726358,Female,24,27000,0
|
||||
15595228,Female,20,23000,0
|
||||
15782530,Female,33,113000,0
|
||||
15592877,Male,32,18000,0
|
||||
15651983,Male,34,112000,1
|
||||
15746737,Male,18,52000,0
|
||||
15774179,Female,22,27000,0
|
||||
15667265,Female,28,87000,0
|
||||
15655123,Female,26,17000,0
|
||||
15595917,Male,30,80000,0
|
||||
15668385,Male,39,42000,0
|
||||
15709476,Male,20,49000,0
|
||||
15711218,Male,35,88000,0
|
||||
15798659,Female,30,62000,0
|
||||
15663939,Female,31,118000,1
|
||||
15694946,Male,24,55000,0
|
||||
15631912,Female,28,85000,0
|
||||
15768816,Male,26,81000,0
|
||||
15682268,Male,35,50000,0
|
||||
15684801,Male,22,81000,0
|
||||
15636428,Female,30,116000,0
|
||||
15809823,Male,26,15000,0
|
||||
15699284,Female,29,28000,0
|
||||
15786993,Female,29,83000,0
|
||||
15709441,Female,35,44000,0
|
||||
15710257,Female,35,25000,0
|
||||
15582492,Male,28,123000,1
|
||||
15575694,Male,35,73000,0
|
||||
15756820,Female,28,37000,0
|
||||
15766289,Male,27,88000,0
|
||||
15593014,Male,28,59000,0
|
||||
15584545,Female,32,86000,0
|
||||
15675949,Female,33,149000,1
|
||||
15672091,Female,19,21000,0
|
||||
15801658,Male,21,72000,0
|
||||
15706185,Female,26,35000,0
|
||||
15789863,Male,27,89000,0
|
||||
15720943,Male,26,86000,0
|
||||
15697997,Female,38,80000,0
|
||||
15665416,Female,39,71000,0
|
||||
15660200,Female,37,71000,0
|
||||
15619653,Male,38,61000,0
|
||||
15773447,Male,37,55000,0
|
||||
15739160,Male,42,80000,0
|
||||
15689237,Male,40,57000,0
|
||||
15679297,Male,35,75000,0
|
||||
15591433,Male,36,52000,0
|
||||
15642725,Male,40,59000,0
|
||||
15701962,Male,41,59000,0
|
||||
15811613,Female,36,75000,0
|
||||
15741049,Male,37,72000,0
|
||||
15724423,Female,40,75000,0
|
||||
15574305,Male,35,53000,0
|
||||
15678168,Female,41,51000,0
|
||||
15697020,Female,39,61000,0
|
||||
15610801,Male,42,65000,0
|
||||
15745232,Male,26,32000,0
|
||||
15722758,Male,30,17000,0
|
||||
15792102,Female,26,84000,0
|
||||
15675185,Male,31,58000,0
|
||||
15801247,Male,33,31000,0
|
||||
15725660,Male,30,87000,0
|
||||
15638963,Female,21,68000,0
|
||||
15800061,Female,28,55000,0
|
||||
15578006,Male,23,63000,0
|
||||
15668504,Female,20,82000,0
|
||||
15687491,Male,30,107000,1
|
||||
15610403,Female,28,59000,0
|
||||
15741094,Male,19,25000,0
|
||||
15807909,Male,19,85000,0
|
||||
15666141,Female,18,68000,0
|
||||
15617134,Male,35,59000,0
|
||||
15783029,Male,30,89000,0
|
||||
15622833,Female,34,25000,0
|
||||
15746422,Female,24,89000,0
|
||||
15750839,Female,27,96000,1
|
||||
15749130,Female,41,30000,0
|
||||
15779862,Male,29,61000,0
|
||||
15767871,Male,20,74000,0
|
||||
15679651,Female,26,15000,0
|
||||
15576219,Male,41,45000,0
|
||||
15699247,Male,31,76000,0
|
||||
15619087,Female,36,50000,0
|
||||
15605327,Male,40,47000,0
|
||||
15610140,Female,31,15000,0
|
||||
15791174,Male,46,59000,0
|
||||
15602373,Male,29,75000,0
|
||||
15762605,Male,26,30000,0
|
||||
15598840,Female,32,135000,1
|
||||
15744279,Male,32,100000,1
|
||||
15670619,Male,25,90000,0
|
||||
15599533,Female,37,33000,0
|
||||
15757837,Male,35,38000,0
|
||||
15697574,Female,33,69000,0
|
||||
15578738,Female,18,86000,0
|
||||
15762228,Female,22,55000,0
|
||||
15614827,Female,35,71000,0
|
||||
15789815,Male,29,148000,1
|
||||
15579781,Female,29,47000,0
|
||||
15587013,Male,21,88000,0
|
||||
15570932,Male,34,115000,0
|
||||
15794661,Female,26,118000,0
|
||||
15581654,Female,34,43000,0
|
||||
15644296,Female,34,72000,0
|
||||
15614420,Female,23,28000,0
|
||||
15609653,Female,35,47000,0
|
||||
15594577,Male,25,22000,0
|
||||
15584114,Male,24,23000,0
|
||||
15673367,Female,31,34000,0
|
||||
15685576,Male,26,16000,0
|
||||
15774727,Female,31,71000,0
|
||||
15694288,Female,32,117000,1
|
||||
15603319,Male,33,43000,0
|
||||
15759066,Female,33,60000,0
|
||||
15814816,Male,31,66000,0
|
||||
15724402,Female,20,82000,0
|
||||
15571059,Female,33,41000,0
|
||||
15674206,Male,35,72000,0
|
||||
15715160,Male,28,32000,0
|
||||
15730448,Male,24,84000,0
|
||||
15662067,Female,19,26000,0
|
||||
15779581,Male,29,43000,0
|
||||
15662901,Male,19,70000,0
|
||||
15689751,Male,28,89000,0
|
||||
15667742,Male,34,43000,0
|
||||
15738448,Female,30,79000,0
|
||||
15680243,Female,20,36000,0
|
||||
15745083,Male,26,80000,0
|
||||
15708228,Male,35,22000,0
|
||||
15628523,Male,35,39000,0
|
||||
15708196,Male,49,74000,0
|
||||
15735549,Female,39,134000,1
|
||||
15809347,Female,41,71000,0
|
||||
15660866,Female,58,101000,1
|
||||
15766609,Female,47,47000,0
|
||||
15654230,Female,55,130000,1
|
||||
15794566,Female,52,114000,0
|
||||
15800890,Female,40,142000,1
|
||||
15697424,Female,46,22000,0
|
||||
15724536,Female,48,96000,1
|
||||
15735878,Male,52,150000,1
|
||||
15707596,Female,59,42000,0
|
||||
15657163,Male,35,58000.0,0
|
||||
15622478,Male,47.0,43000,0
|
||||
15779529,Female,60,108000,1
|
||||
15636023,Male,49,65000,0
|
||||
15582066,Male,40,78000,0
|
||||
15666675,Female,46,96000,0
|
||||
15732987,Male,59,143000,1
|
||||
15789432,Female,41,80000,0
|
||||
15663161,Male,35,91000,1
|
||||
15694879,Male,37,144000,1
|
||||
15593715,Male,60,102000,1
|
||||
15575002,Female,35,60000,0
|
||||
15622171,Male,37,53000,0
|
||||
15795224,Female,36,126000,1
|
||||
15685346,Male,56,133000,1
|
||||
15691808,Female,40,72000,0
|
||||
15721007,Female,42,80000,1
|
||||
15794253,Female,35,147000,1
|
||||
15694453,Male,39,42000,0
|
||||
15813113,Male,40,107000,1
|
||||
15614187,Male,49,86000,1
|
||||
15619407,Female,38,112000,0
|
||||
15646227,Male,46,79000,1
|
||||
15660541,Male,40,57000,0
|
||||
15753874,Female,37,80000,0
|
||||
15617877,Female,46,82000,0
|
||||
15772073,Female,53,143000,1
|
||||
15701537,Male,42,149000,1
|
||||
15736228,Male,38,59000,0
|
||||
15780572,Female,50,88000,1
|
||||
15769596,Female,56,104000,1
|
||||
15586996,Female,41,72000,0
|
||||
15722061,Female,51,146000,1
|
||||
15638003,Female,35,50000,0
|
||||
15775590,Female,57,122000,1
|
||||
15730688,Male,41,52000,0
|
||||
15753102,Female,35,97000,1
|
||||
15810075,Female,44,39000,0
|
||||
15723373,Male,37,52000,0
|
||||
15795298,Female,48,134000,1
|
||||
15584320,Female,37,146000,1
|
||||
15724161,Female,50,44000,0
|
||||
15750056,Female,52,90000,1
|
||||
15609637,Female,41,72000,0
|
||||
15794493,Male,40,57000,0
|
||||
15569641,Female,58,95000,1
|
||||
15815236,Female,45,131000,1
|
||||
15811177,Female,35,77000,0
|
||||
15680587,Male,36,144000,1
|
||||
15672821,Female,55,125000,1
|
||||
15767681,Female,35,72000,0
|
||||
15600379,Male,48,90000,1
|
||||
15801336,Female,42,108000,1
|
||||
15721592,Male,40,75000,0
|
||||
15581282,Male,37,74000,0
|
||||
15746203,Female,47,144000,1
|
||||
15583137,Male,40,61000,0
|
||||
15680752,Female,43,133000,0
|
||||
15688172,Female,59,76000,1
|
||||
15791373,Male,60,42000,1
|
||||
15589449,Male,39,106000,1
|
||||
15692819,Female,57,26000,1
|
||||
15727467,Male,57,74000,1
|
||||
15734312,Male,38,71000,0
|
||||
15764604,Male,49,88000,1
|
||||
15613014,Female,52,38000,1
|
||||
15759684,Female,50,36000,1
|
||||
15609669,Female,59,88000,1
|
||||
15685536,Male,35,61000,0
|
||||
15750447,Male,37,70000,1
|
||||
15663249,Female,52,21000,1
|
||||
15638646,Male,48,141000,0
|
||||
15734161,Female,37,93000,1
|
||||
15631070,Female,37,62000,0
|
||||
15761950,Female,48,138000,1
|
||||
15649668,Male,41,79000,0
|
||||
15713912,Female,37,78000,1
|
||||
15586757,Male,39,134000,1
|
||||
15596522,Male,49,89000,1
|
||||
15625395,Male,55,39000,1
|
||||
15760570,Male,37,77000,0
|
||||
15566689,Female,35,57000,0
|
||||
15725794,Female,36,63000,0
|
||||
15673539,Male,42,73000,1
|
||||
15705298,Female,43,112000,1
|
||||
15675791,Male,45,79000,0
|
||||
15747043,Male,46,117000,1
|
||||
15736397,Female,58,38000,1
|
||||
15678201,Male,48,74000,1
|
||||
15720745,Female,37,137000,1
|
||||
15637593,Male,37,79000,1
|
||||
15598070,Female,40,60000,0
|
||||
15787550,Male,42,54000,0
|
||||
15603942,Female,51,134000,0
|
||||
15733973,Female,47,113000,1
|
||||
15596761,Male,36,125000,1
|
||||
15652400,Female,38,50000,0
|
||||
15717893,Female,42,70000,0
|
||||
15622585,Male,39,96000,1
|
||||
15733964,Female,38,50000,0
|
||||
15753861,Female,49,141000,1
|
||||
15747097,Female,39,79000,0
|
||||
15594762,Female,39,75000,1
|
||||
15667417,Female,54,104000,1
|
||||
15684861,Male,35,55000,0
|
||||
15742204,Male,45,32000,1
|
||||
15623502,Male,36,60000,0
|
||||
15774872,Female,52,138000,1
|
||||
15611191,Female,53,82000,1
|
||||
15674331,Male,41,52000,0
|
||||
15619465,Female,48,30000,1
|
||||
15575247,Female,48,131000,1
|
||||
15695679,Female,41,60000,0
|
||||
15713463,Male,41,72000,0
|
||||
15785170,Female,42,75000,0
|
||||
15796351,Male,36,118000,1
|
||||
15639576,Female,47,107000,1
|
||||
15693264,Male,38,51000,0
|
||||
15589715,Female,48,119000,1
|
||||
15769902,Male,42,65000,0
|
||||
15587177,Male,40,65000,0
|
||||
15814553,Male,57,60000,1
|
||||
15601550,Female,36,54000,0
|
||||
15664907,Male,58,144000,1
|
||||
15612465,Male,35,79000,0
|
||||
15810800,Female,38,55000,0
|
||||
15665760,Male,39,122000,1
|
||||
15588080,Female,53,104000,1
|
||||
15776844,Male,35,75000,0
|
||||
15717560,Female,38,65000,0
|
||||
15629739,Female,47,51000,1
|
||||
15729908,Male,47,105000,1
|
||||
15716781,Female,41,63000,0
|
||||
15646936,Male,53,72000,1
|
||||
15768151,Female,54,108000,1
|
||||
15579212,Male,39,77000,0
|
||||
15721835,Male,38,61000,0
|
||||
15800515,Female,38,113000,1
|
||||
15591279,Male,37,75000,0
|
||||
15587419,Female,42,90000,1
|
||||
15750335,Female,37,57000,0
|
||||
15699619,Male,36,99000,1
|
||||
15606472,Male,60,34000,1
|
||||
15778368,Male,54,70000,1
|
||||
15671387,Female,41,72000,0
|
||||
15573926,Male,40,71000,1
|
||||
15709183,Male,42,54000,0
|
||||
15577514,Male,43,129000,1
|
||||
15778830,Female,53,34000,1
|
||||
15768072,Female,47,50000,1
|
||||
15768293,Female,42,79000,0
|
||||
15654456,Male,42,104000,1
|
||||
15807525,Female,59,29000,1
|
||||
15574372,Female,58,47000,1
|
||||
15671249,Male,46,88000,1
|
||||
15779744,Male,38,71000,0
|
||||
15624755,Female,54,26000,1
|
||||
15611430,Female,60,46000,1
|
||||
15774744,Male,60,83000,1
|
||||
15629885,Female,39,73000,0
|
||||
15708791,Male,59,130000,1
|
||||
15793890,Female,37,80000,0
|
||||
15646091,Female,46,32000,1
|
||||
15596984,Female,46,74000,0
|
||||
15800215,Female,42,53000,0
|
||||
15577806,Male,41,87000,1
|
||||
15749381,Female,58,23000,1
|
||||
15683758,Male,42,64000,0
|
||||
15670615,Male,48,33000,1
|
||||
15715622,Female,44,139000,1
|
||||
15707634,Male,49,28000,1
|
||||
15806901,Female,57,33000,1
|
||||
15775335,Male,56,60000,1
|
||||
15724150,Female,49,39000,1
|
||||
15627220,Male,39,71000,0
|
||||
15672330,Male,47,34000,1
|
||||
15668521,Female,48,35000,1
|
||||
15807837,Male,48,33000,1
|
||||
15592570,Male,47,23000,1
|
||||
15748589,Female,45,45000,1
|
||||
15635893,Male,60,42000,1
|
||||
15757632,Female,39,59000,0
|
||||
15691863,Female,46,41000,1
|
||||
15706071,Male,51,23000,1
|
||||
15654296,Female,50,20000,1
|
||||
15755018,Male,36,33000,0
|
||||
15594041,Female,49,36000,1
|
|
82
section_10_model_selection/main.py
Normal file
82
section_10_model_selection/main.py
Normal file
@ -0,0 +1,82 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Social_Network_Ads.csv")
|
||||
X = dataset.iloc[:, [2,3]].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.svm import SVC
|
||||
|
||||
classifier = SVC(kernel='rbf', random_state=0)
|
||||
|
||||
classifier.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_train, Y_train
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue'))(i), label = j)
|
||||
|
||||
# 交叉驗證
|
||||
from sklearn.model_selection import cross_val_predict
|
||||
|
||||
accuracies = cross_val_predict(classifier, X_set, y_set, cv=10)
|
||||
|
||||
means = accuracies.mean()
|
||||
|
||||
stds = accuracies.std()
|
||||
|
||||
# 網格檢索
|
||||
from sklearn.model_selection import GridSearchCV
|
||||
|
||||
parameters = [{'C': [1, 10, 100, 1000], 'kernel': ['linear']}, {'C': [1, 10, 100, 1000], 'kernel': ['rbf'], 'gamma': [0.5, 0.1, 0.01, 0.001, 0.0001]}] # 超參數的範圍
|
||||
|
||||
grid_search = GridSearchCV(estimator = classifier, param_grid = parameters, scoring = 'accuracy', cv = 10, n_jobs = -1) # n_jobs 是使用的 CPU 核心數,-1 表示使用所有核心
|
||||
|
||||
grid_search = grid_search.fit(X_train, Y_train)
|
||||
|
||||
best_accuracy = grid_search.best_score_
|
||||
|
||||
best_parameters = grid_search.best_params_
|
||||
|
||||
# 視覺化
|
||||
plt.title('Logistic Regression (Training set)')
|
||||
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
11
section_1_data_processing/Data.csv
Normal file
11
section_1_data_processing/Data.csv
Normal file
@ -0,0 +1,11 @@
|
||||
Country,Age,Salary,Purchased
|
||||
France,44,72000,No
|
||||
Spain,27,48000,Yes
|
||||
Germany,30,54000,No
|
||||
Spain,38,61000,No
|
||||
Germany,40,,Yes
|
||||
France,35,58000,Yes
|
||||
Spain,,52000,No
|
||||
France,48,79000,Yes
|
||||
Germany,50,83000,No
|
||||
France,37,67000,Yes
|
|
43
section_1_data_processing/main.py
Normal file
43
section_1_data_processing/main.py
Normal file
@ -0,0 +1,43 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
# 引入資料集
|
||||
dataset = pd.read_csv("Data.csv")
|
||||
X = dataset.iloc[:, :-1].values
|
||||
Y = dataset.iloc[:, 3].values
|
||||
|
||||
# 缺損資料處理
|
||||
from sklearn.impute import SimpleImputer
|
||||
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
|
||||
X[:, 1: 3] = imputer.fit_transform(X[:, 1:3])
|
||||
|
||||
# 分類資料的處理
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
labelencoder_X = LabelEncoder()
|
||||
|
||||
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
|
||||
|
||||
from sklearn.preprocessing import OneHotEncoder
|
||||
from sklearn.compose import ColumnTransformer
|
||||
|
||||
ct = ColumnTransformer(transformers=[
|
||||
('col-0', OneHotEncoder(), [0])
|
||||
], remainder='passthrough')
|
||||
X = np.array(ct.fit_transform(X), dtype=float)
|
||||
|
||||
labelencoder_Y = LabelEncoder()
|
||||
Y = labelencoder_Y.fit_transform(Y)
|
||||
|
||||
# 將資料集分為訓練集和測試集
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
|
||||
|
||||
# 特徵縮放
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
print(X_train)
|
@ -0,0 +1,31 @@
|
||||
YearsExperience,Salary
|
||||
1.1,39343.00
|
||||
1.3,46205.00
|
||||
1.5,37731.00
|
||||
2.0,43525.00
|
||||
2.2,39891.00
|
||||
2.9,56642.00
|
||||
3.0,60150.00
|
||||
3.2,54445.00
|
||||
3.2,64445.00
|
||||
3.7,57189.00
|
||||
3.9,63218.00
|
||||
4.0,55794.00
|
||||
4.0,56957.00
|
||||
4.1,57081.00
|
||||
4.5,61111.00
|
||||
4.9,67938.00
|
||||
5.1,66029.00
|
||||
5.3,83088.00
|
||||
5.9,81363.00
|
||||
6.0,93940.00
|
||||
6.8,91738.00
|
||||
7.1,98273.00
|
||||
7.9,101302.00
|
||||
8.2,113812.00
|
||||
8.7,109431.00
|
||||
9.0,105582.00
|
||||
9.5,116969.00
|
||||
9.6,112635.00
|
||||
10.3,122391.00
|
||||
10.5,121872.00
|
|
@ -0,0 +1,31 @@
|
||||
# 資料預處理
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
dataset = pd.read_csv("Salary_Data.csv")
|
||||
|
||||
X = dataset.iloc[:, :-1].values
|
||||
Y = dataset.iloc[:, 1].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
|
||||
|
||||
# 開始擬合
|
||||
from sklearn.linear_model import LinearRegression
|
||||
regressor = LinearRegression()
|
||||
|
||||
regressor.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = regressor.predict(X_test)
|
||||
|
||||
plt.scatter(X_train, Y_train, color="red")
|
||||
|
||||
plt.scatter(X_test, Y_test, color="green")
|
||||
|
||||
plt.plot(X_train, regressor.predict(X_train), color="blue")
|
||||
|
||||
plt.title("Salary vs Experience (Training set)")
|
||||
plt.xlabel("Years of Experience")
|
||||
plt.ylabel("Salary")
|
||||
plt.show()
|
@ -0,0 +1,51 @@
|
||||
R&D Spend,Administration,Marketing Spend,State,Profit
|
||||
165349.2,136897.8,471784.1,New York,192261.83
|
||||
162597.7,151377.59,443898.53,California,191792.06
|
||||
153441.51,101145.55,407934.54,Florida,191050.39
|
||||
144372.41,118671.85,383199.62,New York,182901.99
|
||||
142107.34,91391.77,366168.42,Florida,166187.94
|
||||
131876.9,99814.71,362861.36,New York,156991.12
|
||||
134615.46,147198.87,127716.82,California,156122.51
|
||||
130298.13,145530.06,323876.68,Florida,155752.6
|
||||
120542.52,148718.95,311613.29,New York,152211.77
|
||||
123334.88,108679.17,304981.62,California,149759.96
|
||||
101913.08,110594.11,229160.95,Florida,146121.95
|
||||
100671.96,91790.61,249744.55,California,144259.4
|
||||
93863.75,127320.38,249839.44,Florida,141585.52
|
||||
91992.39,135495.07,252664.93,California,134307.35
|
||||
119943.24,156547.42,256512.92,Florida,132602.65
|
||||
114523.61,122616.84,261776.23,New York,129917.04
|
||||
78013.11,121597.55,264346.06,California,126992.93
|
||||
94657.16,145077.58,282574.31,New York,125370.37
|
||||
91749.16,114175.79,294919.57,Florida,124266.9
|
||||
86419.7,153514.11,0,New York,122776.86
|
||||
76253.86,113867.3,298664.47,California,118474.03
|
||||
78389.47,153773.43,299737.29,New York,111313.02
|
||||
73994.56,122782.75,303319.26,Florida,110352.25
|
||||
67532.53,105751.03,304768.73,Florida,108733.99
|
||||
77044.01,99281.34,140574.81,New York,108552.04
|
||||
64664.71,139553.16,137962.62,California,107404.34
|
||||
75328.87,144135.98,134050.07,Florida,105733.54
|
||||
72107.6,127864.55,353183.81,New York,105008.31
|
||||
66051.52,182645.56,118148.2,Florida,103282.38
|
||||
65605.48,153032.06,107138.38,New York,101004.64
|
||||
61994.48,115641.28,91131.24,Florida,99937.59
|
||||
61136.38,152701.92,88218.23,New York,97483.56
|
||||
63408.86,129219.61,46085.25,California,97427.84
|
||||
55493.95,103057.49,214634.81,Florida,96778.92
|
||||
46426.07,157693.92,210797.67,California,96712.8
|
||||
46014.02,85047.44,205517.64,New York,96479.51
|
||||
28663.76,127056.21,201126.82,Florida,90708.19
|
||||
44069.95,51283.14,197029.42,California,89949.14
|
||||
20229.59,65947.93,185265.1,New York,81229.06
|
||||
38558.51,82982.09,174999.3,California,81005.76
|
||||
28754.33,118546.05,172795.67,California,78239.91
|
||||
27892.92,84710.77,164470.71,Florida,77798.83
|
||||
23640.93,96189.63,148001.11,California,71498.49
|
||||
15505.73,127382.3,35534.17,New York,69758.98
|
||||
22177.74,154806.14,28334.72,California,65200.33
|
||||
1000.23,124153.04,1903.93,New York,64926.08
|
||||
1315.46,115816.21,297114.46,Florida,49490.75
|
||||
0,135426.92,0,California,42559.73
|
||||
542.05,51743.15,0,New York,35673.41
|
||||
0,116983.8,45173.06,California,14681.4
|
|
@ -0,0 +1,42 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import LinearRegression
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.preprocessing import OneHotEncoder
|
||||
from sklearn.compose import ColumnTransformer
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("50_Startups.csv")
|
||||
X = dataset.iloc[:, :-1].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
labelencoder_X = LabelEncoder()
|
||||
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
|
||||
|
||||
ct = ColumnTransformer(transformers=[
|
||||
('col-0', OneHotEncoder(), [3])
|
||||
], remainder='passthrough')
|
||||
X = np.array(ct.fit_transform(X), dtype=float)
|
||||
|
||||
X = X[:, 1:]
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
|
||||
|
||||
# 創Create the regressor
|
||||
# regressor = LinearRegression()
|
||||
# regressor.fit(X_train, Y_train)
|
||||
# Y_pred = regressor.predict(X_test)
|
||||
|
||||
# 即反向淘汰Using the backward elimination to update the model
|
||||
import statsmodels.api as sm
|
||||
|
||||
X_train = np.append(arr=np.ones((40, 1)).astype(int), values=X_train, axis=1)
|
||||
|
||||
## Get the variables from x_train
|
||||
X_opt = X_train[:, [0, 3, 5]]
|
||||
|
||||
regressor_ols = sm.OLS(endog=Y_train, exog=X_opt).fit()
|
||||
|
||||
print(regressor_ols.summary())
|
@ -0,0 +1,11 @@
|
||||
Position,Level,Salary
|
||||
Business Analyst,1,45000
|
||||
Junior Consultant,2,50000
|
||||
Senior Consultant,3,60000
|
||||
Manager,4,80000
|
||||
Country Manager,5,110000
|
||||
Region Manager,6,150000
|
||||
Partner,7,200000
|
||||
Senior Partner,8,300000
|
||||
C-level,9,500000
|
||||
CEO,10,1000000
|
|
@ -0,0 +1,38 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import LinearRegression
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
dataset = pd.read_csv("Position_Salaries.csv")
|
||||
X = dataset.iloc[:, 1:2].values # 1:2 取出的是 matrix
|
||||
Y = dataset.iloc[:, 2].values
|
||||
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
|
||||
poly = PolynomialFeatures(degree=4)
|
||||
|
||||
X_poly = poly.fit_transform(X)
|
||||
|
||||
lin_reg = LinearRegression()
|
||||
|
||||
lin_reg.fit(X_poly, Y)
|
||||
|
||||
# 避免線段
|
||||
X_grid = np.arange(min(X), max(X), 0.1)
|
||||
X_grid = X_grid.reshape((len(X_grid), 1))
|
||||
|
||||
plt.scatter(X, Y, color='red')
|
||||
|
||||
plt.plot(X_grid, lin_reg.predict(poly.fit_transform(X_grid)), color='blue')
|
||||
|
||||
plt.title('Truth or Bluf')
|
||||
|
||||
plt.xlabel('Position level')
|
||||
|
||||
plt.ylabel('Salary')
|
||||
|
||||
plt.show()
|
||||
|
||||
X_pred = np.array(6.5).reshape(1, 1)
|
||||
print(lin_reg.predict(poly.fit_transform(X_pred)))
|
@ -0,0 +1,401 @@
|
||||
User ID,Gender,Age,EstimatedSalary,Purchased
|
||||
15624510,Male,19,19000,0
|
||||
15810944,Male,35,20000,0
|
||||
15668575,Female,26,43000,0
|
||||
15603246,Female,27,57000,0
|
||||
15804002,Male,19,76000,0
|
||||
15728773,Male,27,58000,0
|
||||
15598044,Female,27,84000,0
|
||||
15694829,Female,32,150000,1
|
||||
15600575,Male,25,33000,0
|
||||
15727311,Female,35,65000,0
|
||||
15570769,Female,26,80000,0
|
||||
15606274,Female,26,52000,0
|
||||
15746139,Male,20,86000,0
|
||||
15704987,Male,32,18000,0
|
||||
15628972,Male,18,82000,0
|
||||
15697686,Male,29,80000,0
|
||||
15733883,Male,47,25000,1
|
||||
15617482,Male,45,26000,1
|
||||
15704583,Male,46,28000,1
|
||||
15621083,Female,48,29000,1
|
||||
15649487,Male,45,22000,1
|
||||
15736760,Female,47,49000,1
|
||||
15714658,Male,48,41000,1
|
||||
15599081,Female,45,22000,1
|
||||
15705113,Male,46,23000,1
|
||||
15631159,Male,47,20000,1
|
||||
15792818,Male,49,28000,1
|
||||
15633531,Female,47,30000,1
|
||||
15744529,Male,29,43000,0
|
||||
15669656,Male,31,18000,0
|
||||
15581198,Male,31,74000,0
|
||||
15729054,Female,27,137000,1
|
||||
15573452,Female,21,16000,0
|
||||
15776733,Female,28,44000,0
|
||||
15724858,Male,27,90000,0
|
||||
15713144,Male,35,27000,0
|
||||
15690188,Female,33,28000,0
|
||||
15689425,Male,30,49000,0
|
||||
15671766,Female,26,72000,0
|
||||
15782806,Female,27,31000,0
|
||||
15764419,Female,27,17000,0
|
||||
15591915,Female,33,51000,0
|
||||
15772798,Male,35,108000,0
|
||||
15792008,Male,30,15000,0
|
||||
15715541,Female,28,84000,0
|
||||
15639277,Male,23,20000,0
|
||||
15798850,Male,25,79000,0
|
||||
15776348,Female,27,54000,0
|
||||
15727696,Male,30,135000,1
|
||||
15793813,Female,31,89000,0
|
||||
15694395,Female,24,32000,0
|
||||
15764195,Female,18,44000,0
|
||||
15744919,Female,29,83000,0
|
||||
15671655,Female,35,23000,0
|
||||
15654901,Female,27,58000,0
|
||||
15649136,Female,24,55000,0
|
||||
15775562,Female,23,48000,0
|
||||
15807481,Male,28,79000,0
|
||||
15642885,Male,22,18000,0
|
||||
15789109,Female,32,117000,0
|
||||
15814004,Male,27,20000,0
|
||||
15673619,Male,25,87000,0
|
||||
15595135,Female,23,66000,0
|
||||
15583681,Male,32,120000,1
|
||||
15605000,Female,59,83000,0
|
||||
15718071,Male,24,58000,0
|
||||
15679760,Male,24,19000,0
|
||||
15654574,Female,23,82000,0
|
||||
15577178,Female,22,63000,0
|
||||
15595324,Female,31,68000,0
|
||||
15756932,Male,25,80000,0
|
||||
15726358,Female,24,27000,0
|
||||
15595228,Female,20,23000,0
|
||||
15782530,Female,33,113000,0
|
||||
15592877,Male,32,18000,0
|
||||
15651983,Male,34,112000,1
|
||||
15746737,Male,18,52000,0
|
||||
15774179,Female,22,27000,0
|
||||
15667265,Female,28,87000,0
|
||||
15655123,Female,26,17000,0
|
||||
15595917,Male,30,80000,0
|
||||
15668385,Male,39,42000,0
|
||||
15709476,Male,20,49000,0
|
||||
15711218,Male,35,88000,0
|
||||
15798659,Female,30,62000,0
|
||||
15663939,Female,31,118000,1
|
||||
15694946,Male,24,55000,0
|
||||
15631912,Female,28,85000,0
|
||||
15768816,Male,26,81000,0
|
||||
15682268,Male,35,50000,0
|
||||
15684801,Male,22,81000,0
|
||||
15636428,Female,30,116000,0
|
||||
15809823,Male,26,15000,0
|
||||
15699284,Female,29,28000,0
|
||||
15786993,Female,29,83000,0
|
||||
15709441,Female,35,44000,0
|
||||
15710257,Female,35,25000,0
|
||||
15582492,Male,28,123000,1
|
||||
15575694,Male,35,73000,0
|
||||
15756820,Female,28,37000,0
|
||||
15766289,Male,27,88000,0
|
||||
15593014,Male,28,59000,0
|
||||
15584545,Female,32,86000,0
|
||||
15675949,Female,33,149000,1
|
||||
15672091,Female,19,21000,0
|
||||
15801658,Male,21,72000,0
|
||||
15706185,Female,26,35000,0
|
||||
15789863,Male,27,89000,0
|
||||
15720943,Male,26,86000,0
|
||||
15697997,Female,38,80000,0
|
||||
15665416,Female,39,71000,0
|
||||
15660200,Female,37,71000,0
|
||||
15619653,Male,38,61000,0
|
||||
15773447,Male,37,55000,0
|
||||
15739160,Male,42,80000,0
|
||||
15689237,Male,40,57000,0
|
||||
15679297,Male,35,75000,0
|
||||
15591433,Male,36,52000,0
|
||||
15642725,Male,40,59000,0
|
||||
15701962,Male,41,59000,0
|
||||
15811613,Female,36,75000,0
|
||||
15741049,Male,37,72000,0
|
||||
15724423,Female,40,75000,0
|
||||
15574305,Male,35,53000,0
|
||||
15678168,Female,41,51000,0
|
||||
15697020,Female,39,61000,0
|
||||
15610801,Male,42,65000,0
|
||||
15745232,Male,26,32000,0
|
||||
15722758,Male,30,17000,0
|
||||
15792102,Female,26,84000,0
|
||||
15675185,Male,31,58000,0
|
||||
15801247,Male,33,31000,0
|
||||
15725660,Male,30,87000,0
|
||||
15638963,Female,21,68000,0
|
||||
15800061,Female,28,55000,0
|
||||
15578006,Male,23,63000,0
|
||||
15668504,Female,20,82000,0
|
||||
15687491,Male,30,107000,1
|
||||
15610403,Female,28,59000,0
|
||||
15741094,Male,19,25000,0
|
||||
15807909,Male,19,85000,0
|
||||
15666141,Female,18,68000,0
|
||||
15617134,Male,35,59000,0
|
||||
15783029,Male,30,89000,0
|
||||
15622833,Female,34,25000,0
|
||||
15746422,Female,24,89000,0
|
||||
15750839,Female,27,96000,1
|
||||
15749130,Female,41,30000,0
|
||||
15779862,Male,29,61000,0
|
||||
15767871,Male,20,74000,0
|
||||
15679651,Female,26,15000,0
|
||||
15576219,Male,41,45000,0
|
||||
15699247,Male,31,76000,0
|
||||
15619087,Female,36,50000,0
|
||||
15605327,Male,40,47000,0
|
||||
15610140,Female,31,15000,0
|
||||
15791174,Male,46,59000,0
|
||||
15602373,Male,29,75000,0
|
||||
15762605,Male,26,30000,0
|
||||
15598840,Female,32,135000,1
|
||||
15744279,Male,32,100000,1
|
||||
15670619,Male,25,90000,0
|
||||
15599533,Female,37,33000,0
|
||||
15757837,Male,35,38000,0
|
||||
15697574,Female,33,69000,0
|
||||
15578738,Female,18,86000,0
|
||||
15762228,Female,22,55000,0
|
||||
15614827,Female,35,71000,0
|
||||
15789815,Male,29,148000,1
|
||||
15579781,Female,29,47000,0
|
||||
15587013,Male,21,88000,0
|
||||
15570932,Male,34,115000,0
|
||||
15794661,Female,26,118000,0
|
||||
15581654,Female,34,43000,0
|
||||
15644296,Female,34,72000,0
|
||||
15614420,Female,23,28000,0
|
||||
15609653,Female,35,47000,0
|
||||
15594577,Male,25,22000,0
|
||||
15584114,Male,24,23000,0
|
||||
15673367,Female,31,34000,0
|
||||
15685576,Male,26,16000,0
|
||||
15774727,Female,31,71000,0
|
||||
15694288,Female,32,117000,1
|
||||
15603319,Male,33,43000,0
|
||||
15759066,Female,33,60000,0
|
||||
15814816,Male,31,66000,0
|
||||
15724402,Female,20,82000,0
|
||||
15571059,Female,33,41000,0
|
||||
15674206,Male,35,72000,0
|
||||
15715160,Male,28,32000,0
|
||||
15730448,Male,24,84000,0
|
||||
15662067,Female,19,26000,0
|
||||
15779581,Male,29,43000,0
|
||||
15662901,Male,19,70000,0
|
||||
15689751,Male,28,89000,0
|
||||
15667742,Male,34,43000,0
|
||||
15738448,Female,30,79000,0
|
||||
15680243,Female,20,36000,0
|
||||
15745083,Male,26,80000,0
|
||||
15708228,Male,35,22000,0
|
||||
15628523,Male,35,39000,0
|
||||
15708196,Male,49,74000,0
|
||||
15735549,Female,39,134000,1
|
||||
15809347,Female,41,71000,0
|
||||
15660866,Female,58,101000,1
|
||||
15766609,Female,47,47000,0
|
||||
15654230,Female,55,130000,1
|
||||
15794566,Female,52,114000,0
|
||||
15800890,Female,40,142000,1
|
||||
15697424,Female,46,22000,0
|
||||
15724536,Female,48,96000,1
|
||||
15735878,Male,52,150000,1
|
||||
15707596,Female,59,42000,0
|
||||
15657163,Male,35,58000.0,0
|
||||
15622478,Male,47.0,43000,0
|
||||
15779529,Female,60,108000,1
|
||||
15636023,Male,49,65000,0
|
||||
15582066,Male,40,78000,0
|
||||
15666675,Female,46,96000,0
|
||||
15732987,Male,59,143000,1
|
||||
15789432,Female,41,80000,0
|
||||
15663161,Male,35,91000,1
|
||||
15694879,Male,37,144000,1
|
||||
15593715,Male,60,102000,1
|
||||
15575002,Female,35,60000,0
|
||||
15622171,Male,37,53000,0
|
||||
15795224,Female,36,126000,1
|
||||
15685346,Male,56,133000,1
|
||||
15691808,Female,40,72000,0
|
||||
15721007,Female,42,80000,1
|
||||
15794253,Female,35,147000,1
|
||||
15694453,Male,39,42000,0
|
||||
15813113,Male,40,107000,1
|
||||
15614187,Male,49,86000,1
|
||||
15619407,Female,38,112000,0
|
||||
15646227,Male,46,79000,1
|
||||
15660541,Male,40,57000,0
|
||||
15753874,Female,37,80000,0
|
||||
15617877,Female,46,82000,0
|
||||
15772073,Female,53,143000,1
|
||||
15701537,Male,42,149000,1
|
||||
15736228,Male,38,59000,0
|
||||
15780572,Female,50,88000,1
|
||||
15769596,Female,56,104000,1
|
||||
15586996,Female,41,72000,0
|
||||
15722061,Female,51,146000,1
|
||||
15638003,Female,35,50000,0
|
||||
15775590,Female,57,122000,1
|
||||
15730688,Male,41,52000,0
|
||||
15753102,Female,35,97000,1
|
||||
15810075,Female,44,39000,0
|
||||
15723373,Male,37,52000,0
|
||||
15795298,Female,48,134000,1
|
||||
15584320,Female,37,146000,1
|
||||
15724161,Female,50,44000,0
|
||||
15750056,Female,52,90000,1
|
||||
15609637,Female,41,72000,0
|
||||
15794493,Male,40,57000,0
|
||||
15569641,Female,58,95000,1
|
||||
15815236,Female,45,131000,1
|
||||
15811177,Female,35,77000,0
|
||||
15680587,Male,36,144000,1
|
||||
15672821,Female,55,125000,1
|
||||
15767681,Female,35,72000,0
|
||||
15600379,Male,48,90000,1
|
||||
15801336,Female,42,108000,1
|
||||
15721592,Male,40,75000,0
|
||||
15581282,Male,37,74000,0
|
||||
15746203,Female,47,144000,1
|
||||
15583137,Male,40,61000,0
|
||||
15680752,Female,43,133000,0
|
||||
15688172,Female,59,76000,1
|
||||
15791373,Male,60,42000,1
|
||||
15589449,Male,39,106000,1
|
||||
15692819,Female,57,26000,1
|
||||
15727467,Male,57,74000,1
|
||||
15734312,Male,38,71000,0
|
||||
15764604,Male,49,88000,1
|
||||
15613014,Female,52,38000,1
|
||||
15759684,Female,50,36000,1
|
||||
15609669,Female,59,88000,1
|
||||
15685536,Male,35,61000,0
|
||||
15750447,Male,37,70000,1
|
||||
15663249,Female,52,21000,1
|
||||
15638646,Male,48,141000,0
|
||||
15734161,Female,37,93000,1
|
||||
15631070,Female,37,62000,0
|
||||
15761950,Female,48,138000,1
|
||||
15649668,Male,41,79000,0
|
||||
15713912,Female,37,78000,1
|
||||
15586757,Male,39,134000,1
|
||||
15596522,Male,49,89000,1
|
||||
15625395,Male,55,39000,1
|
||||
15760570,Male,37,77000,0
|
||||
15566689,Female,35,57000,0
|
||||
15725794,Female,36,63000,0
|
||||
15673539,Male,42,73000,1
|
||||
15705298,Female,43,112000,1
|
||||
15675791,Male,45,79000,0
|
||||
15747043,Male,46,117000,1
|
||||
15736397,Female,58,38000,1
|
||||
15678201,Male,48,74000,1
|
||||
15720745,Female,37,137000,1
|
||||
15637593,Male,37,79000,1
|
||||
15598070,Female,40,60000,0
|
||||
15787550,Male,42,54000,0
|
||||
15603942,Female,51,134000,0
|
||||
15733973,Female,47,113000,1
|
||||
15596761,Male,36,125000,1
|
||||
15652400,Female,38,50000,0
|
||||
15717893,Female,42,70000,0
|
||||
15622585,Male,39,96000,1
|
||||
15733964,Female,38,50000,0
|
||||
15753861,Female,49,141000,1
|
||||
15747097,Female,39,79000,0
|
||||
15594762,Female,39,75000,1
|
||||
15667417,Female,54,104000,1
|
||||
15684861,Male,35,55000,0
|
||||
15742204,Male,45,32000,1
|
||||
15623502,Male,36,60000,0
|
||||
15774872,Female,52,138000,1
|
||||
15611191,Female,53,82000,1
|
||||
15674331,Male,41,52000,0
|
||||
15619465,Female,48,30000,1
|
||||
15575247,Female,48,131000,1
|
||||
15695679,Female,41,60000,0
|
||||
15713463,Male,41,72000,0
|
||||
15785170,Female,42,75000,0
|
||||
15796351,Male,36,118000,1
|
||||
15639576,Female,47,107000,1
|
||||
15693264,Male,38,51000,0
|
||||
15589715,Female,48,119000,1
|
||||
15769902,Male,42,65000,0
|
||||
15587177,Male,40,65000,0
|
||||
15814553,Male,57,60000,1
|
||||
15601550,Female,36,54000,0
|
||||
15664907,Male,58,144000,1
|
||||
15612465,Male,35,79000,0
|
||||
15810800,Female,38,55000,0
|
||||
15665760,Male,39,122000,1
|
||||
15588080,Female,53,104000,1
|
||||
15776844,Male,35,75000,0
|
||||
15717560,Female,38,65000,0
|
||||
15629739,Female,47,51000,1
|
||||
15729908,Male,47,105000,1
|
||||
15716781,Female,41,63000,0
|
||||
15646936,Male,53,72000,1
|
||||
15768151,Female,54,108000,1
|
||||
15579212,Male,39,77000,0
|
||||
15721835,Male,38,61000,0
|
||||
15800515,Female,38,113000,1
|
||||
15591279,Male,37,75000,0
|
||||
15587419,Female,42,90000,1
|
||||
15750335,Female,37,57000,0
|
||||
15699619,Male,36,99000,1
|
||||
15606472,Male,60,34000,1
|
||||
15778368,Male,54,70000,1
|
||||
15671387,Female,41,72000,0
|
||||
15573926,Male,40,71000,1
|
||||
15709183,Male,42,54000,0
|
||||
15577514,Male,43,129000,1
|
||||
15778830,Female,53,34000,1
|
||||
15768072,Female,47,50000,1
|
||||
15768293,Female,42,79000,0
|
||||
15654456,Male,42,104000,1
|
||||
15807525,Female,59,29000,1
|
||||
15574372,Female,58,47000,1
|
||||
15671249,Male,46,88000,1
|
||||
15779744,Male,38,71000,0
|
||||
15624755,Female,54,26000,1
|
||||
15611430,Female,60,46000,1
|
||||
15774744,Male,60,83000,1
|
||||
15629885,Female,39,73000,0
|
||||
15708791,Male,59,130000,1
|
||||
15793890,Female,37,80000,0
|
||||
15646091,Female,46,32000,1
|
||||
15596984,Female,46,74000,0
|
||||
15800215,Female,42,53000,0
|
||||
15577806,Male,41,87000,1
|
||||
15749381,Female,58,23000,1
|
||||
15683758,Male,42,64000,0
|
||||
15670615,Male,48,33000,1
|
||||
15715622,Female,44,139000,1
|
||||
15707634,Male,49,28000,1
|
||||
15806901,Female,57,33000,1
|
||||
15775335,Male,56,60000,1
|
||||
15724150,Female,49,39000,1
|
||||
15627220,Male,39,71000,0
|
||||
15672330,Male,47,34000,1
|
||||
15668521,Female,48,35000,1
|
||||
15807837,Male,48,33000,1
|
||||
15592570,Male,47,23000,1
|
||||
15748589,Female,45,45000,1
|
||||
15635893,Male,60,42000,1
|
||||
15757632,Female,39,59000,0
|
||||
15691863,Female,46,41000,1
|
||||
15706071,Male,51,23000,1
|
||||
15654296,Female,50,20000,1
|
||||
15755018,Male,36,33000,0
|
||||
15594041,Female,49,36000,1
|
|
@ -0,0 +1,62 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Social_Network_Ads.csv")
|
||||
X = dataset.iloc[:, [2,3]].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
|
||||
classifier = LogisticRegression(random_state=0)
|
||||
|
||||
classifier.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
print(Y_test)
|
||||
print(Y_pred)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_test, Y_test
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue'))(i), label = j)
|
||||
|
||||
plt.title('Logistic Regression (Training set)')
|
||||
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
401
section_3_classification/section_3_2_svm/Social_Network_Ads.csv
Normal file
401
section_3_classification/section_3_2_svm/Social_Network_Ads.csv
Normal file
@ -0,0 +1,401 @@
|
||||
User ID,Gender,Age,EstimatedSalary,Purchased
|
||||
15624510,Male,19,19000,0
|
||||
15810944,Male,35,20000,0
|
||||
15668575,Female,26,43000,0
|
||||
15603246,Female,27,57000,0
|
||||
15804002,Male,19,76000,0
|
||||
15728773,Male,27,58000,0
|
||||
15598044,Female,27,84000,0
|
||||
15694829,Female,32,150000,1
|
||||
15600575,Male,25,33000,0
|
||||
15727311,Female,35,65000,0
|
||||
15570769,Female,26,80000,0
|
||||
15606274,Female,26,52000,0
|
||||
15746139,Male,20,86000,0
|
||||
15704987,Male,32,18000,0
|
||||
15628972,Male,18,82000,0
|
||||
15697686,Male,29,80000,0
|
||||
15733883,Male,47,25000,1
|
||||
15617482,Male,45,26000,1
|
||||
15704583,Male,46,28000,1
|
||||
15621083,Female,48,29000,1
|
||||
15649487,Male,45,22000,1
|
||||
15736760,Female,47,49000,1
|
||||
15714658,Male,48,41000,1
|
||||
15599081,Female,45,22000,1
|
||||
15705113,Male,46,23000,1
|
||||
15631159,Male,47,20000,1
|
||||
15792818,Male,49,28000,1
|
||||
15633531,Female,47,30000,1
|
||||
15744529,Male,29,43000,0
|
||||
15669656,Male,31,18000,0
|
||||
15581198,Male,31,74000,0
|
||||
15729054,Female,27,137000,1
|
||||
15573452,Female,21,16000,0
|
||||
15776733,Female,28,44000,0
|
||||
15724858,Male,27,90000,0
|
||||
15713144,Male,35,27000,0
|
||||
15690188,Female,33,28000,0
|
||||
15689425,Male,30,49000,0
|
||||
15671766,Female,26,72000,0
|
||||
15782806,Female,27,31000,0
|
||||
15764419,Female,27,17000,0
|
||||
15591915,Female,33,51000,0
|
||||
15772798,Male,35,108000,0
|
||||
15792008,Male,30,15000,0
|
||||
15715541,Female,28,84000,0
|
||||
15639277,Male,23,20000,0
|
||||
15798850,Male,25,79000,0
|
||||
15776348,Female,27,54000,0
|
||||
15727696,Male,30,135000,1
|
||||
15793813,Female,31,89000,0
|
||||
15694395,Female,24,32000,0
|
||||
15764195,Female,18,44000,0
|
||||
15744919,Female,29,83000,0
|
||||
15671655,Female,35,23000,0
|
||||
15654901,Female,27,58000,0
|
||||
15649136,Female,24,55000,0
|
||||
15775562,Female,23,48000,0
|
||||
15807481,Male,28,79000,0
|
||||
15642885,Male,22,18000,0
|
||||
15789109,Female,32,117000,0
|
||||
15814004,Male,27,20000,0
|
||||
15673619,Male,25,87000,0
|
||||
15595135,Female,23,66000,0
|
||||
15583681,Male,32,120000,1
|
||||
15605000,Female,59,83000,0
|
||||
15718071,Male,24,58000,0
|
||||
15679760,Male,24,19000,0
|
||||
15654574,Female,23,82000,0
|
||||
15577178,Female,22,63000,0
|
||||
15595324,Female,31,68000,0
|
||||
15756932,Male,25,80000,0
|
||||
15726358,Female,24,27000,0
|
||||
15595228,Female,20,23000,0
|
||||
15782530,Female,33,113000,0
|
||||
15592877,Male,32,18000,0
|
||||
15651983,Male,34,112000,1
|
||||
15746737,Male,18,52000,0
|
||||
15774179,Female,22,27000,0
|
||||
15667265,Female,28,87000,0
|
||||
15655123,Female,26,17000,0
|
||||
15595917,Male,30,80000,0
|
||||
15668385,Male,39,42000,0
|
||||
15709476,Male,20,49000,0
|
||||
15711218,Male,35,88000,0
|
||||
15798659,Female,30,62000,0
|
||||
15663939,Female,31,118000,1
|
||||
15694946,Male,24,55000,0
|
||||
15631912,Female,28,85000,0
|
||||
15768816,Male,26,81000,0
|
||||
15682268,Male,35,50000,0
|
||||
15684801,Male,22,81000,0
|
||||
15636428,Female,30,116000,0
|
||||
15809823,Male,26,15000,0
|
||||
15699284,Female,29,28000,0
|
||||
15786993,Female,29,83000,0
|
||||
15709441,Female,35,44000,0
|
||||
15710257,Female,35,25000,0
|
||||
15582492,Male,28,123000,1
|
||||
15575694,Male,35,73000,0
|
||||
15756820,Female,28,37000,0
|
||||
15766289,Male,27,88000,0
|
||||
15593014,Male,28,59000,0
|
||||
15584545,Female,32,86000,0
|
||||
15675949,Female,33,149000,1
|
||||
15672091,Female,19,21000,0
|
||||
15801658,Male,21,72000,0
|
||||
15706185,Female,26,35000,0
|
||||
15789863,Male,27,89000,0
|
||||
15720943,Male,26,86000,0
|
||||
15697997,Female,38,80000,0
|
||||
15665416,Female,39,71000,0
|
||||
15660200,Female,37,71000,0
|
||||
15619653,Male,38,61000,0
|
||||
15773447,Male,37,55000,0
|
||||
15739160,Male,42,80000,0
|
||||
15689237,Male,40,57000,0
|
||||
15679297,Male,35,75000,0
|
||||
15591433,Male,36,52000,0
|
||||
15642725,Male,40,59000,0
|
||||
15701962,Male,41,59000,0
|
||||
15811613,Female,36,75000,0
|
||||
15741049,Male,37,72000,0
|
||||
15724423,Female,40,75000,0
|
||||
15574305,Male,35,53000,0
|
||||
15678168,Female,41,51000,0
|
||||
15697020,Female,39,61000,0
|
||||
15610801,Male,42,65000,0
|
||||
15745232,Male,26,32000,0
|
||||
15722758,Male,30,17000,0
|
||||
15792102,Female,26,84000,0
|
||||
15675185,Male,31,58000,0
|
||||
15801247,Male,33,31000,0
|
||||
15725660,Male,30,87000,0
|
||||
15638963,Female,21,68000,0
|
||||
15800061,Female,28,55000,0
|
||||
15578006,Male,23,63000,0
|
||||
15668504,Female,20,82000,0
|
||||
15687491,Male,30,107000,1
|
||||
15610403,Female,28,59000,0
|
||||
15741094,Male,19,25000,0
|
||||
15807909,Male,19,85000,0
|
||||
15666141,Female,18,68000,0
|
||||
15617134,Male,35,59000,0
|
||||
15783029,Male,30,89000,0
|
||||
15622833,Female,34,25000,0
|
||||
15746422,Female,24,89000,0
|
||||
15750839,Female,27,96000,1
|
||||
15749130,Female,41,30000,0
|
||||
15779862,Male,29,61000,0
|
||||
15767871,Male,20,74000,0
|
||||
15679651,Female,26,15000,0
|
||||
15576219,Male,41,45000,0
|
||||
15699247,Male,31,76000,0
|
||||
15619087,Female,36,50000,0
|
||||
15605327,Male,40,47000,0
|
||||
15610140,Female,31,15000,0
|
||||
15791174,Male,46,59000,0
|
||||
15602373,Male,29,75000,0
|
||||
15762605,Male,26,30000,0
|
||||
15598840,Female,32,135000,1
|
||||
15744279,Male,32,100000,1
|
||||
15670619,Male,25,90000,0
|
||||
15599533,Female,37,33000,0
|
||||
15757837,Male,35,38000,0
|
||||
15697574,Female,33,69000,0
|
||||
15578738,Female,18,86000,0
|
||||
15762228,Female,22,55000,0
|
||||
15614827,Female,35,71000,0
|
||||
15789815,Male,29,148000,1
|
||||
15579781,Female,29,47000,0
|
||||
15587013,Male,21,88000,0
|
||||
15570932,Male,34,115000,0
|
||||
15794661,Female,26,118000,0
|
||||
15581654,Female,34,43000,0
|
||||
15644296,Female,34,72000,0
|
||||
15614420,Female,23,28000,0
|
||||
15609653,Female,35,47000,0
|
||||
15594577,Male,25,22000,0
|
||||
15584114,Male,24,23000,0
|
||||
15673367,Female,31,34000,0
|
||||
15685576,Male,26,16000,0
|
||||
15774727,Female,31,71000,0
|
||||
15694288,Female,32,117000,1
|
||||
15603319,Male,33,43000,0
|
||||
15759066,Female,33,60000,0
|
||||
15814816,Male,31,66000,0
|
||||
15724402,Female,20,82000,0
|
||||
15571059,Female,33,41000,0
|
||||
15674206,Male,35,72000,0
|
||||
15715160,Male,28,32000,0
|
||||
15730448,Male,24,84000,0
|
||||
15662067,Female,19,26000,0
|
||||
15779581,Male,29,43000,0
|
||||
15662901,Male,19,70000,0
|
||||
15689751,Male,28,89000,0
|
||||
15667742,Male,34,43000,0
|
||||
15738448,Female,30,79000,0
|
||||
15680243,Female,20,36000,0
|
||||
15745083,Male,26,80000,0
|
||||
15708228,Male,35,22000,0
|
||||
15628523,Male,35,39000,0
|
||||
15708196,Male,49,74000,0
|
||||
15735549,Female,39,134000,1
|
||||
15809347,Female,41,71000,0
|
||||
15660866,Female,58,101000,1
|
||||
15766609,Female,47,47000,0
|
||||
15654230,Female,55,130000,1
|
||||
15794566,Female,52,114000,0
|
||||
15800890,Female,40,142000,1
|
||||
15697424,Female,46,22000,0
|
||||
15724536,Female,48,96000,1
|
||||
15735878,Male,52,150000,1
|
||||
15707596,Female,59,42000,0
|
||||
15657163,Male,35,58000.0,0
|
||||
15622478,Male,47.0,43000,0
|
||||
15779529,Female,60,108000,1
|
||||
15636023,Male,49,65000,0
|
||||
15582066,Male,40,78000,0
|
||||
15666675,Female,46,96000,0
|
||||
15732987,Male,59,143000,1
|
||||
15789432,Female,41,80000,0
|
||||
15663161,Male,35,91000,1
|
||||
15694879,Male,37,144000,1
|
||||
15593715,Male,60,102000,1
|
||||
15575002,Female,35,60000,0
|
||||
15622171,Male,37,53000,0
|
||||
15795224,Female,36,126000,1
|
||||
15685346,Male,56,133000,1
|
||||
15691808,Female,40,72000,0
|
||||
15721007,Female,42,80000,1
|
||||
15794253,Female,35,147000,1
|
||||
15694453,Male,39,42000,0
|
||||
15813113,Male,40,107000,1
|
||||
15614187,Male,49,86000,1
|
||||
15619407,Female,38,112000,0
|
||||
15646227,Male,46,79000,1
|
||||
15660541,Male,40,57000,0
|
||||
15753874,Female,37,80000,0
|
||||
15617877,Female,46,82000,0
|
||||
15772073,Female,53,143000,1
|
||||
15701537,Male,42,149000,1
|
||||
15736228,Male,38,59000,0
|
||||
15780572,Female,50,88000,1
|
||||
15769596,Female,56,104000,1
|
||||
15586996,Female,41,72000,0
|
||||
15722061,Female,51,146000,1
|
||||
15638003,Female,35,50000,0
|
||||
15775590,Female,57,122000,1
|
||||
15730688,Male,41,52000,0
|
||||
15753102,Female,35,97000,1
|
||||
15810075,Female,44,39000,0
|
||||
15723373,Male,37,52000,0
|
||||
15795298,Female,48,134000,1
|
||||
15584320,Female,37,146000,1
|
||||
15724161,Female,50,44000,0
|
||||
15750056,Female,52,90000,1
|
||||
15609637,Female,41,72000,0
|
||||
15794493,Male,40,57000,0
|
||||
15569641,Female,58,95000,1
|
||||
15815236,Female,45,131000,1
|
||||
15811177,Female,35,77000,0
|
||||
15680587,Male,36,144000,1
|
||||
15672821,Female,55,125000,1
|
||||
15767681,Female,35,72000,0
|
||||
15600379,Male,48,90000,1
|
||||
15801336,Female,42,108000,1
|
||||
15721592,Male,40,75000,0
|
||||
15581282,Male,37,74000,0
|
||||
15746203,Female,47,144000,1
|
||||
15583137,Male,40,61000,0
|
||||
15680752,Female,43,133000,0
|
||||
15688172,Female,59,76000,1
|
||||
15791373,Male,60,42000,1
|
||||
15589449,Male,39,106000,1
|
||||
15692819,Female,57,26000,1
|
||||
15727467,Male,57,74000,1
|
||||
15734312,Male,38,71000,0
|
||||
15764604,Male,49,88000,1
|
||||
15613014,Female,52,38000,1
|
||||
15759684,Female,50,36000,1
|
||||
15609669,Female,59,88000,1
|
||||
15685536,Male,35,61000,0
|
||||
15750447,Male,37,70000,1
|
||||
15663249,Female,52,21000,1
|
||||
15638646,Male,48,141000,0
|
||||
15734161,Female,37,93000,1
|
||||
15631070,Female,37,62000,0
|
||||
15761950,Female,48,138000,1
|
||||
15649668,Male,41,79000,0
|
||||
15713912,Female,37,78000,1
|
||||
15586757,Male,39,134000,1
|
||||
15596522,Male,49,89000,1
|
||||
15625395,Male,55,39000,1
|
||||
15760570,Male,37,77000,0
|
||||
15566689,Female,35,57000,0
|
||||
15725794,Female,36,63000,0
|
||||
15673539,Male,42,73000,1
|
||||
15705298,Female,43,112000,1
|
||||
15675791,Male,45,79000,0
|
||||
15747043,Male,46,117000,1
|
||||
15736397,Female,58,38000,1
|
||||
15678201,Male,48,74000,1
|
||||
15720745,Female,37,137000,1
|
||||
15637593,Male,37,79000,1
|
||||
15598070,Female,40,60000,0
|
||||
15787550,Male,42,54000,0
|
||||
15603942,Female,51,134000,0
|
||||
15733973,Female,47,113000,1
|
||||
15596761,Male,36,125000,1
|
||||
15652400,Female,38,50000,0
|
||||
15717893,Female,42,70000,0
|
||||
15622585,Male,39,96000,1
|
||||
15733964,Female,38,50000,0
|
||||
15753861,Female,49,141000,1
|
||||
15747097,Female,39,79000,0
|
||||
15594762,Female,39,75000,1
|
||||
15667417,Female,54,104000,1
|
||||
15684861,Male,35,55000,0
|
||||
15742204,Male,45,32000,1
|
||||
15623502,Male,36,60000,0
|
||||
15774872,Female,52,138000,1
|
||||
15611191,Female,53,82000,1
|
||||
15674331,Male,41,52000,0
|
||||
15619465,Female,48,30000,1
|
||||
15575247,Female,48,131000,1
|
||||
15695679,Female,41,60000,0
|
||||
15713463,Male,41,72000,0
|
||||
15785170,Female,42,75000,0
|
||||
15796351,Male,36,118000,1
|
||||
15639576,Female,47,107000,1
|
||||
15693264,Male,38,51000,0
|
||||
15589715,Female,48,119000,1
|
||||
15769902,Male,42,65000,0
|
||||
15587177,Male,40,65000,0
|
||||
15814553,Male,57,60000,1
|
||||
15601550,Female,36,54000,0
|
||||
15664907,Male,58,144000,1
|
||||
15612465,Male,35,79000,0
|
||||
15810800,Female,38,55000,0
|
||||
15665760,Male,39,122000,1
|
||||
15588080,Female,53,104000,1
|
||||
15776844,Male,35,75000,0
|
||||
15717560,Female,38,65000,0
|
||||
15629739,Female,47,51000,1
|
||||
15729908,Male,47,105000,1
|
||||
15716781,Female,41,63000,0
|
||||
15646936,Male,53,72000,1
|
||||
15768151,Female,54,108000,1
|
||||
15579212,Male,39,77000,0
|
||||
15721835,Male,38,61000,0
|
||||
15800515,Female,38,113000,1
|
||||
15591279,Male,37,75000,0
|
||||
15587419,Female,42,90000,1
|
||||
15750335,Female,37,57000,0
|
||||
15699619,Male,36,99000,1
|
||||
15606472,Male,60,34000,1
|
||||
15778368,Male,54,70000,1
|
||||
15671387,Female,41,72000,0
|
||||
15573926,Male,40,71000,1
|
||||
15709183,Male,42,54000,0
|
||||
15577514,Male,43,129000,1
|
||||
15778830,Female,53,34000,1
|
||||
15768072,Female,47,50000,1
|
||||
15768293,Female,42,79000,0
|
||||
15654456,Male,42,104000,1
|
||||
15807525,Female,59,29000,1
|
||||
15574372,Female,58,47000,1
|
||||
15671249,Male,46,88000,1
|
||||
15779744,Male,38,71000,0
|
||||
15624755,Female,54,26000,1
|
||||
15611430,Female,60,46000,1
|
||||
15774744,Male,60,83000,1
|
||||
15629885,Female,39,73000,0
|
||||
15708791,Male,59,130000,1
|
||||
15793890,Female,37,80000,0
|
||||
15646091,Female,46,32000,1
|
||||
15596984,Female,46,74000,0
|
||||
15800215,Female,42,53000,0
|
||||
15577806,Male,41,87000,1
|
||||
15749381,Female,58,23000,1
|
||||
15683758,Male,42,64000,0
|
||||
15670615,Male,48,33000,1
|
||||
15715622,Female,44,139000,1
|
||||
15707634,Male,49,28000,1
|
||||
15806901,Female,57,33000,1
|
||||
15775335,Male,56,60000,1
|
||||
15724150,Female,49,39000,1
|
||||
15627220,Male,39,71000,0
|
||||
15672330,Male,47,34000,1
|
||||
15668521,Female,48,35000,1
|
||||
15807837,Male,48,33000,1
|
||||
15592570,Male,47,23000,1
|
||||
15748589,Female,45,45000,1
|
||||
15635893,Male,60,42000,1
|
||||
15757632,Female,39,59000,0
|
||||
15691863,Female,46,41000,1
|
||||
15706071,Male,51,23000,1
|
||||
15654296,Female,50,20000,1
|
||||
15755018,Male,36,33000,0
|
||||
15594041,Female,49,36000,1
|
|
59
section_3_classification/section_3_2_svm/main.py
Normal file
59
section_3_classification/section_3_2_svm/main.py
Normal file
@ -0,0 +1,59 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Social_Network_Ads.csv")
|
||||
X = dataset.iloc[:, [2,3]].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.svm import SVC
|
||||
|
||||
classifier = SVC(kernel='linear', random_state=0)
|
||||
|
||||
classifier.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_test, Y_test
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue'))(i), label = j)
|
||||
|
||||
plt.title('Logistic Regression (Training set)')
|
||||
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
@ -0,0 +1,401 @@
|
||||
User ID,Gender,Age,EstimatedSalary,Purchased
|
||||
15624510,Male,19,19000,0
|
||||
15810944,Male,35,20000,0
|
||||
15668575,Female,26,43000,0
|
||||
15603246,Female,27,57000,0
|
||||
15804002,Male,19,76000,0
|
||||
15728773,Male,27,58000,0
|
||||
15598044,Female,27,84000,0
|
||||
15694829,Female,32,150000,1
|
||||
15600575,Male,25,33000,0
|
||||
15727311,Female,35,65000,0
|
||||
15570769,Female,26,80000,0
|
||||
15606274,Female,26,52000,0
|
||||
15746139,Male,20,86000,0
|
||||
15704987,Male,32,18000,0
|
||||
15628972,Male,18,82000,0
|
||||
15697686,Male,29,80000,0
|
||||
15733883,Male,47,25000,1
|
||||
15617482,Male,45,26000,1
|
||||
15704583,Male,46,28000,1
|
||||
15621083,Female,48,29000,1
|
||||
15649487,Male,45,22000,1
|
||||
15736760,Female,47,49000,1
|
||||
15714658,Male,48,41000,1
|
||||
15599081,Female,45,22000,1
|
||||
15705113,Male,46,23000,1
|
||||
15631159,Male,47,20000,1
|
||||
15792818,Male,49,28000,1
|
||||
15633531,Female,47,30000,1
|
||||
15744529,Male,29,43000,0
|
||||
15669656,Male,31,18000,0
|
||||
15581198,Male,31,74000,0
|
||||
15729054,Female,27,137000,1
|
||||
15573452,Female,21,16000,0
|
||||
15776733,Female,28,44000,0
|
||||
15724858,Male,27,90000,0
|
||||
15713144,Male,35,27000,0
|
||||
15690188,Female,33,28000,0
|
||||
15689425,Male,30,49000,0
|
||||
15671766,Female,26,72000,0
|
||||
15782806,Female,27,31000,0
|
||||
15764419,Female,27,17000,0
|
||||
15591915,Female,33,51000,0
|
||||
15772798,Male,35,108000,0
|
||||
15792008,Male,30,15000,0
|
||||
15715541,Female,28,84000,0
|
||||
15639277,Male,23,20000,0
|
||||
15798850,Male,25,79000,0
|
||||
15776348,Female,27,54000,0
|
||||
15727696,Male,30,135000,1
|
||||
15793813,Female,31,89000,0
|
||||
15694395,Female,24,32000,0
|
||||
15764195,Female,18,44000,0
|
||||
15744919,Female,29,83000,0
|
||||
15671655,Female,35,23000,0
|
||||
15654901,Female,27,58000,0
|
||||
15649136,Female,24,55000,0
|
||||
15775562,Female,23,48000,0
|
||||
15807481,Male,28,79000,0
|
||||
15642885,Male,22,18000,0
|
||||
15789109,Female,32,117000,0
|
||||
15814004,Male,27,20000,0
|
||||
15673619,Male,25,87000,0
|
||||
15595135,Female,23,66000,0
|
||||
15583681,Male,32,120000,1
|
||||
15605000,Female,59,83000,0
|
||||
15718071,Male,24,58000,0
|
||||
15679760,Male,24,19000,0
|
||||
15654574,Female,23,82000,0
|
||||
15577178,Female,22,63000,0
|
||||
15595324,Female,31,68000,0
|
||||
15756932,Male,25,80000,0
|
||||
15726358,Female,24,27000,0
|
||||
15595228,Female,20,23000,0
|
||||
15782530,Female,33,113000,0
|
||||
15592877,Male,32,18000,0
|
||||
15651983,Male,34,112000,1
|
||||
15746737,Male,18,52000,0
|
||||
15774179,Female,22,27000,0
|
||||
15667265,Female,28,87000,0
|
||||
15655123,Female,26,17000,0
|
||||
15595917,Male,30,80000,0
|
||||
15668385,Male,39,42000,0
|
||||
15709476,Male,20,49000,0
|
||||
15711218,Male,35,88000,0
|
||||
15798659,Female,30,62000,0
|
||||
15663939,Female,31,118000,1
|
||||
15694946,Male,24,55000,0
|
||||
15631912,Female,28,85000,0
|
||||
15768816,Male,26,81000,0
|
||||
15682268,Male,35,50000,0
|
||||
15684801,Male,22,81000,0
|
||||
15636428,Female,30,116000,0
|
||||
15809823,Male,26,15000,0
|
||||
15699284,Female,29,28000,0
|
||||
15786993,Female,29,83000,0
|
||||
15709441,Female,35,44000,0
|
||||
15710257,Female,35,25000,0
|
||||
15582492,Male,28,123000,1
|
||||
15575694,Male,35,73000,0
|
||||
15756820,Female,28,37000,0
|
||||
15766289,Male,27,88000,0
|
||||
15593014,Male,28,59000,0
|
||||
15584545,Female,32,86000,0
|
||||
15675949,Female,33,149000,1
|
||||
15672091,Female,19,21000,0
|
||||
15801658,Male,21,72000,0
|
||||
15706185,Female,26,35000,0
|
||||
15789863,Male,27,89000,0
|
||||
15720943,Male,26,86000,0
|
||||
15697997,Female,38,80000,0
|
||||
15665416,Female,39,71000,0
|
||||
15660200,Female,37,71000,0
|
||||
15619653,Male,38,61000,0
|
||||
15773447,Male,37,55000,0
|
||||
15739160,Male,42,80000,0
|
||||
15689237,Male,40,57000,0
|
||||
15679297,Male,35,75000,0
|
||||
15591433,Male,36,52000,0
|
||||
15642725,Male,40,59000,0
|
||||
15701962,Male,41,59000,0
|
||||
15811613,Female,36,75000,0
|
||||
15741049,Male,37,72000,0
|
||||
15724423,Female,40,75000,0
|
||||
15574305,Male,35,53000,0
|
||||
15678168,Female,41,51000,0
|
||||
15697020,Female,39,61000,0
|
||||
15610801,Male,42,65000,0
|
||||
15745232,Male,26,32000,0
|
||||
15722758,Male,30,17000,0
|
||||
15792102,Female,26,84000,0
|
||||
15675185,Male,31,58000,0
|
||||
15801247,Male,33,31000,0
|
||||
15725660,Male,30,87000,0
|
||||
15638963,Female,21,68000,0
|
||||
15800061,Female,28,55000,0
|
||||
15578006,Male,23,63000,0
|
||||
15668504,Female,20,82000,0
|
||||
15687491,Male,30,107000,1
|
||||
15610403,Female,28,59000,0
|
||||
15741094,Male,19,25000,0
|
||||
15807909,Male,19,85000,0
|
||||
15666141,Female,18,68000,0
|
||||
15617134,Male,35,59000,0
|
||||
15783029,Male,30,89000,0
|
||||
15622833,Female,34,25000,0
|
||||
15746422,Female,24,89000,0
|
||||
15750839,Female,27,96000,1
|
||||
15749130,Female,41,30000,0
|
||||
15779862,Male,29,61000,0
|
||||
15767871,Male,20,74000,0
|
||||
15679651,Female,26,15000,0
|
||||
15576219,Male,41,45000,0
|
||||
15699247,Male,31,76000,0
|
||||
15619087,Female,36,50000,0
|
||||
15605327,Male,40,47000,0
|
||||
15610140,Female,31,15000,0
|
||||
15791174,Male,46,59000,0
|
||||
15602373,Male,29,75000,0
|
||||
15762605,Male,26,30000,0
|
||||
15598840,Female,32,135000,1
|
||||
15744279,Male,32,100000,1
|
||||
15670619,Male,25,90000,0
|
||||
15599533,Female,37,33000,0
|
||||
15757837,Male,35,38000,0
|
||||
15697574,Female,33,69000,0
|
||||
15578738,Female,18,86000,0
|
||||
15762228,Female,22,55000,0
|
||||
15614827,Female,35,71000,0
|
||||
15789815,Male,29,148000,1
|
||||
15579781,Female,29,47000,0
|
||||
15587013,Male,21,88000,0
|
||||
15570932,Male,34,115000,0
|
||||
15794661,Female,26,118000,0
|
||||
15581654,Female,34,43000,0
|
||||
15644296,Female,34,72000,0
|
||||
15614420,Female,23,28000,0
|
||||
15609653,Female,35,47000,0
|
||||
15594577,Male,25,22000,0
|
||||
15584114,Male,24,23000,0
|
||||
15673367,Female,31,34000,0
|
||||
15685576,Male,26,16000,0
|
||||
15774727,Female,31,71000,0
|
||||
15694288,Female,32,117000,1
|
||||
15603319,Male,33,43000,0
|
||||
15759066,Female,33,60000,0
|
||||
15814816,Male,31,66000,0
|
||||
15724402,Female,20,82000,0
|
||||
15571059,Female,33,41000,0
|
||||
15674206,Male,35,72000,0
|
||||
15715160,Male,28,32000,0
|
||||
15730448,Male,24,84000,0
|
||||
15662067,Female,19,26000,0
|
||||
15779581,Male,29,43000,0
|
||||
15662901,Male,19,70000,0
|
||||
15689751,Male,28,89000,0
|
||||
15667742,Male,34,43000,0
|
||||
15738448,Female,30,79000,0
|
||||
15680243,Female,20,36000,0
|
||||
15745083,Male,26,80000,0
|
||||
15708228,Male,35,22000,0
|
||||
15628523,Male,35,39000,0
|
||||
15708196,Male,49,74000,0
|
||||
15735549,Female,39,134000,1
|
||||
15809347,Female,41,71000,0
|
||||
15660866,Female,58,101000,1
|
||||
15766609,Female,47,47000,0
|
||||
15654230,Female,55,130000,1
|
||||
15794566,Female,52,114000,0
|
||||
15800890,Female,40,142000,1
|
||||
15697424,Female,46,22000,0
|
||||
15724536,Female,48,96000,1
|
||||
15735878,Male,52,150000,1
|
||||
15707596,Female,59,42000,0
|
||||
15657163,Male,35,58000.0,0
|
||||
15622478,Male,47.0,43000,0
|
||||
15779529,Female,60,108000,1
|
||||
15636023,Male,49,65000,0
|
||||
15582066,Male,40,78000,0
|
||||
15666675,Female,46,96000,0
|
||||
15732987,Male,59,143000,1
|
||||
15789432,Female,41,80000,0
|
||||
15663161,Male,35,91000,1
|
||||
15694879,Male,37,144000,1
|
||||
15593715,Male,60,102000,1
|
||||
15575002,Female,35,60000,0
|
||||
15622171,Male,37,53000,0
|
||||
15795224,Female,36,126000,1
|
||||
15685346,Male,56,133000,1
|
||||
15691808,Female,40,72000,0
|
||||
15721007,Female,42,80000,1
|
||||
15794253,Female,35,147000,1
|
||||
15694453,Male,39,42000,0
|
||||
15813113,Male,40,107000,1
|
||||
15614187,Male,49,86000,1
|
||||
15619407,Female,38,112000,0
|
||||
15646227,Male,46,79000,1
|
||||
15660541,Male,40,57000,0
|
||||
15753874,Female,37,80000,0
|
||||
15617877,Female,46,82000,0
|
||||
15772073,Female,53,143000,1
|
||||
15701537,Male,42,149000,1
|
||||
15736228,Male,38,59000,0
|
||||
15780572,Female,50,88000,1
|
||||
15769596,Female,56,104000,1
|
||||
15586996,Female,41,72000,0
|
||||
15722061,Female,51,146000,1
|
||||
15638003,Female,35,50000,0
|
||||
15775590,Female,57,122000,1
|
||||
15730688,Male,41,52000,0
|
||||
15753102,Female,35,97000,1
|
||||
15810075,Female,44,39000,0
|
||||
15723373,Male,37,52000,0
|
||||
15795298,Female,48,134000,1
|
||||
15584320,Female,37,146000,1
|
||||
15724161,Female,50,44000,0
|
||||
15750056,Female,52,90000,1
|
||||
15609637,Female,41,72000,0
|
||||
15794493,Male,40,57000,0
|
||||
15569641,Female,58,95000,1
|
||||
15815236,Female,45,131000,1
|
||||
15811177,Female,35,77000,0
|
||||
15680587,Male,36,144000,1
|
||||
15672821,Female,55,125000,1
|
||||
15767681,Female,35,72000,0
|
||||
15600379,Male,48,90000,1
|
||||
15801336,Female,42,108000,1
|
||||
15721592,Male,40,75000,0
|
||||
15581282,Male,37,74000,0
|
||||
15746203,Female,47,144000,1
|
||||
15583137,Male,40,61000,0
|
||||
15680752,Female,43,133000,0
|
||||
15688172,Female,59,76000,1
|
||||
15791373,Male,60,42000,1
|
||||
15589449,Male,39,106000,1
|
||||
15692819,Female,57,26000,1
|
||||
15727467,Male,57,74000,1
|
||||
15734312,Male,38,71000,0
|
||||
15764604,Male,49,88000,1
|
||||
15613014,Female,52,38000,1
|
||||
15759684,Female,50,36000,1
|
||||
15609669,Female,59,88000,1
|
||||
15685536,Male,35,61000,0
|
||||
15750447,Male,37,70000,1
|
||||
15663249,Female,52,21000,1
|
||||
15638646,Male,48,141000,0
|
||||
15734161,Female,37,93000,1
|
||||
15631070,Female,37,62000,0
|
||||
15761950,Female,48,138000,1
|
||||
15649668,Male,41,79000,0
|
||||
15713912,Female,37,78000,1
|
||||
15586757,Male,39,134000,1
|
||||
15596522,Male,49,89000,1
|
||||
15625395,Male,55,39000,1
|
||||
15760570,Male,37,77000,0
|
||||
15566689,Female,35,57000,0
|
||||
15725794,Female,36,63000,0
|
||||
15673539,Male,42,73000,1
|
||||
15705298,Female,43,112000,1
|
||||
15675791,Male,45,79000,0
|
||||
15747043,Male,46,117000,1
|
||||
15736397,Female,58,38000,1
|
||||
15678201,Male,48,74000,1
|
||||
15720745,Female,37,137000,1
|
||||
15637593,Male,37,79000,1
|
||||
15598070,Female,40,60000,0
|
||||
15787550,Male,42,54000,0
|
||||
15603942,Female,51,134000,0
|
||||
15733973,Female,47,113000,1
|
||||
15596761,Male,36,125000,1
|
||||
15652400,Female,38,50000,0
|
||||
15717893,Female,42,70000,0
|
||||
15622585,Male,39,96000,1
|
||||
15733964,Female,38,50000,0
|
||||
15753861,Female,49,141000,1
|
||||
15747097,Female,39,79000,0
|
||||
15594762,Female,39,75000,1
|
||||
15667417,Female,54,104000,1
|
||||
15684861,Male,35,55000,0
|
||||
15742204,Male,45,32000,1
|
||||
15623502,Male,36,60000,0
|
||||
15774872,Female,52,138000,1
|
||||
15611191,Female,53,82000,1
|
||||
15674331,Male,41,52000,0
|
||||
15619465,Female,48,30000,1
|
||||
15575247,Female,48,131000,1
|
||||
15695679,Female,41,60000,0
|
||||
15713463,Male,41,72000,0
|
||||
15785170,Female,42,75000,0
|
||||
15796351,Male,36,118000,1
|
||||
15639576,Female,47,107000,1
|
||||
15693264,Male,38,51000,0
|
||||
15589715,Female,48,119000,1
|
||||
15769902,Male,42,65000,0
|
||||
15587177,Male,40,65000,0
|
||||
15814553,Male,57,60000,1
|
||||
15601550,Female,36,54000,0
|
||||
15664907,Male,58,144000,1
|
||||
15612465,Male,35,79000,0
|
||||
15810800,Female,38,55000,0
|
||||
15665760,Male,39,122000,1
|
||||
15588080,Female,53,104000,1
|
||||
15776844,Male,35,75000,0
|
||||
15717560,Female,38,65000,0
|
||||
15629739,Female,47,51000,1
|
||||
15729908,Male,47,105000,1
|
||||
15716781,Female,41,63000,0
|
||||
15646936,Male,53,72000,1
|
||||
15768151,Female,54,108000,1
|
||||
15579212,Male,39,77000,0
|
||||
15721835,Male,38,61000,0
|
||||
15800515,Female,38,113000,1
|
||||
15591279,Male,37,75000,0
|
||||
15587419,Female,42,90000,1
|
||||
15750335,Female,37,57000,0
|
||||
15699619,Male,36,99000,1
|
||||
15606472,Male,60,34000,1
|
||||
15778368,Male,54,70000,1
|
||||
15671387,Female,41,72000,0
|
||||
15573926,Male,40,71000,1
|
||||
15709183,Male,42,54000,0
|
||||
15577514,Male,43,129000,1
|
||||
15778830,Female,53,34000,1
|
||||
15768072,Female,47,50000,1
|
||||
15768293,Female,42,79000,0
|
||||
15654456,Male,42,104000,1
|
||||
15807525,Female,59,29000,1
|
||||
15574372,Female,58,47000,1
|
||||
15671249,Male,46,88000,1
|
||||
15779744,Male,38,71000,0
|
||||
15624755,Female,54,26000,1
|
||||
15611430,Female,60,46000,1
|
||||
15774744,Male,60,83000,1
|
||||
15629885,Female,39,73000,0
|
||||
15708791,Male,59,130000,1
|
||||
15793890,Female,37,80000,0
|
||||
15646091,Female,46,32000,1
|
||||
15596984,Female,46,74000,0
|
||||
15800215,Female,42,53000,0
|
||||
15577806,Male,41,87000,1
|
||||
15749381,Female,58,23000,1
|
||||
15683758,Male,42,64000,0
|
||||
15670615,Male,48,33000,1
|
||||
15715622,Female,44,139000,1
|
||||
15707634,Male,49,28000,1
|
||||
15806901,Female,57,33000,1
|
||||
15775335,Male,56,60000,1
|
||||
15724150,Female,49,39000,1
|
||||
15627220,Male,39,71000,0
|
||||
15672330,Male,47,34000,1
|
||||
15668521,Female,48,35000,1
|
||||
15807837,Male,48,33000,1
|
||||
15592570,Male,47,23000,1
|
||||
15748589,Female,45,45000,1
|
||||
15635893,Male,60,42000,1
|
||||
15757632,Female,39,59000,0
|
||||
15691863,Female,46,41000,1
|
||||
15706071,Male,51,23000,1
|
||||
15654296,Female,50,20000,1
|
||||
15755018,Male,36,33000,0
|
||||
15594041,Female,49,36000,1
|
|
59
section_3_classification/section_3_3_kernel_svm/main.py
Normal file
59
section_3_classification/section_3_3_kernel_svm/main.py
Normal file
@ -0,0 +1,59 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Social_Network_Ads.csv")
|
||||
X = dataset.iloc[:, [2,3]].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.svm import SVC
|
||||
|
||||
classifier = SVC(kernel='rbf', random_state=0)
|
||||
|
||||
classifier.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_train, Y_train
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue'))(i), label = j)
|
||||
|
||||
plt.title('Logistic Regression (Training set)')
|
||||
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
@ -0,0 +1,401 @@
|
||||
User ID,Gender,Age,EstimatedSalary,Purchased
|
||||
15624510,Male,19,19000,0
|
||||
15810944,Male,35,20000,0
|
||||
15668575,Female,26,43000,0
|
||||
15603246,Female,27,57000,0
|
||||
15804002,Male,19,76000,0
|
||||
15728773,Male,27,58000,0
|
||||
15598044,Female,27,84000,0
|
||||
15694829,Female,32,150000,1
|
||||
15600575,Male,25,33000,0
|
||||
15727311,Female,35,65000,0
|
||||
15570769,Female,26,80000,0
|
||||
15606274,Female,26,52000,0
|
||||
15746139,Male,20,86000,0
|
||||
15704987,Male,32,18000,0
|
||||
15628972,Male,18,82000,0
|
||||
15697686,Male,29,80000,0
|
||||
15733883,Male,47,25000,1
|
||||
15617482,Male,45,26000,1
|
||||
15704583,Male,46,28000,1
|
||||
15621083,Female,48,29000,1
|
||||
15649487,Male,45,22000,1
|
||||
15736760,Female,47,49000,1
|
||||
15714658,Male,48,41000,1
|
||||
15599081,Female,45,22000,1
|
||||
15705113,Male,46,23000,1
|
||||
15631159,Male,47,20000,1
|
||||
15792818,Male,49,28000,1
|
||||
15633531,Female,47,30000,1
|
||||
15744529,Male,29,43000,0
|
||||
15669656,Male,31,18000,0
|
||||
15581198,Male,31,74000,0
|
||||
15729054,Female,27,137000,1
|
||||
15573452,Female,21,16000,0
|
||||
15776733,Female,28,44000,0
|
||||
15724858,Male,27,90000,0
|
||||
15713144,Male,35,27000,0
|
||||
15690188,Female,33,28000,0
|
||||
15689425,Male,30,49000,0
|
||||
15671766,Female,26,72000,0
|
||||
15782806,Female,27,31000,0
|
||||
15764419,Female,27,17000,0
|
||||
15591915,Female,33,51000,0
|
||||
15772798,Male,35,108000,0
|
||||
15792008,Male,30,15000,0
|
||||
15715541,Female,28,84000,0
|
||||
15639277,Male,23,20000,0
|
||||
15798850,Male,25,79000,0
|
||||
15776348,Female,27,54000,0
|
||||
15727696,Male,30,135000,1
|
||||
15793813,Female,31,89000,0
|
||||
15694395,Female,24,32000,0
|
||||
15764195,Female,18,44000,0
|
||||
15744919,Female,29,83000,0
|
||||
15671655,Female,35,23000,0
|
||||
15654901,Female,27,58000,0
|
||||
15649136,Female,24,55000,0
|
||||
15775562,Female,23,48000,0
|
||||
15807481,Male,28,79000,0
|
||||
15642885,Male,22,18000,0
|
||||
15789109,Female,32,117000,0
|
||||
15814004,Male,27,20000,0
|
||||
15673619,Male,25,87000,0
|
||||
15595135,Female,23,66000,0
|
||||
15583681,Male,32,120000,1
|
||||
15605000,Female,59,83000,0
|
||||
15718071,Male,24,58000,0
|
||||
15679760,Male,24,19000,0
|
||||
15654574,Female,23,82000,0
|
||||
15577178,Female,22,63000,0
|
||||
15595324,Female,31,68000,0
|
||||
15756932,Male,25,80000,0
|
||||
15726358,Female,24,27000,0
|
||||
15595228,Female,20,23000,0
|
||||
15782530,Female,33,113000,0
|
||||
15592877,Male,32,18000,0
|
||||
15651983,Male,34,112000,1
|
||||
15746737,Male,18,52000,0
|
||||
15774179,Female,22,27000,0
|
||||
15667265,Female,28,87000,0
|
||||
15655123,Female,26,17000,0
|
||||
15595917,Male,30,80000,0
|
||||
15668385,Male,39,42000,0
|
||||
15709476,Male,20,49000,0
|
||||
15711218,Male,35,88000,0
|
||||
15798659,Female,30,62000,0
|
||||
15663939,Female,31,118000,1
|
||||
15694946,Male,24,55000,0
|
||||
15631912,Female,28,85000,0
|
||||
15768816,Male,26,81000,0
|
||||
15682268,Male,35,50000,0
|
||||
15684801,Male,22,81000,0
|
||||
15636428,Female,30,116000,0
|
||||
15809823,Male,26,15000,0
|
||||
15699284,Female,29,28000,0
|
||||
15786993,Female,29,83000,0
|
||||
15709441,Female,35,44000,0
|
||||
15710257,Female,35,25000,0
|
||||
15582492,Male,28,123000,1
|
||||
15575694,Male,35,73000,0
|
||||
15756820,Female,28,37000,0
|
||||
15766289,Male,27,88000,0
|
||||
15593014,Male,28,59000,0
|
||||
15584545,Female,32,86000,0
|
||||
15675949,Female,33,149000,1
|
||||
15672091,Female,19,21000,0
|
||||
15801658,Male,21,72000,0
|
||||
15706185,Female,26,35000,0
|
||||
15789863,Male,27,89000,0
|
||||
15720943,Male,26,86000,0
|
||||
15697997,Female,38,80000,0
|
||||
15665416,Female,39,71000,0
|
||||
15660200,Female,37,71000,0
|
||||
15619653,Male,38,61000,0
|
||||
15773447,Male,37,55000,0
|
||||
15739160,Male,42,80000,0
|
||||
15689237,Male,40,57000,0
|
||||
15679297,Male,35,75000,0
|
||||
15591433,Male,36,52000,0
|
||||
15642725,Male,40,59000,0
|
||||
15701962,Male,41,59000,0
|
||||
15811613,Female,36,75000,0
|
||||
15741049,Male,37,72000,0
|
||||
15724423,Female,40,75000,0
|
||||
15574305,Male,35,53000,0
|
||||
15678168,Female,41,51000,0
|
||||
15697020,Female,39,61000,0
|
||||
15610801,Male,42,65000,0
|
||||
15745232,Male,26,32000,0
|
||||
15722758,Male,30,17000,0
|
||||
15792102,Female,26,84000,0
|
||||
15675185,Male,31,58000,0
|
||||
15801247,Male,33,31000,0
|
||||
15725660,Male,30,87000,0
|
||||
15638963,Female,21,68000,0
|
||||
15800061,Female,28,55000,0
|
||||
15578006,Male,23,63000,0
|
||||
15668504,Female,20,82000,0
|
||||
15687491,Male,30,107000,1
|
||||
15610403,Female,28,59000,0
|
||||
15741094,Male,19,25000,0
|
||||
15807909,Male,19,85000,0
|
||||
15666141,Female,18,68000,0
|
||||
15617134,Male,35,59000,0
|
||||
15783029,Male,30,89000,0
|
||||
15622833,Female,34,25000,0
|
||||
15746422,Female,24,89000,0
|
||||
15750839,Female,27,96000,1
|
||||
15749130,Female,41,30000,0
|
||||
15779862,Male,29,61000,0
|
||||
15767871,Male,20,74000,0
|
||||
15679651,Female,26,15000,0
|
||||
15576219,Male,41,45000,0
|
||||
15699247,Male,31,76000,0
|
||||
15619087,Female,36,50000,0
|
||||
15605327,Male,40,47000,0
|
||||
15610140,Female,31,15000,0
|
||||
15791174,Male,46,59000,0
|
||||
15602373,Male,29,75000,0
|
||||
15762605,Male,26,30000,0
|
||||
15598840,Female,32,135000,1
|
||||
15744279,Male,32,100000,1
|
||||
15670619,Male,25,90000,0
|
||||
15599533,Female,37,33000,0
|
||||
15757837,Male,35,38000,0
|
||||
15697574,Female,33,69000,0
|
||||
15578738,Female,18,86000,0
|
||||
15762228,Female,22,55000,0
|
||||
15614827,Female,35,71000,0
|
||||
15789815,Male,29,148000,1
|
||||
15579781,Female,29,47000,0
|
||||
15587013,Male,21,88000,0
|
||||
15570932,Male,34,115000,0
|
||||
15794661,Female,26,118000,0
|
||||
15581654,Female,34,43000,0
|
||||
15644296,Female,34,72000,0
|
||||
15614420,Female,23,28000,0
|
||||
15609653,Female,35,47000,0
|
||||
15594577,Male,25,22000,0
|
||||
15584114,Male,24,23000,0
|
||||
15673367,Female,31,34000,0
|
||||
15685576,Male,26,16000,0
|
||||
15774727,Female,31,71000,0
|
||||
15694288,Female,32,117000,1
|
||||
15603319,Male,33,43000,0
|
||||
15759066,Female,33,60000,0
|
||||
15814816,Male,31,66000,0
|
||||
15724402,Female,20,82000,0
|
||||
15571059,Female,33,41000,0
|
||||
15674206,Male,35,72000,0
|
||||
15715160,Male,28,32000,0
|
||||
15730448,Male,24,84000,0
|
||||
15662067,Female,19,26000,0
|
||||
15779581,Male,29,43000,0
|
||||
15662901,Male,19,70000,0
|
||||
15689751,Male,28,89000,0
|
||||
15667742,Male,34,43000,0
|
||||
15738448,Female,30,79000,0
|
||||
15680243,Female,20,36000,0
|
||||
15745083,Male,26,80000,0
|
||||
15708228,Male,35,22000,0
|
||||
15628523,Male,35,39000,0
|
||||
15708196,Male,49,74000,0
|
||||
15735549,Female,39,134000,1
|
||||
15809347,Female,41,71000,0
|
||||
15660866,Female,58,101000,1
|
||||
15766609,Female,47,47000,0
|
||||
15654230,Female,55,130000,1
|
||||
15794566,Female,52,114000,0
|
||||
15800890,Female,40,142000,1
|
||||
15697424,Female,46,22000,0
|
||||
15724536,Female,48,96000,1
|
||||
15735878,Male,52,150000,1
|
||||
15707596,Female,59,42000,0
|
||||
15657163,Male,35,58000.0,0
|
||||
15622478,Male,47.0,43000,0
|
||||
15779529,Female,60,108000,1
|
||||
15636023,Male,49,65000,0
|
||||
15582066,Male,40,78000,0
|
||||
15666675,Female,46,96000,0
|
||||
15732987,Male,59,143000,1
|
||||
15789432,Female,41,80000,0
|
||||
15663161,Male,35,91000,1
|
||||
15694879,Male,37,144000,1
|
||||
15593715,Male,60,102000,1
|
||||
15575002,Female,35,60000,0
|
||||
15622171,Male,37,53000,0
|
||||
15795224,Female,36,126000,1
|
||||
15685346,Male,56,133000,1
|
||||
15691808,Female,40,72000,0
|
||||
15721007,Female,42,80000,1
|
||||
15794253,Female,35,147000,1
|
||||
15694453,Male,39,42000,0
|
||||
15813113,Male,40,107000,1
|
||||
15614187,Male,49,86000,1
|
||||
15619407,Female,38,112000,0
|
||||
15646227,Male,46,79000,1
|
||||
15660541,Male,40,57000,0
|
||||
15753874,Female,37,80000,0
|
||||
15617877,Female,46,82000,0
|
||||
15772073,Female,53,143000,1
|
||||
15701537,Male,42,149000,1
|
||||
15736228,Male,38,59000,0
|
||||
15780572,Female,50,88000,1
|
||||
15769596,Female,56,104000,1
|
||||
15586996,Female,41,72000,0
|
||||
15722061,Female,51,146000,1
|
||||
15638003,Female,35,50000,0
|
||||
15775590,Female,57,122000,1
|
||||
15730688,Male,41,52000,0
|
||||
15753102,Female,35,97000,1
|
||||
15810075,Female,44,39000,0
|
||||
15723373,Male,37,52000,0
|
||||
15795298,Female,48,134000,1
|
||||
15584320,Female,37,146000,1
|
||||
15724161,Female,50,44000,0
|
||||
15750056,Female,52,90000,1
|
||||
15609637,Female,41,72000,0
|
||||
15794493,Male,40,57000,0
|
||||
15569641,Female,58,95000,1
|
||||
15815236,Female,45,131000,1
|
||||
15811177,Female,35,77000,0
|
||||
15680587,Male,36,144000,1
|
||||
15672821,Female,55,125000,1
|
||||
15767681,Female,35,72000,0
|
||||
15600379,Male,48,90000,1
|
||||
15801336,Female,42,108000,1
|
||||
15721592,Male,40,75000,0
|
||||
15581282,Male,37,74000,0
|
||||
15746203,Female,47,144000,1
|
||||
15583137,Male,40,61000,0
|
||||
15680752,Female,43,133000,0
|
||||
15688172,Female,59,76000,1
|
||||
15791373,Male,60,42000,1
|
||||
15589449,Male,39,106000,1
|
||||
15692819,Female,57,26000,1
|
||||
15727467,Male,57,74000,1
|
||||
15734312,Male,38,71000,0
|
||||
15764604,Male,49,88000,1
|
||||
15613014,Female,52,38000,1
|
||||
15759684,Female,50,36000,1
|
||||
15609669,Female,59,88000,1
|
||||
15685536,Male,35,61000,0
|
||||
15750447,Male,37,70000,1
|
||||
15663249,Female,52,21000,1
|
||||
15638646,Male,48,141000,0
|
||||
15734161,Female,37,93000,1
|
||||
15631070,Female,37,62000,0
|
||||
15761950,Female,48,138000,1
|
||||
15649668,Male,41,79000,0
|
||||
15713912,Female,37,78000,1
|
||||
15586757,Male,39,134000,1
|
||||
15596522,Male,49,89000,1
|
||||
15625395,Male,55,39000,1
|
||||
15760570,Male,37,77000,0
|
||||
15566689,Female,35,57000,0
|
||||
15725794,Female,36,63000,0
|
||||
15673539,Male,42,73000,1
|
||||
15705298,Female,43,112000,1
|
||||
15675791,Male,45,79000,0
|
||||
15747043,Male,46,117000,1
|
||||
15736397,Female,58,38000,1
|
||||
15678201,Male,48,74000,1
|
||||
15720745,Female,37,137000,1
|
||||
15637593,Male,37,79000,1
|
||||
15598070,Female,40,60000,0
|
||||
15787550,Male,42,54000,0
|
||||
15603942,Female,51,134000,0
|
||||
15733973,Female,47,113000,1
|
||||
15596761,Male,36,125000,1
|
||||
15652400,Female,38,50000,0
|
||||
15717893,Female,42,70000,0
|
||||
15622585,Male,39,96000,1
|
||||
15733964,Female,38,50000,0
|
||||
15753861,Female,49,141000,1
|
||||
15747097,Female,39,79000,0
|
||||
15594762,Female,39,75000,1
|
||||
15667417,Female,54,104000,1
|
||||
15684861,Male,35,55000,0
|
||||
15742204,Male,45,32000,1
|
||||
15623502,Male,36,60000,0
|
||||
15774872,Female,52,138000,1
|
||||
15611191,Female,53,82000,1
|
||||
15674331,Male,41,52000,0
|
||||
15619465,Female,48,30000,1
|
||||
15575247,Female,48,131000,1
|
||||
15695679,Female,41,60000,0
|
||||
15713463,Male,41,72000,0
|
||||
15785170,Female,42,75000,0
|
||||
15796351,Male,36,118000,1
|
||||
15639576,Female,47,107000,1
|
||||
15693264,Male,38,51000,0
|
||||
15589715,Female,48,119000,1
|
||||
15769902,Male,42,65000,0
|
||||
15587177,Male,40,65000,0
|
||||
15814553,Male,57,60000,1
|
||||
15601550,Female,36,54000,0
|
||||
15664907,Male,58,144000,1
|
||||
15612465,Male,35,79000,0
|
||||
15810800,Female,38,55000,0
|
||||
15665760,Male,39,122000,1
|
||||
15588080,Female,53,104000,1
|
||||
15776844,Male,35,75000,0
|
||||
15717560,Female,38,65000,0
|
||||
15629739,Female,47,51000,1
|
||||
15729908,Male,47,105000,1
|
||||
15716781,Female,41,63000,0
|
||||
15646936,Male,53,72000,1
|
||||
15768151,Female,54,108000,1
|
||||
15579212,Male,39,77000,0
|
||||
15721835,Male,38,61000,0
|
||||
15800515,Female,38,113000,1
|
||||
15591279,Male,37,75000,0
|
||||
15587419,Female,42,90000,1
|
||||
15750335,Female,37,57000,0
|
||||
15699619,Male,36,99000,1
|
||||
15606472,Male,60,34000,1
|
||||
15778368,Male,54,70000,1
|
||||
15671387,Female,41,72000,0
|
||||
15573926,Male,40,71000,1
|
||||
15709183,Male,42,54000,0
|
||||
15577514,Male,43,129000,1
|
||||
15778830,Female,53,34000,1
|
||||
15768072,Female,47,50000,1
|
||||
15768293,Female,42,79000,0
|
||||
15654456,Male,42,104000,1
|
||||
15807525,Female,59,29000,1
|
||||
15574372,Female,58,47000,1
|
||||
15671249,Male,46,88000,1
|
||||
15779744,Male,38,71000,0
|
||||
15624755,Female,54,26000,1
|
||||
15611430,Female,60,46000,1
|
||||
15774744,Male,60,83000,1
|
||||
15629885,Female,39,73000,0
|
||||
15708791,Male,59,130000,1
|
||||
15793890,Female,37,80000,0
|
||||
15646091,Female,46,32000,1
|
||||
15596984,Female,46,74000,0
|
||||
15800215,Female,42,53000,0
|
||||
15577806,Male,41,87000,1
|
||||
15749381,Female,58,23000,1
|
||||
15683758,Male,42,64000,0
|
||||
15670615,Male,48,33000,1
|
||||
15715622,Female,44,139000,1
|
||||
15707634,Male,49,28000,1
|
||||
15806901,Female,57,33000,1
|
||||
15775335,Male,56,60000,1
|
||||
15724150,Female,49,39000,1
|
||||
15627220,Male,39,71000,0
|
||||
15672330,Male,47,34000,1
|
||||
15668521,Female,48,35000,1
|
||||
15807837,Male,48,33000,1
|
||||
15592570,Male,47,23000,1
|
||||
15748589,Female,45,45000,1
|
||||
15635893,Male,60,42000,1
|
||||
15757632,Female,39,59000,0
|
||||
15691863,Female,46,41000,1
|
||||
15706071,Male,51,23000,1
|
||||
15654296,Female,50,20000,1
|
||||
15755018,Male,36,33000,0
|
||||
15594041,Female,49,36000,1
|
|
59
section_3_classification/section_3_4_naive_bayes/main.py
Normal file
59
section_3_classification/section_3_4_naive_bayes/main.py
Normal file
@ -0,0 +1,59 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Social_Network_Ads.csv")
|
||||
X = dataset.iloc[:, [2,3]].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.naive_bayes import GaussianNB
|
||||
|
||||
classifier = GaussianNB()
|
||||
|
||||
classifier.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_test, Y_test
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue'))(i), label = j)
|
||||
|
||||
plt.title('Naive Bayes (Training set)')
|
||||
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
@ -0,0 +1,401 @@
|
||||
User ID,Gender,Age,EstimatedSalary,Purchased
|
||||
15624510,Male,19,19000,0
|
||||
15810944,Male,35,20000,0
|
||||
15668575,Female,26,43000,0
|
||||
15603246,Female,27,57000,0
|
||||
15804002,Male,19,76000,0
|
||||
15728773,Male,27,58000,0
|
||||
15598044,Female,27,84000,0
|
||||
15694829,Female,32,150000,1
|
||||
15600575,Male,25,33000,0
|
||||
15727311,Female,35,65000,0
|
||||
15570769,Female,26,80000,0
|
||||
15606274,Female,26,52000,0
|
||||
15746139,Male,20,86000,0
|
||||
15704987,Male,32,18000,0
|
||||
15628972,Male,18,82000,0
|
||||
15697686,Male,29,80000,0
|
||||
15733883,Male,47,25000,1
|
||||
15617482,Male,45,26000,1
|
||||
15704583,Male,46,28000,1
|
||||
15621083,Female,48,29000,1
|
||||
15649487,Male,45,22000,1
|
||||
15736760,Female,47,49000,1
|
||||
15714658,Male,48,41000,1
|
||||
15599081,Female,45,22000,1
|
||||
15705113,Male,46,23000,1
|
||||
15631159,Male,47,20000,1
|
||||
15792818,Male,49,28000,1
|
||||
15633531,Female,47,30000,1
|
||||
15744529,Male,29,43000,0
|
||||
15669656,Male,31,18000,0
|
||||
15581198,Male,31,74000,0
|
||||
15729054,Female,27,137000,1
|
||||
15573452,Female,21,16000,0
|
||||
15776733,Female,28,44000,0
|
||||
15724858,Male,27,90000,0
|
||||
15713144,Male,35,27000,0
|
||||
15690188,Female,33,28000,0
|
||||
15689425,Male,30,49000,0
|
||||
15671766,Female,26,72000,0
|
||||
15782806,Female,27,31000,0
|
||||
15764419,Female,27,17000,0
|
||||
15591915,Female,33,51000,0
|
||||
15772798,Male,35,108000,0
|
||||
15792008,Male,30,15000,0
|
||||
15715541,Female,28,84000,0
|
||||
15639277,Male,23,20000,0
|
||||
15798850,Male,25,79000,0
|
||||
15776348,Female,27,54000,0
|
||||
15727696,Male,30,135000,1
|
||||
15793813,Female,31,89000,0
|
||||
15694395,Female,24,32000,0
|
||||
15764195,Female,18,44000,0
|
||||
15744919,Female,29,83000,0
|
||||
15671655,Female,35,23000,0
|
||||
15654901,Female,27,58000,0
|
||||
15649136,Female,24,55000,0
|
||||
15775562,Female,23,48000,0
|
||||
15807481,Male,28,79000,0
|
||||
15642885,Male,22,18000,0
|
||||
15789109,Female,32,117000,0
|
||||
15814004,Male,27,20000,0
|
||||
15673619,Male,25,87000,0
|
||||
15595135,Female,23,66000,0
|
||||
15583681,Male,32,120000,1
|
||||
15605000,Female,59,83000,0
|
||||
15718071,Male,24,58000,0
|
||||
15679760,Male,24,19000,0
|
||||
15654574,Female,23,82000,0
|
||||
15577178,Female,22,63000,0
|
||||
15595324,Female,31,68000,0
|
||||
15756932,Male,25,80000,0
|
||||
15726358,Female,24,27000,0
|
||||
15595228,Female,20,23000,0
|
||||
15782530,Female,33,113000,0
|
||||
15592877,Male,32,18000,0
|
||||
15651983,Male,34,112000,1
|
||||
15746737,Male,18,52000,0
|
||||
15774179,Female,22,27000,0
|
||||
15667265,Female,28,87000,0
|
||||
15655123,Female,26,17000,0
|
||||
15595917,Male,30,80000,0
|
||||
15668385,Male,39,42000,0
|
||||
15709476,Male,20,49000,0
|
||||
15711218,Male,35,88000,0
|
||||
15798659,Female,30,62000,0
|
||||
15663939,Female,31,118000,1
|
||||
15694946,Male,24,55000,0
|
||||
15631912,Female,28,85000,0
|
||||
15768816,Male,26,81000,0
|
||||
15682268,Male,35,50000,0
|
||||
15684801,Male,22,81000,0
|
||||
15636428,Female,30,116000,0
|
||||
15809823,Male,26,15000,0
|
||||
15699284,Female,29,28000,0
|
||||
15786993,Female,29,83000,0
|
||||
15709441,Female,35,44000,0
|
||||
15710257,Female,35,25000,0
|
||||
15582492,Male,28,123000,1
|
||||
15575694,Male,35,73000,0
|
||||
15756820,Female,28,37000,0
|
||||
15766289,Male,27,88000,0
|
||||
15593014,Male,28,59000,0
|
||||
15584545,Female,32,86000,0
|
||||
15675949,Female,33,149000,1
|
||||
15672091,Female,19,21000,0
|
||||
15801658,Male,21,72000,0
|
||||
15706185,Female,26,35000,0
|
||||
15789863,Male,27,89000,0
|
||||
15720943,Male,26,86000,0
|
||||
15697997,Female,38,80000,0
|
||||
15665416,Female,39,71000,0
|
||||
15660200,Female,37,71000,0
|
||||
15619653,Male,38,61000,0
|
||||
15773447,Male,37,55000,0
|
||||
15739160,Male,42,80000,0
|
||||
15689237,Male,40,57000,0
|
||||
15679297,Male,35,75000,0
|
||||
15591433,Male,36,52000,0
|
||||
15642725,Male,40,59000,0
|
||||
15701962,Male,41,59000,0
|
||||
15811613,Female,36,75000,0
|
||||
15741049,Male,37,72000,0
|
||||
15724423,Female,40,75000,0
|
||||
15574305,Male,35,53000,0
|
||||
15678168,Female,41,51000,0
|
||||
15697020,Female,39,61000,0
|
||||
15610801,Male,42,65000,0
|
||||
15745232,Male,26,32000,0
|
||||
15722758,Male,30,17000,0
|
||||
15792102,Female,26,84000,0
|
||||
15675185,Male,31,58000,0
|
||||
15801247,Male,33,31000,0
|
||||
15725660,Male,30,87000,0
|
||||
15638963,Female,21,68000,0
|
||||
15800061,Female,28,55000,0
|
||||
15578006,Male,23,63000,0
|
||||
15668504,Female,20,82000,0
|
||||
15687491,Male,30,107000,1
|
||||
15610403,Female,28,59000,0
|
||||
15741094,Male,19,25000,0
|
||||
15807909,Male,19,85000,0
|
||||
15666141,Female,18,68000,0
|
||||
15617134,Male,35,59000,0
|
||||
15783029,Male,30,89000,0
|
||||
15622833,Female,34,25000,0
|
||||
15746422,Female,24,89000,0
|
||||
15750839,Female,27,96000,1
|
||||
15749130,Female,41,30000,0
|
||||
15779862,Male,29,61000,0
|
||||
15767871,Male,20,74000,0
|
||||
15679651,Female,26,15000,0
|
||||
15576219,Male,41,45000,0
|
||||
15699247,Male,31,76000,0
|
||||
15619087,Female,36,50000,0
|
||||
15605327,Male,40,47000,0
|
||||
15610140,Female,31,15000,0
|
||||
15791174,Male,46,59000,0
|
||||
15602373,Male,29,75000,0
|
||||
15762605,Male,26,30000,0
|
||||
15598840,Female,32,135000,1
|
||||
15744279,Male,32,100000,1
|
||||
15670619,Male,25,90000,0
|
||||
15599533,Female,37,33000,0
|
||||
15757837,Male,35,38000,0
|
||||
15697574,Female,33,69000,0
|
||||
15578738,Female,18,86000,0
|
||||
15762228,Female,22,55000,0
|
||||
15614827,Female,35,71000,0
|
||||
15789815,Male,29,148000,1
|
||||
15579781,Female,29,47000,0
|
||||
15587013,Male,21,88000,0
|
||||
15570932,Male,34,115000,0
|
||||
15794661,Female,26,118000,0
|
||||
15581654,Female,34,43000,0
|
||||
15644296,Female,34,72000,0
|
||||
15614420,Female,23,28000,0
|
||||
15609653,Female,35,47000,0
|
||||
15594577,Male,25,22000,0
|
||||
15584114,Male,24,23000,0
|
||||
15673367,Female,31,34000,0
|
||||
15685576,Male,26,16000,0
|
||||
15774727,Female,31,71000,0
|
||||
15694288,Female,32,117000,1
|
||||
15603319,Male,33,43000,0
|
||||
15759066,Female,33,60000,0
|
||||
15814816,Male,31,66000,0
|
||||
15724402,Female,20,82000,0
|
||||
15571059,Female,33,41000,0
|
||||
15674206,Male,35,72000,0
|
||||
15715160,Male,28,32000,0
|
||||
15730448,Male,24,84000,0
|
||||
15662067,Female,19,26000,0
|
||||
15779581,Male,29,43000,0
|
||||
15662901,Male,19,70000,0
|
||||
15689751,Male,28,89000,0
|
||||
15667742,Male,34,43000,0
|
||||
15738448,Female,30,79000,0
|
||||
15680243,Female,20,36000,0
|
||||
15745083,Male,26,80000,0
|
||||
15708228,Male,35,22000,0
|
||||
15628523,Male,35,39000,0
|
||||
15708196,Male,49,74000,0
|
||||
15735549,Female,39,134000,1
|
||||
15809347,Female,41,71000,0
|
||||
15660866,Female,58,101000,1
|
||||
15766609,Female,47,47000,0
|
||||
15654230,Female,55,130000,1
|
||||
15794566,Female,52,114000,0
|
||||
15800890,Female,40,142000,1
|
||||
15697424,Female,46,22000,0
|
||||
15724536,Female,48,96000,1
|
||||
15735878,Male,52,150000,1
|
||||
15707596,Female,59,42000,0
|
||||
15657163,Male,35,58000.0,0
|
||||
15622478,Male,47.0,43000,0
|
||||
15779529,Female,60,108000,1
|
||||
15636023,Male,49,65000,0
|
||||
15582066,Male,40,78000,0
|
||||
15666675,Female,46,96000,0
|
||||
15732987,Male,59,143000,1
|
||||
15789432,Female,41,80000,0
|
||||
15663161,Male,35,91000,1
|
||||
15694879,Male,37,144000,1
|
||||
15593715,Male,60,102000,1
|
||||
15575002,Female,35,60000,0
|
||||
15622171,Male,37,53000,0
|
||||
15795224,Female,36,126000,1
|
||||
15685346,Male,56,133000,1
|
||||
15691808,Female,40,72000,0
|
||||
15721007,Female,42,80000,1
|
||||
15794253,Female,35,147000,1
|
||||
15694453,Male,39,42000,0
|
||||
15813113,Male,40,107000,1
|
||||
15614187,Male,49,86000,1
|
||||
15619407,Female,38,112000,0
|
||||
15646227,Male,46,79000,1
|
||||
15660541,Male,40,57000,0
|
||||
15753874,Female,37,80000,0
|
||||
15617877,Female,46,82000,0
|
||||
15772073,Female,53,143000,1
|
||||
15701537,Male,42,149000,1
|
||||
15736228,Male,38,59000,0
|
||||
15780572,Female,50,88000,1
|
||||
15769596,Female,56,104000,1
|
||||
15586996,Female,41,72000,0
|
||||
15722061,Female,51,146000,1
|
||||
15638003,Female,35,50000,0
|
||||
15775590,Female,57,122000,1
|
||||
15730688,Male,41,52000,0
|
||||
15753102,Female,35,97000,1
|
||||
15810075,Female,44,39000,0
|
||||
15723373,Male,37,52000,0
|
||||
15795298,Female,48,134000,1
|
||||
15584320,Female,37,146000,1
|
||||
15724161,Female,50,44000,0
|
||||
15750056,Female,52,90000,1
|
||||
15609637,Female,41,72000,0
|
||||
15794493,Male,40,57000,0
|
||||
15569641,Female,58,95000,1
|
||||
15815236,Female,45,131000,1
|
||||
15811177,Female,35,77000,0
|
||||
15680587,Male,36,144000,1
|
||||
15672821,Female,55,125000,1
|
||||
15767681,Female,35,72000,0
|
||||
15600379,Male,48,90000,1
|
||||
15801336,Female,42,108000,1
|
||||
15721592,Male,40,75000,0
|
||||
15581282,Male,37,74000,0
|
||||
15746203,Female,47,144000,1
|
||||
15583137,Male,40,61000,0
|
||||
15680752,Female,43,133000,0
|
||||
15688172,Female,59,76000,1
|
||||
15791373,Male,60,42000,1
|
||||
15589449,Male,39,106000,1
|
||||
15692819,Female,57,26000,1
|
||||
15727467,Male,57,74000,1
|
||||
15734312,Male,38,71000,0
|
||||
15764604,Male,49,88000,1
|
||||
15613014,Female,52,38000,1
|
||||
15759684,Female,50,36000,1
|
||||
15609669,Female,59,88000,1
|
||||
15685536,Male,35,61000,0
|
||||
15750447,Male,37,70000,1
|
||||
15663249,Female,52,21000,1
|
||||
15638646,Male,48,141000,0
|
||||
15734161,Female,37,93000,1
|
||||
15631070,Female,37,62000,0
|
||||
15761950,Female,48,138000,1
|
||||
15649668,Male,41,79000,0
|
||||
15713912,Female,37,78000,1
|
||||
15586757,Male,39,134000,1
|
||||
15596522,Male,49,89000,1
|
||||
15625395,Male,55,39000,1
|
||||
15760570,Male,37,77000,0
|
||||
15566689,Female,35,57000,0
|
||||
15725794,Female,36,63000,0
|
||||
15673539,Male,42,73000,1
|
||||
15705298,Female,43,112000,1
|
||||
15675791,Male,45,79000,0
|
||||
15747043,Male,46,117000,1
|
||||
15736397,Female,58,38000,1
|
||||
15678201,Male,48,74000,1
|
||||
15720745,Female,37,137000,1
|
||||
15637593,Male,37,79000,1
|
||||
15598070,Female,40,60000,0
|
||||
15787550,Male,42,54000,0
|
||||
15603942,Female,51,134000,0
|
||||
15733973,Female,47,113000,1
|
||||
15596761,Male,36,125000,1
|
||||
15652400,Female,38,50000,0
|
||||
15717893,Female,42,70000,0
|
||||
15622585,Male,39,96000,1
|
||||
15733964,Female,38,50000,0
|
||||
15753861,Female,49,141000,1
|
||||
15747097,Female,39,79000,0
|
||||
15594762,Female,39,75000,1
|
||||
15667417,Female,54,104000,1
|
||||
15684861,Male,35,55000,0
|
||||
15742204,Male,45,32000,1
|
||||
15623502,Male,36,60000,0
|
||||
15774872,Female,52,138000,1
|
||||
15611191,Female,53,82000,1
|
||||
15674331,Male,41,52000,0
|
||||
15619465,Female,48,30000,1
|
||||
15575247,Female,48,131000,1
|
||||
15695679,Female,41,60000,0
|
||||
15713463,Male,41,72000,0
|
||||
15785170,Female,42,75000,0
|
||||
15796351,Male,36,118000,1
|
||||
15639576,Female,47,107000,1
|
||||
15693264,Male,38,51000,0
|
||||
15589715,Female,48,119000,1
|
||||
15769902,Male,42,65000,0
|
||||
15587177,Male,40,65000,0
|
||||
15814553,Male,57,60000,1
|
||||
15601550,Female,36,54000,0
|
||||
15664907,Male,58,144000,1
|
||||
15612465,Male,35,79000,0
|
||||
15810800,Female,38,55000,0
|
||||
15665760,Male,39,122000,1
|
||||
15588080,Female,53,104000,1
|
||||
15776844,Male,35,75000,0
|
||||
15717560,Female,38,65000,0
|
||||
15629739,Female,47,51000,1
|
||||
15729908,Male,47,105000,1
|
||||
15716781,Female,41,63000,0
|
||||
15646936,Male,53,72000,1
|
||||
15768151,Female,54,108000,1
|
||||
15579212,Male,39,77000,0
|
||||
15721835,Male,38,61000,0
|
||||
15800515,Female,38,113000,1
|
||||
15591279,Male,37,75000,0
|
||||
15587419,Female,42,90000,1
|
||||
15750335,Female,37,57000,0
|
||||
15699619,Male,36,99000,1
|
||||
15606472,Male,60,34000,1
|
||||
15778368,Male,54,70000,1
|
||||
15671387,Female,41,72000,0
|
||||
15573926,Male,40,71000,1
|
||||
15709183,Male,42,54000,0
|
||||
15577514,Male,43,129000,1
|
||||
15778830,Female,53,34000,1
|
||||
15768072,Female,47,50000,1
|
||||
15768293,Female,42,79000,0
|
||||
15654456,Male,42,104000,1
|
||||
15807525,Female,59,29000,1
|
||||
15574372,Female,58,47000,1
|
||||
15671249,Male,46,88000,1
|
||||
15779744,Male,38,71000,0
|
||||
15624755,Female,54,26000,1
|
||||
15611430,Female,60,46000,1
|
||||
15774744,Male,60,83000,1
|
||||
15629885,Female,39,73000,0
|
||||
15708791,Male,59,130000,1
|
||||
15793890,Female,37,80000,0
|
||||
15646091,Female,46,32000,1
|
||||
15596984,Female,46,74000,0
|
||||
15800215,Female,42,53000,0
|
||||
15577806,Male,41,87000,1
|
||||
15749381,Female,58,23000,1
|
||||
15683758,Male,42,64000,0
|
||||
15670615,Male,48,33000,1
|
||||
15715622,Female,44,139000,1
|
||||
15707634,Male,49,28000,1
|
||||
15806901,Female,57,33000,1
|
||||
15775335,Male,56,60000,1
|
||||
15724150,Female,49,39000,1
|
||||
15627220,Male,39,71000,0
|
||||
15672330,Male,47,34000,1
|
||||
15668521,Female,48,35000,1
|
||||
15807837,Male,48,33000,1
|
||||
15592570,Male,47,23000,1
|
||||
15748589,Female,45,45000,1
|
||||
15635893,Male,60,42000,1
|
||||
15757632,Female,39,59000,0
|
||||
15691863,Female,46,41000,1
|
||||
15706071,Male,51,23000,1
|
||||
15654296,Female,50,20000,1
|
||||
15755018,Male,36,33000,0
|
||||
15594041,Female,49,36000,1
|
|
56
section_3_classification/section_3_5_decision_tree/main.py
Normal file
56
section_3_classification/section_3_5_decision_tree/main.py
Normal file
@ -0,0 +1,56 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Social_Network_Ads.csv")
|
||||
X = dataset.iloc[:, [2,3]].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
classifier = DecisionTreeClassifier(criterion='gini', random_state=0)
|
||||
classifier.fit(X_train, Y_train)
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_train, Y_train
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue'))(i), label = j)
|
||||
|
||||
plt.title('Decision Tree (Training set)')
|
||||
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
@ -0,0 +1,401 @@
|
||||
User ID,Gender,Age,EstimatedSalary,Purchased
|
||||
15624510,Male,19,19000,0
|
||||
15810944,Male,35,20000,0
|
||||
15668575,Female,26,43000,0
|
||||
15603246,Female,27,57000,0
|
||||
15804002,Male,19,76000,0
|
||||
15728773,Male,27,58000,0
|
||||
15598044,Female,27,84000,0
|
||||
15694829,Female,32,150000,1
|
||||
15600575,Male,25,33000,0
|
||||
15727311,Female,35,65000,0
|
||||
15570769,Female,26,80000,0
|
||||
15606274,Female,26,52000,0
|
||||
15746139,Male,20,86000,0
|
||||
15704987,Male,32,18000,0
|
||||
15628972,Male,18,82000,0
|
||||
15697686,Male,29,80000,0
|
||||
15733883,Male,47,25000,1
|
||||
15617482,Male,45,26000,1
|
||||
15704583,Male,46,28000,1
|
||||
15621083,Female,48,29000,1
|
||||
15649487,Male,45,22000,1
|
||||
15736760,Female,47,49000,1
|
||||
15714658,Male,48,41000,1
|
||||
15599081,Female,45,22000,1
|
||||
15705113,Male,46,23000,1
|
||||
15631159,Male,47,20000,1
|
||||
15792818,Male,49,28000,1
|
||||
15633531,Female,47,30000,1
|
||||
15744529,Male,29,43000,0
|
||||
15669656,Male,31,18000,0
|
||||
15581198,Male,31,74000,0
|
||||
15729054,Female,27,137000,1
|
||||
15573452,Female,21,16000,0
|
||||
15776733,Female,28,44000,0
|
||||
15724858,Male,27,90000,0
|
||||
15713144,Male,35,27000,0
|
||||
15690188,Female,33,28000,0
|
||||
15689425,Male,30,49000,0
|
||||
15671766,Female,26,72000,0
|
||||
15782806,Female,27,31000,0
|
||||
15764419,Female,27,17000,0
|
||||
15591915,Female,33,51000,0
|
||||
15772798,Male,35,108000,0
|
||||
15792008,Male,30,15000,0
|
||||
15715541,Female,28,84000,0
|
||||
15639277,Male,23,20000,0
|
||||
15798850,Male,25,79000,0
|
||||
15776348,Female,27,54000,0
|
||||
15727696,Male,30,135000,1
|
||||
15793813,Female,31,89000,0
|
||||
15694395,Female,24,32000,0
|
||||
15764195,Female,18,44000,0
|
||||
15744919,Female,29,83000,0
|
||||
15671655,Female,35,23000,0
|
||||
15654901,Female,27,58000,0
|
||||
15649136,Female,24,55000,0
|
||||
15775562,Female,23,48000,0
|
||||
15807481,Male,28,79000,0
|
||||
15642885,Male,22,18000,0
|
||||
15789109,Female,32,117000,0
|
||||
15814004,Male,27,20000,0
|
||||
15673619,Male,25,87000,0
|
||||
15595135,Female,23,66000,0
|
||||
15583681,Male,32,120000,1
|
||||
15605000,Female,59,83000,0
|
||||
15718071,Male,24,58000,0
|
||||
15679760,Male,24,19000,0
|
||||
15654574,Female,23,82000,0
|
||||
15577178,Female,22,63000,0
|
||||
15595324,Female,31,68000,0
|
||||
15756932,Male,25,80000,0
|
||||
15726358,Female,24,27000,0
|
||||
15595228,Female,20,23000,0
|
||||
15782530,Female,33,113000,0
|
||||
15592877,Male,32,18000,0
|
||||
15651983,Male,34,112000,1
|
||||
15746737,Male,18,52000,0
|
||||
15774179,Female,22,27000,0
|
||||
15667265,Female,28,87000,0
|
||||
15655123,Female,26,17000,0
|
||||
15595917,Male,30,80000,0
|
||||
15668385,Male,39,42000,0
|
||||
15709476,Male,20,49000,0
|
||||
15711218,Male,35,88000,0
|
||||
15798659,Female,30,62000,0
|
||||
15663939,Female,31,118000,1
|
||||
15694946,Male,24,55000,0
|
||||
15631912,Female,28,85000,0
|
||||
15768816,Male,26,81000,0
|
||||
15682268,Male,35,50000,0
|
||||
15684801,Male,22,81000,0
|
||||
15636428,Female,30,116000,0
|
||||
15809823,Male,26,15000,0
|
||||
15699284,Female,29,28000,0
|
||||
15786993,Female,29,83000,0
|
||||
15709441,Female,35,44000,0
|
||||
15710257,Female,35,25000,0
|
||||
15582492,Male,28,123000,1
|
||||
15575694,Male,35,73000,0
|
||||
15756820,Female,28,37000,0
|
||||
15766289,Male,27,88000,0
|
||||
15593014,Male,28,59000,0
|
||||
15584545,Female,32,86000,0
|
||||
15675949,Female,33,149000,1
|
||||
15672091,Female,19,21000,0
|
||||
15801658,Male,21,72000,0
|
||||
15706185,Female,26,35000,0
|
||||
15789863,Male,27,89000,0
|
||||
15720943,Male,26,86000,0
|
||||
15697997,Female,38,80000,0
|
||||
15665416,Female,39,71000,0
|
||||
15660200,Female,37,71000,0
|
||||
15619653,Male,38,61000,0
|
||||
15773447,Male,37,55000,0
|
||||
15739160,Male,42,80000,0
|
||||
15689237,Male,40,57000,0
|
||||
15679297,Male,35,75000,0
|
||||
15591433,Male,36,52000,0
|
||||
15642725,Male,40,59000,0
|
||||
15701962,Male,41,59000,0
|
||||
15811613,Female,36,75000,0
|
||||
15741049,Male,37,72000,0
|
||||
15724423,Female,40,75000,0
|
||||
15574305,Male,35,53000,0
|
||||
15678168,Female,41,51000,0
|
||||
15697020,Female,39,61000,0
|
||||
15610801,Male,42,65000,0
|
||||
15745232,Male,26,32000,0
|
||||
15722758,Male,30,17000,0
|
||||
15792102,Female,26,84000,0
|
||||
15675185,Male,31,58000,0
|
||||
15801247,Male,33,31000,0
|
||||
15725660,Male,30,87000,0
|
||||
15638963,Female,21,68000,0
|
||||
15800061,Female,28,55000,0
|
||||
15578006,Male,23,63000,0
|
||||
15668504,Female,20,82000,0
|
||||
15687491,Male,30,107000,1
|
||||
15610403,Female,28,59000,0
|
||||
15741094,Male,19,25000,0
|
||||
15807909,Male,19,85000,0
|
||||
15666141,Female,18,68000,0
|
||||
15617134,Male,35,59000,0
|
||||
15783029,Male,30,89000,0
|
||||
15622833,Female,34,25000,0
|
||||
15746422,Female,24,89000,0
|
||||
15750839,Female,27,96000,1
|
||||
15749130,Female,41,30000,0
|
||||
15779862,Male,29,61000,0
|
||||
15767871,Male,20,74000,0
|
||||
15679651,Female,26,15000,0
|
||||
15576219,Male,41,45000,0
|
||||
15699247,Male,31,76000,0
|
||||
15619087,Female,36,50000,0
|
||||
15605327,Male,40,47000,0
|
||||
15610140,Female,31,15000,0
|
||||
15791174,Male,46,59000,0
|
||||
15602373,Male,29,75000,0
|
||||
15762605,Male,26,30000,0
|
||||
15598840,Female,32,135000,1
|
||||
15744279,Male,32,100000,1
|
||||
15670619,Male,25,90000,0
|
||||
15599533,Female,37,33000,0
|
||||
15757837,Male,35,38000,0
|
||||
15697574,Female,33,69000,0
|
||||
15578738,Female,18,86000,0
|
||||
15762228,Female,22,55000,0
|
||||
15614827,Female,35,71000,0
|
||||
15789815,Male,29,148000,1
|
||||
15579781,Female,29,47000,0
|
||||
15587013,Male,21,88000,0
|
||||
15570932,Male,34,115000,0
|
||||
15794661,Female,26,118000,0
|
||||
15581654,Female,34,43000,0
|
||||
15644296,Female,34,72000,0
|
||||
15614420,Female,23,28000,0
|
||||
15609653,Female,35,47000,0
|
||||
15594577,Male,25,22000,0
|
||||
15584114,Male,24,23000,0
|
||||
15673367,Female,31,34000,0
|
||||
15685576,Male,26,16000,0
|
||||
15774727,Female,31,71000,0
|
||||
15694288,Female,32,117000,1
|
||||
15603319,Male,33,43000,0
|
||||
15759066,Female,33,60000,0
|
||||
15814816,Male,31,66000,0
|
||||
15724402,Female,20,82000,0
|
||||
15571059,Female,33,41000,0
|
||||
15674206,Male,35,72000,0
|
||||
15715160,Male,28,32000,0
|
||||
15730448,Male,24,84000,0
|
||||
15662067,Female,19,26000,0
|
||||
15779581,Male,29,43000,0
|
||||
15662901,Male,19,70000,0
|
||||
15689751,Male,28,89000,0
|
||||
15667742,Male,34,43000,0
|
||||
15738448,Female,30,79000,0
|
||||
15680243,Female,20,36000,0
|
||||
15745083,Male,26,80000,0
|
||||
15708228,Male,35,22000,0
|
||||
15628523,Male,35,39000,0
|
||||
15708196,Male,49,74000,0
|
||||
15735549,Female,39,134000,1
|
||||
15809347,Female,41,71000,0
|
||||
15660866,Female,58,101000,1
|
||||
15766609,Female,47,47000,0
|
||||
15654230,Female,55,130000,1
|
||||
15794566,Female,52,114000,0
|
||||
15800890,Female,40,142000,1
|
||||
15697424,Female,46,22000,0
|
||||
15724536,Female,48,96000,1
|
||||
15735878,Male,52,150000,1
|
||||
15707596,Female,59,42000,0
|
||||
15657163,Male,35,58000.0,0
|
||||
15622478,Male,47.0,43000,0
|
||||
15779529,Female,60,108000,1
|
||||
15636023,Male,49,65000,0
|
||||
15582066,Male,40,78000,0
|
||||
15666675,Female,46,96000,0
|
||||
15732987,Male,59,143000,1
|
||||
15789432,Female,41,80000,0
|
||||
15663161,Male,35,91000,1
|
||||
15694879,Male,37,144000,1
|
||||
15593715,Male,60,102000,1
|
||||
15575002,Female,35,60000,0
|
||||
15622171,Male,37,53000,0
|
||||
15795224,Female,36,126000,1
|
||||
15685346,Male,56,133000,1
|
||||
15691808,Female,40,72000,0
|
||||
15721007,Female,42,80000,1
|
||||
15794253,Female,35,147000,1
|
||||
15694453,Male,39,42000,0
|
||||
15813113,Male,40,107000,1
|
||||
15614187,Male,49,86000,1
|
||||
15619407,Female,38,112000,0
|
||||
15646227,Male,46,79000,1
|
||||
15660541,Male,40,57000,0
|
||||
15753874,Female,37,80000,0
|
||||
15617877,Female,46,82000,0
|
||||
15772073,Female,53,143000,1
|
||||
15701537,Male,42,149000,1
|
||||
15736228,Male,38,59000,0
|
||||
15780572,Female,50,88000,1
|
||||
15769596,Female,56,104000,1
|
||||
15586996,Female,41,72000,0
|
||||
15722061,Female,51,146000,1
|
||||
15638003,Female,35,50000,0
|
||||
15775590,Female,57,122000,1
|
||||
15730688,Male,41,52000,0
|
||||
15753102,Female,35,97000,1
|
||||
15810075,Female,44,39000,0
|
||||
15723373,Male,37,52000,0
|
||||
15795298,Female,48,134000,1
|
||||
15584320,Female,37,146000,1
|
||||
15724161,Female,50,44000,0
|
||||
15750056,Female,52,90000,1
|
||||
15609637,Female,41,72000,0
|
||||
15794493,Male,40,57000,0
|
||||
15569641,Female,58,95000,1
|
||||
15815236,Female,45,131000,1
|
||||
15811177,Female,35,77000,0
|
||||
15680587,Male,36,144000,1
|
||||
15672821,Female,55,125000,1
|
||||
15767681,Female,35,72000,0
|
||||
15600379,Male,48,90000,1
|
||||
15801336,Female,42,108000,1
|
||||
15721592,Male,40,75000,0
|
||||
15581282,Male,37,74000,0
|
||||
15746203,Female,47,144000,1
|
||||
15583137,Male,40,61000,0
|
||||
15680752,Female,43,133000,0
|
||||
15688172,Female,59,76000,1
|
||||
15791373,Male,60,42000,1
|
||||
15589449,Male,39,106000,1
|
||||
15692819,Female,57,26000,1
|
||||
15727467,Male,57,74000,1
|
||||
15734312,Male,38,71000,0
|
||||
15764604,Male,49,88000,1
|
||||
15613014,Female,52,38000,1
|
||||
15759684,Female,50,36000,1
|
||||
15609669,Female,59,88000,1
|
||||
15685536,Male,35,61000,0
|
||||
15750447,Male,37,70000,1
|
||||
15663249,Female,52,21000,1
|
||||
15638646,Male,48,141000,0
|
||||
15734161,Female,37,93000,1
|
||||
15631070,Female,37,62000,0
|
||||
15761950,Female,48,138000,1
|
||||
15649668,Male,41,79000,0
|
||||
15713912,Female,37,78000,1
|
||||
15586757,Male,39,134000,1
|
||||
15596522,Male,49,89000,1
|
||||
15625395,Male,55,39000,1
|
||||
15760570,Male,37,77000,0
|
||||
15566689,Female,35,57000,0
|
||||
15725794,Female,36,63000,0
|
||||
15673539,Male,42,73000,1
|
||||
15705298,Female,43,112000,1
|
||||
15675791,Male,45,79000,0
|
||||
15747043,Male,46,117000,1
|
||||
15736397,Female,58,38000,1
|
||||
15678201,Male,48,74000,1
|
||||
15720745,Female,37,137000,1
|
||||
15637593,Male,37,79000,1
|
||||
15598070,Female,40,60000,0
|
||||
15787550,Male,42,54000,0
|
||||
15603942,Female,51,134000,0
|
||||
15733973,Female,47,113000,1
|
||||
15596761,Male,36,125000,1
|
||||
15652400,Female,38,50000,0
|
||||
15717893,Female,42,70000,0
|
||||
15622585,Male,39,96000,1
|
||||
15733964,Female,38,50000,0
|
||||
15753861,Female,49,141000,1
|
||||
15747097,Female,39,79000,0
|
||||
15594762,Female,39,75000,1
|
||||
15667417,Female,54,104000,1
|
||||
15684861,Male,35,55000,0
|
||||
15742204,Male,45,32000,1
|
||||
15623502,Male,36,60000,0
|
||||
15774872,Female,52,138000,1
|
||||
15611191,Female,53,82000,1
|
||||
15674331,Male,41,52000,0
|
||||
15619465,Female,48,30000,1
|
||||
15575247,Female,48,131000,1
|
||||
15695679,Female,41,60000,0
|
||||
15713463,Male,41,72000,0
|
||||
15785170,Female,42,75000,0
|
||||
15796351,Male,36,118000,1
|
||||
15639576,Female,47,107000,1
|
||||
15693264,Male,38,51000,0
|
||||
15589715,Female,48,119000,1
|
||||
15769902,Male,42,65000,0
|
||||
15587177,Male,40,65000,0
|
||||
15814553,Male,57,60000,1
|
||||
15601550,Female,36,54000,0
|
||||
15664907,Male,58,144000,1
|
||||
15612465,Male,35,79000,0
|
||||
15810800,Female,38,55000,0
|
||||
15665760,Male,39,122000,1
|
||||
15588080,Female,53,104000,1
|
||||
15776844,Male,35,75000,0
|
||||
15717560,Female,38,65000,0
|
||||
15629739,Female,47,51000,1
|
||||
15729908,Male,47,105000,1
|
||||
15716781,Female,41,63000,0
|
||||
15646936,Male,53,72000,1
|
||||
15768151,Female,54,108000,1
|
||||
15579212,Male,39,77000,0
|
||||
15721835,Male,38,61000,0
|
||||
15800515,Female,38,113000,1
|
||||
15591279,Male,37,75000,0
|
||||
15587419,Female,42,90000,1
|
||||
15750335,Female,37,57000,0
|
||||
15699619,Male,36,99000,1
|
||||
15606472,Male,60,34000,1
|
||||
15778368,Male,54,70000,1
|
||||
15671387,Female,41,72000,0
|
||||
15573926,Male,40,71000,1
|
||||
15709183,Male,42,54000,0
|
||||
15577514,Male,43,129000,1
|
||||
15778830,Female,53,34000,1
|
||||
15768072,Female,47,50000,1
|
||||
15768293,Female,42,79000,0
|
||||
15654456,Male,42,104000,1
|
||||
15807525,Female,59,29000,1
|
||||
15574372,Female,58,47000,1
|
||||
15671249,Male,46,88000,1
|
||||
15779744,Male,38,71000,0
|
||||
15624755,Female,54,26000,1
|
||||
15611430,Female,60,46000,1
|
||||
15774744,Male,60,83000,1
|
||||
15629885,Female,39,73000,0
|
||||
15708791,Male,59,130000,1
|
||||
15793890,Female,37,80000,0
|
||||
15646091,Female,46,32000,1
|
||||
15596984,Female,46,74000,0
|
||||
15800215,Female,42,53000,0
|
||||
15577806,Male,41,87000,1
|
||||
15749381,Female,58,23000,1
|
||||
15683758,Male,42,64000,0
|
||||
15670615,Male,48,33000,1
|
||||
15715622,Female,44,139000,1
|
||||
15707634,Male,49,28000,1
|
||||
15806901,Female,57,33000,1
|
||||
15775335,Male,56,60000,1
|
||||
15724150,Female,49,39000,1
|
||||
15627220,Male,39,71000,0
|
||||
15672330,Male,47,34000,1
|
||||
15668521,Female,48,35000,1
|
||||
15807837,Male,48,33000,1
|
||||
15592570,Male,47,23000,1
|
||||
15748589,Female,45,45000,1
|
||||
15635893,Male,60,42000,1
|
||||
15757632,Female,39,59000,0
|
||||
15691863,Female,46,41000,1
|
||||
15706071,Male,51,23000,1
|
||||
15654296,Female,50,20000,1
|
||||
15755018,Male,36,33000,0
|
||||
15594041,Female,49,36000,1
|
|
56
section_3_classification/section_3_6_random_forest/main.py
Normal file
56
section_3_classification/section_3_6_random_forest/main.py
Normal file
@ -0,0 +1,56 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Social_Network_Ads.csv")
|
||||
X = dataset.iloc[:, [2,3]].values
|
||||
Y = dataset.iloc[:, 4].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
classifier = RandomForestClassifier(n_estimators=10, random_state=0, criterion='entropy')
|
||||
classifier.fit(X_train, Y_train)
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_test, Y_test
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue'))(i), label = j)
|
||||
|
||||
plt.title('Random Forest (Training set)')
|
||||
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
201
section_4_K-means_clustering/Mall_Customers.csv
Normal file
201
section_4_K-means_clustering/Mall_Customers.csv
Normal file
@ -0,0 +1,201 @@
|
||||
CustomerID,Genre,Age,Annual Income (k$),Spending Score (1-100)
|
||||
0001,Male,19,15,39
|
||||
0002,Male,21,15,81
|
||||
0003,Female,20,16,6
|
||||
0004,Female,23,16,77
|
||||
0005,Female,31,17,40
|
||||
0006,Female,22,17,76
|
||||
0007,Female,35,18,6
|
||||
0008,Female,23,18,94
|
||||
0009,Male,64,19,3
|
||||
0010,Female,30,19,72
|
||||
0011,Male,67,19,14
|
||||
0012,Female,35,19,99
|
||||
0013,Female,58,20,15
|
||||
0014,Female,24,20,77
|
||||
0015,Male,37,20,13
|
||||
0016,Male,22,20,79
|
||||
0017,Female,35,21,35
|
||||
0018,Male,20,21,66
|
||||
0019,Male,52,23,29
|
||||
0020,Female,35,23,98
|
||||
0021,Male,35,24,35
|
||||
0022,Male,25,24,73
|
||||
0023,Female,46,25,5
|
||||
0024,Male,31,25,73
|
||||
0025,Female,54,28,14
|
||||
0026,Male,29,28,82
|
||||
0027,Female,45,28,32
|
||||
0028,Male,35,28,61
|
||||
0029,Female,40,29,31
|
||||
0030,Female,23,29,87
|
||||
0031,Male,60,30,4
|
||||
0032,Female,21,30,73
|
||||
0033,Male,53,33,4
|
||||
0034,Male,18,33,92
|
||||
0035,Female,49,33,14
|
||||
0036,Female,21,33,81
|
||||
0037,Female,42,34,17
|
||||
0038,Female,30,34,73
|
||||
0039,Female,36,37,26
|
||||
0040,Female,20,37,75
|
||||
0041,Female,65,38,35
|
||||
0042,Male,24,38,92
|
||||
0043,Male,48,39,36
|
||||
0044,Female,31,39,61
|
||||
0045,Female,49,39,28
|
||||
0046,Female,24,39,65
|
||||
0047,Female,50,40,55
|
||||
0048,Female,27,40,47
|
||||
0049,Female,29,40,42
|
||||
0050,Female,31,40,42
|
||||
0051,Female,49,42,52
|
||||
0052,Male,33,42,60
|
||||
0053,Female,31,43,54
|
||||
0054,Male,59,43,60
|
||||
0055,Female,50,43,45
|
||||
0056,Male,47,43,41
|
||||
0057,Female,51,44,50
|
||||
0058,Male,69,44,46
|
||||
0059,Female,27,46,51
|
||||
0060,Male,53,46,46
|
||||
0061,Male,70,46,56
|
||||
0062,Male,19,46,55
|
||||
0063,Female,67,47,52
|
||||
0064,Female,54,47,59
|
||||
0065,Male,63,48,51
|
||||
0066,Male,18,48,59
|
||||
0067,Female,43,48,50
|
||||
0068,Female,68,48,48
|
||||
0069,Male,19,48,59
|
||||
0070,Female,32,48,47
|
||||
0071,Male,70,49,55
|
||||
0072,Female,47,49,42
|
||||
0073,Female,60,50,49
|
||||
0074,Female,60,50,56
|
||||
0075,Male,59,54,47
|
||||
0076,Male,26,54,54
|
||||
0077,Female,45,54,53
|
||||
0078,Male,40,54,48
|
||||
0079,Female,23,54,52
|
||||
0080,Female,49,54,42
|
||||
0081,Male,57,54,51
|
||||
0082,Male,38,54,55
|
||||
0083,Male,67,54,41
|
||||
0084,Female,46,54,44
|
||||
0085,Female,21,54,57
|
||||
0086,Male,48,54,46
|
||||
0087,Female,55,57,58
|
||||
0088,Female,22,57,55
|
||||
0089,Female,34,58,60
|
||||
0090,Female,50,58,46
|
||||
0091,Female,68,59,55
|
||||
0092,Male,18,59,41
|
||||
0093,Male,48,60,49
|
||||
0094,Female,40,60,40
|
||||
0095,Female,32,60,42
|
||||
0096,Male,24,60,52
|
||||
0097,Female,47,60,47
|
||||
0098,Female,27,60,50
|
||||
0099,Male,48,61,42
|
||||
0100,Male,20,61,49
|
||||
0101,Female,23,62,41
|
||||
0102,Female,49,62,48
|
||||
0103,Male,67,62,59
|
||||
0104,Male,26,62,55
|
||||
0105,Male,49,62,56
|
||||
0106,Female,21,62,42
|
||||
0107,Female,66,63,50
|
||||
0108,Male,54,63,46
|
||||
0109,Male,68,63,43
|
||||
0110,Male,66,63,48
|
||||
0111,Male,65,63,52
|
||||
0112,Female,19,63,54
|
||||
0113,Female,38,64,42
|
||||
0114,Male,19,64,46
|
||||
0115,Female,18,65,48
|
||||
0116,Female,19,65,50
|
||||
0117,Female,63,65,43
|
||||
0118,Female,49,65,59
|
||||
0119,Female,51,67,43
|
||||
0120,Female,50,67,57
|
||||
0121,Male,27,67,56
|
||||
0122,Female,38,67,40
|
||||
0123,Female,40,69,58
|
||||
0124,Male,39,69,91
|
||||
0125,Female,23,70,29
|
||||
0126,Female,31,70,77
|
||||
0127,Male,43,71,35
|
||||
0128,Male,40,71,95
|
||||
0129,Male,59,71,11
|
||||
0130,Male,38,71,75
|
||||
0131,Male,47,71,9
|
||||
0132,Male,39,71,75
|
||||
0133,Female,25,72,34
|
||||
0134,Female,31,72,71
|
||||
0135,Male,20,73,5
|
||||
0136,Female,29,73,88
|
||||
0137,Female,44,73,7
|
||||
0138,Male,32,73,73
|
||||
0139,Male,19,74,10
|
||||
0140,Female,35,74,72
|
||||
0141,Female,57,75,5
|
||||
0142,Male,32,75,93
|
||||
0143,Female,28,76,40
|
||||
0144,Female,32,76,87
|
||||
0145,Male,25,77,12
|
||||
0146,Male,28,77,97
|
||||
0147,Male,48,77,36
|
||||
0148,Female,32,77,74
|
||||
0149,Female,34,78,22
|
||||
0150,Male,34,78,90
|
||||
0151,Male,43,78,17
|
||||
0152,Male,39,78,88
|
||||
0153,Female,44,78,20
|
||||
0154,Female,38,78,76
|
||||
0155,Female,47,78,16
|
||||
0156,Female,27,78,89
|
||||
0157,Male,37,78,1
|
||||
0158,Female,30,78,78
|
||||
0159,Male,34,78,1
|
||||
0160,Female,30,78,73
|
||||
0161,Female,56,79,35
|
||||
0162,Female,29,79,83
|
||||
0163,Male,19,81,5
|
||||
0164,Female,31,81,93
|
||||
0165,Male,50,85,26
|
||||
0166,Female,36,85,75
|
||||
0167,Male,42,86,20
|
||||
0168,Female,33,86,95
|
||||
0169,Female,36,87,27
|
||||
0170,Male,32,87,63
|
||||
0171,Male,40,87,13
|
||||
0172,Male,28,87,75
|
||||
0173,Male,36,87,10
|
||||
0174,Male,36,87,92
|
||||
0175,Female,52,88,13
|
||||
0176,Female,30,88,86
|
||||
0177,Male,58,88,15
|
||||
0178,Male,27,88,69
|
||||
0179,Male,59,93,14
|
||||
0180,Male,35,93,90
|
||||
0181,Female,37,97,32
|
||||
0182,Female,32,97,86
|
||||
0183,Male,46,98,15
|
||||
0184,Female,29,98,88
|
||||
0185,Female,41,99,39
|
||||
0186,Male,30,99,97
|
||||
0187,Female,54,101,24
|
||||
0188,Male,28,101,68
|
||||
0189,Female,41,103,17
|
||||
0190,Female,36,103,85
|
||||
0191,Female,34,103,23
|
||||
0192,Female,32,103,69
|
||||
0193,Male,33,113,8
|
||||
0194,Female,38,113,91
|
||||
0195,Female,47,120,16
|
||||
0196,Female,35,120,79
|
||||
0197,Female,45,126,28
|
||||
0198,Male,32,126,74
|
||||
0199,Male,32,137,18
|
||||
0200,Male,30,137,83
|
|
52
section_4_K-means_clustering/main.py
Normal file
52
section_4_K-means_clustering/main.py
Normal file
@ -0,0 +1,52 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Mall_Customers.csv")
|
||||
X = dataset.iloc[:, 3:5].values # 無監督學習,無需應變數
|
||||
|
||||
# 獲取 K,得到結果 K = 5
|
||||
from sklearn.cluster import KMeans
|
||||
# wcss = []
|
||||
#
|
||||
# for i in range(1, 11):
|
||||
# kmeans = KMeans(n_clusters=i, max_iter=300, n_init=10, init='k-means++', random_state=0)
|
||||
# kmeans.fit(X)
|
||||
# wcss.append(kmeans.inertia_)
|
||||
#
|
||||
# plt.plot(range(1, 11), wcss)
|
||||
# plt.title('The Elbow Method')
|
||||
# plt.xlabel('Number of clusters')
|
||||
# plt.ylabel('WCSS')
|
||||
# plt.show()
|
||||
|
||||
# 開始分析
|
||||
kmeans = KMeans(n_clusters=5, max_iter=300, n_init=10, init='k-means++', random_state=0)
|
||||
|
||||
Y_kmeans = kmeans.fit_predict(X)
|
||||
|
||||
print(Y_kmeans)
|
||||
|
||||
# 視覺化
|
||||
plt.scatter(X[Y_kmeans == 0, 0], X[Y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')
|
||||
|
||||
plt.scatter(X[Y_kmeans == 1, 0], X[Y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
|
||||
|
||||
plt.scatter(X[Y_kmeans == 2, 0], X[Y_kmeans == 2, 1], s = 100, c = 'green', label = 'Cluster 3')
|
||||
|
||||
plt.scatter(X[Y_kmeans == 3, 0], X[Y_kmeans == 3, 1], s = 100, c = 'cyan', label = 'Cluster 4')
|
||||
|
||||
plt.scatter(X[Y_kmeans == 4, 0], X[Y_kmeans == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5')
|
||||
|
||||
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 300, c = 'yellow', label = 'Centroids')
|
||||
|
||||
plt.title('Clusters of customers')
|
||||
|
||||
plt.xlabel('Annual Income (k$)')
|
||||
|
||||
plt.ylabel('Spending Score (1-100)')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
7501
section_5_apriori/Market_Basket_Optimisation.csv
Normal file
7501
section_5_apriori/Market_Basket_Optimisation.csv
Normal file
File diff suppressed because it is too large
Load Diff
441
section_5_apriori/apyori.py
Executable file
441
section_5_apriori/apyori.py
Executable file
@ -0,0 +1,441 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
a simple implementation of Apriori algorithm by Python.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import csv
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from collections import namedtuple
|
||||
from itertools import combinations
|
||||
from itertools import chain
|
||||
|
||||
|
||||
# Meta informations.
|
||||
__version__ = '1.1.1'
|
||||
__author__ = 'Yu Mochizuki'
|
||||
__author_email__ = 'ymoch.dev@gmail.com'
|
||||
|
||||
|
||||
################################################################################
|
||||
# Data structures.
|
||||
################################################################################
|
||||
class TransactionManager(object):
|
||||
"""
|
||||
Transaction managers.
|
||||
"""
|
||||
|
||||
def __init__(self, transactions):
|
||||
"""
|
||||
Initialize.
|
||||
|
||||
Arguments:
|
||||
transactions -- A transaction iterable object
|
||||
(eg. [['A', 'B'], ['B', 'C']]).
|
||||
"""
|
||||
self.__num_transaction = 0
|
||||
self.__items = []
|
||||
self.__transaction_index_map = {}
|
||||
|
||||
for transaction in transactions:
|
||||
self.add_transaction(transaction)
|
||||
|
||||
def add_transaction(self, transaction):
|
||||
"""
|
||||
Add a transaction.
|
||||
|
||||
Arguments:
|
||||
transaction -- A transaction as an iterable object (eg. ['A', 'B']).
|
||||
"""
|
||||
for item in transaction:
|
||||
if item not in self.__transaction_index_map:
|
||||
self.__items.append(item)
|
||||
self.__transaction_index_map[item] = set()
|
||||
self.__transaction_index_map[item].add(self.__num_transaction)
|
||||
self.__num_transaction += 1
|
||||
|
||||
def calc_support(self, items):
|
||||
"""
|
||||
Returns a support for items.
|
||||
|
||||
Arguments:
|
||||
items -- Items as an iterable object (eg. ['A', 'B']).
|
||||
"""
|
||||
# Empty items is supported by all transactions.
|
||||
if not items:
|
||||
return 1.0
|
||||
|
||||
# Empty transactions supports no items.
|
||||
if not self.num_transaction:
|
||||
return 0.0
|
||||
|
||||
# Create the transaction index intersection.
|
||||
sum_indexes = None
|
||||
for item in items:
|
||||
indexes = self.__transaction_index_map.get(item)
|
||||
if indexes is None:
|
||||
# No support for any set that contains a not existing item.
|
||||
return 0.0
|
||||
|
||||
if sum_indexes is None:
|
||||
# Assign the indexes on the first time.
|
||||
sum_indexes = indexes
|
||||
else:
|
||||
# Calculate the intersection on not the first time.
|
||||
sum_indexes = sum_indexes.intersection(indexes)
|
||||
|
||||
# Calculate and return the support.
|
||||
return float(len(sum_indexes)) / self.__num_transaction
|
||||
|
||||
def initial_candidates(self):
|
||||
"""
|
||||
Returns the initial candidates.
|
||||
"""
|
||||
return [frozenset([item]) for item in self.items]
|
||||
|
||||
@property
|
||||
def num_transaction(self):
|
||||
"""
|
||||
Returns the number of transactions.
|
||||
"""
|
||||
return self.__num_transaction
|
||||
|
||||
@property
|
||||
def items(self):
|
||||
"""
|
||||
Returns the item list that the transaction is consisted of.
|
||||
"""
|
||||
return sorted(self.__items)
|
||||
|
||||
@staticmethod
|
||||
def create(transactions):
|
||||
"""
|
||||
Create the TransactionManager with a transaction instance.
|
||||
If the given instance is a TransactionManager, this returns itself.
|
||||
"""
|
||||
if isinstance(transactions, TransactionManager):
|
||||
return transactions
|
||||
return TransactionManager(transactions)
|
||||
|
||||
|
||||
# Ignore name errors because these names are namedtuples.
|
||||
SupportRecord = namedtuple( # pylint: disable=C0103
|
||||
'SupportRecord', ('items', 'support'))
|
||||
RelationRecord = namedtuple( # pylint: disable=C0103
|
||||
'RelationRecord', SupportRecord._fields + ('ordered_statistics',))
|
||||
OrderedStatistic = namedtuple( # pylint: disable=C0103
|
||||
'OrderedStatistic', ('items_base', 'items_add', 'confidence', 'lift',))
|
||||
|
||||
|
||||
################################################################################
|
||||
# Inner functions.
|
||||
################################################################################
|
||||
def create_next_candidates(prev_candidates, length):
|
||||
"""
|
||||
Returns the apriori candidates as a list.
|
||||
|
||||
Arguments:
|
||||
prev_candidates -- Previous candidates as a list.
|
||||
length -- The lengths of the next candidates.
|
||||
"""
|
||||
# Solve the items.
|
||||
item_set = set()
|
||||
for candidate in prev_candidates:
|
||||
for item in candidate:
|
||||
item_set.add(item)
|
||||
items = sorted(item_set)
|
||||
|
||||
# Create the temporary candidates. These will be filtered below.
|
||||
tmp_next_candidates = (frozenset(x) for x in combinations(items, length))
|
||||
|
||||
# Return all the candidates if the length of the next candidates is 2
|
||||
# because their subsets are the same as items.
|
||||
if length < 3:
|
||||
return list(tmp_next_candidates)
|
||||
|
||||
# Filter candidates that all of their subsets are
|
||||
# in the previous candidates.
|
||||
next_candidates = [
|
||||
candidate for candidate in tmp_next_candidates
|
||||
if all(
|
||||
True if frozenset(x) in prev_candidates else False
|
||||
for x in combinations(candidate, length - 1))
|
||||
]
|
||||
return next_candidates
|
||||
|
||||
|
||||
def gen_support_records(transaction_manager, min_support, **kwargs):
|
||||
"""
|
||||
Returns a generator of support records with given transactions.
|
||||
|
||||
Arguments:
|
||||
transaction_manager -- Transactions as a TransactionManager instance.
|
||||
min_support -- A minimum support (float).
|
||||
|
||||
Keyword arguments:
|
||||
max_length -- The maximum length of relations (integer).
|
||||
"""
|
||||
# Parse arguments.
|
||||
max_length = kwargs.get('max_length')
|
||||
|
||||
# For testing.
|
||||
_create_next_candidates = kwargs.get(
|
||||
'_create_next_candidates', create_next_candidates)
|
||||
|
||||
# Process.
|
||||
candidates = transaction_manager.initial_candidates()
|
||||
length = 1
|
||||
while candidates:
|
||||
relations = set()
|
||||
for relation_candidate in candidates:
|
||||
support = transaction_manager.calc_support(relation_candidate)
|
||||
if support < min_support:
|
||||
continue
|
||||
candidate_set = frozenset(relation_candidate)
|
||||
relations.add(candidate_set)
|
||||
yield SupportRecord(candidate_set, support)
|
||||
length += 1
|
||||
if max_length and length > max_length:
|
||||
break
|
||||
candidates = _create_next_candidates(relations, length)
|
||||
|
||||
|
||||
def gen_ordered_statistics(transaction_manager, record):
|
||||
"""
|
||||
Returns a generator of ordered statistics as OrderedStatistic instances.
|
||||
|
||||
Arguments:
|
||||
transaction_manager -- Transactions as a TransactionManager instance.
|
||||
record -- A support record as a SupportRecord instance.
|
||||
"""
|
||||
items = record.items
|
||||
for combination_set in combinations(sorted(items), len(items) - 1):
|
||||
items_base = frozenset(combination_set)
|
||||
items_add = frozenset(items.difference(items_base))
|
||||
confidence = (
|
||||
record.support / transaction_manager.calc_support(items_base))
|
||||
lift = confidence / transaction_manager.calc_support(items_add)
|
||||
yield OrderedStatistic(
|
||||
frozenset(items_base), frozenset(items_add), confidence, lift)
|
||||
|
||||
|
||||
def filter_ordered_statistics(ordered_statistics, **kwargs):
|
||||
"""
|
||||
Filter OrderedStatistic objects.
|
||||
|
||||
Arguments:
|
||||
ordered_statistics -- A OrderedStatistic iterable object.
|
||||
|
||||
Keyword arguments:
|
||||
min_confidence -- The minimum confidence of relations (float).
|
||||
min_lift -- The minimum lift of relations (float).
|
||||
"""
|
||||
min_confidence = kwargs.get('min_confidence', 0.0)
|
||||
min_lift = kwargs.get('min_lift', 0.0)
|
||||
|
||||
for ordered_statistic in ordered_statistics:
|
||||
if ordered_statistic.confidence < min_confidence:
|
||||
continue
|
||||
if ordered_statistic.lift < min_lift:
|
||||
continue
|
||||
yield ordered_statistic
|
||||
|
||||
|
||||
################################################################################
|
||||
# API function.
|
||||
################################################################################
|
||||
def apriori(transactions, **kwargs):
|
||||
"""
|
||||
Executes Apriori algorithm and returns a RelationRecord generator.
|
||||
|
||||
Arguments:
|
||||
transactions -- A transaction iterable object
|
||||
(eg. [['A', 'B'], ['B', 'C']]).
|
||||
|
||||
Keyword arguments:
|
||||
min_support -- The minimum support of relations (float).
|
||||
min_confidence -- The minimum confidence of relations (float).
|
||||
min_lift -- The minimum lift of relations (float).
|
||||
max_length -- The maximum length of the relation (integer).
|
||||
"""
|
||||
# Parse the arguments.
|
||||
min_support = kwargs.get('min_support', 0.1)
|
||||
min_confidence = kwargs.get('min_confidence', 0.0)
|
||||
min_lift = kwargs.get('min_lift', 0.0)
|
||||
max_length = kwargs.get('max_length', None)
|
||||
|
||||
# Check arguments.
|
||||
if min_support <= 0:
|
||||
raise ValueError('minimum support must be > 0')
|
||||
|
||||
# For testing.
|
||||
_gen_support_records = kwargs.get(
|
||||
'_gen_support_records', gen_support_records)
|
||||
_gen_ordered_statistics = kwargs.get(
|
||||
'_gen_ordered_statistics', gen_ordered_statistics)
|
||||
_filter_ordered_statistics = kwargs.get(
|
||||
'_filter_ordered_statistics', filter_ordered_statistics)
|
||||
|
||||
# Calculate supports.
|
||||
transaction_manager = TransactionManager.create(transactions)
|
||||
support_records = _gen_support_records(
|
||||
transaction_manager, min_support, max_length=max_length)
|
||||
|
||||
# Calculate ordered stats.
|
||||
for support_record in support_records:
|
||||
ordered_statistics = list(
|
||||
_filter_ordered_statistics(
|
||||
_gen_ordered_statistics(transaction_manager, support_record),
|
||||
min_confidence=min_confidence,
|
||||
min_lift=min_lift,
|
||||
)
|
||||
)
|
||||
if not ordered_statistics:
|
||||
continue
|
||||
yield RelationRecord(
|
||||
support_record.items, support_record.support, ordered_statistics)
|
||||
|
||||
|
||||
################################################################################
|
||||
# Application functions.
|
||||
################################################################################
|
||||
def parse_args(argv):
|
||||
"""
|
||||
Parse commandline arguments.
|
||||
|
||||
Arguments:
|
||||
argv -- An argument list without the program name.
|
||||
"""
|
||||
output_funcs = {
|
||||
'json': dump_as_json,
|
||||
'tsv': dump_as_two_item_tsv,
|
||||
}
|
||||
default_output_func_key = 'json'
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'-v', '--version', action='version',
|
||||
version='%(prog)s {0}'.format(__version__))
|
||||
parser.add_argument(
|
||||
'input', metavar='inpath', nargs='*',
|
||||
help='Input transaction file (default: stdin).',
|
||||
type=argparse.FileType('r'), default=[sys.stdin])
|
||||
parser.add_argument(
|
||||
'-o', '--output', metavar='outpath',
|
||||
help='Output file (default: stdout).',
|
||||
type=argparse.FileType('w'), default=sys.stdout)
|
||||
parser.add_argument(
|
||||
'-l', '--max-length', metavar='int',
|
||||
help='Max length of relations (default: infinite).',
|
||||
type=int, default=None)
|
||||
parser.add_argument(
|
||||
'-s', '--min-support', metavar='float',
|
||||
help='Minimum support ratio (must be > 0, default: 0.1).',
|
||||
type=float, default=0.1)
|
||||
parser.add_argument(
|
||||
'-c', '--min-confidence', metavar='float',
|
||||
help='Minimum confidence (default: 0.5).',
|
||||
type=float, default=0.5)
|
||||
parser.add_argument(
|
||||
'-t', '--min-lift', metavar='float',
|
||||
help='Minimum lift (default: 0.0).',
|
||||
type=float, default=0.0)
|
||||
parser.add_argument(
|
||||
'-d', '--delimiter', metavar='str',
|
||||
help='Delimiter for items of transactions (default: tab).',
|
||||
type=str, default='\t')
|
||||
parser.add_argument(
|
||||
'-f', '--out-format', metavar='str',
|
||||
help='Output format ({0}; default: {1}).'.format(
|
||||
', '.join(output_funcs.keys()), default_output_func_key),
|
||||
type=str, choices=output_funcs.keys(), default=default_output_func_key)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
args.output_func = output_funcs[args.out_format]
|
||||
return args
|
||||
|
||||
|
||||
def load_transactions(input_file, **kwargs):
|
||||
"""
|
||||
Load transactions and returns a generator for transactions.
|
||||
|
||||
Arguments:
|
||||
input_file -- An input file.
|
||||
|
||||
Keyword arguments:
|
||||
delimiter -- The delimiter of the transaction.
|
||||
"""
|
||||
delimiter = kwargs.get('delimiter', '\t')
|
||||
for transaction in csv.reader(input_file, delimiter=delimiter):
|
||||
yield transaction if transaction else ['']
|
||||
|
||||
|
||||
def dump_as_json(record, output_file):
|
||||
"""
|
||||
Dump an relation record as a json value.
|
||||
|
||||
Arguments:
|
||||
record -- A RelationRecord instance to dump.
|
||||
output_file -- A file to output.
|
||||
"""
|
||||
def default_func(value):
|
||||
"""
|
||||
Default conversion for JSON value.
|
||||
"""
|
||||
if isinstance(value, frozenset):
|
||||
return sorted(value)
|
||||
raise TypeError(repr(value) + " is not JSON serializable")
|
||||
|
||||
converted_record = record._replace(
|
||||
ordered_statistics=[x._asdict() for x in record.ordered_statistics])
|
||||
json.dump(
|
||||
converted_record._asdict(), output_file,
|
||||
default=default_func, ensure_ascii=False)
|
||||
output_file.write(os.linesep)
|
||||
|
||||
|
||||
def dump_as_two_item_tsv(record, output_file):
|
||||
"""
|
||||
Dump a relation record as TSV only for 2 item relations.
|
||||
|
||||
Arguments:
|
||||
record -- A RelationRecord instance to dump.
|
||||
output_file -- A file to output.
|
||||
"""
|
||||
for ordered_stats in record.ordered_statistics:
|
||||
if len(ordered_stats.items_base) != 1:
|
||||
continue
|
||||
if len(ordered_stats.items_add) != 1:
|
||||
continue
|
||||
output_file.write('{0}\t{1}\t{2:.8f}\t{3:.8f}\t{4:.8f}{5}'.format(
|
||||
list(ordered_stats.items_base)[0], list(ordered_stats.items_add)[0],
|
||||
record.support, ordered_stats.confidence, ordered_stats.lift,
|
||||
os.linesep))
|
||||
|
||||
|
||||
def main(**kwargs):
|
||||
"""
|
||||
Executes Apriori algorithm and print its result.
|
||||
"""
|
||||
# For tests.
|
||||
_parse_args = kwargs.get('_parse_args', parse_args)
|
||||
_load_transactions = kwargs.get('_load_transactions', load_transactions)
|
||||
_apriori = kwargs.get('_apriori', apriori)
|
||||
|
||||
args = _parse_args(sys.argv[1:])
|
||||
transactions = _load_transactions(
|
||||
chain(*args.input), delimiter=args.delimiter)
|
||||
result = _apriori(
|
||||
transactions,
|
||||
max_length=args.max_length,
|
||||
min_support=args.min_support,
|
||||
min_confidence=args.min_confidence)
|
||||
for record in result:
|
||||
args.output_func(record, args.output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
18
section_5_apriori/main.py
Normal file
18
section_5_apriori/main.py
Normal file
@ -0,0 +1,18 @@
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
dataset = pd.read_csv('Market_Basket_Optimisation.csv', header=None)
|
||||
|
||||
transactions = []
|
||||
|
||||
for i in range(0, 7501):
|
||||
transactions.append([str(dataset.values[i, j]) for j in range(0, 20)])
|
||||
|
||||
from apyori import apriori
|
||||
|
||||
rules = apriori(transactions, min_lengt = 2, min_support=0.003, min_confidence=0.2, min_lift=3)
|
||||
|
||||
results = list(rules)
|
||||
|
||||
print(results)
|
10001
section_6_ucb/Ads_CTR_Optimisation.csv
Normal file
10001
section_6_ucb/Ads_CTR_Optimisation.csv
Normal file
File diff suppressed because it is too large
Load Diff
42
section_6_ucb/main.py
Normal file
42
section_6_ucb/main.py
Normal file
@ -0,0 +1,42 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
import math
|
||||
|
||||
dataset = pd.read_csv('Ads_CTR_Optimisation.csv')
|
||||
|
||||
# 第一步,計算 N_i(n) 和 R_i(n)
|
||||
N = 10000 # 輪數
|
||||
d = 10 # 廣告數
|
||||
numbers_of_selections = [0] * d # 每個廣告被選中的次數
|
||||
sums_of_rewards = [0] * d # 每個廣告的獎勵總和
|
||||
ads_selected = [] # 選擇的廣告
|
||||
total_reward = 0 # 總獎勵
|
||||
|
||||
# 第二步
|
||||
for n in range(0, N):
|
||||
ad = 0 # 最大 UCB 的廣告
|
||||
max_upper_bound = 0 # 最大 UCB
|
||||
for i in range(0, d):
|
||||
if numbers_of_selections[i] > 0:
|
||||
average_award = sums_of_rewards[i] / numbers_of_selections[i] # 平均獎勵
|
||||
delta_i = math.sqrt(3/2 * math.log(n + 1) / numbers_of_selections[i]) # Delta_i(n)
|
||||
upper_bound = average_award + delta_i # UCB
|
||||
else:
|
||||
upper_bound = 1e400
|
||||
if upper_bound > max_upper_bound:
|
||||
max_upper_bound = upper_bound
|
||||
ad = i # 選擇擁有最大 UCB 的廣告 i
|
||||
ads_selected.append(ad)
|
||||
reward = dataset.values[n, ad] # 獎勵
|
||||
numbers_of_selections[ad] = numbers_of_selections[ad] + 1
|
||||
sums_of_rewards[ad] = sums_of_rewards[ad] + reward
|
||||
total_reward = total_reward + reward
|
||||
|
||||
print(total_reward)
|
||||
|
||||
plt.hist(ads_selected)
|
||||
plt.title('Histogram of ads selections')
|
||||
plt.xlabel('Ads')
|
||||
plt.ylabel('Number of times each ad was selected')
|
||||
plt.show()
|
28
section_6_ucb/random_selection.py
Normal file
28
section_6_ucb/random_selection.py
Normal file
@ -0,0 +1,28 @@
|
||||
# Random Selection
|
||||
|
||||
# Importing the libraries
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
# Importing the dataset
|
||||
dataset = pd.read_csv('Ads_CTR_Optimisation.csv')
|
||||
|
||||
# Implementing Random Selection
|
||||
import random
|
||||
N = 10000
|
||||
d = 10
|
||||
ads_selected = []
|
||||
total_reward = 0
|
||||
for n in range(0, N):
|
||||
ad = random.randrange(d)
|
||||
ads_selected.append(ad)
|
||||
reward = dataset.values[n, ad]
|
||||
total_reward = total_reward + reward
|
||||
|
||||
# Visualising the results
|
||||
plt.hist(ads_selected)
|
||||
plt.title('Histogram of ads selections')
|
||||
plt.xlabel('Ads')
|
||||
plt.ylabel('Number of times each ad was selected')
|
||||
plt.show()
|
10001
section_7_thompson_sampling/Ads_CTR_Optimisation.csv
Normal file
10001
section_7_thompson_sampling/Ads_CTR_Optimisation.csv
Normal file
File diff suppressed because it is too large
Load Diff
44
section_7_thompson_sampling/main.py
Normal file
44
section_7_thompson_sampling/main.py
Normal file
@ -0,0 +1,44 @@
|
||||
import random
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
dataset = pd.read_csv('Ads_CTR_Optimisation.csv')
|
||||
|
||||
# 第一步,計算 N_i(n) 和 R_i(n)
|
||||
N = 10000 # 輪數
|
||||
d = 10 # 廣告數
|
||||
numbers_of_rewards_1 = [0] * d
|
||||
numbers_of_rewards_0 = [0] * d
|
||||
ads_selected = [] # 選擇的廣告
|
||||
total_reward = 0 # 總獎勵
|
||||
|
||||
# 第二步
|
||||
for n in range(0, N):
|
||||
ad = 0 # 最大 UCB 的廣告
|
||||
max_random = 0 # 最大 UCB
|
||||
for i in range(0, d):
|
||||
random_beta = random.betavariate(numbers_of_rewards_1[i] + 1, numbers_of_rewards_0[i] + 1)
|
||||
if random_beta > max_random:
|
||||
max_random = random_beta
|
||||
ad = i
|
||||
ads_selected.append(ad)
|
||||
reward = dataset.values[n, ad] # 獎勵
|
||||
if reward == 1:
|
||||
numbers_of_rewards_1[ad] += 1
|
||||
else:
|
||||
numbers_of_rewards_0[ad] += 1
|
||||
total_reward = total_reward + reward
|
||||
|
||||
print(total_reward)
|
||||
|
||||
plt.hist(ads_selected)
|
||||
|
||||
plt.title('Histogram of ads selections')
|
||||
|
||||
plt.xlabel('Ads')
|
||||
|
||||
plt.ylabel('Number of times each ad was selected')
|
||||
|
||||
plt.show()
|
1001
section_8_natural_language_processing/Restaurant_Reviews.tsv
Normal file
1001
section_8_natural_language_processing/Restaurant_Reviews.tsv
Normal file
File diff suppressed because it is too large
Load Diff
52
section_8_natural_language_processing/main.py
Normal file
52
section_8_natural_language_processing/main.py
Normal file
@ -0,0 +1,52 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
dataset = pd.read_csv("Restaurant_Reviews.tsv", delimiter="\t", quoting=3)
|
||||
|
||||
import re
|
||||
# import nltk
|
||||
from nltk.corpus import stopwords
|
||||
from nltk.stem.porter import PorterStemmer
|
||||
# nltk.download('stopwords')
|
||||
ps = PorterStemmer()
|
||||
|
||||
corpus = []
|
||||
for i in range(0, 1000):
|
||||
review = re.sub('[^a-zA-Z]', ' ', dataset['Review'][i])
|
||||
review = review.lower()
|
||||
review = review.split()
|
||||
review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))]
|
||||
review = ' '.join(review)
|
||||
corpus.append(review)
|
||||
|
||||
from sklearn.feature_extraction.text import CountVectorizer
|
||||
|
||||
cv = CountVectorizer(max_features=1500)
|
||||
|
||||
X = cv.fit_transform(corpus).toarray()
|
||||
|
||||
Y = dataset.iloc[:, 1].values
|
||||
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.20, random_state = 0)
|
||||
|
||||
from sklearn.naive_bayes import GaussianNB
|
||||
|
||||
classifier = GaussianNB()
|
||||
|
||||
classifier.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
print(Y_pred)
|
||||
|
||||
print(Y_test)
|
||||
|
||||
count = 0
|
||||
for i in range(0,len(Y_pred)):
|
||||
if Y_pred[i] == Y_test[i]:
|
||||
count += 1
|
||||
|
||||
print(count / len(Y_pred))
|
179
section_9_pca/Wine.csv
Normal file
179
section_9_pca/Wine.csv
Normal file
@ -0,0 +1,179 @@
|
||||
Alcohol,Malic_Acid,Ash,Ash_Alcanity,Magnesium,Total_Phenols,Flavanoids,Nonflavanoid_Phenols,Proanthocyanins,Color_Intensity,Hue,OD280,Proline,Customer_Segment
|
||||
14.23,1.71,2.43,15.6,127,2.8,3.06,0.28,2.29,5.64,1.04,3.92,1065,1
|
||||
13.2,1.78,2.14,11.2,100,2.65,2.76,0.26,1.28,4.38,1.05,3.4,1050,1
|
||||
13.16,2.36,2.67,18.6,101,2.8,3.24,0.3,2.81,5.68,1.03,3.17,1185,1
|
||||
14.37,1.95,2.5,16.8,113,3.85,3.49,0.24,2.18,7.8,0.86,3.45,1480,1
|
||||
13.24,2.59,2.87,21,118,2.8,2.69,0.39,1.82,4.32,1.04,2.93,735,1
|
||||
14.2,1.76,2.45,15.2,112,3.27,3.39,0.34,1.97,6.75,1.05,2.85,1450,1
|
||||
14.39,1.87,2.45,14.6,96,2.5,2.52,0.3,1.98,5.25,1.02,3.58,1290,1
|
||||
14.06,2.15,2.61,17.6,121,2.6,2.51,0.31,1.25,5.05,1.06,3.58,1295,1
|
||||
14.83,1.64,2.17,14,97,2.8,2.98,0.29,1.98,5.2,1.08,2.85,1045,1
|
||||
13.86,1.35,2.27,16,98,2.98,3.15,0.22,1.85,7.22,1.01,3.55,1045,1
|
||||
14.1,2.16,2.3,18,105,2.95,3.32,0.22,2.38,5.75,1.25,3.17,1510,1
|
||||
14.12,1.48,2.32,16.8,95,2.2,2.43,0.26,1.57,5,1.17,2.82,1280,1
|
||||
13.75,1.73,2.41,16,89,2.6,2.76,0.29,1.81,5.6,1.15,2.9,1320,1
|
||||
14.75,1.73,2.39,11.4,91,3.1,3.69,0.43,2.81,5.4,1.25,2.73,1150,1
|
||||
14.38,1.87,2.38,12,102,3.3,3.64,0.29,2.96,7.5,1.2,3,1547,1
|
||||
13.63,1.81,2.7,17.2,112,2.85,2.91,0.3,1.46,7.3,1.28,2.88,1310,1
|
||||
14.3,1.92,2.72,20,120,2.8,3.14,0.33,1.97,6.2,1.07,2.65,1280,1
|
||||
13.83,1.57,2.62,20,115,2.95,3.4,0.4,1.72,6.6,1.13,2.57,1130,1
|
||||
14.19,1.59,2.48,16.5,108,3.3,3.93,0.32,1.86,8.7,1.23,2.82,1680,1
|
||||
13.64,3.1,2.56,15.2,116,2.7,3.03,0.17,1.66,5.1,0.96,3.36,845,1
|
||||
14.06,1.63,2.28,16,126,3,3.17,0.24,2.1,5.65,1.09,3.71,780,1
|
||||
12.93,3.8,2.65,18.6,102,2.41,2.41,0.25,1.98,4.5,1.03,3.52,770,1
|
||||
13.71,1.86,2.36,16.6,101,2.61,2.88,0.27,1.69,3.8,1.11,4,1035,1
|
||||
12.85,1.6,2.52,17.8,95,2.48,2.37,0.26,1.46,3.93,1.09,3.63,1015,1
|
||||
13.5,1.81,2.61,20,96,2.53,2.61,0.28,1.66,3.52,1.12,3.82,845,1
|
||||
13.05,2.05,3.22,25,124,2.63,2.68,0.47,1.92,3.58,1.13,3.2,830,1
|
||||
13.39,1.77,2.62,16.1,93,2.85,2.94,0.34,1.45,4.8,0.92,3.22,1195,1
|
||||
13.3,1.72,2.14,17,94,2.4,2.19,0.27,1.35,3.95,1.02,2.77,1285,1
|
||||
13.87,1.9,2.8,19.4,107,2.95,2.97,0.37,1.76,4.5,1.25,3.4,915,1
|
||||
14.02,1.68,2.21,16,96,2.65,2.33,0.26,1.98,4.7,1.04,3.59,1035,1
|
||||
13.73,1.5,2.7,22.5,101,3,3.25,0.29,2.38,5.7,1.19,2.71,1285,1
|
||||
13.58,1.66,2.36,19.1,106,2.86,3.19,0.22,1.95,6.9,1.09,2.88,1515,1
|
||||
13.68,1.83,2.36,17.2,104,2.42,2.69,0.42,1.97,3.84,1.23,2.87,990,1
|
||||
13.76,1.53,2.7,19.5,132,2.95,2.74,0.5,1.35,5.4,1.25,3,1235,1
|
||||
13.51,1.8,2.65,19,110,2.35,2.53,0.29,1.54,4.2,1.1,2.87,1095,1
|
||||
13.48,1.81,2.41,20.5,100,2.7,2.98,0.26,1.86,5.1,1.04,3.47,920,1
|
||||
13.28,1.64,2.84,15.5,110,2.6,2.68,0.34,1.36,4.6,1.09,2.78,880,1
|
||||
13.05,1.65,2.55,18,98,2.45,2.43,0.29,1.44,4.25,1.12,2.51,1105,1
|
||||
13.07,1.5,2.1,15.5,98,2.4,2.64,0.28,1.37,3.7,1.18,2.69,1020,1
|
||||
14.22,3.99,2.51,13.2,128,3,3.04,0.2,2.08,5.1,0.89,3.53,760,1
|
||||
13.56,1.71,2.31,16.2,117,3.15,3.29,0.34,2.34,6.13,0.95,3.38,795,1
|
||||
13.41,3.84,2.12,18.8,90,2.45,2.68,0.27,1.48,4.28,0.91,3,1035,1
|
||||
13.88,1.89,2.59,15,101,3.25,3.56,0.17,1.7,5.43,0.88,3.56,1095,1
|
||||
13.24,3.98,2.29,17.5,103,2.64,2.63,0.32,1.66,4.36,0.82,3,680,1
|
||||
13.05,1.77,2.1,17,107,3,3,0.28,2.03,5.04,0.88,3.35,885,1
|
||||
14.21,4.04,2.44,18.9,111,2.85,2.65,0.3,1.25,5.24,0.87,3.33,1080,1
|
||||
14.38,3.59,2.28,16,102,3.25,3.17,0.27,2.19,4.9,1.04,3.44,1065,1
|
||||
13.9,1.68,2.12,16,101,3.1,3.39,0.21,2.14,6.1,0.91,3.33,985,1
|
||||
14.1,2.02,2.4,18.8,103,2.75,2.92,0.32,2.38,6.2,1.07,2.75,1060,1
|
||||
13.94,1.73,2.27,17.4,108,2.88,3.54,0.32,2.08,8.9,1.12,3.1,1260,1
|
||||
13.05,1.73,2.04,12.4,92,2.72,3.27,0.17,2.91,7.2,1.12,2.91,1150,1
|
||||
13.83,1.65,2.6,17.2,94,2.45,2.99,0.22,2.29,5.6,1.24,3.37,1265,1
|
||||
13.82,1.75,2.42,14,111,3.88,3.74,0.32,1.87,7.05,1.01,3.26,1190,1
|
||||
13.77,1.9,2.68,17.1,115,3,2.79,0.39,1.68,6.3,1.13,2.93,1375,1
|
||||
13.74,1.67,2.25,16.4,118,2.6,2.9,0.21,1.62,5.85,0.92,3.2,1060,1
|
||||
13.56,1.73,2.46,20.5,116,2.96,2.78,0.2,2.45,6.25,0.98,3.03,1120,1
|
||||
14.22,1.7,2.3,16.3,118,3.2,3,0.26,2.03,6.38,0.94,3.31,970,1
|
||||
13.29,1.97,2.68,16.8,102,3,3.23,0.31,1.66,6,1.07,2.84,1270,1
|
||||
13.72,1.43,2.5,16.7,108,3.4,3.67,0.19,2.04,6.8,0.89,2.87,1285,1
|
||||
12.37,0.94,1.36,10.6,88,1.98,0.57,0.28,0.42,1.95,1.05,1.82,520,2
|
||||
12.33,1.1,2.28,16,101,2.05,1.09,0.63,0.41,3.27,1.25,1.67,680,2
|
||||
12.64,1.36,2.02,16.8,100,2.02,1.41,0.53,0.62,5.75,0.98,1.59,450,2
|
||||
13.67,1.25,1.92,18,94,2.1,1.79,0.32,0.73,3.8,1.23,2.46,630,2
|
||||
12.37,1.13,2.16,19,87,3.5,3.1,0.19,1.87,4.45,1.22,2.87,420,2
|
||||
12.17,1.45,2.53,19,104,1.89,1.75,0.45,1.03,2.95,1.45,2.23,355,2
|
||||
12.37,1.21,2.56,18.1,98,2.42,2.65,0.37,2.08,4.6,1.19,2.3,678,2
|
||||
13.11,1.01,1.7,15,78,2.98,3.18,0.26,2.28,5.3,1.12,3.18,502,2
|
||||
12.37,1.17,1.92,19.6,78,2.11,2,0.27,1.04,4.68,1.12,3.48,510,2
|
||||
13.34,0.94,2.36,17,110,2.53,1.3,0.55,0.42,3.17,1.02,1.93,750,2
|
||||
12.21,1.19,1.75,16.8,151,1.85,1.28,0.14,2.5,2.85,1.28,3.07,718,2
|
||||
12.29,1.61,2.21,20.4,103,1.1,1.02,0.37,1.46,3.05,0.906,1.82,870,2
|
||||
13.86,1.51,2.67,25,86,2.95,2.86,0.21,1.87,3.38,1.36,3.16,410,2
|
||||
13.49,1.66,2.24,24,87,1.88,1.84,0.27,1.03,3.74,0.98,2.78,472,2
|
||||
12.99,1.67,2.6,30,139,3.3,2.89,0.21,1.96,3.35,1.31,3.5,985,2
|
||||
11.96,1.09,2.3,21,101,3.38,2.14,0.13,1.65,3.21,0.99,3.13,886,2
|
||||
11.66,1.88,1.92,16,97,1.61,1.57,0.34,1.15,3.8,1.23,2.14,428,2
|
||||
13.03,0.9,1.71,16,86,1.95,2.03,0.24,1.46,4.6,1.19,2.48,392,2
|
||||
11.84,2.89,2.23,18,112,1.72,1.32,0.43,0.95,2.65,0.96,2.52,500,2
|
||||
12.33,0.99,1.95,14.8,136,1.9,1.85,0.35,2.76,3.4,1.06,2.31,750,2
|
||||
12.7,3.87,2.4,23,101,2.83,2.55,0.43,1.95,2.57,1.19,3.13,463,2
|
||||
12,0.92,2,19,86,2.42,2.26,0.3,1.43,2.5,1.38,3.12,278,2
|
||||
12.72,1.81,2.2,18.8,86,2.2,2.53,0.26,1.77,3.9,1.16,3.14,714,2
|
||||
12.08,1.13,2.51,24,78,2,1.58,0.4,1.4,2.2,1.31,2.72,630,2
|
||||
13.05,3.86,2.32,22.5,85,1.65,1.59,0.61,1.62,4.8,0.84,2.01,515,2
|
||||
11.84,0.89,2.58,18,94,2.2,2.21,0.22,2.35,3.05,0.79,3.08,520,2
|
||||
12.67,0.98,2.24,18,99,2.2,1.94,0.3,1.46,2.62,1.23,3.16,450,2
|
||||
12.16,1.61,2.31,22.8,90,1.78,1.69,0.43,1.56,2.45,1.33,2.26,495,2
|
||||
11.65,1.67,2.62,26,88,1.92,1.61,0.4,1.34,2.6,1.36,3.21,562,2
|
||||
11.64,2.06,2.46,21.6,84,1.95,1.69,0.48,1.35,2.8,1,2.75,680,2
|
||||
12.08,1.33,2.3,23.6,70,2.2,1.59,0.42,1.38,1.74,1.07,3.21,625,2
|
||||
12.08,1.83,2.32,18.5,81,1.6,1.5,0.52,1.64,2.4,1.08,2.27,480,2
|
||||
12,1.51,2.42,22,86,1.45,1.25,0.5,1.63,3.6,1.05,2.65,450,2
|
||||
12.69,1.53,2.26,20.7,80,1.38,1.46,0.58,1.62,3.05,0.96,2.06,495,2
|
||||
12.29,2.83,2.22,18,88,2.45,2.25,0.25,1.99,2.15,1.15,3.3,290,2
|
||||
11.62,1.99,2.28,18,98,3.02,2.26,0.17,1.35,3.25,1.16,2.96,345,2
|
||||
12.47,1.52,2.2,19,162,2.5,2.27,0.32,3.28,2.6,1.16,2.63,937,2
|
||||
11.81,2.12,2.74,21.5,134,1.6,0.99,0.14,1.56,2.5,0.95,2.26,625,2
|
||||
12.29,1.41,1.98,16,85,2.55,2.5,0.29,1.77,2.9,1.23,2.74,428,2
|
||||
12.37,1.07,2.1,18.5,88,3.52,3.75,0.24,1.95,4.5,1.04,2.77,660,2
|
||||
12.29,3.17,2.21,18,88,2.85,2.99,0.45,2.81,2.3,1.42,2.83,406,2
|
||||
12.08,2.08,1.7,17.5,97,2.23,2.17,0.26,1.4,3.3,1.27,2.96,710,2
|
||||
12.6,1.34,1.9,18.5,88,1.45,1.36,0.29,1.35,2.45,1.04,2.77,562,2
|
||||
12.34,2.45,2.46,21,98,2.56,2.11,0.34,1.31,2.8,0.8,3.38,438,2
|
||||
11.82,1.72,1.88,19.5,86,2.5,1.64,0.37,1.42,2.06,0.94,2.44,415,2
|
||||
12.51,1.73,1.98,20.5,85,2.2,1.92,0.32,1.48,2.94,1.04,3.57,672,2
|
||||
12.42,2.55,2.27,22,90,1.68,1.84,0.66,1.42,2.7,0.86,3.3,315,2
|
||||
12.25,1.73,2.12,19,80,1.65,2.03,0.37,1.63,3.4,1,3.17,510,2
|
||||
12.72,1.75,2.28,22.5,84,1.38,1.76,0.48,1.63,3.3,0.88,2.42,488,2
|
||||
12.22,1.29,1.94,19,92,2.36,2.04,0.39,2.08,2.7,0.86,3.02,312,2
|
||||
11.61,1.35,2.7,20,94,2.74,2.92,0.29,2.49,2.65,0.96,3.26,680,2
|
||||
11.46,3.74,1.82,19.5,107,3.18,2.58,0.24,3.58,2.9,0.75,2.81,562,2
|
||||
12.52,2.43,2.17,21,88,2.55,2.27,0.26,1.22,2,0.9,2.78,325,2
|
||||
11.76,2.68,2.92,20,103,1.75,2.03,0.6,1.05,3.8,1.23,2.5,607,2
|
||||
11.41,0.74,2.5,21,88,2.48,2.01,0.42,1.44,3.08,1.1,2.31,434,2
|
||||
12.08,1.39,2.5,22.5,84,2.56,2.29,0.43,1.04,2.9,0.93,3.19,385,2
|
||||
11.03,1.51,2.2,21.5,85,2.46,2.17,0.52,2.01,1.9,1.71,2.87,407,2
|
||||
11.82,1.47,1.99,20.8,86,1.98,1.6,0.3,1.53,1.95,0.95,3.33,495,2
|
||||
12.42,1.61,2.19,22.5,108,2,2.09,0.34,1.61,2.06,1.06,2.96,345,2
|
||||
12.77,3.43,1.98,16,80,1.63,1.25,0.43,0.83,3.4,0.7,2.12,372,2
|
||||
12,3.43,2,19,87,2,1.64,0.37,1.87,1.28,0.93,3.05,564,2
|
||||
11.45,2.4,2.42,20,96,2.9,2.79,0.32,1.83,3.25,0.8,3.39,625,2
|
||||
11.56,2.05,3.23,28.5,119,3.18,5.08,0.47,1.87,6,0.93,3.69,465,2
|
||||
12.42,4.43,2.73,26.5,102,2.2,2.13,0.43,1.71,2.08,0.92,3.12,365,2
|
||||
13.05,5.8,2.13,21.5,86,2.62,2.65,0.3,2.01,2.6,0.73,3.1,380,2
|
||||
11.87,4.31,2.39,21,82,2.86,3.03,0.21,2.91,2.8,0.75,3.64,380,2
|
||||
12.07,2.16,2.17,21,85,2.6,2.65,0.37,1.35,2.76,0.86,3.28,378,2
|
||||
12.43,1.53,2.29,21.5,86,2.74,3.15,0.39,1.77,3.94,0.69,2.84,352,2
|
||||
11.79,2.13,2.78,28.5,92,2.13,2.24,0.58,1.76,3,0.97,2.44,466,2
|
||||
12.37,1.63,2.3,24.5,88,2.22,2.45,0.4,1.9,2.12,0.89,2.78,342,2
|
||||
12.04,4.3,2.38,22,80,2.1,1.75,0.42,1.35,2.6,0.79,2.57,580,2
|
||||
12.86,1.35,2.32,18,122,1.51,1.25,0.21,0.94,4.1,0.76,1.29,630,3
|
||||
12.88,2.99,2.4,20,104,1.3,1.22,0.24,0.83,5.4,0.74,1.42,530,3
|
||||
12.81,2.31,2.4,24,98,1.15,1.09,0.27,0.83,5.7,0.66,1.36,560,3
|
||||
12.7,3.55,2.36,21.5,106,1.7,1.2,0.17,0.84,5,0.78,1.29,600,3
|
||||
12.51,1.24,2.25,17.5,85,2,0.58,0.6,1.25,5.45,0.75,1.51,650,3
|
||||
12.6,2.46,2.2,18.5,94,1.62,0.66,0.63,0.94,7.1,0.73,1.58,695,3
|
||||
12.25,4.72,2.54,21,89,1.38,0.47,0.53,0.8,3.85,0.75,1.27,720,3
|
||||
12.53,5.51,2.64,25,96,1.79,0.6,0.63,1.1,5,0.82,1.69,515,3
|
||||
13.49,3.59,2.19,19.5,88,1.62,0.48,0.58,0.88,5.7,0.81,1.82,580,3
|
||||
12.84,2.96,2.61,24,101,2.32,0.6,0.53,0.81,4.92,0.89,2.15,590,3
|
||||
12.93,2.81,2.7,21,96,1.54,0.5,0.53,0.75,4.6,0.77,2.31,600,3
|
||||
13.36,2.56,2.35,20,89,1.4,0.5,0.37,0.64,5.6,0.7,2.47,780,3
|
||||
13.52,3.17,2.72,23.5,97,1.55,0.52,0.5,0.55,4.35,0.89,2.06,520,3
|
||||
13.62,4.95,2.35,20,92,2,0.8,0.47,1.02,4.4,0.91,2.05,550,3
|
||||
12.25,3.88,2.2,18.5,112,1.38,0.78,0.29,1.14,8.21,0.65,2,855,3
|
||||
13.16,3.57,2.15,21,102,1.5,0.55,0.43,1.3,4,0.6,1.68,830,3
|
||||
13.88,5.04,2.23,20,80,0.98,0.34,0.4,0.68,4.9,0.58,1.33,415,3
|
||||
12.87,4.61,2.48,21.5,86,1.7,0.65,0.47,0.86,7.65,0.54,1.86,625,3
|
||||
13.32,3.24,2.38,21.5,92,1.93,0.76,0.45,1.25,8.42,0.55,1.62,650,3
|
||||
13.08,3.9,2.36,21.5,113,1.41,1.39,0.34,1.14,9.4,0.57,1.33,550,3
|
||||
13.5,3.12,2.62,24,123,1.4,1.57,0.22,1.25,8.6,0.59,1.3,500,3
|
||||
12.79,2.67,2.48,22,112,1.48,1.36,0.24,1.26,10.8,0.48,1.47,480,3
|
||||
13.11,1.9,2.75,25.5,116,2.2,1.28,0.26,1.56,7.1,0.61,1.33,425,3
|
||||
13.23,3.3,2.28,18.5,98,1.8,0.83,0.61,1.87,10.52,0.56,1.51,675,3
|
||||
12.58,1.29,2.1,20,103,1.48,0.58,0.53,1.4,7.6,0.58,1.55,640,3
|
||||
13.17,5.19,2.32,22,93,1.74,0.63,0.61,1.55,7.9,0.6,1.48,725,3
|
||||
13.84,4.12,2.38,19.5,89,1.8,0.83,0.48,1.56,9.01,0.57,1.64,480,3
|
||||
12.45,3.03,2.64,27,97,1.9,0.58,0.63,1.14,7.5,0.67,1.73,880,3
|
||||
14.34,1.68,2.7,25,98,2.8,1.31,0.53,2.7,13,0.57,1.96,660,3
|
||||
13.48,1.67,2.64,22.5,89,2.6,1.1,0.52,2.29,11.75,0.57,1.78,620,3
|
||||
12.36,3.83,2.38,21,88,2.3,0.92,0.5,1.04,7.65,0.56,1.58,520,3
|
||||
13.69,3.26,2.54,20,107,1.83,0.56,0.5,0.8,5.88,0.96,1.82,680,3
|
||||
12.85,3.27,2.58,22,106,1.65,0.6,0.6,0.96,5.58,0.87,2.11,570,3
|
||||
12.96,3.45,2.35,18.5,106,1.39,0.7,0.4,0.94,5.28,0.68,1.75,675,3
|
||||
13.78,2.76,2.3,22,90,1.35,0.68,0.41,1.03,9.58,0.7,1.68,615,3
|
||||
13.73,4.36,2.26,22.5,88,1.28,0.47,0.52,1.15,6.62,0.78,1.75,520,3
|
||||
13.45,3.7,2.6,23,111,1.7,0.92,0.43,1.46,10.68,0.85,1.56,695,3
|
||||
12.82,3.37,2.3,19.5,88,1.48,0.66,0.4,0.97,10.26,0.72,1.75,685,3
|
||||
13.58,2.58,2.69,24.5,105,1.55,0.84,0.39,1.54,8.66,0.74,1.8,750,3
|
||||
13.4,4.6,2.86,25,112,1.98,0.96,0.27,1.11,8.5,0.67,1.92,630,3
|
||||
12.2,3.03,2.32,19,96,1.25,0.49,0.4,0.73,5.5,0.66,1.83,510,3
|
||||
12.77,2.39,2.28,19.5,86,1.39,0.51,0.48,0.64,9.899999,0.57,1.63,470,3
|
||||
14.16,2.51,2.48,20,91,1.68,0.7,0.44,1.24,9.7,0.62,1.71,660,3
|
||||
13.71,5.65,2.45,20.5,95,1.68,0.61,0.52,1.06,7.7,0.64,1.74,740,3
|
||||
13.4,3.91,2.48,23,102,1.8,0.75,0.43,1.41,7.3,0.7,1.56,750,3
|
||||
13.27,4.28,2.26,20,120,1.59,0.69,0.43,1.35,10.2,0.59,1.56,835,3
|
||||
13.17,2.59,2.37,20,120,1.65,0.68,0.53,1.46,9.3,0.6,1.62,840,3
|
||||
14.13,4.1,2.74,24.5,96,2.05,0.76,0.56,1.35,9.2,0.61,1.6,560,3
|
|
69
section_9_pca/main.py
Normal file
69
section_9_pca/main.py
Normal file
@ -0,0 +1,69 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 資料預處理
|
||||
dataset = pd.read_csv("Wine.csv")
|
||||
X = dataset.iloc[:, [0,13]].values
|
||||
Y = dataset.iloc[:, 13].values
|
||||
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
|
||||
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# PCA
|
||||
from sklearn.decomposition import PCA
|
||||
pca = PCA(n_components=2)
|
||||
X_train = pca.fit_transform(X_train)
|
||||
X_test = pca.transform(X_test)
|
||||
explained_variance = pca.explained_variance_ratio_
|
||||
|
||||
# 擬合和預測
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
|
||||
classifier = LogisticRegression(random_state=0)
|
||||
|
||||
classifier.fit(X_train, Y_train)
|
||||
|
||||
Y_pred = classifier.predict(X_test)
|
||||
|
||||
print(Y_test)
|
||||
print(Y_pred)
|
||||
|
||||
# 評估性能
|
||||
from sklearn.metrics import confusion_matrix
|
||||
|
||||
cm = confusion_matrix(Y_test, Y_pred)
|
||||
|
||||
print(cm)
|
||||
|
||||
# 視覺化
|
||||
from matplotlib.colors import ListedColormap
|
||||
|
||||
X_set, y_set = X_train, Y_train
|
||||
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green', 'black')))
|
||||
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('orange', 'blue', 'grey'))(i), label = j)
|
||||
|
||||
plt.title('Logistic Regression (Training set)')
|
||||
|
||||
plt.xlabel('PC1')
|
||||
plt.ylabel('PC2')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
Loading…
Reference in New Issue
Block a user