Posts
Q&A
์ ์๋! ๊ฐ์ ์ ๋ณด๊ณ ์์ต๋๋ค.!
๊ฐ์ฌํฉ๋๋ค!!
- Likes
- 0
- Comments
- 2
- Viewcount
- 208
Q&A
๋ฌด์จ ๋ฌธ์ ์ธ์ง ๋ชจ๋ฅด๊ฒ ์ด์
ํด๊ฒฐ๋์์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค ์ ์๋!
- Likes
- 0
- Comments
- 4
- Viewcount
- 8.2K
Q&A
๋ฌด์จ ๋ฌธ์ ์ธ์ง ๋ชจ๋ฅด๊ฒ ์ด์
import matplotlib.pyplot as plt from sklearn.pipeline import Pipeline from sklearn.model_selection import train_test_split from sklearn.model_selection import cross_val_score from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression import numpy as np from sklearn.metrics import mean_squared_error, r2_score %matplotlib inline def true_fun(x): return np.cos(1.5 *np.pi * x) # random ๊ฐ์ผ๋ก ๊ตฌ์ฑ๋ X๊ฐ์ ๋ํด Cosine ๋ณํ๊ฐ์ ๋ฐํ np.random.seed(0) n_samples=30 x=np.sort(np.random.rand(n_samples)) y=true_fun(x)+np.random.randn(n_samples)*0.1 plt.figure(figsize=(14,5)) degrees=[1,4,15] for i in range(len(degrees)): ax=plt.subplot(1,len(degrees),i+1) plt.setp(ax,xticks=(),yticks=()) # ๊ฐ๋ณ degree๋ณ๋ก polynomial ๋ณํํฉ๋๋ค. polynomial_features=PolynomialFeatures(degree=degrees[i],include_bias=False) # ์์ํญ ํฌํจ X linear_regression=LinearRegression() pipe=Pipeline([('polynomial_features',PolynomialFeatures),('linear_regression',LinearRegression)]) pipe.fit(x.reshape(-1,1),y) scores=cross_val_score(pipe,x.reshape(-1,1),y,scoring='neg_mean_squared_error',cv=10) #pipeline์ ๊ตฌ์ฑํ๋ ์ธ๋ถ ๊ฐ์ฒด๋ฅผ ์ ๊ทผํ๋ named_steps['๊ฐ์ฒด๋ช ']์ ์ด์ฉํด ํ๊ท๊ณ์ ์ถ์ถ coefficients=pipe.named_steps['linear_regression'].coef_ print("\n Degree {0} ํ๊ท ๊ณ์๋ {1} ์ ๋๋ค.".format(degrees[i],np.round(coefficients,2))) print('\n Degree {0} MSE๋ {1}์ ๋๋ค.'.format(degrees[i],-1*np.mean(scores))) # 0๋ถํฐ 1๊น์ง ํ ์คํธ ๋ฐ์ดํฐ ์ธํธ๋ฅผ 100๊ฐ๋ก ๋๋ ์์ธก์ ์ํํฉ๋๋ค. # ํ ์คํธ ๋ฐ์ดํฐ ์ธํธ์ ํ๊ท ์์ธก์ ์ํํ๊ณ ์์ธก ๊ณก์ ๊ณผ ์ค์ ๊ณก์ ์ ๊ทธ๋ ค์ ๋น๊ตํฉ๋๋ค. X_test=np.linspace(0,1,100) # ์์ธก๊ฐ ๊ณก์ plt.plot(X_test,pipe.predict(X_test[:,np.newaxis]),label="Model") # ์ค์ ๊ฐ ๊ณก์ plt.plot(X_test.true_fun(X_test),'--',label="True function") plt.scatter(X,y,edgecolor='b',s=20,label="Samples") plt.xlabel("X");plt.ylabel("y");plt.xlim((0,1));plt.ylim((-2,2));plt.legend(loc='best') # ์ต์ ์ ์์น์ ๋ฒ๋ก ์์น์ํด plt.title("Degree {}\nMSE={:.2e}(+/-{:.2e})".formate(degrees[i],-scores.mean(),scores.std())) plt.show()
- Likes
- 0
- Comments
- 4
- Viewcount
- 8.2K




