Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
34497c4c
Commit
34497c4c
authored
May 03, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplified the groupby and group_mapping function
parent
636bc2c2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
23 deletions
+13
-23
groupby.pyx
alphamind/groupby.pyx
+13
-23
No files found.
alphamind/groupby.pyx
View file @
34497c4c
...
...
@@ -9,13 +9,8 @@ Created on 2017-4-26
import numpy as np
from numpy import zeros
cimport numpy as np
from numpy import array
cimport cython
from libcpp.vector cimport vector as cpp_vector
from libcpp.unordered_map cimport unordered_map as cpp_map
from cython.operator cimport dereference as deref
ctypedef long long int64_t
@cython.boundscheck(False)
...
...
@@ -23,23 +18,20 @@ ctypedef long long int64_t
@cython.initializedcheck(False)
cpdef groupby(long[:] groups):
cdef
long long
length = groups.shape[0]
cdef
cpp_map[long, cpp_vector[int64_t]] group_ids
cdef
long long
i
cdef
size_t
length = groups.shape[0]
cdef
dict group_ids = {}
cdef
size_t
i
cdef long curr_tag
cdef cpp_map[long, cpp_vector[int64_t]].iterator it
cdef np.ndarray[long long, ndim=1] npy_array
for i in range(length):
curr_tag = groups[i]
it = group_ids.find(curr_tag)
if it == group_ids.end():
try:
group_ids[curr_tag].append(i)
except KeyError:
group_ids[curr_tag] = [i]
else:
deref(it).second.push_back(i)
return [
np.
array(v) for v in group_ids.values()]
return [array(v) for v in group_ids.values()]
@cython.boundscheck(False)
...
...
@@ -48,20 +40,18 @@ cpdef groupby(long[:] groups):
cpdef np.ndarray[long, ndim=1] group_mapping(long[:] groups):
cdef size_t length = groups.shape[0]
cdef np.ndarray[long, ndim=1] res= zeros(length, dtype=long)
cdef
cpp_map[long, long] current_hold
cdef
dict current_hold = {}
cdef long curr_tag
cdef long running_tag = -1
cdef size_t i = 0
cdef cpp_map[long, long].iterator it
cdef size_t i
for i in range(length):
curr_tag = groups[i]
it = current_hold.find(curr_tag)
if it == current_hold.end():
try:
res[i] = current_hold[curr_tag]
except KeyError:
running_tag += 1
res[i] = running_tag
current_hold[curr_tag] = running_tag
else:
res[i] = deref(it).second
return res
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment