54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
import pandas as pd
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.preprocessing import PolynomialFeatures
|
|
from sklearn.linear_model import LinearRegression
|
|
|
|
data = pd.read_csv("data6.tsv", sep="\t")
|
|
|
|
x = data.iloc[:, 0].values
|
|
y = data.iloc[:, 1].values
|
|
|
|
x = x.reshape(-1, 1)
|
|
|
|
two_dim = PolynomialFeatures(degree=2)
|
|
five_dim = PolynomialFeatures(degree=5)
|
|
|
|
X_two_dim = two_dim.fit_transform(x)
|
|
X_five_dim = five_dim.fit_transform(x)
|
|
|
|
one_dim_reg = LinearRegression()
|
|
one_dim_reg.fit(x, y)
|
|
|
|
two_dim_reg = LinearRegression()
|
|
two_dim_reg.fit(X_two_dim, y)
|
|
|
|
five_dim_reg = LinearRegression()
|
|
five_dim_reg.fit(X_five_dim, y)
|
|
|
|
myline = np.linspace(min(x), max(x), 100).reshape(-1, 1)
|
|
|
|
one_dim_pred = one_dim_reg.predict(x)
|
|
two_dim_pred = two_dim_reg.predict(two_dim.fit_transform(myline))
|
|
five_dim_pred = five_dim_reg.predict(five_dim.fit_transform(myline))
|
|
|
|
|
|
fig1 = plt.figure(figsize=(6, 10))
|
|
ax1 = fig1.add_subplot(3, 1, 1)
|
|
ax1.set_title('Regresja pierwszego stopnia')
|
|
ax1.scatter(x, y)
|
|
ax1.plot(x, one_dim_pred, color="red")
|
|
|
|
ax2 = fig1.add_subplot(3, 1, 2)
|
|
ax2.set_title('Regresja drugiego stopnia')
|
|
ax2.scatter(x, y)
|
|
ax2.plot(myline, two_dim_pred, color="green")
|
|
|
|
ax3 = fig1.add_subplot(3, 1, 3)
|
|
ax3.set_title('Regresja piątego stopnia')
|
|
ax3.scatter(x, y)
|
|
ax3.plot(myline, five_dim_pred, color="orange")
|
|
|
|
plt.tight_layout()
|
|
plt.show()
|