Commit 614007d7 authored by lisj's avatar lisj
Browse files

修复numpy.bool

parent fa71fb44
...@@ -73,7 +73,7 @@ for _, item in train_data.items(): ...@@ -73,7 +73,7 @@ for _, item in train_data.items():
for col, row in zip(cols, rows): for col, row in zip(cols, rows):
bg_matrix[gt_classes[col], gt_classes[row]] += 1 bg_matrix[gt_classes[col], gt_classes[row]] += 1
else: else:
all_possib = np.ones_like(iou_mat, dtype=np.bool) all_possib = np.ones_like(iou_mat, dtype=bool)
np.fill_diagonal(all_possib, 0) np.fill_diagonal(all_possib, 0)
cols, rows = np.where(all_possib) cols, rows = np.where(all_possib)
for col, row in zip(cols, rows): for col, row in zip(cols, rows):
......
...@@ -11,7 +11,7 @@ def fit_logistic_regression(X, y, data_random_seed=1, repeat=1): ...@@ -11,7 +11,7 @@ def fit_logistic_regression(X, y, data_random_seed=1, repeat=1):
# transform targets to one-hot vector # transform targets to one-hot vector
one_hot_encoder = OneHotEncoder(categories='auto', sparse=False) one_hot_encoder = OneHotEncoder(categories='auto', sparse=False)
y = one_hot_encoder.fit_transform(y.reshape(-1, 1)).astype(np.bool) y = one_hot_encoder.fit_transform(y.reshape(-1, 1)).astype(bool)
# normalize x # normalize x
X = normalize(X, norm='l2') X = normalize(X, norm='l2')
...@@ -34,7 +34,7 @@ def fit_logistic_regression(X, y, data_random_seed=1, repeat=1): ...@@ -34,7 +34,7 @@ def fit_logistic_regression(X, y, data_random_seed=1, repeat=1):
y_pred = clf.predict_proba(X_test) y_pred = clf.predict_proba(X_test)
y_pred = np.argmax(y_pred, axis=1) y_pred = np.argmax(y_pred, axis=1)
y_pred = one_hot_encoder.transform(y_pred.reshape(-1, 1)).astype(np.bool) y_pred = one_hot_encoder.transform(y_pred.reshape(-1, 1)).astype(bool)
test_acc = metrics.accuracy_score(y_test, y_pred) test_acc = metrics.accuracy_score(y_test, y_pred)
accuracies.append(test_acc) accuracies.append(test_acc)
...@@ -44,7 +44,7 @@ def fit_logistic_regression(X, y, data_random_seed=1, repeat=1): ...@@ -44,7 +44,7 @@ def fit_logistic_regression(X, y, data_random_seed=1, repeat=1):
def fit_logistic_regression_preset_splits(X, y, train_mask, val_mask, test_mask): def fit_logistic_regression_preset_splits(X, y, train_mask, val_mask, test_mask):
# transform targets to one-hot vector # transform targets to one-hot vector
one_hot_encoder = OneHotEncoder(categories='auto', sparse=False) one_hot_encoder = OneHotEncoder(categories='auto', sparse=False)
y = one_hot_encoder.fit_transform(y.reshape(-1, 1)).astype(np.bool) y = one_hot_encoder.fit_transform(y.reshape(-1, 1)).astype(bool)
# normalize x # normalize x
X = normalize(X, norm='l2') X = normalize(X, norm='l2')
...@@ -67,13 +67,13 @@ def fit_logistic_regression_preset_splits(X, y, train_mask, val_mask, test_mask) ...@@ -67,13 +67,13 @@ def fit_logistic_regression_preset_splits(X, y, train_mask, val_mask, test_mask)
y_pred = clf.predict_proba(X_val) y_pred = clf.predict_proba(X_val)
y_pred = np.argmax(y_pred, axis=1) y_pred = np.argmax(y_pred, axis=1)
y_pred = one_hot_encoder.transform(y_pred.reshape(-1, 1)).astype(np.bool) y_pred = one_hot_encoder.transform(y_pred.reshape(-1, 1)).astype(bool)
val_acc = metrics.accuracy_score(y_val, y_pred) val_acc = metrics.accuracy_score(y_val, y_pred)
if val_acc > best_acc: if val_acc > best_acc:
best_acc = val_acc best_acc = val_acc
y_pred = clf.predict_proba(X_test) y_pred = clf.predict_proba(X_test)
y_pred = np.argmax(y_pred, axis=1) y_pred = np.argmax(y_pred, axis=1)
y_pred = one_hot_encoder.transform(y_pred.reshape(-1, 1)).astype(np.bool) y_pred = one_hot_encoder.transform(y_pred.reshape(-1, 1)).astype(bool)
best_test_acc = metrics.accuracy_score(y_test, y_pred) best_test_acc = metrics.accuracy_score(y_test, y_pred)
accuracies.append(best_test_acc) accuracies.append(best_test_acc)
......
...@@ -152,7 +152,7 @@ class QM9(QM9Dataset): ...@@ -152,7 +152,7 @@ class QM9(QM9Dataset):
# calculate the distance between all atoms # calculate the distance between all atoms
dist = np.linalg.norm(R[:, None, :] - R[None, :, :], axis=-1) dist = np.linalg.norm(R[:, None, :] - R[None, :, :], axis=-1)
# keep all edges that don't exceed the cutoff and delete self-loops # keep all edges that don't exceed the cutoff and delete self-loops
adj = sp.csr_matrix(dist <= self.cutoff) - sp.eye(n_atoms, dtype=np.bool) adj = sp.csr_matrix(dist <= self.cutoff) - sp.eye(n_atoms, dtype=bool)
adj = adj.tocoo() adj = adj.tocoo()
u, v = torch.tensor(adj.row), torch.tensor(adj.col) u, v = torch.tensor(adj.row), torch.tensor(adj.col)
g = dgl_graph((u, v)) g = dgl_graph((u, v))
......
...@@ -118,9 +118,9 @@ class GASDataset(DGLBuiltinDataset): ...@@ -118,9 +118,9 @@ class GASDataset(DGLBuiltinDataset):
train_idx = index[:int(train_size * num_edges)] train_idx = index[:int(train_size * num_edges)]
val_idx = index[num_edges - int(val_size * num_edges):] val_idx = index[num_edges - int(val_size * num_edges):]
test_idx = index[int(train_size * num_edges):num_edges - int(val_size * num_edges)] test_idx = index[int(train_size * num_edges):num_edges - int(val_size * num_edges)]
train_mask = np.zeros(num_edges, dtype=np.bool) train_mask = np.zeros(num_edges, dtype=bool)
val_mask = np.zeros(num_edges, dtype=np.bool) val_mask = np.zeros(num_edges, dtype=bool)
test_mask = np.zeros(num_edges, dtype=np.bool) test_mask = np.zeros(num_edges, dtype=bool)
train_mask[train_idx] = True train_mask[train_idx] = True
val_mask[val_idx] = True val_mask[val_idx] = True
test_mask[test_idx] = True test_mask[test_idx] = True
......
...@@ -33,7 +33,7 @@ def repeat(n_times): ...@@ -33,7 +33,7 @@ def repeat(n_times):
def prob_to_one_hot(y_pred): def prob_to_one_hot(y_pred):
ret = np.zeros(y_pred.shape, np.bool) ret = np.zeros(y_pred.shape, bool)
indices = np.argmax(y_pred, axis=1) indices = np.argmax(y_pred, axis=1)
for i in range(y_pred.shape[0]): for i in range(y_pred.shape[0]):
ret[i][indices[i]] = True ret[i][indices[i]] = True
...@@ -58,7 +58,7 @@ def label_classification(embeddings, y, train_mask, test_mask, split='random', r ...@@ -58,7 +58,7 @@ def label_classification(embeddings, y, train_mask, test_mask, split='random', r
Y = y.detach().cpu().numpy() Y = y.detach().cpu().numpy()
Y = Y.reshape(-1, 1) Y = Y.reshape(-1, 1)
onehot_encoder = OneHotEncoder(categories='auto').fit(Y) onehot_encoder = OneHotEncoder(categories='auto').fit(Y)
Y = onehot_encoder.transform(Y).toarray().astype(np.bool) Y = onehot_encoder.transform(Y).toarray().astype(bool)
X = normalize(X, norm='l2') X = normalize(X, norm='l2')
......
...@@ -64,11 +64,11 @@ def load_data(args, multilabel): ...@@ -64,11 +64,11 @@ def load_data(args, multilabel):
prefix = "data/{}".format(args.dataset) prefix = "data/{}".format(args.dataset)
DataType = namedtuple('Dataset', ['num_classes', 'train_nid', 'g']) DataType = namedtuple('Dataset', ['num_classes', 'train_nid', 'g'])
adj_full = scipy.sparse.load_npz('./{}/adj_full.npz'.format(prefix)).astype(np.bool) adj_full = scipy.sparse.load_npz('./{}/adj_full.npz'.format(prefix)).astype(bool)
g = dgl.from_scipy(adj_full) g = dgl.from_scipy(adj_full)
num_nodes = g.num_nodes() num_nodes = g.num_nodes()
adj_train = scipy.sparse.load_npz('./{}/adj_train.npz'.format(prefix)).astype(np.bool) adj_train = scipy.sparse.load_npz('./{}/adj_train.npz'.format(prefix)).astype(bool)
train_nid = np.array(list(set(adj_train.nonzero()[0]))) train_nid = np.array(list(set(adj_train.nonzero()[0])))
role = json.load(open('./{}/role.json'.format(prefix))) role = json.load(open('./{}/role.json'.format(prefix)))
......
...@@ -152,7 +152,7 @@ def HEM_one_level(rr, cc, vv, rid, weights): ...@@ -152,7 +152,7 @@ def HEM_one_level(rr, cc, vv, rid, weights):
nnz = rr.shape[0] nnz = rr.shape[0]
N = rr[nnz - 1] + 1 N = rr[nnz - 1] + 1
marked = np.zeros(N, np.bool) marked = np.zeros(N, bool)
rowstart = np.zeros(N, np.int32) rowstart = np.zeros(N, np.int32)
rowlength = np.zeros(N, np.int32) rowlength = np.zeros(N, np.int32)
cluster_id = np.zeros(N, np.int32) cluster_id = np.zeros(N, np.int32)
......
...@@ -9,9 +9,9 @@ import dask.dataframe as dd ...@@ -9,9 +9,9 @@ import dask.dataframe as dd
# takes. It essentially follows the intuition of "training on the past and predict the future". # takes. It essentially follows the intuition of "training on the past and predict the future".
# One can also change the threshold to make validation and test set take larger proportions. # One can also change the threshold to make validation and test set take larger proportions.
def train_test_split_by_time(df, timestamp, user): def train_test_split_by_time(df, timestamp, user):
df['train_mask'] = np.ones((len(df),), dtype=np.bool) df['train_mask'] = np.ones((len(df),), dtype=bool)
df['val_mask'] = np.zeros((len(df),), dtype=np.bool) df['val_mask'] = np.zeros((len(df),), dtype=bool)
df['test_mask'] = np.zeros((len(df),), dtype=np.bool) df['test_mask'] = np.zeros((len(df),), dtype=bool)
df = dd.from_pandas(df, npartitions=10) df = dd.from_pandas(df, npartitions=10)
def train_test_split(df): def train_test_split(df):
df = df.sort_values([timestamp]) df = df.sort_values([timestamp])
......
...@@ -28,13 +28,13 @@ def data_type_dict(): ...@@ -28,13 +28,13 @@ def data_type_dict():
'int16' : np.int16, 'int16' : np.int16,
'int32' : np.int32, 'int32' : np.int32,
'int64' : np.int64, 'int64' : np.int64,
'bool' : np.bool} # mxnet does not support bool 'bool' : bool} # mxnet does not support bool
def cpu(): def cpu():
return mx.cpu() return mx.cpu()
def tensor(data, dtype=None): def tensor(data, dtype=None):
if dtype == np.bool: if dtype == bool:
# mxnet doesn't support bool # mxnet doesn't support bool
dtype = np.int32 dtype = np.int32
if isinstance(data, nd.NDArray): if isinstance(data, nd.NDArray):
...@@ -47,7 +47,7 @@ def tensor(data, dtype=None): ...@@ -47,7 +47,7 @@ def tensor(data, dtype=None):
data = [data] data = [data]
if dtype is None: if dtype is None:
if isinstance(data, np.ndarray): if isinstance(data, np.ndarray):
dtype = np.int32 if data.dtype == np.bool else data.dtype dtype = np.int32 if data.dtype == bool else data.dtype
elif len(data) == 0: elif len(data) == 0:
dtype = np.int64 dtype = np.int64
else: else:
...@@ -133,7 +133,7 @@ def to_backend_ctx(dglctx): ...@@ -133,7 +133,7 @@ def to_backend_ctx(dglctx):
raise ValueError('Unsupported DGL device context:', dglctx) raise ValueError('Unsupported DGL device context:', dglctx)
def astype(input, ty): def astype(input, ty):
if ty == np.bool: if ty == bool:
ty = np.int32 ty = np.int32
return input.astype(ty) return input.astype(ty)
......
...@@ -151,9 +151,9 @@ class FakeNewsDataset(DGLBuiltinDataset): ...@@ -151,9 +151,9 @@ class FakeNewsDataset(DGLBuiltinDataset):
train_idx = np.load(os.path.join(self.raw_path, 'train_idx.npy')) train_idx = np.load(os.path.join(self.raw_path, 'train_idx.npy'))
val_idx = np.load(os.path.join(self.raw_path, 'val_idx.npy')) val_idx = np.load(os.path.join(self.raw_path, 'val_idx.npy'))
test_idx = np.load(os.path.join(self.raw_path, 'test_idx.npy')) test_idx = np.load(os.path.join(self.raw_path, 'test_idx.npy'))
train_mask = np.zeros(num_graphs, dtype=np.bool) train_mask = np.zeros(num_graphs, dtype=bool)
val_mask = np.zeros(num_graphs, dtype=np.bool) val_mask = np.zeros(num_graphs, dtype=bool)
test_mask = np.zeros(num_graphs, dtype=np.bool) test_mask = np.zeros(num_graphs, dtype=bool)
train_mask[train_idx] = True train_mask[train_idx] = True
val_mask[val_idx] = True val_mask[val_idx] = True
test_mask[test_idx] = True test_mask[test_idx] = True
......
...@@ -203,9 +203,9 @@ class FraudDataset(DGLBuiltinDataset): ...@@ -203,9 +203,9 @@ class FraudDataset(DGLBuiltinDataset):
train_idx = index[:int(train_size * len(index))] train_idx = index[:int(train_size * len(index))]
val_idx = index[len(index) - int(val_size * len(index)):] val_idx = index[len(index) - int(val_size * len(index)):]
test_idx = index[int(train_size * len(index)):len(index) - int(val_size * len(index))] test_idx = index[int(train_size * len(index)):len(index) - int(val_size * len(index))]
train_mask = np.zeros(N, dtype=np.bool) train_mask = np.zeros(N, dtype=bool)
val_mask = np.zeros(N, dtype=np.bool) val_mask = np.zeros(N, dtype=bool)
test_mask = np.zeros(N, dtype=np.bool) test_mask = np.zeros(N, dtype=bool)
train_mask[train_idx] = True train_mask[train_idx] = True
val_mask[val_idx] = True val_mask[val_idx] = True
test_mask[test_idx] = True test_mask[test_idx] = True
......
...@@ -182,7 +182,7 @@ class QM9Dataset(DGLDataset): ...@@ -182,7 +182,7 @@ class QM9Dataset(DGLDataset):
n_atoms = self.N[idx] n_atoms = self.N[idx]
R = self.R[self.N_cumsum[idx]:self.N_cumsum[idx + 1]] R = self.R[self.N_cumsum[idx]:self.N_cumsum[idx + 1]]
dist = np.linalg.norm(R[:, None, :] - R[None, :, :], axis=-1) dist = np.linalg.norm(R[:, None, :] - R[None, :, :], axis=-1)
adj = sp.csr_matrix(dist <= self.cutoff) - sp.eye(n_atoms, dtype=np.bool) adj = sp.csr_matrix(dist <= self.cutoff) - sp.eye(n_atoms, dtype=bool)
adj = adj.tocoo() adj = adj.tocoo()
u, v = F.tensor(adj.row), F.tensor(adj.col) u, v = F.tensor(adj.row), F.tensor(adj.col)
g = dgl_graph((u, v)) g = dgl_graph((u, v))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment