Created
February 18, 2021 16:59
-
-
Save keimina/518bab6312ac79a2effe678c660569f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
def is_not_join(i): | |
if i in ["A", "B", "C"]: | |
ret = True | |
else: | |
ret = False | |
return ret | |
def label_1d(arr1d, first_lbl_num): | |
lbl = np.zeros_like(arr1d, dtype=int) | |
i = 0 | |
lbl_num = 1 | |
first = True | |
while i < len(arr1d): | |
if first: | |
if arr1d[i]: | |
lbl[i] = lbl_num | |
else: | |
lbl_num = 0 | |
i += 1 | |
first = False | |
continue | |
if not arr1d[i-1]: | |
if not arr1d[i]: | |
pass | |
else: | |
lbl_num += 1 | |
lbl[i] = lbl_num | |
else: | |
if not arr1d[i]: | |
pass | |
else: | |
if is_not_join(arr1d[i-1]): | |
lbl_num += 1 | |
lbl[i] = lbl_num | |
elif is_not_join(arr1d[i]): | |
lbl_num += 1 | |
lbl[i] = lbl_num | |
else: | |
lbl[i] = lbl_num | |
i += 1 | |
lbl[lbl > 0] += first_lbl_num - 1 | |
return lbl | |
def label_2d(arr2d): | |
lbl2d = np.zeros_like(arr2d, dtype=int) | |
for n, arr in enumerate(arr2d): | |
lbl2d[n] = label_1d(arr, lbl2d.max() + 1) | |
return lbl2d | |
arr1d = pd.Series(["", "B", "", "C", "D", "E", "", "A", "D", "E","A"]).values | |
arr2d = pd.DataFrame([["", "B", "", "C", "D", "E", "", "A", "D", "E", ""], | |
["", "B", "A", "", "", "E", "", "A", "D", "E", ""]]).values | |
arr2d = pd.DataFrame([["","C", "D", "D"], ["","E", "E", ""],["C","B", "", ""]]).values | |
t = label_1d(arr1d, 10) | |
t = label_2d(arr2d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment