Skip to content
Snippets Groups Projects
Commit 7f7f72b8 authored by hachmeis@hu-berlin.de's avatar hachmeis@hu-berlin.de
Browse files

+Titanic Classifier

parent 57236942
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:little-production tags: %% Cell type:markdown id:little-production tags:
# Data Mining # Data Mining
<br/> <br/>
<br/> <br/>
Dieses Notebook finden Sie hier: https://scm.cms.hu-berlin.de/ibi/python/-/blob/master/programmierspass/Data_Mining.ipynb Dieses Notebook finden Sie hier: https://scm.cms.hu-berlin.de/ibi/python/-/blob/master/programmierspass/Data_Mining.ipynb
<br/> <br/>
![CC-BY-NC](https://scm.cms.hu-berlin.de/ibi/python/-/raw/master/img/cc-by-nc.png) ![CC-BY-NC](https://scm.cms.hu-berlin.de/ibi/python/-/raw/master/img/cc-by-nc.png)
Dieses Notebook ist als freies Werk unter der Lizenz [Creative Commons Attribution-NonCommercial 3.0 Unported](http://creativecommons.org/licenses/by-nc/3.0/) verfügbar. Sie dürfen die Inhalte kopieren, verteilen und verändern, solange Sie die Urheber nennen und sie nicht für kommerzielle Zwecke nutzen. Dieses Notebook ist als freies Werk unter der Lizenz [Creative Commons Attribution-NonCommercial 3.0 Unported](http://creativecommons.org/licenses/by-nc/3.0/) verfügbar. Sie dürfen die Inhalte kopieren, verteilen und verändern, solange Sie die Urheber nennen und sie nicht für kommerzielle Zwecke nutzen.
%% Cell type:markdown id:rising-farming tags: %% Cell type:markdown id:rising-farming tags:
## Was ist es und worum geht es? ## Was ist es und worum geht es?
**Data Mining** wird wie folgt definiert: **Data Mining** wird wie folgt definiert:
- **Definition 1 (Synonym zu *Knowledge Discovery in Databases (KDD)*)**: Prozess der (semi-) automatischen Extraktion von Wissen aus Datenbanken, das statistisch gültig, bisher unbekannt und potentiell nützlich ist. [1] - **Definition 1 (Synonym zu *Knowledge Discovery in Databases (KDD)*)**: Prozess der (semi-) automatischen Extraktion von Wissen aus Datenbanken, das statistisch gültig, bisher unbekannt und potentiell nützlich ist. [1]
- **Definition 2 (Teil des KDD)**: - **Definition 2 (Teil des KDD)**:
- Mustergewinnung/Modellierung - Mustergewinnung/Modellierung
- Interpretation - Interpretation
- Anwendung von Algorithmen, die unter gewissen Ressourcenbeschränkungen Muster/Modelle bei gegebener Faktenmenge erzeugen - Anwendung von Algorithmen, die unter gewissen Ressourcenbeschränkungen Muster/Modelle bei gegebener Faktenmenge erzeugen
**Anwendungsfälle** **Anwendungsfälle**
- Spamfilterung - Spamfilterung
- Objekterkennung auf Bildern - Objekterkennung auf Bildern
- Prüfung der Kreditwürdigkeit von potentiellen Kunden - Prüfung der Kreditwürdigkeit von potentiellen Kunden
- Personalisierte Empfehlungen (zum Beispiel auf YouTube und Spotify) - Personalisierte Empfehlungen (zum Beispiel auf YouTube und Spotify)
%% Cell type:markdown id:60ef8533 tags: %% Cell type:markdown id:60ef8533 tags:
## Ziel für heute ## Ziel für heute
- kurze Einführung in das Thema - kurze Einführung in das Thema
- Anwendungsbeispiele zeigen - Anwendungsbeispiele zeigen
- Teaser für die Master-Lehrveranstaltungen *Knowledge Discovery in Databases (KDD)* und *Web Science* - Teaser für die Master-Lehrveranstaltungen *Knowledge Discovery in Databases (KDD)* und *Web Science*
- Inspiration :) - Inspiration :)
%% Cell type:markdown id:e0ffe431 tags: %% Cell type:markdown id:e0ffe431 tags:
## Prozess-Model des Data Mining [1] ## Prozess-Model des Data Mining [1]
![Screenshot%20from%202023-01-26%2011-31-48.png](attachment:Screenshot%20from%202023-01-26%2011-31-48.png) ![Screenshot%20from%202023-01-26%2011-31-48.png](attachment:Screenshot%20from%202023-01-26%2011-31-48.png)
**Referenzen** **Referenzen**
[1]: (Fayyad, Piatetsky-Shapiro und Smyth 1996) https://ojs.aaai.org//index.php/aimagazine/article/view/1230 [1]: (Fayyad, Piatetsky-Shapiro und Smyth 1996) https://ojs.aaai.org//index.php/aimagazine/article/view/1230
%% Cell type:markdown id:88429751 tags: %% Cell type:markdown id:88429751 tags:
### Data Mining Tasks ### Data Mining Tasks
- **Clustering** - **Clustering**
- **Klassifikation** - **Klassifikation**
- Regression - Regression
- Assoziationsregeln - Assoziationsregeln
- ... - ...
%% Cell type:markdown id:2f2a0afc tags: %% Cell type:markdown id:2f2a0afc tags:
## Python Bibliotheken die wir heute benutzen: ## Python Bibliotheken die wir heute benutzen:
- [Pandas](https://pandas.pydata.org/) für die **Vorverarbeitung** - [Pandas](https://pandas.pydata.org/) für die **Vorverarbeitung**
- [Seaborn](https://seaborn.pydata.org/) und [Matplotlib](https://matplotlib.org/) zur **Visualisierung** - [Seaborn](https://seaborn.pydata.org/) und [Matplotlib](https://matplotlib.org/) zur **Visualisierung**
- [Scikit Learn](https://scikit-learn.org/stable/) zur **Implementierung von Algorithmen** und deren **Evaluation** - [Scikit Learn](https://scikit-learn.org/stable/) zur **Implementierung von Algorithmen** und deren **Evaluation**
%% Cell type:code id:broadband-bathroom tags: %% Cell type:code id:broadband-bathroom tags:
``` ```
import pandas as pd import pandas as pd
import seaborn as sns import seaborn as sns
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
``` ```
%% Cell type:markdown id:91ec21a4 tags: %% Cell type:markdown id:91ec21a4 tags:
## Wie entscheide ich mich für ein Modell? ## Wie entscheide ich mich für ein Modell?
Abgeleitet vom Ziel der Data Mining Anwendung, wird ein Modell auf Basis des jeweiligen Tasks (Klassifikation, Clustering, Regression, Assoziationsregeln, ...) gewählt. Es gibt eine Vielzahl von Modellen. Eine erste Heuristik zur Entscheidung für angemessene Modelle zum experimentieren bietet [SciKit Learn's Machine Learning Map](https://scikit-learn.org/stable/tutorial/machine_learning_map/index.html). Allerdings, hat auch die Bibliothek von SciKit Learn seine Grenzen. Assoziationsregeln sind hier nicht implementiert und auch komplexere Ansätze die auf Neuronalen Netzen basieren, werden üblicherweise mit anderen Bibliotheken implementiert wie bspw. [PyTorch](https://pytorch.org/), [Tensorflow](https://www.tensorflow.org/) oder [Keras](https://keras.io/). Ein paar Beispiele die typischerweise verwendet werden in den jeweiligen Tasks: Abgeleitet vom Ziel der Data Mining Anwendung, wird ein Modell auf Basis des jeweiligen Tasks (Klassifikation, Clustering, Regression, Assoziationsregeln, ...) gewählt. Es gibt eine Vielzahl von Modellen. Eine erste Heuristik zur Entscheidung für angemessene Modelle zum experimentieren bietet [SciKit Learn's Machine Learning Map](https://scikit-learn.org/stable/tutorial/machine_learning_map/index.html). Allerdings, hat auch die Bibliothek von SciKit Learn seine Grenzen. Assoziationsregeln sind hier nicht implementiert und auch komplexere Ansätze die auf Neuronalen Netzen basieren, werden üblicherweise mit anderen Bibliotheken implementiert wie bspw. [PyTorch](https://pytorch.org/), [Tensorflow](https://www.tensorflow.org/) oder [Keras](https://keras.io/). Ein paar Beispiele die typischerweise verwendet werden in den jeweiligen Tasks:
**Clustering** **Clustering**
- K-Means - K-Means
- DBSCAN - DBSCAN
**Klassifikation** **Klassifikation**
- Naive Bayes Klassifikator - Naive Bayes Klassifikator
- Decision Trees - Decision Trees
- Random Forests - Random Forests
- Support Vector Machines - Support Vector Machines
- Neural Networks (bspw. Convolutional Neural Networks, Transformers) - Neural Networks (bspw. Convolutional Neural Networks, Transformers)
**Regression** **Regression**
- SGD Regressor - SGD Regressor
- RidgeRegression - RidgeRegression
**Assoziationsregeln** **Assoziationsregeln**
- Apriori Algorithm - Apriori Algorithm
- Eclat - Eclat
%% Cell type:markdown id:collected-genius tags: %% Cell type:markdown id:collected-genius tags:
# Clustering # Clustering
Welche Ähnlichkeitsstrukturen liegen in den Daten vor? Welche Ähnlichkeitsstrukturen liegen in den Daten vor?
Beispiele: Beispiele:
- Welche Gruppen von Kunden sind in der Datenbank? - Welche Gruppen von Kunden sind in der Datenbank?
- Welche Genre sind in der Musiklandschaft vorhanden? - Welche Genre sind in der Musiklandschaft vorhanden?
### Exploration ### Exploration
%% Cell type:code id:dd3d70cf tags: %% Cell type:code id:dd3d70cf tags:
``` ```
df = sns.load_dataset('iris') df = sns.load_dataset('titanic')
df.head() df.head()
```
%% Cell type:markdown id:3a49871e tags:
# Preprocessing: Columns to keep:
- survived/alive --> binary
- pclass & class zusammen --> ordinary
- sex/who --> binary
- age --> int/float
- parch-->
- fare --> float
- embarked --> class
- adult_male --> binary
- deck --> class
- embark_town --> class
- alone --> binary
%% Cell type:code id:e42d157a tags:
```
df["survived"]
df["alive"] = df["alive"].apply(lambda x: 1 if x == "yes" else 0)
```
%% Cell type:code id:c0b31b9c tags:
```
df = df.drop(["alive", "who", "deck"], axis=1)
```
%% Cell type:markdown id:be414200 tags:
# Spalten kodieren
- sklearn Encodern
- Pandas encoding
%% Cell type:code id:8f65e19c tags:
```
df
```
%% Cell type:code id:d69ef59e tags:
```
# Spalten zum ordinal kodieren
ordinal_cols = ["sex", "pclass", "adult_male", "alone"]
# OneHotEncoding Columns
onehot_cols = ["embarked"]
```
%% Cell type:code id:aa0cc913 tags:
```
df[ordinal_cols]
```
%% Cell type:markdown id:145a7892 tags:
# Ordinales Encoden
%% Cell type:code id:8d29be5c tags:
```
from sklearn.preprocessing import OrdinalEncoder
# reduce dataframe to cols to encode ordinally
df_ord = df[ordinal_cols]
enc = OrdinalEncoder()
# tell the encoder how to encode
enc.fit(df_ord)
enc.categories_
# transform the dataframe
X_ord = enc.transform(df_ord)
```
%% Cell type:code id:7242b07d tags:
```
df[onehot_cols]
```
%% Cell type:code id:e962c970 tags:
```
from sklearn.preprocessing import OneHotEncoder
# reduce dataframe to cols to encode ordinally
df_onehot = df[onehot_cols]
enc = OneHotEncoder(handle_unknown='ignore')
# tell the encoder how to encode
enc.fit(df_onehot)
enc.categories_
# transform the dataframe
X_onehot = enc.transform(df_onehot)
```
%% Cell type:markdown id:060a8fcf tags:
# Numpy Package importieren
%% Cell type:code id:5cd9c8ef tags:
```
import numpy as np
```
%% Cell type:markdown id:25c8c6c6 tags:
## Matrizen zusammenhängen
%% Cell type:code id:fbd6df67 tags:
```
X = np.concatenate((X_ord, X_onehot.todense()), axis=1)
X_onehot.shape
```
%% Cell type:markdown id:ad399bfb tags:
# Label absplitten
%% Cell type:code id:ceffbb2b tags:
```
X
y = df.survived.values
```
%% Cell type:markdown id:2e2601be tags:
# Train Test Split
%% Cell type:code id:c5521f70 tags:
```
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
X_train = np.array(X_train)
X_test = np.array(X_test)
y_train = np.array(y_train)
y_test = np.array(y_test)
```
%% Cell type:markdown id:6d8f5935 tags:
# Modelle trainieren
%% Cell type:markdown id:f63616a2 tags:
## Naive Bayes Estimator
%% Cell type:code id:b106a7a3 tags:
```
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
# Modell Training
gnb.fit(X_train, y_train)
# Vorhersagen Trainingsset
y_pred = gnb.predict(X_test)
```
%% Cell type:code id:d2fcba4b tags:
```
print("Number of mislabeled points out of a total %d points : %d"
% (X_test.shape[0], (y_test != y_pred).sum()))
```
%% Cell type:code id:0300a959 tags:
```
from sklearn.metrics import confusion_matrix
confusion_matrix(y_true=y_test, y_pred=y_pred)
```
%% Cell type:code id:683327a8 tags:
```
```
%% Cell type:markdown id:1000a61b tags:
## K-Nearest Neighbors
%% Cell type:code id:68c97117 tags:
```
X_ord[:20]
```
%% Cell type:code id:14eeb8ca tags:
```
```
%% Cell type:code id:324cb959 tags:
```
df.deck.value_counts()
``` ```
%% Cell type:code id:f606e9d2 tags: %% Cell type:code id:f606e9d2 tags:
``` ```
from sklearn.cluster import KMeans from sklearn.cluster import KMeans
import numpy as np import numpy as np
# feature extraction # feature extraction
X = df.drop('species', axis=1) X = df.drop('species', axis=1)
# model initialization # model initialization
kmeans = KMeans(n_clusters=3) kmeans = KMeans(n_clusters=3)
# training # training
kmeans.fit(X) kmeans.fit(X)
# #
y_pred = kmeans.predict(X) y_pred = kmeans.predict(X)
# assign predictions # assign predictions
df['predicted'] = y_pred df['predicted'] = y_pred
``` ```
%% Cell type:code id:6adb3d3d tags: %% Cell type:code id:6adb3d3d tags:
``` ```
# Ergebnisse plotten # Ergebnisse plotten
sns.scatterplot(data=df, x='sepal_length', y='petal_width', hue='predicted', style='species') sns.scatterplot(data=df, x='sepal_length', y='petal_width', hue='predicted', style='species')
plt.title('Clusters based on KMeans') plt.title('Clusters based on KMeans')
plt.show() plt.show()
``` ```
%% Cell type:code id:39fbe1e9 tags: %% Cell type:code id:39fbe1e9 tags:
``` ```
from sklearn.cluster import DBSCAN from sklearn.cluster import DBSCAN
import numpy as np import numpy as np
# init DBSCAN # init DBSCAN
dbscan = DBSCAN(eps=1, min_samples=2) dbscan = DBSCAN(eps=1, min_samples=2)
# train and predict DBSCAN # train and predict DBSCAN
y_pred2 = dbscan.fit_predict(X) y_pred2 = dbscan.fit_predict(X)
# #
df["predict2"] = y_pred df["predict2"] = y_pred
sns.scatterplot(data=df, x='sepal_length', y='petal_width', hue='predict2', style='species') sns.scatterplot(data=df, x='sepal_length', y='petal_width', hue='predict2', style='species')
plt.title('Clusters based on DBSCAN') plt.title('Clusters based on DBSCAN')
plt.show() plt.show()
``` ```
%% Cell type:markdown id:fixed-corporation tags: %% Cell type:markdown id:fixed-corporation tags:
## Klassifikation ## Klassifikation
Beispiele: Beispiele:
- Objekt-Klassifizierung - Objekt-Klassifizierung
- Kreditwürdigkeitsprüfung - Kreditwürdigkeitsprüfung
%% Cell type:markdown id:26e3add5 tags: %% Cell type:markdown id:26e3add5 tags:
### Exploration ### Exploration
%% Cell type:code id:64300d50 tags: %% Cell type:code id:64300d50 tags:
``` ```
from sklearn.datasets import load_iris from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB from sklearn.naive_bayes import GaussianNB
df = sns.load_dataset('iris') df = sns.load_dataset('iris')
X = df.drop('species', axis=1) X = df.drop('species', axis=1)
y = df['species'] y = df['species']
# train and test set split with a test set size of 50% # train and test set split with a test set size of 50%
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
# initialize Naive Bayes Classifier # initialize Naive Bayes Classifier
gnb = GaussianNB() gnb = GaussianNB()
# training # training
gnb.fit(X_train, y_train) gnb.fit(X_train, y_train)
# predict # predict
gnb.predict(X_test) gnb.predict(X_test)
# Evaluation # Evaluation
gnb.score(X_test, y_test) gnb.score(X_test, y_test)
``` ```
%% Cell type:markdown id:bae8b8bf tags: %% Cell type:markdown id:bae8b8bf tags:
<div class="alert alert-info"> <div class="alert alert-info">
Wir haben hier *nur* die Accuracy berechnet. Generell gibt es noch weitere Metriken die zur Evaluation von Klassifikationsverfahren verwendet werden können. Beispielsweise: **Precision, Recall und F1 Score.** Wir haben hier *nur* die Accuracy berechnet. Generell gibt es noch weitere Metriken die zur Evaluation von Klassifikationsverfahren verwendet werden können. Beispielsweise: **Precision, Recall und F1 Score.**
</div> </div>
%% Cell type:markdown id:1216f42c tags: %% Cell type:markdown id:1216f42c tags:
## Klassifikation auf Textdaten ## Klassifikation auf Textdaten
%% Cell type:markdown id:6644498a tags: %% Cell type:markdown id:6644498a tags:
### Exploration ### Exploration
%% Cell type:code id:bf0bbab7 tags: %% Cell type:code id:bf0bbab7 tags:
``` ```
``` ```
%% Cell type:markdown id:1b58a4cb tags: %% Cell type:markdown id:1b58a4cb tags:
### Vorverarbeitung ### Vorverarbeitung
%% Cell type:code id:e46bc6d3 tags: %% Cell type:code id:e46bc6d3 tags:
``` ```
``` ```
%% Cell type:markdown id:67a56b07 tags: %% Cell type:markdown id:67a56b07 tags:
### Training ### Training
%% Cell type:code id:63ea7162 tags: %% Cell type:code id:63ea7162 tags:
``` ```
``` ```
%% Cell type:markdown id:730b3c58 tags: %% Cell type:markdown id:730b3c58 tags:
### Evaluation ### Evaluation
%% Cell type:code id:a9f736fb tags: %% Cell type:code id:a9f736fb tags:
``` ```
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment