1
0
mirror of https://github.com/gsi-upm/sitc synced 2026-03-02 09:48:17 +00:00

Compare commits

5 Commits

Author SHA1 Message Date
Carlos A. Iglesias
5d01f26e72 Add files via upload
Updated visualize decision tree functions
2026-02-25 17:19:08 +01:00
Carlos A. Iglesias
8e177963af Update 2_5_0_Machine_Learning.ipynb
Updated image path
2026-02-25 16:32:10 +01:00
Carlos A. Iglesias
ecd53ceee5 Update 2_3_1_Advanced_Visualisation.ipynb
Updated violinplot
2026-02-25 16:27:04 +01:00
Carlos A. Iglesias
99f8032d05 Update 2_3_1_Advanced_Visualisation.ipynb
Updated stripplot to auto instead of gray
2026-02-25 16:21:44 +01:00
cif
470a3d692d updated fuseki 2026-02-19 12:34:40 +01:00
4 changed files with 116 additions and 97 deletions

View File

@@ -7,7 +7,7 @@
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "7276f055a8c504d3c80098c62ed41a4f",
"checksum": "91ddca8e4ff354f86a8bdbfee755a886",
"grade": false,
"grade_id": "cell-0bfe38f97f6ab2d2",
"locked": true,
@@ -19,10 +19,10 @@
"<header style=\"width:100%;position:relative\">\n",
" <div style=\"width:80%;float:right;\">\n",
" <h1>Course Notes for Learning Intelligent Systems</h1>\n",
" <h3>Department of Telematic Engineering Systems</h3>\n",
" <h5>Universidad Politécnica de Madrid</h5>\n",
" <h2>Department of Telematic Engineering Systems</h2>\n",
" <h3>Universidad Politécnica de Madrid</h3>\n",
" </div>\n",
" <img style=\"width:15%;\" src=\"../logo.jpg\" alt=\"UPM\" />\n",
" <img style=\"width:15%;\" src=\"https://github.com/gsi-upm/sitc/blob/9844820e6653b0e169113a06538f8e54554c4fbc/images/EscUpmPolit_p.gif?raw=true\" alt=\"UPM\" />\n",
"</header>"
]
},
@@ -58,7 +58,7 @@
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "40ccd05ad0704781327031a84dfb9939",
"checksum": "5760eec341771cfd3ebd91627da0a481",
"grade": false,
"grade_id": "cell-4f8492996e74bf20",
"locked": true,
@@ -71,10 +71,10 @@
"\n",
"* This notebook\n",
"* External SPARQL editors (optional)\n",
" * YASGUI-GSI http://yasgui.gsi.upm.es\n",
" * DBpedia virtuoso http://dbpedia.org/sparql\n",
" * *YASGUI* https://yasgui.org/\n",
" * *DBpedia Virtuoso* http://dbpedia.org/sparql\n",
"\n",
"Using the YASGUI-GSI editor has several advantages over other options.\n",
"Using the YASGUI editor has several advantages over other options.\n",
"It features:\n",
"\n",
"* Selection of data source, either by specifying the URL or by selecting from a dropdown menu\n",
@@ -96,7 +96,7 @@
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "81894e9d65e5dd9f3b6e1c5f66804bf6",
"checksum": "b1285fb82e4438a22e05ff134d1e080d",
"grade": false,
"grade_id": "cell-70ac24910356c3cf",
"locked": true,
@@ -107,13 +107,15 @@
"source": [
"## Instructions\n",
"\n",
"We will be using a semantic server, available at: http://fuseki.gsi.upm.es/sitc.\n",
"We will use an available semantic server. There are two possible settings:\n",
"* A local server available at [http://fuseki.gsi.upm.es/sitc]( http://fuseki.gsi.upm.es/sitc/)\n",
"* Install the SPARQL server yourself and run it locally. To do this, follow the instructions in the notebook [how to install a SPARQL Server](01_1_SPARQL_Server.ipynb). In this case, your server is available at [http://localhost:3030](http://localhost:3030).\n",
"\n",
"This server contains a dataset about [Beatles songs](http://www.snee.com/bobdc.blog/2017/11/sparql-queries-of-beatles-reco.html), which we will query with SPARQL.\n",
"\n",
"We will provide you some example code to get you started, the *question* you will have to answer using SPARQL, a template for the answer.\n",
"We will provide you with some example code to get you started, along with the *question* you will have to answer using SPARQL and a template for the answer.\n",
"\n",
"After every query, you will find some python code to test the results of the query.\n",
"After every query, you will find some Python code to test the results of the query.\n",
"**Make sure you've run the tests before moving to the next exercise**.\n",
"If the test gives you an error, you've probably done something wrong.\n",
"You do not need to understand or modify the test code."
@@ -126,7 +128,7 @@
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "1d332d3d11fd6b57f0ec0ac3c358c6cb",
"checksum": "a5918241a0b402cb091a85d245aaa3fd",
"grade": false,
"grade_id": "cell-eb13908482825e42",
"locked": true,
@@ -138,7 +140,7 @@
"For convenience, the examples in the notebook are executable (using the `%%sparql` magic command), and they are accompanied by some code to test the results.\n",
"If the tests pass, you probably got the answer right.\n",
"\n",
"**Run this line to enable the `%%sparql` magic command.**"
"### Run this line to enable the `%%sparql` magic command.**"
]
},
{
@@ -200,7 +202,7 @@
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "34710d3bb8e2cf826833a43adb7fb448",
"checksum": "77cd823ca5a1556311f3dcf7e1533bce",
"grade": false,
"grade_id": "cell-2a44c0da2c206d01",
"locked": true,
@@ -216,10 +218,23 @@
"In addition to running queries from your browser, they provide useful features such as syntax highlighting and autocompletion.\n",
"Some examples are:\n",
"\n",
"* DBpedia's virtuoso query editor https://dbpedia.org/sparql\n",
"* A javascript based client hosted at GSI: http://yasgui.gsi.upm.es/\n",
"* DBpedia's Virtuoso query editor https://dbpedia.org/sparql\n",
"* A JavaScript-based client: https://yasgui.org/\n",
"\n",
"[^1]: http://www.snee.com/bobdc.blog/2017/11/sparql-queries-of-beatles-reco.html"
"[^1]: http://www.snee.com/bobdc.blog/2017/11/sparql-queries-of-beatles-reco.html\n",
"\n",
"### Set your SPARQL server"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Set your Fuseki server URL\n",
"\n",
"fuseki = 'http://localhost:3030/beatles/sparql'"
]
},
{
@@ -250,7 +265,7 @@
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "f7428fe79cd33383dfd3b09a0d951b6e",
"checksum": "1d6bd8b03621fb0b2d74c80b91e9b91a",
"grade": false,
"grade_id": "cell-8391a5322a9ad4a7",
"locked": true,
@@ -259,7 +274,7 @@
}
},
"source": [
"#### First select - Exploring the dataset\n",
"### First select - Exploring the dataset\n",
"\n"
]
},
@@ -324,7 +339,7 @@
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "3bc71f851a33fa401d18ea3ab02cf61f",
"checksum": "3b3a78b2676844ff59d3555a5f7690a0",
"grade": false,
"grade_id": "cell-8ce8c954513f17e7",
"locked": true,
@@ -334,7 +349,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"SELECT ?entity ?type\n",
"WHERE {\n",
@@ -388,7 +403,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "65be7168bedb4f6dc2f19e2138bab232",
"checksum": "1879bf78f30a1ec22499eec5f975803d",
"grade": false,
"grade_id": "cell-6e904d692b5facad",
"locked": false,
@@ -398,7 +413,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"SELECT ?entity ?prop\n",
"WHERE {\n",
@@ -454,7 +469,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"SELECT ?type\n",
"WHERE {\n",
@@ -479,7 +494,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"SELECT DISTINCT ?type\n",
"WHERE {\n",
@@ -522,7 +537,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "35563ff455c7e8b1c91f61db97b2011b",
"checksum": "73cbbe78a138e5f927f18155106c0761",
"grade": false,
"grade_id": "cell-e615f9a77c4bc9a5",
"locked": false,
@@ -532,7 +547,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"SELECT DISTINCT ?property\n",
"WHERE {\n",
@@ -585,7 +600,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
@@ -655,7 +670,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "069811507dbac4b86dc5d3adc82ba4ec",
"checksum": "79d1ab4722a04ee11b1f892cd4434510",
"grade": false,
"grade_id": "cell-0223a51f609edcf9",
"locked": false,
@@ -665,7 +680,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
@@ -725,7 +740,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b68a279085a1ed087f5e474a6602299e",
"checksum": "47ad334640472eeec3d52ee87035fc60",
"grade": false,
"grade_id": "cell-8f43547dd788bb33",
"locked": false,
@@ -735,7 +750,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
@@ -812,7 +827,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "335403f01e484ce5563ff059e9764ff4",
"checksum": "ac6cb33399d7efab311bd58479a75929",
"grade": false,
"grade_id": "cell-a0f0b9d9b05c9631",
"locked": false,
@@ -822,7 +837,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
@@ -891,7 +906,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "8fb253675d2e8510e2c6780b960721e5",
"checksum": "d2ed010ea9127e45edc81b2c1a8c94d9",
"grade": false,
"grade_id": "cell-523b963fa4e288d0",
"locked": false,
@@ -901,7 +916,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
@@ -971,7 +986,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "c7b6620f5ba28b482197ab693cb7142a",
"checksum": "390eea10127829419f4026f292d907ad",
"grade": false,
"grade_id": "cell-e89d08031e30b299",
"locked": false,
@@ -981,7 +996,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX m: <http://learningsparql.com/ns/musician/>\n",
@@ -1039,7 +1054,7 @@
"\n",
"Once results are grouped, they can be aggregated using any aggregation function, such as `COUNT`.\n",
"\n",
"Using `GROUP BY` and `COUNT`, get the count of songs in which Ringo Starr has played each of the instruments:"
"Using `GROUP BY` and `COUNT`, get the count of songs that use each instrument:"
]
},
{
@@ -1049,7 +1064,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "7556bacb20c1fbd059dec165c982908d",
"checksum": "899b2cf4ee7d010e5e5d02ca28ead13d",
"grade": false,
"grade_id": "cell-1429e4eb5400dbc7",
"locked": false,
@@ -1059,7 +1074,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX m: <http://learningsparql.com/ns/musician/>\n",
@@ -1123,7 +1138,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
@@ -1156,7 +1171,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "3139d9b7e620266946ffe1ae0cf67581",
"checksum": "24f94cc322288f4c467af5bfadd6a4c9",
"grade": false,
"grade_id": "cell-ee208c762d00da9c",
"locked": false,
@@ -1166,7 +1181,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n",
@@ -1228,7 +1243,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX i: <http://learningsparql.com/ns/instrument/>\n",
@@ -1263,7 +1278,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX i: <http://learningsparql.com/ns/instrument/>\n",
@@ -1297,7 +1312,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "3bc508872193750d57d07efbf334c212",
"checksum": "513916421ec8451b8a10a6923fde775b",
"grade": false,
"grade_id": "cell-dcd68c45c1608a28",
"locked": false,
@@ -1307,7 +1322,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX i: <http://learningsparql.com/ns/instrument/>\n",
@@ -1381,7 +1396,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "300df0a3cf9729dd4814b3153b2fedb4",
"checksum": "7b6c7de8beb6cd78b99fbc8eaa1b2c87",
"grade": false,
"grade_id": "cell-0c7cc924a13d792a",
"locked": false,
@@ -1391,7 +1406,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX i: <http://learningsparql.com/ns/instrument/>\n",
@@ -1456,7 +1471,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "e4e898c8a16b8aa5865dfde2f6e68ec6",
"checksum": "f4b8b1601e9cd1c05464ebad8e6836a6",
"grade": false,
"grade_id": "cell-d750b6d64c6aa0a7",
"locked": false,
@@ -1466,7 +1481,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
@@ -1521,7 +1536,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "fade6ab714376e0eabfa595dd6bd6a8b",
"checksum": "b3a570ae42656d907a1ce60f199fdbec",
"grade": false,
"grade_id": "cell-2f5aa516f8191787",
"locked": false,
@@ -1531,7 +1546,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
@@ -1577,7 +1592,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Which songs had Ringo in drums OR Lennon in lead vocals? (UNION)"
"### Which songs had Ringo in dums OR Lennon in lead vocals? (UNION)"
]
},
{
@@ -1612,7 +1627,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "09262d81449c498c37e4b9d9b1dcdfed",
"checksum": "4c1d0eaf45f7e69233bb998f5dfb9a48",
"grade": false,
"grade_id": "cell-d3a742bd87d9c793",
"locked": false,
@@ -1622,7 +1637,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
@@ -1643,7 +1658,7 @@
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "11061e79ec06ccb3a9c496319a528366",
"checksum": "d583b30a1e00960df3a4411b6854c8c8",
"grade": true,
"grade_id": "cell-409402df0e801d09",
"locked": true,
@@ -1654,7 +1669,7 @@
},
"outputs": [],
"source": [
"assert len(solution()['tuples']) == 209"
"assert len(solution()['tuples']) == 246"
]
},
{
@@ -1695,7 +1710,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "9ddd2d1f50f841b889bfd29b175d06da",
"checksum": "161fd1c6ed06206d4661e4e6c3e255c7",
"grade": false,
"grade_id": "cell-9d1ec854eb530235",
"locked": false,
@@ -1705,7 +1720,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n",
"\n",
@@ -1789,7 +1804,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "d18e8b6e1d32aed395a533febb29fcb5",
"checksum": "16d79b02f510bbfffcb2cc36af159081",
"grade": false,
"grade_id": "cell-7ea1f5154cdd8324",
"locked": false,
@@ -1799,7 +1814,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX i: <http://learningsparql.com/ns/instrument/>\n",
@@ -1836,7 +1851,7 @@
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "f926fa3a3568d122454a12312859cda1",
"checksum": "65cece835581895b44257668948a5130",
"grade": false,
"grade_id": "cell-b6bee887a1b1fc60",
"locked": false,
@@ -1846,7 +1861,7 @@
},
"outputs": [],
"source": [
"%%sparql http://fuseki.gsi.upm.es/sitc/\n",
"%%sparql $fuseki\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n",
"PREFIX s: <http://learningsparql.com/ns/schema/>\n",
"PREFIX i: <http://learningsparql.com/ns/instrument/>\n",

View File

@@ -330,7 +330,7 @@
"# Saving the resulting axes as ax each time causes the resulting plot to be shown\n",
"# on top of the previous axes\n",
"ax = sns.boxplot(x=\"species\", y=\"petal length (cm)\", data=iris_df)\n",
"ax = sns.stripplot(x=\"species\", y=\"petal length (cm)\", data=iris_df, jitter=True, edgecolor=\"gray\")"
"ax = sns.stripplot(x=\"species\", y=\"petal length (cm)\", data=iris_df, jitter=True, edgecolor=\"auto\")"
]
},
{
@@ -348,7 +348,7 @@
"source": [
"# A violin plot combines the benefits of the previous two plots and simplifies them\n",
"# Denser regions of the data are fatter, and sparser thinner in a violin plot\n",
"sns.violinplot(x=\"species\", y=\"petal length (cm)\", data=iris_df, size=6)"
"sns.violinplot(x=\"species\", y=\"petal length (cm)\", data=iris_df)"
]
},
{

View File

@@ -72,7 +72,7 @@
"Machine learning algorithms are programs that learn a model from a dataset to make predictions or learn structures to organize the data.\n",
"\n",
"In scikit-learn, machine learning algorithms take as input a *numpy* array (n_samples, n_features), where\n",
"* **n_samples**: number of samples. Each sample is an item to process (i.e., classify). A sample can be a document, a picture, a sound, a video, a row in a database or CSV file, or whatever you can describe with a fixed set of quantitative traits.\n",
"* **n_samples**: number of samples. Each sample is an item to be processed (i.e., classified). A sample can be a document, a picture, a sound, a video, a row in a database or CSV file, or whatever you can describe with a fixed set of quantitative traits.\n",
"* **n_features**: The number of features or distinct traits that can be used to describe each item quantitatively.\n",
"\n",
"The number of features should be defined in advance. A specific type of feature set is high-dimensional (e.g., millions of features), but most values are zero for a given sample. Using (numpy) arrays, all those zero values would also take up memory. For this reason, these feature sets are often represented with sparse matrices (scipy.sparse) instead of (numpy) arrays.\n",
@@ -112,7 +112,7 @@
"metadata": {},
"source": [
"In *unsupervised machine learning models*, the machine learning model algorithm takes as input the feature vectors. It produces a predictive model that is used to fit its parameters to summarize the best regularities found in the data.\n",
"![](files/images/plot_ML_flow_chart_3.png)"
"![](./images/plot_ML_flow_chart_3.png)"
]
},
{
@@ -140,7 +140,7 @@
" * **model.fit_transform()**: Some estimators implement this method, which performs a fit and a transform on the same input data.\n",
"\n",
"\n",
"![](files/images/plot_ML_flow_chart_2.png)"
"![](./images/plot_ML_flow_chart_2.png)"
]
},
{

View File

@@ -53,10 +53,10 @@ import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.inspection import DecisionBoundaryDisplay
def plot_tree_iris():
"""
Taken fromhttp://scikit-learn.org/stable/auto_examples/tree/plot_iris.html
"""
# Parameters
@@ -67,11 +67,11 @@ def plot_tree_iris():
# Load data
iris = load_iris()
for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3],
[1, 2], [1, 3], [2, 3]]):
for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]):
# We only take the two corresponding features
X = iris.data[:, pair]
y = iris.target
'''
# Shuffle
idx = np.arange(X.shape[0])
@@ -84,34 +84,38 @@ def plot_tree_iris():
mean = X.mean(axis=0)
std = X.std(axis=0)
X = (X - mean) / std
'''
# Train
model = DecisionTreeClassifier(max_depth=3, random_state=1).fit(X, y)
clf = DecisionTreeClassifier(max_depth=3, random_state=1).fit(X, y)
# Plot the decision boundary
plt.subplot(2, 3, pairidx + 1)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
plt.xlabel(iris.feature_names[pair[0]])
plt.ylabel(iris.feature_names[pair[1]])
plt.axis("tight")
# Taken from https://scikit-learn.org/stable/auto_examples/tree/plot_iris_dtc.html
# Plot the decision boundary
ax = plt.subplot(2, 3, pairidx + 1)
plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)
DecisionBoundaryDisplay.from_estimator(
clf,
X,
cmap=plt.cm.RdYlBu,
response_method="predict",
ax=ax,
xlabel=iris.feature_names[pair[0]],
ylabel=iris.feature_names[pair[1]],
)
# Plot the training points
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],
cmap=plt.cm.Paired)
idx = np.asarray(y == i).nonzero()
plt.scatter(
X[idx, 0],
X[idx, 1],
c=color,
label=iris.target_names[i],
edgecolor="black",
s=15
)
plt.axis("tight")
plt.suptitle("Decision surface of a decision tree using paired features")
plt.legend()
#plt.legend()
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
plt.show()