1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| """ demo sklearn.pipeline """ import numpy as np import matplotlib.pyplot as plt import sklearn.linear_model as lm import sklearn.preprocessing as sp import sklearn.metrics as sm import sklearn.pipeline as pl
x, y = np.loadtxt( '../ml_data/single.txt', delimiter=',', usecols=(0, 1), unpack=True)
x = x.reshape(-1, 1) model = pl.make_pipeline( sp.PolynomialFeatures(8), lm.LinearRegression())
model.fit(x, y)
pred_y = model.predict(x) print(sm.r2_score(y, pred_y)) print(sm.r2_score(pred_y, y))
px = np.linspace(x.min(), x.max(), 1000) pred_y = model.predict(px.reshape(-1, 1))
plt.figure('Poly Regression', facecolor='lightgray') plt.title('Poly Regression', fontsize=16) plt.xlabel('X') plt.ylabel('Y') plt.tick_params(labelsize=10) plt.grid(linestyle=':') plt.scatter(x, y, color='dodgerblue', label='Samples', s=70, marker='o') plt.plot(px, pred_y, color='orangered', label='Poly Line') plt.legend() plt.savefig('polynominal.png') plt.show()
|