import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
x = np.array([1,2,3,4,5])
y = np.array([4,2,1,3,7])
print(np.corrcoef(x, y))
plt.scatter(x, y)
plt.show()
model = LinearRegression()
model.fit(x.reshape(-1, 1), y)
y_pred = model.predict(x.reshape(-1, 1))
print(y_pred.round(0).astype(int))
print(y)
plt.scatter(x, y)
plt.plot(x, y_pred)
plt.show()
from sklearn.preprocessing import PolynomialFeatures
- 2. 특징행렬 생성 : 1차항 feature를 다항 feature로 만들기
poly = PolynomialFeatures(degree=2, include_bias=False)
x2 = poly.fit_transform(x.reshape(-1, 1))
print(x2)
model = LinearRegression()
model.fit(x2, y)
y_pred = model.predict(x2)
print(y_pred.round(0).astype(int))
print(y)
plt.scatter(x, y)
plt.plot(x, y_pred, c='g')
plt.show()