質問編集履歴

7

.

2020/06/28 13:48

投稿

mango55
mango55

スコア22

test CHANGED
File without changes
test CHANGED
@@ -84,9 +84,9 @@
84
84
 
85
85
 
86
86
 
87
- Y_vars = target_col
87
+ y = dataset[target_col]
88
-
88
+
89
- X_vars = feature_cols
89
+ X = dataset[feature_cols]
90
90
 
91
91
 
92
92
 

6

.

2020/06/28 13:48

投稿

mango55
mango55

スコア22

test CHANGED
File without changes
test CHANGED
@@ -140,6 +140,8 @@
140
140
 
141
141
  ```
142
142
 
143
+ コード修正後のエラー(初回とはエラー内容が異なります)
144
+
143
145
  KeyError Traceback (most recent call last)
144
146
 
145
147
  ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)

5

.

2020/06/28 11:58

投稿

mango55
mango55

スコア22

test CHANGED
File without changes
test CHANGED
@@ -138,7 +138,7 @@
138
138
 
139
139
  ```
140
140
 
141
- ```エラー内容
141
+ ```
142
142
 
143
143
  KeyError Traceback (most recent call last)
144
144
 
@@ -240,7 +240,9 @@
240
240
 
241
241
 
242
242
 
243
-
243
+ ```
244
+
245
+ file_0627.csv
244
246
 
245
247
  |date|patient|6day_exclusion_rate|14day_exclusion_rate
246
248
 

4

修正追記しました

2020/06/27 02:08

投稿

mango55
mango55

スコア22

test CHANGED
File without changes
test CHANGED
@@ -28,9 +28,65 @@
28
28
 
29
29
  ```Python
30
30
 
31
+ %matplotlib inline
32
+
33
+ import matplotlib
34
+
35
+ import matplotlib.pyplot as plt
36
+
37
+ import numpy as np
38
+
39
+ import pandas as pd
40
+
41
+ from sklearn.linear_model import LinearRegression
42
+
43
+ from sklearn.tree import DecisionTreeRegressor
44
+
45
+ from sklearn.ensemble import RandomForestRegressor
46
+
47
+ from sklearn.model_selection import GridSearchCV
48
+
49
+ from sklearn.model_selection import train_test_split
50
+
51
+ from sklearn.metrics import mean_squared_error
52
+
53
+
54
+
55
+ dataset = pd.read_csv('file_0627.csv')
56
+
57
+ dataset.head()
58
+
59
+
60
+
31
- Y_vars = dataset['patient']
61
+ target_col = 'patient'
32
-
62
+
33
- X_vars = dataset['exclusion_rate']
63
+ exclude_cols = ['date','patient','14day_exclusion_rate']
64
+
65
+ feature_cols = []
66
+
67
+ for col in dataset.columns:
68
+
69
+ if col not in exclude_cols:
70
+
71
+ feature_cols.append(col)
72
+
73
+
74
+
75
+ X_train_val, X_test, y_train_val, y_test = \
76
+
77
+ train_test_split(X, y, test_size=0.3, random_state=1234) #分割1
78
+
79
+
80
+
81
+ X_train, X_val, y_train, y_val = \
82
+
83
+ train_test_split(X_train_val, y_train_val, test_size=0.3, random_state=1234) #分割2
84
+
85
+
86
+
87
+ Y_vars = target_col
88
+
89
+ X_vars = feature_cols
34
90
 
35
91
 
36
92
 
@@ -86,7 +142,43 @@
86
142
 
87
143
  KeyError Traceback (most recent call last)
88
144
 
145
+ ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
146
+
147
+ 2656 try:
148
+
149
+ -> 2657 return self._engine.get_loc(key)
150
+
151
+ 2658 except KeyError:
152
+
153
+
154
+
155
+ pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
156
+
157
+
158
+
159
+ pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
160
+
161
+
162
+
163
+ pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
164
+
165
+
166
+
167
+ pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
168
+
169
+
170
+
171
+ KeyError: 'patient'
172
+
173
+
174
+
175
+ During handling of the above exception, another exception occurred:
176
+
177
+
178
+
179
+ KeyError Traceback (most recent call last)
180
+
89
- <ipython-input-31-84f18d09bd67> in <module>
181
+ <ipython-input-57-82605815cc01> in <module>
90
182
 
91
183
  3
92
184
 
@@ -102,62 +194,56 @@
102
194
 
103
195
  ~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
104
196
 
105
- 2932 key = list(key)
106
-
107
- 2933 indexer = self.loc._convert_to_indexer(key, axis=1,
108
-
109
- -> 2934 raise_missing=True)
110
-
111
- 2935
112
-
113
- 2936 # take() does not accept boolean indexers
114
-
115
-
116
-
117
- ~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)
118
-
119
- 1352 kwargs = {'raise_missing': True if is_setter else
120
-
121
- 1353 raise_missing}
122
-
123
- -> 1354 return self._get_listlike_indexer(obj, axis, **kwargs)[1]
124
-
125
- 1355 else:
126
-
127
- 1356 try:
128
-
129
-
130
-
131
- ~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
132
-
133
- 1159 self._validate_read_indexer(keyarr, indexer,
134
-
135
- 1160 o._get_axis_number(axis),
136
-
137
- -> 1161 raise_missing=raise_missing)
138
-
139
- 1162 return keyarr, indexer
140
-
141
- 1163
142
-
143
-
144
-
145
- ~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
146
-
147
- 1244 raise KeyError(
148
-
149
- 1245 u"None of [{key}] are in the [{axis}]".format(
150
-
151
- -> 1246 key=key, axis=self.obj._get_axis_name(axis)))
152
-
153
- 1247
154
-
155
- 1248 # We (temporarily) allow for some missing keys with .loc, except in
156
-
157
-
158
-
159
- KeyError: "None of [Float64Index([ 0.140526, 0.131246, 0.134081,\n 0.258836, 0.183608, 0.121047,\n 0.12695399999999998, 0.130412, 0.129215,\n -0.000315,\n ...\n 0.133338, 0.120761, 0.20714499999999997,\n 0.255416, 0.11556300000000001, 0.15191500000000002,\n 0.136953, 0.140565, 0.132261,\n 0.215802],\n dtype='float64', length=4465)] are in the [columns]"
160
-
161
-
162
-
163
- ```
197
+ 2925 if self.columns.nlevels > 1:
198
+
199
+ 2926 return self._getitem_multilevel(key)
200
+
201
+ -> 2927 indexer = self.columns.get_loc(key)
202
+
203
+ 2928 if is_integer(indexer):
204
+
205
+ 2929 indexer = [indexer]
206
+
207
+
208
+
209
+ ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
210
+
211
+ 2657 return self._engine.get_loc(key)
212
+
213
+ 2658 except KeyError:
214
+
215
+ -> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
216
+
217
+ 2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
218
+
219
+ 2661 if indexer.ndim > 1 or indexer.size > 1:
220
+
221
+
222
+
223
+ pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
224
+
225
+
226
+
227
+ pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
228
+
229
+
230
+
231
+ pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
232
+
233
+
234
+
235
+ pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
236
+
237
+
238
+
239
+ KeyError: 'patient'
240
+
241
+
242
+
243
+
244
+
245
+ |date|patient|6day_exclusion_rate|14day_exclusion_rate
246
+
247
+ |2020/4/1|181|0.117179|0.130412
248
+
249
+ |2020/4/2|186|0.17748|0.129215

3

.

2020/06/27 02:06

投稿

mango55
mango55

スコア22

test CHANGED
@@ -1 +1 @@
1
- [Python] 時系列分析(ランダムフォレスト)コーディングエラーの意味
1
+ [Python] 時系列分析(ランダムフォレスト)コーディングエラーの意味.
test CHANGED
@@ -27,18 +27,6 @@
27
27
 
28
28
 
29
29
  ```Python
30
-
31
- rf = RandomForestRegressor(random_state=1234)
32
-
33
- rf.fit(X_train, y_train)
34
-
35
- y_pred = rf.predict(X_val)
36
-
37
- rf_mse = mean_squared_error(y_val, y_pred)
38
-
39
- print('Random Forest RMSE: ', np.sqrt(rf_mse))
40
-
41
-
42
30
 
43
31
  Y_vars = dataset['patient']
44
32
 

2

.

2020/06/25 13:26

投稿

mango55
mango55

スコア22

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
1
  時系列分析(ランダムフォレスト)で将来の人数を予測しています。
2
+
3
+
2
4
 
3
5
  手元のデータ集計期間が短いため、ある特定日を用いて前日、前々日の人数を予測したものを学習データ、実際の人数をテストデータとして予測を行いたいです。
4
6
 

1

.

2020/06/25 13:22

投稿

mango55
mango55

スコア22

test CHANGED
@@ -1 +1 @@
1
- [Python] エラーの意味
1
+ [Python] 時系列分析(ランダムフォレスト)コーディングエラーの意味
test CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
  実現したいこと
12
12
 
13
- ① 学習用データの作成 4/1のデータから3/31,3/30の人数を予測したもの
13
+ ① 学習用データの作成 4/1のデータから3/31,3/30の人数を予測
14
14
 
15
- ② ①を学習用データ、実際の人数をテストデータとして予測する
15
+ ② ①を学習用データ、実際の人数をテストデータとして予測
16
16
 
17
- ③ ランダムフォレストでRMSEを算出
17
+ ①②を用いてランダムフォレストでRMSEを算出
18
18
 
19
19
 
20
20