-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsparsesort_andrew.py
More file actions
58 lines (42 loc) · 1.25 KB
/
sparsesort_andrew.py
File metadata and controls
58 lines (42 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 25 02:17:43 2020
@author: andrew
"""
'''
Like "Finding needles in a haystack"
This script creates x random values in large matrices
filled with zeroes and then
sorts them with their coordinate location in the matrix '''
import sparse
import random
import numpy as np
import collections
def sparsesort(numbers,coords):
X,Y=coords
matrix=np.zeros((X,Y))
for i in range(numbers):
rx,ry = random.randint(0,X-1), random.randint(0,Y-1)
matrix[rx,ry] = random.random()
print()
print(r'Sorting {} random numbers in a sparse ("zero-filled") {} x {} matrix:'.\
format(numbers,X,Y))
print(matrix)
print()
sparse_matrix = sparse.COO(matrix)
where = sparse.where(sparse_matrix)
smd = sparse_matrix.data
matrix_dict = dict(zip(smd, zip(*where)))
matrix_sorted = collections.OrderedDict(sorted(matrix_dict.items()))
mitems = matrix_sorted.items()
print('sorting results:')
k=1
for i, j in mitems:
print('item #{}: {} @ coordinate {}'.format(k,i, j))
k+=1
print()
print('\nTINY MATRIX')
sparsesort(10,(5,5))
print('\nHUGE MATRIX')
sparsesort(100,(6000,6000))