假设我有以下2D数组:
导入numpy为np
np.random.seed(123)a = np.random.randint(1,6,size =(5,3))产生:
在[371]中:a出[371]:数组([[3,5,3], [2,4,3], …
我有一个想法,代码如下。 我的代码的最大缺点是它随着列的增加而运行得非常慢,并且比@Paul Panzer的代码慢。我向Paul Panzer道歉。
如果你想更快,只需忽略num_to_items的功能。因为 (1, 1) 等于 1*2**20 + 1 。
(1, 1)
1*2**20 + 1
import numpy as np from random import choice from itertools import izip from scipy.sparse import csr_matrix, csc_matrix from scipy import sparse as sp c_10 = np.array([[choice(range(1, 10)) for _ in range(3)] for _ in range(1000)]) c_1000 = np.array([[choice(range(1, 1000)) for _ in range(3)] for _ in range(1000)]) def _bit_to_items(num): return (num >> 20, num & 0b1111111111111111111) def unique_bit_shit(c): cc = c << 20 # suppose that: 2**20 > max(c) dialog_mtx_1 = np.array([[1, 0, 0], [1, 0, 0], [0, 1, 0]]) dialog_mtx_2 = np.array([[0, 1, 0], [0, 0, 1], [0, 0, 1]]) dialog_mtx_1 = dialog_mtx_1.T dialog_mtx_2 = dialog_mtx_2.T pairs = cc.dot(dialog_mtx_1) + c.dot(dialog_mtx_2) pairs_num, count = np.unique(pairs, return_counts=True) return [(_bit_to_items(num), v) for num, v in izip(pairs_num, count)] def _dot_to_items(num): # 2**20 is 1048576 return (num / 1048576, num % 1048576) def unique_dot(c): dialog_mtx_3 = np.array([[2**20, 2**20, 0], [1, 0, 2**20], [0, 1, 1]]) pairs = c.dot(dialog_mtx_3) pairs_num, count = np.unique(pairs, return_counts=True) return [(_dot_to_items(num), v) for num, v in izip(pairs_num, count)]