質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

680閲覧

連続したものをグルーピングするアルゴリズム

furukawatomoya

総合スコア13

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

1グッド

0クリップ

投稿2019/01/05 05:25

前提・実現したいこと

pd.read_csv()で読み込んだ以下のようなデータに対して、つながっているものを一つとしてグルーピングが行いたいです。
(例)上のものを下もののように、縦横斜めでつながっているものを一つのグループとして各々に数字を当てて返したいです。
100010000000001
010100000000011
011000000000011
010000000000001

200020000000003
020200000000033
022000000000033
020000000000003

発生している問題・エラーメッセージ

しかし、私が考えたアルゴリズムだと
200030000000004
030300000000044
033000000000044
030000000000004

のようにうまくグルーピングできないものになってしまします

該当のソースコード

python

def hedge_care_max(max_point,point):
if (point > max_point):
return max_point
else:
return point

def hedge_care_min(min_point,point):
if (point < min_point):
return min_point
else:
return point

reader = pd.read_csv()#自分のファイルから上の例のようなCSVを読み込んでいます
reader_numpy = reader.values

y_max_point = reader.shape[0]-1
x_max_point = reader.shape[1]-1
y_min_point = 0
x_min_point = 0

for x in range(0,reader.shape[1]):
for y in range(0,reader.shape[0]):
reader_numpy = reader.values
if reader.loc[y,x] == 0:
reader.loc[y,x] = 0

elif reader.loc[y,x] == 1: reader.loc[y,x] = np.amax(reader_numpy)+1 if(reader.loc[hedge_care_min(y_min_point,y-1),x] != 0): reader.loc[hedge_care_min(y_min_point,y-1),x] = reader.loc[y,x] if(reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_max(x_max_point,x+1)] != 0): reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_max(x_max_point,x+1)] = reader.loc[y,x] if(reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_min(x_min_point,x-1)] != 0): reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_min(x_min_point,x-1)] = reader.loc[y,x] if(reader.loc[y,hedge_care_max(x_max_point,x+1)] != 0): reader.loc[y,hedge_care_max(x_max_point,x+1)] = reader.loc[y,x] if(reader.loc[y,hedge_care_min(x_min_point,x-1)] != 0): reader.loc[y,hedge_care_min(x_min_point,x-1)] = reader.loc[y,x] if(reader.loc[hedge_care_max(y_max_point,y+1),x] != 0): reader.loc[hedge_care_max(y_max_point,y+1),x] = reader.loc[y,x] if(reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_max(x_max_point,x+1)] != 0): reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_max(x_max_point,x+1)] = reader.loc[y,x] if(reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_min(x_min_point,x-1)] != 0): reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_min(x_min_point,x-1)] = reader.loc[y,x] else: if(reader.loc[hedge_care_min(y_min_point,y-1),x] != 0): reader.loc[hedge_care_min(y_min_point,y-1),x] = reader.loc[y,x] if(reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_max(x_max_point,x+1)] != 0): reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_max(x_max_point,x+1)] = reader.loc[y,x] if(reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_min(x_min_point,x-1)] != 0): reader.loc[hedge_care_min(y_min_point,y-1),hedge_care_min(x_min_point,x-1)] = reader.loc[y,x] if(reader.loc[y,hedge_care_max(x_max_point,x+1)] != 0): reader.loc[y,hedge_care_max(x_max_point,x+1)] = reader.loc[y,x] if(reader.loc[y,hedge_care_min(x_min_point,x-1)] != 0): reader.loc[y,hedge_care_min(x_min_point,x-1)] = reader.loc[y,x] if(reader.loc[hedge_care_max(y_max_point,y+1),x] != 0): reader.loc[hedge_care_max(y_max_point,y+1),x] = reader.loc[y,x] if(reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_max(x_max_point,x+1)] != 0): reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_max(x_max_point,x+1)] = reader.loc[y,x] if(reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_min(x_min_point,x-1)] != 0): reader.loc[hedge_care_max(y_max_point,y+1),hedge_care_min(x_min_point,x-1)] = reader.loc[y,x]

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

naikoru👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2019/01/05 06:06

コードが読めないので、https://teratail.com/tour を参考にシンタックスハイライト・マークダウンを用いてコード部分を囲んでください。質問は再編集できます。
guest

回答1

0

ベストアンサー

挙動的に、端の境界判定をミスっているような気がします。

参考までに私が書いたコードを示しておきます。

python

1import numpy as np 2from itertools import product 3 4def check(i, j, label, data, result): 5 if result[i,j] == -1: # 未チェックなら処理する 6 if data[i,j] == 0: 7 result[i,j] = 0 8 else: 9 result[i,j] = label 10 for i_n, j_n in product([i-1, i, i+1], [j-1, j, j+1]): 11 if 0 <= i_n < ilim and 0 <= j_n < jlim: 12 check(i_n, j_n, label, data, result) 13 return True 14 else: 15 return False 16 17data = np.array([[1,0,0,0,1,0,0,0,0,0,0,0,0,0,1], 18 [0,1,0,1,0,0,0,0,0,0,0,0,0,1,1], 19 [0,1,1,0,0,0,0,0,0,0,0,0,0,1,1], 20 [0,1,0,0,0,0,0,0,0,0,0,0,0,0,1]]) 21ilim, jlim = data.shape 22result = (-np.ones(data.shape)).astype(int) 23 24label = 2 25for i in range(ilim): 26 for j in range(jlim): 27 if check(i, j, label, data, result): 28 label += 1 29print(data) 30print(result) 31 32""" => 33[[1 0 0 0 1 0 0 0 0 0 0 0 0 0 1] 34 [0 1 0 1 0 0 0 0 0 0 0 0 0 1 1] 35 [0 1 1 0 0 0 0 0 0 0 0 0 0 1 1] 36 [0 1 0 0 0 0 0 0 0 0 0 0 0 0 1]] 37[[2 0 0 0 2 0 0 0 0 0 0 0 0 0 3] 38 [0 2 0 2 0 0 0 0 0 0 0 0 0 3 3] 39 [0 2 2 0 0 0 0 0 0 0 0 0 0 3 3] 40 [0 2 0 0 0 0 0 0 0 0 0 0 0 0 3]] 41"""

投稿2019/01/05 06:30

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

furukawatomoya

2019/01/05 07:04

hayataka2049さんありがとうございます! 完璧にできました! また、質問版の書き方もご教示いただきまことにありがとうございます! 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問