Lineare Regression Python

[wpdm_package id=’5643′]

import pandas as pd

df = pd.read_csv("./input.csv")
df.head()
%matplotlib inline
import matplotlib.pyplot as plt

plt.scatter(df["Quadratmeter"], df["Verkaufspreis"])
plt.show()
from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(df[["Quadratmeter"]], df[["Verkaufspreis"]])

print("Intercept: " + str(model.intercept_))
print("Coef: " + str(model.coef_))

Intercept: [ 3143.28481869]
Coef: [[ 5071.35242619]]

# Verkaufspreis = 3143.28481869 + 5071.35242619 * Quadratmeter
# y = 3143.28481869 + 5071.35242619 * x
print(3143.28481869 + 5071.35242619 * 40)

Ergebnis: 205997.38186629003

min_x = min(df["Quadratmeter"])
max_x = max(df["Quadratmeter"])

predicted = model.predict([[min_x], [max_x]])
plt.scatter(df["Quadratmeter"], df["Verkaufspreis"])
plt.plot([min_x, max_x], predicted, color = "red")
plt.show()
Cookie Consent Banner von Real Cookie Banner