2wenty1ne 2025-01-15 14:16:49 +01:00
parent 9b8a746e15
commit b7841fd8b1
13 changed files with 357 additions and 0 deletions

View File

@ -0,0 +1,22 @@
import doctest
def filter_and_scale(numbers, scale=1):
"""
Eine Funktion um aus einer gegebenen Liste alle positiven Zahlen zu extrahieren, diese mit dem Skalierungsfaktor zu addieren und eine Liste mit den Werten zurückzugeben.
:param numbers: Liste mit allen Zahlen
:param scale: Skalierungsfaktor, default Wert ist 1
:return: Array mit nur positiven skalierten Zahlen
>>> filter_and_scale([1, -2, 3, 0, -4, 5], scale=2)
[2, 6, 10]
"""
returnList = []
for elem in numbers:
if elem > 0:
returnList.append(elem * scale)
return returnList
if __name__ == "__main__":
doctest.testmod()

View File

@ -0,0 +1,17 @@
F = lambda x: x*x
print(F(5))
t = (1,2,3,7)
print(type(t))
u = list(t)
print(type(u))
print(u[-1])
# for idx, x in enumerate(t):
# print(idx, x)

View File

@ -0,0 +1,22 @@
import numpy as np
d=np.load("npdata12.npy")
print(d)
print("\n\nG1")
g1 = d[d[:, 0] == 1][:,1]
print(g1.mean())
print(g1.shape)
# print(head(d))
M = np.array([[1,2,3], [4,5,6]])
r = M[:,[1,0,2,1]]
# print(r)

Binary file not shown.

11
PYTHON/daten.csv 100644
View File

@ -0,0 +1,11 @@
ID;Alter;Geschlecht;Blutgruppe;Raucher;Größe;Chars
10;1;W;6;nein;168;1
10;17;W;6;nein;168;2
20;5;M;0;ja;175;3
30;24;W;1;ja;176;4
40;22;D;2;ja;177;5
50;23;M;3;nein;178;6
60;6;D;4;ja;178;5
70;23;M;5;nein;170;6
75;6;W;6;ja;179;0
90;23;M;7;ja;179;0
1 ID Alter Geschlecht Blutgruppe Raucher Größe Chars
2 10 1 W 6 nein 168 1
3 10 17 W 6 nein 168 2
4 20 5 M 0 ja 175 3
5 30 24 W 1 ja 176 4
6 40 22 D 2 ja 177 5
7 50 23 M 3 nein 178 6
8 60 6 D 4 ja 178 5
9 70 23 M 5 nein 170 6
10 75 6 W 6 ja 179 0
11 90 23 M 7 ja 179 0

28
PYTHON/matplot.py 100644
View File

@ -0,0 +1,28 @@
import matplotlib.pyplot as plt
year = [1800, 1850, 1900, 1950, 1970, 1990, 2010, 2100]
pop = [1.0, 1.262, 1.650, 2.519, 3.692, 5.263, 6.972, 10.85]
values = [0,0.6,1.4,1.6,2.2,2.5,2.6,3.2,3.5,3.9,4.2,6]
# plt.scatter(year, pop)
# # plt.plot(year, pop)
# plt.xlabel('Year')
# plt.ylabel('Population')
# plt.title('World Population')
# plt.yticks([0, 2, 4, 6, 8, 10],
# ['0', '2 Mrd', '4 Mrd',
# '6 Mrd', '8 Mrd', '10 Mrd'])
# plt.show()
# plt.figure()
f, ax = plt.subplots(1,2, figsize=(3,1))
ax[0].plot(year, pop)
ax[1].hist(values, bins=3)
plt.tight_layout()
plt.show()

14
PYTHON/nastja.py 100644
View File

@ -0,0 +1,14 @@
import numpy as np
e = np.array([[1,2,3], [2,3,4]])
s = "Hallo"
for x in dir(e):
print(x)
print(s.lower())

97
PYTHON/p1.py 100644
View File

@ -0,0 +1,97 @@
import numpy as np
s = {1,2,3}
sn = np.array(s)
print(sn)
print(type(sn))
l = [1,2,3]
ln = np.array(l)
print(ln)
print(type(ln))
t = (1,2,3)
tn = np.array(t)
print(tn)
print(type(tn))
s = "Hallo"
print(s[2:])
print(s[:-2])
a = [1,2,3]
c = a
b = a[:]
a[0] = 6
print(f"A: {a}")
print(f"B: {b}")
print(f"C: {c}")
print("\n")
a = np.array([7,3,1])
print(a)
a += [1,2,3]
print(a)
print("\n")
b = np.array([[1,2], [2,3], [4,5]])
print(b)
print("\n")
inner = [[1,2,0], [0,3,4]]
c = np.array([inner, inner])
print(c.ndim)
print(c.shape)
print("\n")
d = np.array([1,2])
print(d)
print(d.T)
print("\n")
M = np.array([[1,2,3], [2,3,4], [3,4,5]])
N = M.copy()
print(M)
print("")
print(f"Test:")
print(M[:,1:3])
print(M[:, [1,2,1]])
MSpalte = M[:, 0:2]
MZeile = M[:, [0,2,0]]
print(f"Spalte:\n {MSpalte}")
print(f"Zeile:\n {MZeile}")
print("\n")
M = np.array([[1,2,3], [2,3,4], [3,4,5]])
MCon = M > 2
print(f"Con:\n {MCon}")
M[M>2]=17
print(M)
print("\n")
print(N)
print("")
new = (N>3) & (N > 3)
print(new)
print((N > 2) & (N < 5))
print("\n")
vec1 = np.array([1,2,3])
vec2 = np.array([2,3,4])
print(np.matmul(vec1, vec2))

