質問編集履歴

2

ソースコードの編集

2018/01/22 12:49

投稿

takahiro_tt
takahiro_tt

スコア9

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ---追記
12
12
 
13
- こちらのエラーは解決しましたが、進めていくうちに次のエラーが発生しました
13
+ 上記のエラーは解決しましたが、進めていくうちに次のエラーが発生しました
14
14
 
15
15
  「SGDの平均2乗誤差」と「SVRの平均二乗誤差」
16
16
 
@@ -27,6 +27,8 @@
27
27
 
28
28
 
29
29
  ###該当のソースコード
30
+
31
+ ```ここに言語を入力
30
32
 
31
33
  -*- coding: utf-8 -*-
32
34
 
@@ -58,7 +60,7 @@
58
60
 
59
61
 
60
62
 
61
- "#3:データの整形-------------------------------------------------------
63
+ #3:データの整形-------------------------------------------------------
62
64
 
63
65
  sc=preprocessing.StandardScaler()
64
66
 
@@ -72,13 +74,13 @@
72
74
 
73
75
 
74
76
 
75
- "#4:学習データとテストデータに分割する-------------------------------
77
+ #4:学習データとテストデータに分割する-------------------------------
76
78
 
77
79
  X_rm_train, X_rm_test, Y_train, Y_test = model_selection.train_test_split(X_rm, Y, test_size=0.5, random_state=0)
78
80
 
79
81
 
80
82
 
81
- " 5:SGD Regressorを適用する-------------------------------------------
83
+ #5:SGD Regressorを適用する-------------------------------------------
82
84
 
83
85
  clf_rm = linear_model.SGDRegressor(max_iter=1000)
84
86
 
@@ -86,7 +88,7 @@
86
88
 
87
89
 
88
90
 
89
- " 解説6:SVR linear Regressorを適用する-------------------------------------------
91
+ #解説6:SVR linear Regressorを適用する-------------------------------------------
90
92
 
91
93
  clf_svr = svm.SVR(kernel='linear', C=1e3, epsilon=2.0)
92
94
 
@@ -94,7 +96,7 @@
94
96
 
95
97
 
96
98
 
97
- " #7:結果をプロットする------------------------------------------------
99
+ #7:結果をプロットする------------------------------------------------
98
100
 
99
101
 
100
102
 
@@ -118,7 +120,7 @@
118
120
 
119
121
 
120
122
 
121
- "# 8:誤差-------------------------------------------------
123
+ # 8:誤差-------------------------------------------------
122
124
 
123
125
  Y_pred_sgd=clf_rm.predict(X_rm_test)
124
126
 
@@ -134,7 +136,7 @@
134
136
 
135
137
  print(RMS_svr)
136
138
 
137
-
139
+ ```
138
140
 
139
141
  ###試したこと
140
142
 

1

エラー個所の更新

2018/01/22 12:49

投稿

takahiro_tt
takahiro_tt

スコア9

test CHANGED
@@ -1 +1 @@
1
- pythonのエラーが対処できません
1
+ scikit-learnのエラー解決できません
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- http://neuro-educator.com/ml14/ のサイトを参考に自分でも回帰を実装しようと思い、プログラムを書いていたところ下記のエラーがでました。
3
+ http://neuro-educator.com/ml18/ のサイトを参考に自分でも回帰を実装しようと思い、プログラムを書いていたところ下記のエラーがでました。
4
4
 
5
5
 
6
6
 
@@ -8,11 +8,27 @@
8
8
 
9
9
  ValueError: Found input variables with inconsistent numbers of samples: [84, 105]
10
10
 
11
+ ---追記
12
+
13
+ こちらのエラーは解決しましたが、進めていくうちに次のエラーが発生しました
14
+
15
+ 「SGDの平均2乗誤差」と「SVRの平均二乗誤差」
16
+
17
+ 47.8755773832
18
+
19
+ 49.403758817
20
+
21
+
22
+
23
+ Data with input dtype int64 was converted to float64 by StandardScaler.
24
+
25
+ warnings.warn(msg, DataConversionWarning)
26
+
11
27
 
12
28
 
13
29
  ###該当のソースコード
14
30
 
15
- " -*- coding: utf-8 -*-
31
+ -*- coding: utf-8 -*-
16
32
 
17
33
  import pandas as pd
18
34
 
@@ -20,15 +36,9 @@
20
36
 
21
37
  import matplotlib.pyplot as plt
22
38
 
23
- from sklearn import cross_validation, preprocessing, linear_model #機械学習用のライブラリを利用
39
+ from sklearn import model_selection, preprocessing, linear_model ,svm#機械学習用のライブラリ
24
-
25
- from sklearn.externals import joblib
26
40
 
27
41
  from matplotlib import pyplot as plt
28
-
29
- from sklearn import datasets
30
-
31
- from sklearn import svm
32
42
 
33
43
 
34
44
 
@@ -48,7 +58,7 @@
48
58
 
49
59
 
50
60
 
51
- "解説 3:データの整形-------------------------------------------------------
61
+ "#3:データの整形-------------------------------------------------------
52
62
 
