From f967eef74cffd3745b20571d7570190a02e4f8d1 Mon Sep 17 00:00:00 2001 From: dev weycloud Date: Thu, 7 Oct 2021 19:58:31 +0200 Subject: [PATCH] Vorlesung 4 --- .gitignore | 1 + README.md | 9 ++++- Sonstiges/STAT2/vl2-varianz-v1.csv | 6 +++ Sonstiges/STAT2/vl2-varianz-v1.py | 37 +++++++++++++++++ Sonstiges/linearfunction.py | 43 ++++++++++++++++++++ Sonstiges/spss-import.py | 8 ++++ Vorlesung 4/Uebung1.py | 65 ++++++++++++++++++++++++++++++ 7 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Sonstiges/STAT2/vl2-varianz-v1.csv create mode 100644 Sonstiges/STAT2/vl2-varianz-v1.py create mode 100644 Sonstiges/linearfunction.py create mode 100644 Sonstiges/spss-import.py create mode 100644 Vorlesung 4/Uebung1.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6f9a44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/settings.json diff --git a/README.md b/README.md index e4b81f2..15e9374 100644 --- a/README.md +++ b/README.md @@ -38,4 +38,11 @@ - ```kontrollstukturen.py``` - ```Uebung1.py``` - Schleifen - - ```Uebung2.py``` \ No newline at end of file + - ```Uebung2.py``` + +# Vorlesung 4 +07.10.2021 +- ```Vorlesung IV.pdf``` + - Listen und Arrays + - ```Uebung1.py``` + - ToDo: ```Uebung2.py``` diff --git a/Sonstiges/STAT2/vl2-varianz-v1.csv b/Sonstiges/STAT2/vl2-varianz-v1.csv new file mode 100644 index 0000000..8b4e7ee --- /dev/null +++ b/Sonstiges/STAT2/vl2-varianz-v1.csv @@ -0,0 +1,6 @@ +x,freq +1,9 +2,7 +3,5 +4,4 +5,2 \ No newline at end of file diff --git a/Sonstiges/STAT2/vl2-varianz-v1.py b/Sonstiges/STAT2/vl2-varianz-v1.py new file mode 100644 index 0000000..51024cd --- /dev/null +++ b/Sonstiges/STAT2/vl2-varianz-v1.py @@ -0,0 +1,37 @@ +import pandas as pd +import numpy as np + +df = pd.read_csv('/home/pi/Documents/Code/Python/ProgrammierungUndDatenanalyse/Sonstiges/STAT2/vl2-varianz-v1.csv') + +# Dataframe +print(df) +# x freq +# 0 1 9 +# 1 2 7 +# 2 3 5 +# 3 4 4 +# 4 5 2 +print(df.sum()) +# x 15 +# freq 27 +sums = df.sum() +print(sums['freq']) +# 27 + +# Calculate Mean, respecting frequencies +rowSum = 0 +for index, row in df.iterrows(): + rowSum = rowSum + row.x * row.freq +mean = rowSum / sums.freq +print("mean: ", mean) + +# Calculate Variance, respecting frequencies +# Sample Variance: ^σ² = (1 / n - 1) * Σ(freq*(x - mean)²) +variancePart1 = (1 / (sums.freq - 1)) +variancePart2 = 0 +for index, row in df.iterrows(): + variancePart2 = variancePart2 + (row.freq * (row.x - mean)**2) + print(row['x'], row['freq'], variancePart2) +variance = variancePart1 * variancePart2 +print("variance: ", variance) +# variance: 1.703703703703704 \ No newline at end of file diff --git a/Sonstiges/linearfunction.py b/Sonstiges/linearfunction.py new file mode 100644 index 0000000..18ff22e --- /dev/null +++ b/Sonstiges/linearfunction.py @@ -0,0 +1,43 @@ +# Entry Level, Single Machine + +# https://jupyter.org/try +# oder: + # apt-get install libatlas-base-dev + # pip3 install numpy + # (pip install matplotlib==2.0.2 (py2: sonst Problem mit "functools_lru_cache")) + # sudo apt-get install python-scipy + # sudo apt update + # sudo apt install libatlas-base-dev + # pip3 install pybind11 + # pip3 install scipy + # apt install python3-gi-cairo + +import matplotlib.pyplot as plt +import numpy as np + +x = np.linspace(0,5,100) # Start und Ende der x-Achse +plt.figure(figsize=(10,8)) # Inches +plt.title('Entry Level, Single Machine') +plt.xlabel('Jahre', color='black') +plt.ylabel('Euro', color='black') + +# NW-110 Essential +y = 240*x+190 +plt.plot(x, y, label='NW-110 Essential', color='blue', linestyle='solid') + +# NW-140 Essential +y = 360*x+190 +plt.plot(x, y, label='NW-140 Essential', color='darkblue', linestyle='dashed') + +# FG-30E FortiCare +y = 80*x+393 +plt.plot(x, y, label='FG-30E FortiCare', color='lightgreen', linestyle='dotted') + +# FG-40F FortiCare +y = 91*x+453 +plt.plot(x, y, label='FG-40F FortiCare', color='green', linestyle='dashdot') + +# Plot +plt.legend(loc='upper left') +plt.grid() +plt.show() \ No newline at end of file diff --git a/Sonstiges/spss-import.py b/Sonstiges/spss-import.py new file mode 100644 index 0000000..75ac552 --- /dev/null +++ b/Sonstiges/spss-import.py @@ -0,0 +1,8 @@ +# pip3 install pyreadstat +import pandas as pd +import numpy as np +import pyreadstat + +df, meta = pyreadstat.read_sav('/home/pi/Downloads/ESS9e03 (2018).sav') + +print(df.head()) \ No newline at end of file diff --git a/Vorlesung 4/Uebung1.py b/Vorlesung 4/Uebung1.py new file mode 100644 index 0000000..d2c6aa9 --- /dev/null +++ b/Vorlesung 4/Uebung1.py @@ -0,0 +1,65 @@ +# Große Datenmenge > 1000 Datensätze +from statistics import mean, median, stdev, variance # z.b. mean(list) + +def inputNumericString(inp): + try: + float(inp) + except ValueError: + print("Could not convert Data to an integer") + else: + return float(inp).inputNumericString() + +list = [] +print("Bitte geben Sie Ihre ersten 5 Modulnoten ein") +list.append(int(input("Bitte geben Sie die nächste Modulnote ein: "))) +list.append(int(input("Bitte geben Sie die nächste Modulnote ein: "))) +list.append(int(input("Bitte geben Sie die nächste Modulnote ein: "))) +list.append(int(input("Bitte geben Sie die nächste Modulnote ein: "))) +list.append(int(input("Bitte geben Sie die nächste Modulnote ein: "))) +# Weitere: .clear() .count(el) .extend(otherList) .index(el) .insert(pos,el) .len() .remove(el) .reverse .sort(reverse) + +# 1.1 +modus = input("Bitte geben Sie 's' ein, wenn sie die Liste sortiert ausgeben möchten.") +if modus != "s": + print("Unsortiert: ", list) +else: + list.sort() + print("Sortiert: ", list) + +# 1.2 +needle = int(input("Welche Note soll gesucht werden?: ")) +i=0 +while i < len(list): + if i < len(list): + try: + print("Die Note", needle, "ist an Listen-Position: ", list.index(needle, i, i+1)) # from pos i to pos i + pass # not: break + except ValueError: + # triggers in every item, that doesnt match the needle + pass # not: break + i += 1 + +# 1.3 +print("Min: ", min(list)) +print("Max: ", max(list)) +print("Median: ", median(list)) +print("Mittelwert: ", mean(list)) +print("Varianz: ", variance(list)) +print("Standardabweichung: ", stdev(list)) +list.remove(5) +print("Median (ohne 5en): ", median(list)) +print("Mittelwert (ohne 5en): ", mean(list)) + +# 1.4 +eingabe = input('Wenn gewünscht, geben Sie bitte eine weitere Note (1-5) ein: ') +if eingabe == '1' or eingabe == '2' or eingabe == '3' or eingabe == '4' or eingabe == '5': + neueZahl = int(eingabe) + #print(list[list.count()]) + #storedLastItem = list[len(list)-1] # Store last item.. + #list.append(storedLastItem) + #list.insert(0, neueZahl) + list.append(neueZahl) + #list.append(storedLastItem) # ReStore last item + print(list) +else: + print('Anzahl der Modulnoten: ', list.count()) \ No newline at end of file