質問編集履歴
4
コードの位置
test
CHANGED
File without changes
|
test
CHANGED
@@ -153,9 +153,9 @@
|
|
153
153
|
num = num + 1
|
154
154
|
|
155
155
|
dfh = pd.read_csv("point.csv",names=["cluster_id","latitude","longitude"])
|
156
|
-
B = pd.read_csv("pps.csv",usecols=["
|
156
|
+
B = pd.read_csv("pps.csv",usecols=["people"])
|
157
|
-
#dfh2= pd.DataFrame(B['
|
157
|
+
#dfh2= pd.DataFrame(B['people'])
|
158
|
-
dfh['
|
158
|
+
dfh['people']= B
|
159
159
|
dfh.to_csv("point.csv")
|
160
160
|
```
|
161
161
|
|
3
コードを戻した
test
CHANGED
File without changes
|
test
CHANGED
@@ -49,9 +49,98 @@
|
|
49
49
|
```python
|
50
50
|
ソースコード
|
51
51
|
|
52
|
+
#1回目の訪問先の抽出
|
53
|
+
|
54
|
+
from matplotlib import pyplot as plt
|
55
|
+
from sklearn import datasets, preprocessing
|
56
|
+
from sklearn.cluster import KMeans
|
57
|
+
import numpy as np
|
58
|
+
import pandas as pd
|
59
|
+
import cartopy.crs as ccrs
|
60
|
+
import cartopy.io.shapereader as shpreader
|
61
|
+
|
62
|
+
pd.set_option('display.max_rows',600)
|
63
|
+
#前処理後のcsvを読み込み
|
64
|
+
yomi=pd.read_csv("simulationkai.csv")
|
65
|
+
df=pd.read_csv("simulationkai.csv",usecols=["longitude","latitude"])
|
66
|
+
|
67
|
+
# DataFrameに変換
|
68
|
+
print(df.head())
|
69
|
+
# データの整形
|
70
|
+
X = df
|
71
|
+
|
72
|
+
|
73
|
+
# クラスタリング
|
74
|
+
cls = KMeans(n_clusters=4)
|
75
|
+
|
76
|
+
result = cls.fit(X)
|
77
|
+
X['cluster'] = result.labels_
|
78
|
+
PC= pd.DataFrame(X['cluster'])
|
79
|
+
PC
|
80
|
+
df.head()
|
81
|
+
#yomiのデータフレームにcluster(クラスタ番号)を追加する
|
82
|
+
yomi['cluster_id']=PC
|
83
|
+
yomi
|
84
|
+
|
85
|
+
#yomi(元データにクラスタ番号を追加したもの)をallclsdata.csvに保存する
|
86
|
+
yomi.to_csv("allclsdata.csv")
|
87
|
+
|
88
|
+
D = X.sort_values(by="cluster")
|
89
|
+
D = D.drop_duplicates(subset='cluster')
|
90
|
+
D
|
91
|
+
# 各クラスタ内のデータ数をカウント
|
92
|
+
V = X['cluster'].value_counts()
|
93
|
+
V
|
94
|
+
# 各クラスタの番号とデータ数をclsvalue.csvに保存
|
95
|
+
V.to_csv("clsvalue.csv")
|
96
|
+
|
97
|
+
|
98
|
+
# クラスタの重心確認
|
99
|
+
C = pd.DataFrame(result.cluster_centers_)
|
100
|
+
C
|
101
|
+
|
102
|
+
C.iloc[0, :]
|
103
|
+
|
104
|
+
|
105
|
+
lat= X['latitude'].tolist()
|
106
|
+
lon= X['longitude'].tolist()
|
107
|
+
|
108
|
+
clat=C[0].tolist()
|
109
|
+
clon=C[1].tolist()
|
110
|
+
|
111
|
+
|
112
|
+
#から1800までのクラスタのデータからそれぞれ同じ被験者の重複を省いてデータ人数を取得して順番にCSVにまとめる
|
113
|
+
from csv import writer
|
114
|
+
#pp = pd.DataFrame
|
115
|
+
#ppi = pd.DataFrame
|
116
|
+
#yomiからWhile文でN番目のクラスタのデータのみ抽出
|
117
|
+
i = 0
|
118
|
+
while i <= 3:
|
119
|
+
yomic = yomi[yomi['cluster_id']== i]
|
120
|
+
#N番目のクラスタdfから被験者idの重複を消す
|
121
|
+
yomics = yomic.drop_duplicates(subset=["id_questionnaire"])
|
122
|
+
#N番目の加工後データの行数をCSVに付け足す
|
123
|
+
#file = [i,len(yomics)]
|
124
|
+
#ppi = pp.append([file], ignore_index=True)
|
125
|
+
#ppi.to_csv("pp.csv")
|
126
|
+
list_data=[i,len(yomics)]
|
127
|
+
with open('pp.csv', 'a', newline='') as f_object:
|
128
|
+
writer_object = writer(f_object)
|
129
|
+
writer_object.writerow(list_data)
|
130
|
+
f_object.close()
|
131
|
+
i = i + 1
|
132
|
+
#else:
|
133
|
+
#ppi.to_csv("pp.csv")
|
134
|
+
|
135
|
+
#pp.csv内の人数を降順にしたものをpps.csvに保存
|
136
|
+
PP = pd.read_csv("pp.csv",names=["cls","people"])
|
137
|
+
T = PP.sort_values(by=["people"],ascending=False)
|
138
|
+
T.to_csv("pps.csv")
|
139
|
+
PP.to_csv("pp.csv")
|
140
|
+
|
52
141
|
#pps.csvの上から順番にクラスタ番号を引き出してその番号の座標をCから引き出す
|
53
142
|
num = 0
|
54
|
-
while num <=
|
143
|
+
while num <= 3:
|
55
144
|
Tn = T.iloc[num,0]
|
56
145
|
#Tno = Tn + 1
|
57
146
|
Cn = C.iloc[Tn,0]
|
@@ -64,9 +153,9 @@
|
|
64
153
|
num = num + 1
|
65
154
|
|
66
155
|
dfh = pd.read_csv("point.csv",names=["cluster_id","latitude","longitude"])
|
67
|
-
B = pd.read_csv("pps.csv",usecols=["people"])
|
156
|
+
B = pd.read_csv("pps.csv",usecols=["#people"])
|
68
|
-
#dfh2= pd.DataFrame(B['people'])
|
157
|
+
#dfh2= pd.DataFrame(B['#people'])
|
69
|
-
dfh['people']= B
|
158
|
+
dfh['#people']= B
|
70
159
|
dfh.to_csv("point.csv")
|
71
160
|
```
|
72
161
|
|
2
最初のエラーを記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -13,43 +13,32 @@
|
|
13
13
|
エラーメッセージ
|
14
14
|
|
15
15
|
|
16
|
-
>>> #pps.csvの上から順番にクラスタ番号を引き出してその番号の座標をCから引き出す
|
17
|
-
>>> num = 0
|
18
|
-
>>> while num <= 3:
|
19
|
-
... Tn = T.iloc[num,0]
|
20
|
-
... #Tno = Tn + 1
|
21
|
-
... Cn = C.iloc[Tn,0]
|
22
|
-
... Cn2 = C.iloc[Tn,1]
|
23
|
-
... list_data2=[Tn,Cn,Cn2]
|
24
|
-
... with open('point.csv', 'a', newline='') as f_object:
|
25
|
-
|
16
|
+
C:\datasyori>python hoge.py
|
26
|
-
... writer_object.writerow(list_data2)
|
27
|
-
... f_object.close()
|
28
|
-
|
17
|
+
latitude longitude
|
29
|
-
..
|
18
|
+
0 35.693590 139.712202
|
19
|
+
1 35.693497 139.712096
|
20
|
+
2 35.693217 139.712261
|
21
|
+
3 35.693549 139.712430
|
22
|
+
4 35.693621 139.712501
|
30
23
|
Traceback (most recent call last):
|
31
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 769, in _validate_tuple_indexer
|
24
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 769, in _validate_tuple_indexer
|
32
|
-
self._validate_key(k, i)
|
25
|
+
self._validate_key(k, i)
|
33
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1378, in _validate_key
|
26
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1378, in _validate_key
|
34
|
-
raise ValueError(f"Can only index by location with a [{self._valid_types}]")
|
27
|
+
raise ValueError(f"Can only index by location with a [{self._valid_types}]")
|
35
28
|
ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]
|
36
29
|
|
37
30
|
The above exception was the direct cause of the following exception:
|
38
31
|
|
39
32
|
Traceback (most recent call last):
|
40
|
-
File "
|
33
|
+
File "C:\datasyori\hoge.py", line 95, in <module>
|
34
|
+
Cn = C.iloc[Tn,0]
|
41
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 961, in __getitem__
|
35
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 961, in __getitem__
|
42
|
-
return self._getitem_tuple(key)
|
36
|
+
return self._getitem_tuple(key)
|
43
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1458, in _getitem_tuple
|
37
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1458, in _getitem_tuple
|
44
|
-
tup = self._validate_tuple_indexer(tup)
|
38
|
+
tup = self._validate_tuple_indexer(tup)
|
45
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 771, in _validate_tuple_indexer
|
39
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 771, in _validate_tuple_indexer
|
46
|
-
raise ValueError(
|
40
|
+
raise ValueError(
|
47
41
|
ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
|
48
|
-
>>> dfh = pd.read_csv("point.csv",names=["cluster_id","latitude","longitude"])
|
49
|
-
>>> B = pd.read_csv("pps.csv",usecols=["people"])
|
50
|
-
>>> #dfh2= pd.DataFrame(B['people'])
|
51
|
-
>>> dfh['people']= B
|
52
|
-
>>> dfh.to_csv("point.csv")
|
53
42
|
|
54
43
|
|
55
44
|
```
|
@@ -59,94 +48,6 @@
|
|
59
48
|
|
60
49
|
```python
|
61
50
|
ソースコード
|
62
|
-
#1回目の訪問先の抽出
|
63
|
-
|
64
|
-
from matplotlib import pyplot as plt
|
65
|
-
from sklearn import datasets, preprocessing
|
66
|
-
from sklearn.cluster import KMeans
|
67
|
-
import numpy as np
|
68
|
-
import pandas as pd
|
69
|
-
import cartopy.crs as ccrs
|
70
|
-
import cartopy.io.shapereader as shpreader
|
71
|
-
|
72
|
-
pd.set_option('display.max_rows',600)
|
73
|
-
#前処理後のcsvを読み込み
|
74
|
-
yomi=pd.read_csv("simulationkai.csv")
|
75
|
-
df=pd.read_csv("simulationkai.csv",usecols=["longitude","latitude"])
|
76
|
-
|
77
|
-
# DataFrameに変換
|
78
|
-
print(df.head())
|
79
|
-
# データの整形
|
80
|
-
X = df
|
81
|
-
|
82
|
-
|
83
|
-
# クラスタリング
|
84
|
-
cls = KMeans(n_clusters=100)
|
85
|
-
|
86
|
-
result = cls.fit(X)
|
87
|
-
X['cluster'] = result.labels_
|
88
|
-
PC= pd.DataFrame(X['cluster'])
|
89
|
-
PC
|
90
|
-
df.head()
|
91
|
-
#yomiのデータフレームにcluster(クラスタ番号)を追加する
|
92
|
-
yomi['cluster_id']=PC
|
93
|
-
yomi
|
94
|
-
|
95
|
-
#yomi(元データにクラスタ番号を追加したもの)をallclsdata.csvに保存する
|
96
|
-
yomi.to_csv("allclsdata.csv")
|
97
|
-
|
98
|
-
D = X.sort_values(by="cluster")
|
99
|
-
D = D.drop_duplicates(subset='cluster')
|
100
|
-
D
|
101
|
-
# 各クラスタ内のデータ数をカウント
|
102
|
-
V = X['cluster'].value_counts()
|
103
|
-
V
|
104
|
-
# 各クラスタの番号とデータ数をclsvalue.csvに保存
|
105
|
-
V.to_csv("clsvalue.csv")
|
106
|
-
|
107
|
-
|
108
|
-
# クラスタの重心確認
|
109
|
-
C = pd.DataFrame(result.cluster_centers_)
|
110
|
-
C
|
111
|
-
|
112
|
-
C.iloc[0, :]
|
113
|
-
|
114
|
-
|
115
|
-
lat= X['latitude'].tolist()
|
116
|
-
lon= X['longitude'].tolist()
|
117
|
-
|
118
|
-
clat=C[0].tolist()
|
119
|
-
clon=C[1].tolist()
|
120
|
-
|
121
|
-
|
122
|
-
#から100までのクラスタのデータからそれぞれ同じ被験者の重複を省いてデータ人数を取得して順番にCSVにまとめる
|
123
|
-
from csv import writer
|
124
|
-
#pp = pd.DataFrame
|
125
|
-
#ppi = pd.DataFrame
|
126
|
-
#yomiからWhile文でN番目のクラスタのデータのみ抽出
|
127
|
-
i = 0
|
128
|
-
while i <= 99:
|
129
|
-
yomic = yomi[yomi['cluster_id']== i]
|
130
|
-
#N番目のクラスタdfから被験者idの重複を消す
|
131
|
-
yomics = yomic.drop_duplicates(subset=["id_questionnaire"])
|
132
|
-
#N番目の加工後データの行数をCSVに付け足す
|
133
|
-
#file = [i,len(yomics)]
|
134
|
-
#ppi = pp.append([file], ignore_index=True)
|
135
|
-
#ppi.to_csv("pp.csv")
|
136
|
-
list_data=[i,len(yomics)]
|
137
|
-
with open('pp.csv', 'a', newline='') as f_object:
|
138
|
-
writer_object = writer(f_object)
|
139
|
-
writer_object.writerow(list_data)
|
140
|
-
f_object.close()
|
141
|
-
i = i + 1
|
142
|
-
#else:
|
143
|
-
#ppi.to_csv("pp.csv")
|
144
|
-
|
145
|
-
#pp.csv内の人数を降順にしたものをpps.csvに保存
|
146
|
-
PP = pd.read_csv("pp.csv",names=["cls","people"])
|
147
|
-
T = PP.sort_values(by=["people"],ascending=False)
|
148
|
-
T.to_csv("pps.csv")
|
149
|
-
PP.to_csv("pp.csv")
|
150
51
|
|
151
52
|
#pps.csvの上から順番にクラスタ番号を引き出してその番号の座標をCから引き出す
|
152
53
|
num = 0
|
@@ -171,8 +72,7 @@
|
|
171
72
|
|
172
73
|
### 試したこと
|
173
74
|
|
174
|
-
peopleの値がみつからないなどと書いてあったのでしっかりと確認しましたがデータにもあるのでよく理解できない。
|
175
|
-
|
75
|
+
勉強不足でエラーの内容が全く分かっていません
|
176
76
|
|
177
77
|
### 補足情報(FW/ツールのバージョンなど)
|
178
78
|
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
|
1
新たなコード
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,62 +12,43 @@
|
|
12
12
|
```python
|
13
13
|
エラーメッセージ
|
14
14
|
|
15
|
+
|
15
|
-
#pps.csvの上から順番にクラスタ番号を引き出してその番号の座標をCから引き出す
|
16
|
+
>>> #pps.csvの上から順番にクラスタ番号を引き出してその番号の座標をCから引き出す
|
16
17
|
>>> num = 0
|
17
|
-
>>> while num <=
|
18
|
+
>>> while num <= 3:
|
18
|
-
...
|
19
|
+
... Tn = T.iloc[num,0]
|
19
|
-
...
|
20
|
+
... #Tno = Tn + 1
|
20
|
-
...
|
21
|
+
... Cn = C.iloc[Tn,0]
|
21
|
-
...
|
22
|
+
... Cn2 = C.iloc[Tn,1]
|
22
|
-
...
|
23
|
+
... list_data2=[Tn,Cn,Cn2]
|
23
|
-
...
|
24
|
+
... with open('point.csv', 'a', newline='') as f_object:
|
24
|
-
...
|
25
|
+
... writer_object = writer(f_object)
|
25
|
-
...
|
26
|
+
... writer_object.writerow(list_data2)
|
26
|
-
...
|
27
|
+
... f_object.close()
|
27
|
-
...
|
28
|
+
... num = num + 1
|
28
29
|
...
|
29
30
|
Traceback (most recent call last):
|
30
|
-
|
31
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 769, in _validate_tuple_indexer
|
31
|
-
|
32
|
+
self._validate_key(k, i)
|
32
|
-
|
33
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1378, in _validate_key
|
33
|
-
|
34
|
+
raise ValueError(f"Can only index by location with a [{self._valid_types}]")
|
34
35
|
ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]
|
35
36
|
|
36
37
|
The above exception was the direct cause of the following exception:
|
37
38
|
|
38
39
|
Traceback (most recent call last):
|
39
|
-
|
40
|
+
File "<stdin>", line 4, in <module>
|
40
|
-
|
41
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 961, in __getitem__
|
41
|
-
|
42
|
+
return self._getitem_tuple(key)
|
42
|
-
|
43
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1458, in _getitem_tuple
|
43
|
-
|
44
|
+
tup = self._validate_tuple_indexer(tup)
|
44
|
-
|
45
|
+
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 771, in _validate_tuple_indexer
|
45
|
-
|
46
|
+
raise ValueError(
|
46
47
|
ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
|
47
48
|
>>> dfh = pd.read_csv("point.csv",names=["cluster_id","latitude","longitude"])
|
48
|
-
>>> B = pd.read_csv("pps.csv",usecols=["
|
49
|
+
>>> B = pd.read_csv("pps.csv",usecols=["people"])
|
49
|
-
Traceback (most recent call last):
|
50
|
-
File "<stdin>", line 1, in <module>
|
51
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
|
52
|
-
return func(*args, **kwargs)
|
53
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
|
54
|
-
return _read(filepath_or_buffer, kwds)
|
55
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 575, in _read
|
56
|
-
parser = TextFileReader(filepath_or_buffer, **kwds)
|
57
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 933, in __init__
|
58
|
-
self._engine = self._make_engine(f, self.engine)
|
59
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1235, in _make_engine
|
60
|
-
return mapping[engine](f, **self.options)
|
61
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 131, in __init__
|
62
|
-
self._validate_usecols_names(usecols, self.orig_names)
|
63
|
-
File "C:\Users\mable\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\base_parser.py", line 913, in _validate_usecols_names
|
64
|
-
raise ValueError(
|
65
|
-
ValueError: Usecols do not match columns, columns expected but not found: ['#people']
|
66
|
-
>>> #dfh2= pd.DataFrame(B['
|
50
|
+
>>> #dfh2= pd.DataFrame(B['people'])
|
67
|
-
>>> dfh['
|
51
|
+
>>> dfh['people']= B
|
68
|
-
Traceback (most recent call last):
|
69
|
-
File "<stdin>", line 1, in <module>
|
70
|
-
NameError: name 'B' is not defined
|
71
52
|
>>> dfh.to_csv("point.csv")
|
72
53
|
|
73
54
|
|
@@ -182,9 +163,9 @@
|
|
182
163
|
num = num + 1
|
183
164
|
|
184
165
|
dfh = pd.read_csv("point.csv",names=["cluster_id","latitude","longitude"])
|
185
|
-
B = pd.read_csv("pps.csv",usecols=["
|
166
|
+
B = pd.read_csv("pps.csv",usecols=["people"])
|
186
|
-
#dfh2= pd.DataFrame(B['
|
167
|
+
#dfh2= pd.DataFrame(B['people'])
|
187
|
-
dfh['
|
168
|
+
dfh['people']= B
|
188
169
|
dfh.to_csv("point.csv")
|
189
170
|
```
|
190
171
|
|