Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
ML4DS_tutorials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
manueh51
ML4DS_tutorials
Commits
9a60c190
Commit
9a60c190
authored
3 years ago
by
manueh51
Browse files
Options
Downloads
Patches
Plain Diff
Delete Assignment02_nb.ipynb
parent
94b1d233
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assignment02_nb.ipynb
+0
-288
0 additions, 288 deletions
Assignment02_nb.ipynb
with
0 additions
and
288 deletions
Assignment02_nb.ipynb
deleted
100644 → 0
+
0
−
288
View file @
94b1d233
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment Sheet 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook is part of the 2nd assignment. We will train a Decision Tree classifier using the sklearn library on the Iris dataset.\n",
"\n",
"Your task is to complete the missing code, where marked with a **TODO**. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import seaborn as sns\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import graphviz\n",
"\n",
"from sklearn import datasets, tree\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import classification_report\n",
"from os import system\n",
"from IPython.display import Image\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# For further info https://archive.ics.uci.edu/ml/datasets/iris\n",
"iris = datasets.load_iris()\n",
"X = iris.data \n",
"y = iris.target"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exploratory data analysis"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Quick look into the data structure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(X.shape)\n",
"print(y.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(X[:5,:])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Using pandas\n",
"data = pd.concat([pd.DataFrame(X),pd.DataFrame(y)], axis=1)\n",
"data.columns=['a','b','c','d','target']\n",
"data.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Exemplary plots"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(8,6))\n",
"sns.scatterplot(x=X[:,0], y=X[:,1], hue=y)\n",
"plt.xlabel('Sepal length')\n",
"plt.ylabel('Sepal width')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Univariate hist_plot 'sepal_length'\n",
"class0_index = [i for i, j in enumerate(y) if j==0]\n",
"class1_index = [i for i, j in enumerate(y) if j==1]\n",
"class2_index = [i for i, j in enumerate(y) if j==2]\n",
"\n",
"sns.histplot(data=X, x=X[:,0], hue=y, element='step')\n",
"plt.xlabel('Sepal length')\n",
"plt.legend(('class1', 'class2','class3'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Barplot over 'sepal-width'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Boxplot of all features"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Classification using decision trees "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Data preparation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Split data\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
"X_train.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Train DT classifier using sklearn; Visualization; Evaluation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Train a DT classifier "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualize: Export to .png image file\n",
"tree.export_graphviz(clf, out_file='tree.dot') \n",
"system(\"dot -Tpng tree.dot -o tree1.png\")\n",
"Image(\"tree1.png\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Evaluation the classifier's performance"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Train a second DT classifier using the Entropy instead of the Gini-Index (default)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Train the second classifier"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualize #2\n",
"tree.export_graphviz(clf2, out_file='tree2.dot') \n",
"system(\"dot -Tpng tree2.dot -o tree2.png\")\n",
"Image(\"tree2.png\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Evaluation"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:markdown id: tags:
# Assignment Sheet 2
%% Cell type:markdown id: tags:
This notebook is part of the 2nd assignment. We will train a Decision Tree classifier using the sklearn library on the Iris dataset.
Your task is to complete the missing code, where marked with a
**TODO**
.
%% Cell type:markdown id: tags:
## Imports
%% Cell type:code id: tags:
```
python
import
seaborn
as
sns
import
pandas
as
pd
import
matplotlib.pyplot
as
plt
import
graphviz
from
sklearn
import
datasets
,
tree
from
sklearn.model_selection
import
train_test_split
from
sklearn.metrics
import
classification_report
from
os
import
system
from
IPython.display
import
Image
%
matplotlib
inline
```
%% Cell type:markdown id: tags:
## Load dataset
%% Cell type:code id: tags:
```
python
# For further info https://archive.ics.uci.edu/ml/datasets/iris
iris
=
datasets
.
load_iris
()
X
=
iris
.
data
y
=
iris
.
target
```
%% Cell type:markdown id: tags:
## Exploratory data analysis
%% Cell type:markdown id: tags:
#### Quick look into the data structure
%% Cell type:code id: tags:
```
python
print
(
X
.
shape
)
print
(
y
.
shape
)
```
%% Cell type:code id: tags:
```
python
print
(
X
[:
5
,:])
```
%% Cell type:code id: tags:
```
python
# Using pandas
data
=
pd
.
concat
([
pd
.
DataFrame
(
X
),
pd
.
DataFrame
(
y
)],
axis
=
1
)
data
.
columns
=
[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
,
'
target
'
]
data
.
head
(
5
)
```
%% Cell type:markdown id: tags:
#### Exemplary plots
%% Cell type:code id: tags:
```
python
plt
.
figure
(
figsize
=
(
8
,
6
))
sns
.
scatterplot
(
x
=
X
[:,
0
],
y
=
X
[:,
1
],
hue
=
y
)
plt
.
xlabel
(
'
Sepal length
'
)
plt
.
ylabel
(
'
Sepal width
'
)
```
%% Cell type:code id: tags:
```
python
# Univariate hist_plot 'sepal_length'
class0_index
=
[
i
for
i
,
j
in
enumerate
(
y
)
if
j
==
0
]
class1_index
=
[
i
for
i
,
j
in
enumerate
(
y
)
if
j
==
1
]
class2_index
=
[
i
for
i
,
j
in
enumerate
(
y
)
if
j
==
2
]
sns
.
histplot
(
data
=
X
,
x
=
X
[:,
0
],
hue
=
y
,
element
=
'
step
'
)
plt
.
xlabel
(
'
Sepal length
'
)
plt
.
legend
((
'
class1
'
,
'
class2
'
,
'
class3
'
))
```
%% Cell type:code id: tags:
```
python
# TODO: Barplot over 'sepal-width'
```
%% Cell type:code id: tags:
```
python
# TODO: Boxplot of all features
```
%% Cell type:markdown id: tags:
## Classification using decision trees
%% Cell type:markdown id: tags:
#### Data preparation
%% Cell type:code id: tags:
```
python
# Split data
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.2
)
X_train
.
shape
```
%% Cell type:markdown id: tags:
#### Train DT classifier using sklearn; Visualization; Evaluation
%% Cell type:code id: tags:
```
python
# TODO: Train a DT classifier
```
%% Cell type:code id: tags:
```
python
# Visualize: Export to .png image file
tree
.
export_graphviz
(
clf
,
out_file
=
'
tree.dot
'
)
system
(
"
dot -Tpng tree.dot -o tree1.png
"
)
Image
(
"
tree1.png
"
)
```
%% Cell type:code id: tags:
```
python
# TODO: Evaluation the classifier's performance
```
%% Cell type:markdown id: tags:
#### Train a second DT classifier using the Entropy instead of the Gini-Index (default)
%% Cell type:code id: tags:
```
python
# TODO: Train the second classifier
```
%% Cell type:code id: tags:
```
python
# Visualize #2
tree
.
export_graphviz
(
clf2
,
out_file
=
'
tree2.dot
'
)
system
(
"
dot -Tpng tree2.dot -o tree2.png
"
)
Image
(
"
tree2.png
"
)
```
%% Cell type:code id: tags:
```
python
# TODO: Evaluation
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment