{ "cells": [ { "cell_type": "markdown", "id": "641931b5", "metadata": {}, "source": [ "# Aufgabe 1\n", "Arbeiten mit NumPy" ] }, { "cell_type": "code", "execution_count": null, "id": "064ce97d", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "id": "64c14dbd", "metadata": {}, "outputs": [], "source": [ "W = np.array([[1., 2, 3], [3, 4, 5]])" ] }, { "cell_type": "code", "execution_count": null, "id": "bf259722", "metadata": {}, "outputs": [], "source": [ "W.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "311adc5f", "metadata": {}, "outputs": [], "source": [ "W.shape[0]" ] }, { "cell_type": "code", "execution_count": null, "id": "d41d05fe", "metadata": {}, "outputs": [], "source": [ "W.shape[1]" ] }, { "cell_type": "code", "execution_count": null, "id": "911571e0", "metadata": {}, "outputs": [], "source": [ "W.T" ] }, { "cell_type": "code", "execution_count": null, "id": "aa7d64ee", "metadata": {}, "outputs": [], "source": [ "W.T.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "6fee5b46", "metadata": {}, "outputs": [], "source": [ "type(W)" ] }, { "cell_type": "code", "execution_count": null, "id": "b064a293", "metadata": {}, "outputs": [], "source": [ "type(W.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "915e6f9e", "metadata": {}, "outputs": [], "source": [ "r = np.arange(10)\n", "print(r)\n", "r.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "716e7295", "metadata": {}, "outputs": [], "source": [ "M = np.arange(12).reshape(3,4)\n", "print(M)\n", "print(M.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "92baea0a", "metadata": {}, "outputs": [], "source": [ "M[2,0]\n", "M[1,2]\n", "M[1,:]\n", "M[1]\n", "M[:,1]\n", "M[:,[1]]\n", "M[:,[3, 0, 1, 1]]\n", "M[:,2:4]\n", "M[-2,:]\n", "M[-2:,:]\n", "M[:,2] = 2" ] }, { "cell_type": "code", "execution_count": null, "id": "6d78e777", "metadata": {}, "outputs": [], "source": [ "M > 4\n", "M[M > 4]\n", "M[M > 4] = -17\n", "M" ] }, { "cell_type": "code", "execution_count": null, "id": "27fefbbb", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "s = 0\n", "for i in range(M.shape[0]):\n", " for j in range(M.shape[1]):\n", " s += M[i,j]\n", " print(s,',',sep='',end='')\n", " print(s)" ] }, { "cell_type": "code", "execution_count": null, "id": "a00b5af1", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "def func(x):\n", " print(x * x)\n", " return x" ] }, { "cell_type": "markdown", "id": "99670ae0", "metadata": {}, "source": [ "# Aufgabe 2\n", "Laden und Plotten des Iris-Blüten Datensatzes" ] }, { "cell_type": "markdown", "id": "941aabcc", "metadata": {}, "source": [ "a) Je Iris-Art gibt es 50 Datenpunkte." ] }, { "cell_type": "markdown", "id": "4e91507b", "metadata": {}, "source": [ "b) Laden des Datensatzes mit `loadtxt`." ] }, { "cell_type": "code", "execution_count": null, "id": "530bf005", "metadata": {}, "outputs": [], "source": [ "iris_data = np.loadtxt(\"../supplement_files/iris.csv\", delimiter=\",\")" ] }, { "cell_type": "markdown", "id": "25b3a3c1", "metadata": {}, "source": [ "c + d) Zerlegen der Daten in Matrizen X und T und den Konventionen anpassen.\n", "\n", "X = iris_data.T[0:4]\n", "T = iris_data.T[4:]" ] }, { "cell_type": "markdown", "id": "561b5bc1", "metadata": {}, "source": [ "e) Scatterplot" ] }, { "cell_type": "code", "execution_count": null, "id": "da07641f", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "# plt.scatter(X[0:1], X[1:2])\n", "# plt.show()" ] }, { "cell_type": "markdown", "id": "e673709f", "metadata": {}, "source": [ "f) Scatterplot mit Farben" ] }, { "cell_type": "code", "execution_count": null, "id": "a7d4ead0", "metadata": {}, "outputs": [], "source": [ "from matplotlib import colors\n", "\n", "cmap = colors.ListedColormap([\"red\", \"green\", \"blue\"])\n", "\n", "plt.scatter(X[0:1], X[1:2], c=T, cmap=cmap)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "1a5307aa", "metadata": {}, "source": [ "# Aufgabe 3\n", "Interaktive 3D Scatter Plots in Python" ] }, { "cell_type": "markdown", "id": "094b1e57", "metadata": {}, "source": [ "a + b) Iris-Blüten Datensatz mit plotly." ] }, { "cell_type": "code", "execution_count": null, "id": "da3b79d5", "metadata": {}, "outputs": [], "source": [ "import plotly.express as px\n", "\n", "df = px.data.iris()\n", "fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species')\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "1466c1b4", "metadata": {}, "source": [ "c) Plot as html speichern." ] }, { "cell_type": "code", "execution_count": null, "id": "4c20a687", "metadata": {}, "outputs": [], "source": [ "df = px.data.iris()\n", "fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species')\n", "fig.write_html(\"iris.html\")" ] }, { "cell_type": "markdown", "id": "a71792ac", "metadata": {}, "source": [ "d) Nur Merkmale zwei bis vier." ] }, { "cell_type": "code", "execution_count": null, "id": "5659a0a1", "metadata": {}, "outputs": [], "source": [ "df = px.data.iris()\n", "fig = px.scatter_3d(df, x=\"sepal_width\", y=\"petal_length\", z=\"petal_width\", color=\"species\")\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "e5d1f634", "metadata": {}, "source": [ "# Aufgabe 4\n", "Der Thyroid Disease Datensatz -- ein etwas gröÿerer Datensatz" ] }, { "cell_type": "markdown", "id": "295ec0d0", "metadata": {}, "source": [ "a) Der Datensatz hat 22 Merkmale." ] }, { "cell_type": "code", "execution_count": null, "id": "0d36e655", "metadata": {}, "outputs": [], "source": [ "patient_data = np.loadtxt(\"../supplement_files/thyroid-ann-train.data\")\n", "print(len(patient_data[0]))" ] }, { "cell_type": "markdown", "id": "a313d84d", "metadata": {}, "source": [ "b) 15 Merkmale sind binär und 6 sind kontinuiertlich. Die Patientengruppe ist nominales Merkmal." ] }, { "cell_type": "markdown", "id": "f6c34fe8", "metadata": {}, "source": [ "c) Es gibt drei verschiedene Patientengruppen." ] }, { "cell_type": "code", "execution_count": null, "id": "91a74614", "metadata": {}, "outputs": [], "source": [ "X = patient_data.T[0:21]\n", "T = patient_data.T[21:]\n", "\n", "print(np.unique(T).size)" ] }, { "cell_type": "markdown", "id": "8d6abcd4", "metadata": {}, "source": [ "d) Anzahl der Patienten pro Gruppe." ] }, { "cell_type": "code", "execution_count": null, "id": "8f075e3d", "metadata": {}, "outputs": [], "source": [ "print(np.unique(T, return_counts=True)[1])" ] }, { "cell_type": "markdown", "id": "e11160cb", "metadata": {}, "source": [ "e) Plotten verschiedener Merkmalskombinationen, eingefärbt nach Patientengruppe." ] }, { "cell_type": "markdown", "id": "a46c1fc9", "metadata": {}, "source": [ "i) Merkmal 0, 16, 17" ] }, { "cell_type": "code", "execution_count": null, "id": "60a641ca", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter_3d(x=X[0], y=X[16], z=X[17], color=T[0].astype(int).astype(str),\n", " labels={'x': 'Merkmal 0', 'y': 'Merkmal 16', 'z': 'Merkmal 17', 'color': 'Gruppe'})\n", "fig.update_traces(marker={'size': 3})\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "65a3fe5e", "metadata": {}, "source": [ "ii) Merkmal 0, 17, 18" ] }, { "cell_type": "code", "execution_count": null, "id": "2c37db7b", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter_3d(x=X[0], y=X[17], z=X[18], color=T[0].astype(int).astype(str),\n", " labels={'x': 'Merkmal 0', 'y': 'Merkmal 17', 'z': 'Merkmal 18', 'color': 'Gruppe'})\n", "fig.update_traces(marker={'size': 3})\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "c1ff9466", "metadata": {}, "source": [ "iii) Merkmal 18, 19, 20" ] }, { "cell_type": "code", "execution_count": null, "id": "72bfc023", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter_3d(x=X[18], y=X[19], z=X[20], color=T[0].astype(int).astype(str),\n", " labels={'x': 'Merkmal 18', 'y': 'Merkmal 19', 'z': 'Merkmal 20', 'color': 'Gruppe'})\n", "fig.update_traces(marker={'size': 3})\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "ed9f06a1", "metadata": {}, "source": [ "iv) Merkmal 16, 18, 20" ] }, { "cell_type": "code", "execution_count": null, "id": "e90baab5", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter_3d(x=X[16], y=X[18], z=X[20], color=T[0].astype(int).astype(str),\n", " labels={'x': 'Merkmal 16', 'y': 'Merkmal 18', 'z': 'Merkmal 20', 'color': 'Gruppe'})\n", "fig.update_traces(marker={'size': 3})\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "7b3af1bc", "metadata": {}, "source": [ "f) Am besten geeignet ist vermutlich Kombination iv) (16, 18, 20), danach i) (0, 16, 17).\n", "Entscheidend ist Merkmal 16: Gruppe 1 hat hier deutlich höhere Werte als Gruppe 2 und diese\n", "wiederum höhere als Gruppe 3. Zusätzlich trennen 18 und 20 Gruppe 1 gut von den anderen.\n", "\n", "Am schlechtesten geeignet ist ii) (0, 17, 18), da Merkmal 16 fehlt und sich Gruppe 2 und 3\n", "dort stark überlappen. iii) (18, 19, 20) trennt Gruppe 1 noch ganz gut ab, Gruppe 2 und 3\n", "überlappen aber ebenfalls stark." ] }, { "cell_type": "markdown", "id": "40ac7c0e", "metadata": {}, "source": [ "g) Ja:\n", "- Merkmal 0 (Alter) ist in i) und ii) vermutlich überflüssig, die Werte unterscheiden sich\n", " zwischen den Gruppen kaum.\n", "- Merkmal 19 in iii) unterscheidet sich zwischen den Gruppen ebenfalls kaum.\n", "- In iv) sind Merkmal 18 und 20 stark korreliert (r ≈ 0.77), eines davon bringt vermutlich\n", " kaum zusätzliche Information." ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }