質問編集履歴
3
正確にいうとエラーではなくて値がおかしかったのでタイトルを直しました
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Python予測(RandomForestRegressor)
|
1
|
+
Pythonで予測(RandomForestRegressor)を使うと負のscoreになるのがどういう意味ですか?
|
body
CHANGED
File without changes
|
2
いただいたコメントを元に違う視点で質問を変更しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,41 @@
|
|
1
|
+
元のデータです
|
1
|
-
|
2
|
+

|
3
|
+
地震のデータセットを用いて、発生時期・緯度・経度からマグニチュードと深さを予測しようと思っていますが、発生日付のフォーマットでうまくいきません
|
2
4
|
```ここに言語を入力
|
3
|
-
|
4
|
-
print(
|
5
|
+
print(data.dtypes)
|
6
|
+
time datetime64[ns, UTC]
|
7
|
+
latitude float64
|
8
|
+
longitude float64
|
9
|
+
depth float64
|
10
|
+
mag float64
|
11
|
+
magType object
|
12
|
+
nst float64
|
13
|
+
gap float64
|
14
|
+
dmin float64
|
15
|
+
rms float64
|
16
|
+
net object
|
17
|
+
id object
|
18
|
+
updated object
|
19
|
+
place object
|
20
|
+
type object
|
21
|
+
horizontalError float64
|
22
|
+
depthError float64
|
23
|
+
magError float64
|
24
|
+
magNst float64
|
25
|
+
status object
|
26
|
+
locationSource object
|
27
|
+
magSource object
|
28
|
+
dtype: object
|
29
|
+
```
|
30
|
+
コードは
|
31
|
+
```ここに言語を入力
|
32
|
+
#timeを日付と時間に分割
|
33
|
+
data['new_date'] = [d.date() for d in data['time']]
|
34
|
+
data['new_time'] = [d.time() for d in data['time']]
|
35
|
+
final_data = data[['time', 'new_date', 'new_time', 'latitude', 'longitude', 'depth', 'mag']]
|
36
|
+
```
|
37
|
+
確認すると
|
38
|
+
```ここに言語を入力
|
5
39
|
time datetime64[ns, UTC]
|
6
40
|
new_date object
|
7
41
|
new_time object
|
@@ -11,27 +45,65 @@
|
|
11
45
|
mag float64
|
12
46
|
dtype: object
|
13
47
|
```
|
14
|
-
データをtrainとtest用に分けます
|
15
48
|
```ここに言語を入力
|
49
|
+
final_data['month']= final_data['time'].dt.month
|
50
|
+
final_data['year']= final_data['time'].dt.year
|
51
|
+
#データをtrainとtest用に分けます
|
16
|
-
X = final_data[['
|
52
|
+
X = final_data[['year', 'month', 'latitude', 'longitude']]
|
17
53
|
y = final_data[['mag', 'depth']]
|
54
|
+
|
18
55
|
from sklearn.model_selection import train_test_split
|
19
56
|
X_train, X_test,y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
|
20
57
|
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
|
21
58
|
```
|
22
|
-
|
59
|
+
(107, 4) (27, 4) (107, 2) (27, 2)
|
23
60
|
```ここに言語を入力
|
61
|
+
#予測をするためにrandomforestを実行する
|
24
62
|
from sklearn.ensemble import RandomForestRegressor
|
25
63
|
|
26
64
|
reg = RandomForestRegressor(random_state=42)
|
27
|
-
reg.fit(
|
65
|
+
reg.fit((X_train), (y_train))
|
28
66
|
reg.predict(X_test)
|
29
67
|
```
|
68
|
+
array([[ 4.65 , 20.75 ],
|
30
|
-
|
69
|
+
[ 4.61 , 10.223],
|
70
|
+
[ 4.68 , 10. ],
|
71
|
+
[ 4.72 , 10. ],
|
72
|
+
[ 4.9 , 11.02 ],
|
73
|
+
[ 4.57 , 9.983],
|
74
|
+
[ 4.59 , 16.735],
|
75
|
+
[ 4.69 , 9.543],
|
76
|
+
[ 5.37 , 17.217],
|
77
|
+
[ 5.02 , 18.4 ],
|
78
|
+
[ 5.1 , 11.2 ],
|
79
|
+
[ 4.66 , 10. ],
|
80
|
+
[ 4.72 , 26.106],
|
81
|
+
[ 5.77 , 11.3 ],
|
82
|
+
[ 4.66 , 10.159],
|
83
|
+
[ 5.09 , 29.443],
|
84
|
+
[ 4.55 , 9.983],
|
85
|
+
[ 4.84 , 16.776],
|
86
|
+
[ 4.55 , 9.983],
|
87
|
+
[ 4.71 , 10. ],
|
88
|
+
[ 5.2 , 13.439],
|
89
|
+
[ 4.72 , 9.847],
|
90
|
+
[ 4.77 , 12.127],
|
91
|
+
[ 4.63 , 22.94 ],
|
92
|
+
[ 4.72 , 10.767],
|
93
|
+
[ 5.49 , 11.3 ],
|
94
|
+
[ 5.04 , 10.2 ]])
|
95
|
+
|
96
|
+
```ここに言語を入力
|
97
|
+
reg.score(X_test, y_test)
|
31
98
|
```
|
32
|
-
|
99
|
+
-7.319324727219347
|
100
|
+
|
101
|
+
答えはマイナスになるのです。
|
102
|
+
new_dateとnew_timeの代わりにtimeを使ってもエラーになります(以下の)。
|
103
|
+
```ここに言語を入力
|
104
|
+
TypeError: float() argument must be a string or a number, not 'Timestamp'
|
33
105
|
```
|
34
|
-
自分のデータにstringがないと思います!
|
35
106
|
|
107
|
+
予測に影響が大きのは月と年です。どうパラメータを正しく取ればいいのかわからないです
|
36
108
|
何が違うかヒントをいただけませんか
|
37
109
|
よろしくお願いします
|
1
前提の言葉に漢字ミスがあった
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
使用しているデータセット(将来、数を増やす予定)が下のようです。
|
2
2
|
```ここに言語を入力
|
3
3
|
|
4
4
|
print(final_data.dtypes)
|