66
PYTHON/panda.py 100644
View File

@ -0,0 +1,66 @@
import pandas as pd
df = pd.read_csv("daten.csv",
delimiter=";",
na_values=["."],
index_col="ID",
encoding="utf-8")
df2 = df.sort_values(["Größe", "ID"])
# df3 = df.drop([80,90])
# df4 = df.drop(columns=["Raucher", "Blutgruppe"])
df3 = df2[df2["Größe"] > 175]
# elems = df2.loc[[10], "Blutgruppe":"Größe"]
# elems = df2.iloc[0:3, 0:2]
# elems = df2.loc[20:80,"Geschlecht":"Raucher"]
print(df2)
# print("")
# print(f"Type: {type(elems)}")
# print(elems)
# print(df2.index)
# df5 = df2.set_index("Alter", append=True)
df5 = df2.set_index(["Alter", "Geschlecht"], append=True)
# print(df5)
# print(df5.index)
rowsIter = df.iterrows()
colsIter = df.items()
# print("Befor row loop:\n")
# for rowLabel, row in rowsIter:
# print(f"Label: {type(rowLabel)}, row: {type(row)}")
# print(f"{rowLabel}:\n{row}\n")
# print("Befor col loop:\n")
# for colLabel, col in colsIter:
# print(f"Label: {type(colLabel)}, Col: {type(col)}")
# print(f"{colLabel}:\n{col}\n")
# print("\n")
# nextCol = next(colsIter)
# nextColLabel = nextCol[0]
# nextColSeries = nextCol[1]
# print(f"Label: {nextColLabel}")
# print(f"Series:\n{nextColSeries}")
# seriesIter = nextColSeries.items()
# nextElem = next(seriesIter)
# print(nextElem)
print(df2)
df7 = df2.fillna(0)
# print(df7)
print(df)
meanRet = df.max()
print(type(meanRet))
print(meanRet)

80
PYTHON/pandas2.py 100644
View File

@ -0,0 +1,80 @@
import pandas as pd
import numpy as np
df = pd.read_csv("daten.csv",
delimiter=";",
na_values=["."],
index_col="ID",
encoding="utf-8")
meanRet = df.max()
# print(type(meanRet))
# print(meanRet)
onlyNumsDf = df[["Alter", "Größe"]]
# print(onlyNumsDf.mean())
df["Alter2"] = df["Alter"] * 2
# print(df)
onlyAlter = df["Alter"]
alterMean = onlyAlter.max()
# print(type(alterMean))
# print(alterMean)
charMap = {
0: "",
1: "A",
2: "M",
3: "O",
4: "G",
5: "U",
6: "S",
}
def intToChar(col):
retString = ""
for num in col:
char = charMap.get(num, " ")
retString += char
return retString
def timesTwo(num):
return num*2
charNums = df[["Alter", "Chars"]]
print(charNums)
print("")
def map_numbers_to_string(column):
return "".join(charMap[num] for num in column)
print(charNums.apply(intToChar, axis=0))
print("")
print(charNums.apply(timesTwo, axis=0))
# print(charNums.apply(intToChar))
print("")
alterSeries = df[["Alter"]]
print(alterSeries.apply(np.sum, axis=0))
print(df)
raucherGeschlechtDf = df.loc[10:70 ,["Raucher","Geschlecht"]]
geschlechtGrößeDf = df.loc[50:90, ["Geschlecht", "Größe"]]
alterBlutgruppeDf = df.loc[50:90 ,["Alter", "Blutgruppe"]]
print("\nRaucher Geschlecht: ")
print(raucherGeschlechtDf)
print("\nGeschlecht Größe: ")
print(geschlechtGrößeDf)
# print("\nMerge: ")
# res = raucherGeschlechtDf.merge(geschlechtGrößeDf, on="Geschlecht")
print("\nGroup: ")
res = raucherGeschlechtDf.groupby(geschlechtGrößeDf, by="Geschlecht")
print(res)

View File