Python (Lineare Regression)?


29.08.2022, 23:10

x = [145, 130, 130, 120, 120, 140, 140, 120, 172, 150, 140, 130, 130, 110, 150]

y =[233, 250, 204, 236, 354, 192, 294, 263, 199, 168, 239, 275, 266, 211, 283] 

plt.scatter(x,y)

plt.xlabel("puls")

plt.ylabel("Cholesterin")

plt.show() 

Hanibal545  29.08.2022, 23:00

Kannst du vielleicht uns den rohen Code zu Verfügung stellen und kein Bild (sodass man es einfach kopieren kann und nicht abtippen muss)

miranda9 
Fragesteller
 29.08.2022, 23:09

Habe es in der Fragestellung hinzugefügt :)

DieZahl3  30.08.2022, 09:20

Warum googelt ihr das nicht einfach?

miranda9 
Fragesteller
 30.08.2022, 09:22

Haben wir schon versucht, Stell dir vor?

3 Antworten

Vom Fragesteller als hilfreich ausgezeichnet

Ihr könntet mit sklearn die Lineare Regressionsrate berechnen und mit matplotlib es reinzeichnen.

from sklearn import linear_model
import numpy as np
import matplotlib.pyplot as plt

x= np.array( [145, 130, 130, 120, 120, 140, 140, 120, 172, 150, 140, 130, 130, 110, 150]).reshape(-1, 1)
y= [233, 250, 204, 236, 354, 192, 294, 263, 199, 168, 239, 275, 266, 211, 283] 
lm = linear_model.LinearRegression()
lm.fit(x, y) 
plt.scatter(x, y, color = "r",marker = "o", s = 30) #https://matplotlib.org/stable/api/markers_api.html#module-matplotlib.markers
y_pred = lm.predict(x)
plt.plot(x, y_pred, color = "k")
plt.ylabel("Cholesterin")
plt.xlabel("puls")
plt.show()

Alternativ könnte man es auch so machen:

import matplotlib.pyplot as plt
from scipy import stats

x= [145, 130, 130, 120, 120, 140, 140, 120, 172, 150, 140, 130, 130, 110, 150]
y= [233, 250, 204, 236, 354, 192, 294, 263, 199, 168, 239, 275, 266, 211, 283] 

slope, intercept, r, p, std_err = stats.linregress(x, y)
def myfunc(x):
  return slope * x + intercept
mymodel = list(map(myfunc, x))
plt.scatter(x, y, color = "r",marker = "o", s = 30)
plt.plot(x, mymodel, color = "k")
plt.ylabel("Cholesterin")
plt.xlabel("puls")
plt.show()
Woher ich das weiß:Hobby
miranda9 
Fragesteller
 30.08.2022, 13:56

Könntest du mir vielleicht noch erklären was marker = o und s = 30 bedeutet :) ?

1
Hanibal545  30.08.2022, 14:06
@miranda9

Marker heißt einfach, welche Form die Punkte haben sollen (Das sind z.B. Beispielmarker: https://matplotlib.org/stable/api/markers_api.html#module-matplotlib.markers)

S bedeutet, wie groß ein Marker sein soll. Und die Einheit ist glaub ich in pixel (also s = 30 = 30px im Durchmesser)

Ich hoffe ich Ihr versteht jetzt was die Zusatzvariablen machen und noch viel Glück bei eurem Projekt (und wenn ihr noch weitere Fragen habt, dann könnt ihr sie mir natürlich stellen :) )

1
Haben wir schon versucht, Stell dir vor?

Wenn ich das mache kommt sofort: https://www.w3schools.com/python/python_ml_linear_regression.asp

miranda9 
Fragesteller
 30.08.2022, 09:27

Die Seite hatten wir auch, aber ab dem Wort slope Funktioniert es bei uns in Python nicht

0
DieZahl3  30.08.2022, 09:28
@miranda9

Was meinst du mit ,,funktioniert es bei uns nicht"? Fehlermeldung?

0
miranda9 
Fragesteller
 30.08.2022, 09:29
@DieZahl3

Das habe ich gerade leider nicht im Kopf, wir setzen uns gegen 13 Uhr heute dran, dann kann ich den Befehl wieder eingeben und Screenshoten

Füge das Bild dann später hierzu

1