d und g optimiert
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import numpy as np # pip3 install numpy
|
||||
import pandas as pd # pip3 install pandas
|
||||
import matplotlib.pyplot as plt
|
||||
import scipy as sp
|
||||
from scipy import stats
|
||||
### Import der benötigten Bibliotheken
|
||||
# pip3 install pandas ...
|
||||
import pandas as pd # für a)
|
||||
import sys # für b)
|
||||
import numpy as np # für b)
|
||||
import matplotlib.pyplot as plt # für d)
|
||||
import scipy as sp # für e)
|
||||
from scipy import stats # für e)
|
||||
|
||||
### a) Einlesen der Quelldaten
|
||||
# Pandas-Methode read_csv() zum Einlesen nutzen, wobei die Spalte namens "id" ausgelassen wird
|
||||
@@ -44,7 +47,10 @@ df.replace(to_replace=' ', value=np.nan, inplace=True)
|
||||
df.dropna(axis=0, how='any', inplace=True)
|
||||
|
||||
# Nach dieser Änderung sind nur noch 200 Zeilen im DataFrame
|
||||
print(len(df.index))
|
||||
if (len(df.index) != 200):
|
||||
print("Es sind zu viele Zeilen im DataFrame")
|
||||
sys.exit()
|
||||
print("Anzahl der Zeilen:", len(df.index))
|
||||
# Ausgabe:
|
||||
# 200
|
||||
|
||||
@@ -71,45 +77,17 @@ dfUnivariateAnalyse.to_csv('UnivariateAnalyse.csv')
|
||||
### d) Balkendiagramme
|
||||
|
||||
# Arbeitstabelle erzeugen, die nur die Spalten der nominalen Merkmale enthält:
|
||||
# inkl. Typenumwandlung zur Ganzzahl: .astype(int)
|
||||
dfNominaleMerkmale = df[["bluetooth", "dual_sim", "4G"]].astype(int)
|
||||
|
||||
# print(df["bluetooth"].value_counts())
|
||||
|
||||
# Jetzt Abwechselnd:
|
||||
# ___Yes = Extrahieren der Datensätze mit Merkmalsausprägung 1 (yes)
|
||||
# anzahl___Yes = Anzahl der Datensätze ermitteln
|
||||
# ___No = Extrahieren der Datensätze mit Merkmalsausprägung 0 (no)
|
||||
# anzahl___No = Anzahl der Datensätze ermitteln
|
||||
bluetoothYes = dfNominaleMerkmale.loc[dfNominaleMerkmale['bluetooth'] == 1]
|
||||
anzahlBluetoothYes = bluetoothYes.shape[0] # Anzahl ermitteln
|
||||
bluetoothNo = dfNominaleMerkmale.loc[dfNominaleMerkmale['bluetooth'] == 0,]
|
||||
anzahlBluetoothNo = bluetoothNo.shape[0] # Anzahl ermitteln
|
||||
dualSimYes = dfNominaleMerkmale.loc[dfNominaleMerkmale['dual_sim'] == 1]
|
||||
anzahlDualSimYes = dualSimYes.shape[0] # Anzahl ermitteln
|
||||
dualSimNo = dfNominaleMerkmale.loc[dfNominaleMerkmale['dual_sim'] == 0]
|
||||
anzahlDualSimNo = dualSimNo.shape[0] # Anzahl ermitteln
|
||||
g4Yes = dfNominaleMerkmale.loc[dfNominaleMerkmale['4G'] == 1]
|
||||
anzahl4GYes = g4Yes.shape[0] # Anzahl ermitteln
|
||||
g4No = dfNominaleMerkmale.loc[dfNominaleMerkmale['4G'] == 0]
|
||||
anzahl4GNo = g4No.shape[0] # Anzahl ermitteln
|
||||
|
||||
# Neues Dataframe erzeugen, das je Merkmal die Anzahl der Yes/No Ausprägungen abbildet
|
||||
anzahlYes = [anzahlBluetoothYes, anzahlDualSimYes, anzahl4GYes]
|
||||
anzahlNo = [anzahlBluetoothNo, anzahlDualSimNo, anzahl4GNo]
|
||||
index = ["bluetooth", "dual_sim", "4G"] # Beschriftung der indeX-Achse
|
||||
dfAnzahlYesNo = pd.DataFrame({'yes': anzahlYes, 'no': anzahlNo}, index=index)
|
||||
# print(dfAnzahlYesNo)
|
||||
# yes no
|
||||
# bluetooth 104 96
|
||||
# dual_sim 109 91
|
||||
# 4G 90 110
|
||||
|
||||
# Für dieses Dataframe ein Balkendiagramm erzeugen, mit Rotation=0
|
||||
dfAnzahlYesNo.plot.bar()
|
||||
#plt.show()
|
||||
dfNominaleMerkmale = df[["bluetooth", "dual_sim", "4G"]]
|
||||
|
||||
# Die Merkmalsausprägungen je Merkmal zählen (0=No, 1=Yes)
|
||||
# + benötigte Typenumwandlungen zu Ganzzahlen (0.0-> 0, 1.0-> 1) per .astype(int)
|
||||
srBluetoothAnzahl = df["bluetooth"].astype(int).value_counts()
|
||||
srDualSimAnzahl = df["dual_sim"].astype(int).value_counts()
|
||||
sr4gAnzahl = df["4G"].astype(int).value_counts()
|
||||
|
||||
dfAnzahlen = pd.concat([srBluetoothAnzahl, srDualSimAnzahl, sr4gAnzahl], axis=1);
|
||||
dfAnzahlen.plot.bar(xlabel="0=No, 1=Yes")
|
||||
plt.show()
|
||||
|
||||
### e) Korrellationen nach Pearson und Lineare Regression zweier Merkmale
|
||||
print(dfMetrischeMerkmale.corr(method="pearson"))
|
||||
@@ -123,24 +101,24 @@ print(dfMetrischeMerkmale.corr(method="pearson"))
|
||||
# Per Modul SciPy Stats: Methode der kleinsten Quadrate für die Lineare Regression nutzen
|
||||
werteListeX = dfMetrischeMerkmale["ram"]
|
||||
werteListeY = dfMetrischeMerkmale["battery_power"]
|
||||
regrErgebnisse = sp.stats.linregress(werteListeX, werteListeY)
|
||||
steigung = round(regrErgebnisse.slope, 4)
|
||||
yAchsAbschn = round(regrErgebnisse.intercept, 4)
|
||||
modell = sp.stats.linregress(werteListeX, werteListeY)
|
||||
steigung = round(modell.slope, 4)
|
||||
yAchsAbschn = round(modell.intercept, 4)
|
||||
arrYpredicted = steigung * werteListeX + yAchsAbschn # using y = m*x + n, calculate every single Y-Value fitting the regression Lines X-Values
|
||||
print("Regressionsgleichung:", "y =", steigung, "* x +", yAchsAbschn)
|
||||
#print("Regressionsgleichung:", "y =", steigung, "* x +", yAchsAbschn)
|
||||
|
||||
# Plot Linear Regression Line
|
||||
plt.clf() # Clear last plot image
|
||||
plt.plot(werteListeX, arrYpredicted, label='Lin Regression', color='red', linestyle='solid') # https://scriptverse.academy/tutorials/python-matplotlib-plot-straight-line.html
|
||||
# Show Plot Image
|
||||
# Linear Regressions Linie plotten
|
||||
plt.clf() # Letzte Plot Grafik löschen
|
||||
plt.xlabel('ram', color='black')
|
||||
plt.ylabel('battery_power', color='black')
|
||||
#plt.xlim([0,50]) # set x-Axis View Range,[from,to]
|
||||
plt.scatter(werteListeX, werteListeY)
|
||||
#plt.show()
|
||||
plt.plot(werteListeX, arrYpredicted, label='Lin Regression', color='red', linestyle='solid') # https://scriptverse.academy/tutorials/python-matplotlib-plot-straight-line.html
|
||||
# Show Plot Image
|
||||
plt.show()
|
||||
|
||||
|
||||
### f) Skalierung in [0, 1]
|
||||
# Je metrischem Merkmal eine Zusatzspalte erzeugen, die deren Werte in das Intervall [0,1] skaliert:
|
||||
dfMetrischeMerkmale["battery_power_skaliert"] = dfMetrischeMerkmale["battery_power"] / dfMetrischeMerkmale["battery_power"].max()
|
||||
dfMetrischeMerkmale["int_memory_skaliert"] = dfMetrischeMerkmale["int_memory"] / dfMetrischeMerkmale["int_memory"].max()
|
||||
dfMetrischeMerkmale["ram_skaliert"] = dfMetrischeMerkmale["ram"] / dfMetrischeMerkmale["ram"].max()
|
||||
@@ -153,16 +131,12 @@ dfMetrischeMerkmale["ram_skaliert"] = dfMetrischeMerkmale["ram"] / dfMetrischeMe
|
||||
|
||||
### g) Boxplots
|
||||
|
||||
plt.clf() # Clear last plot image
|
||||
plt.close('all')
|
||||
plt.boxplot(dfMetrischeMerkmale["battery_power"], showfliers=True)
|
||||
# Boxplots je metrischem Merkmal mit matplotlib erstellen
|
||||
fig, (ax1 , ax2, ax3) = plt.subplots(1,3, figsize=(10, 5)) # (1,3) = 1 Zeile, 3 Spalten
|
||||
ax1.set_title("Battery") # Subplot 1 Titel
|
||||
ax2.set_title("Ram")
|
||||
ax3.set_title("Memory")
|
||||
dfMetrischeMerkmale["battery_power"].plot(ax=ax1, kind="box", grid=True, showfliers=True, whis=[5, 95])
|
||||
dfMetrischeMerkmale["ram"].plot(ax=ax2, kind="box", grid=True, showfliers=True, whis=[5, 95])
|
||||
dfMetrischeMerkmale["int_memory"].plot(ax=ax3, kind="box", grid=True, showfliers=True, whis=[5, 95])
|
||||
plt.show()
|
||||
#dfMetrischeMerkmale.boxplot("battery_power", showfliers=True, backend="matplotlib")
|
||||
#plt.show()
|
||||
plt.clf() # Clear last plot image
|
||||
dfMetrischeMerkmale.boxplot("int_memory", showfliers=True)
|
||||
plt.show()
|
||||
plt.clf() # Clear last plot image
|
||||
dfMetrischeMerkmale.boxplot("ram", showfliers=True)
|
||||
plt.show()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user