53
63
  sc=preprocessing.StandardScaler()
54
64
 
@@ -62,21 +72,13 @@
62
72
 
63
73
 
64
74
 
65
- "解説 4:学習データとテストデータに分割する-------------------------------
75
+ "#4:学習データとテストデータに分割する-------------------------------
66
76
 
67
- X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(X, Y, test_size=0.2, random_state=0)
77
+ X_rm_train, X_rm_test, Y_train, Y_test = model_selection.train_test_split(X_rm, Y, test_size=0.5, random_state=0)
68
-
69
- X_rm_train, X_rm_test, Y_train, Y_test = cross_validation.train_test_split(X_rm, Y, test_size=0.)
70
78
 
71
79
 
72
80
 
73
- "解説 5:SGD Regressorを適用する-------------------------------------------
81
+ " 5:SGD Regressorを適用する-------------------------------------------
74
-
75
- clf = linear_model.SGDRegressor(max_iter=1000)
76
-
77
- clf.fit(X_train, Y_train)
78
-
79
-
80
82
 
81
83
  clf_rm = linear_model.SGDRegressor(max_iter=1000)
82
84
 
@@ -84,75 +86,65 @@
84
86
 
85
87
 
86
88
 
87
- print("全部使したときの回帰式の係数")
89
+ " 解説6:SVR linear Regressorを適する-------------------------------------------
88
90
 
89
- print(clf.intercept_)
91
+ clf_svr = svm.SVR(kernel='linear', C=1e3, epsilon=2.0)
90
92
 
91
- print(clf.coef_)
93
+ clf_svr.fit(X_rm_train, Y_train)
92
94
 
93
95
 
94
96
 
95
- "解説 6:結果をプロットする------------------------------------------------
97
+ " #7:結果をプロットする------------------------------------------------
96
98
 
97
- " %matplotlib inline
98
99
 
99
- line_X = np.arange(0, 250, 0.1) #・・・から。。。まで1刻み
100
100
 
101
- line_Y = clf_rm.predict(line_X[:, np.newaxis])
101
+ line_X=np.arange(0, 250, 1) #3から10まで1刻み
102
102
 
103
+ line_Y_sgd=clf_rm.predict(line_X[:, np.newaxis])
104
+
105
+ line_Y_svr=clf_svr.predict(line_X[:, np.newaxis])
106
+
103
- plt.figure(figsize=(10,10))
107
+ plt.figure(figsize=(30,30))
104
108
 
105
109
  plt.subplot(2, 1, 1)
106
110
 
107
111
  plt.scatter(X_rm_train, Y_train, c='b', marker='s')
108
112
 
109
- plt.plot(line_X, line_Y, c='r')
113
+ plt.plot(line_X, line_Y_sgd, c='r')
114
+
115
+ plt.plot(line_X, line_Y_svr, c='g')
110
116
 
111
117
  plt.show
112
118
 
113
119
 
114
120
 
115
- "解説 7:誤差をプロットする-------------------------------------------------
121
+ "# 8:誤差-------------------------------------------------
116
122
 
117
- Y_rm_pred=clf_rm.predict(X_rm_test)
123
+ Y_pred_sgd=clf_rm.predict(X_rm_test)
118
124
 
119
- plt.subplot(2, 1, 2)
125
+ Y_pred_svr=clf_svr.predict(X_rm_test)
120
126
 
121
- plt.scatter(Y_test, Y_rm_pred-Y_test, c='b', marker='s', label="RM_only")
127
+ print("\n「SGDの平均2乗誤差」と「SVRの平均二乗誤差」")
122
128
 
129
+ RMS_sgd=np.mean((Y_pred_sgd - Y_test) ** 2)
123
130
 
131
+ RMS_svr=np.mean((Y_pred_svr - Y_test) ** 2)
124
132
 
125
- Y_pred=clf.predict(X_test)
133
+ print(RMS_sgd)
126
134
 
127
- plt.scatter(Y_test, Y_pred-Y_test, c='r', marker='s',label="ALL")
128
-
129
- plt.legend()
130
-
131
- plt.hlines(y=0, xmin=0, xmax=50, colors='black')
132
-
133
- plt.show
134
-
135
-
136
-
137
- print("\n「RMだけの平均2乗誤差」と「全部を使用したときの平均二乗誤差」")
138
-
139
- RMS=np.mean((Y_pred - Y_test) ** 2)
140
-
141
- RMS_rm=np.mean((Y_rm_pred - Y_test) ** 2)
142
-
143
- print(RMS_rm)
135
+ print(RMS_svr)
144
-
145
- print(RMS)
146
136
 
147
137
 
148
138
 
149
139
  ###試したこと
150
140
 
151
- csvファイルの(kaozahyou3.csv)は13行106列のファイルです
141
+
152
142
 
153
143
  いままでも小さなエラーは出ていて少しづつ解決してきたのですが、
154
144
 
155
145
  このエラーで詰まってしまいました。
146
+
147
+ ネットを探しながら解決法を見つけているのですが、見つかりません。
156
148
 
157
149
 
158
150