質問するログイン新規登録

質問編集履歴

2

係数からグラフを作成する方法の追加

2021/04/21 09:09

投稿

unser
unser

スコア58

title CHANGED
File without changes
body CHANGED
@@ -70,6 +70,14 @@
70
70
  model4.fit(x_20,y)
71
71
 
72
72
  # visualize model
73
+ # visualize model
74
+ def model_conf(model,x):
75
+ y=[model.intercept_ for _ in range(len(x))]
76
+ for j in range(len(x)):
77
+ for i in range(len(model.coef_)):
78
+ y[j]+=model.coef_[i]*((x[j][0])**(i+1))
79
+ return y
80
+
73
81
  plt.figure(figsize=(10, 7))
74
82
 
75
83
  X_plt=np.arange(0,2,0.01).reshape(200,1)
@@ -85,6 +93,7 @@
85
93
  plt.plot(X_plt, func(X_plt), color='purple', linestyle='-', label='True Function', lw=5)
86
94
  plt.plot(X_plt, y_pred, color='red', linestyle=':', label='LinearRegression', lw=3)
87
95
  plt.plot(X_plt, y_pred2, color='black', linestyle='--', label='Line Ridge(α={})'.format(k_1), lw=3)
96
+ #plt.plot(X_plt, model_conf(model3,X_plt), color='purple', linestyle="dashdot", label='Polynomial regression(20)', lw=5)
88
97
  plt.plot(X_plt, y_pred3, color='green', linestyle="dashdot", label='Polynomial regression(20)', lw=3)
89
98
  plt.plot(X_plt, y_pred4, color='pink', linestyle='-', label='Poly Ridge(α={})'.format(k_1), lw=3)
90
99
  plt.legend()

1

ridge項の追加方法

2021/04/21 09:09

投稿

unser
unser

スコア58

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,5 @@
1
1
  Pythonにて,Ridge回帰などを行い,その損失関数(MSE)の値を算出したいのですが,
2
- その見方の方法がわからないためご教授していただけると幸いです
2
+ その見方の方法がわからないためご教授していただけると幸いです.
3
- (実装は直に損失関数を計算しましたが,Ridgeに関しては方法がわからなかったため,
4
- ひとまず,ridgeの項をゼロとしました).
5
3
 
6
4
  また,Ridge回帰や線形単回帰,他項回帰で得られた,モデルの係数を
7
5
  どのように参照したら良いかを教えていただけると幸いです.
@@ -97,20 +95,22 @@
97
95
  ![イメージ説明](5120432895e976a672b23ee4db9aa60d.png)
98
96
 
99
97
  ```python
100
- #損失関数(仮)を算出する.
98
+ #損失関数を算出する.(修正 4/21, 17:29)
101
99
  ans_lst=[]
102
100
  for i in range(5):
101
+ ans=0
103
102
  if i==0:
104
103
  #score=cross_val_score(model1,x,y,cv=10)
105
104
  ans=statistics.mean(list(map(lambda t: t**2, (y-model1.predict(x)).tolist())))
106
105
  elif i==1:
107
- # 本当はRidgeの項を入れたいが係数がわからない...
106
+ model2.coef_
108
107
  ans=statistics.mean(list(map(lambda t: t**2, (y-model2.predict(x)).tolist())))
108
+ ans+=sum(list(map(lambda t: t**2, model2.coef_)))
109
109
  elif i==2:
110
110
  ans=statistics.mean(list(map(lambda t: t**2, (y-model3.predict(x_20)).tolist())))
111
111
  elif i==3:
112
- # 本当はRidgeの項を入れたいが係数がわからない...
113
112
  ans=statistics.mean(list(map(lambda t: t**2, (y-model4.predict(x_20)).tolist())))
113
+ ans+=sum(list(map(lambda t: t**2, model4.coef_)))
114
114
  else:
115
115
  #試しに真の分布のもののエラー関数を見てみる.
116
116
  ans=statistics.mean(list(map(lambda t: t**2, (y-func(x).reshape(1,-1)[0]).tolist())))
@@ -119,13 +119,19 @@
119
119
  ans_lst.append(ans)
120
120
 
121
121
  """
122
- ans_lst=[878.1943851888309,
122
+ [878.1943851888309,
123
- 6829.205481962394,
123
+ 6835.932288142904,
124
124
  165.22430413762072,
125
- 1472.468473432889,
125
+ 1480.3156315725134,
126
126
  660.6537337438299]
127
127
  """
128
128
  ```
129
+ ~~ans_lst=[878.1943851888309,
130
+ 6829.205481962394,
131
+ 165.22430413762072,
132
+ 1472.468473432889,
133
+ 660.6537337438299]~~ <- ridge項を入れる前.
134
+
129
135
  こちら,最後をみると明らかに過学習しているmodel3が
130
136
  最も損失関数が低いため良いモデルとなってしまいました.
131
137