0% found this document useful (0 votes)
5 views19 pages

Sorting NumPy Arrays Efficiently

Uploaded by

NagaRaju
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

Sorting NumPy Arrays Efficiently

Uploaded by

NagaRaju
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

NumPy Arrays: Sorting Arrays

Fast Sorting in NumPy


 By default [Link] uses quick‐ sort algorithm- BigO[N log N]
 In[5]: x = [Link]([2, 1, 4, 3, 5])
 [Link](x)
 Out[5]: array([1, 2, 3, 4, 5])

 In[6]: [Link]()
 print(x)
 [1 2 3 4 5]

 In[7]: x = [Link]([2, 1, 4, 3, 5])


 i = [Link](x)
 print(i) # index of the smallest element
 [1 0 3 2 4]
Sorting along rows or columns
In[9]: rand = [Link](42)
X = [Link](0, 10, (4, 6))
print(X)
[[6 3 7 4 6 9]
[2 6 7 4 3 7]
[7 2 5 4 1 7]
[5 1 4 0 9 5]]
In[10]: # sort each column of X
[Link](X, axis=0)
Out[10]: array([[2, 1, 4, 0, 1, 5],
[5, 2, 5, 4, 3, 7],
[6, 3, 7, 4, 6, 7],
[7, 6, 7, 4, 9, 9]])
Sorting along rows or columns
In[11]: # sort each row of X
[Link](X, axis=1)
Out[11]: array([[3, 4, 6, 6, 7, 9],
[2, 3, 4, 6, 7, 7],
[1, 2, 4, 5, 7, 7],
[0, 1, 4, 5, 5, 9]])
Partial Sorts: Partitioning
In[12]: x = [Link]([7, 2, 3, 1, 6, 5, 4])
[Link](x, 3)
Out[12]: array([2, 1, 3, 4, 6, 5, 7])

print(X)
[[6 3 7 4 6 9]
[2 6 7 4 3 7]
[7 2 5 4 1 7]
[5 1 4 0 9 5]]

#the first two slots in each row contain the smallest values from that row
In[13]: [Link](X, 2, axis=1)
Out[13]: array([[3, 4, 6, 7, 6, 9],
[2, 3, 4, 7, 6, 7],
[1, 2, 4, 5, 7, 7],
[0, 1, 4, 5, 9, 5]])
Example: k-Nearest Neighbors
In[14]: X = [Link](10, 2)
print(X)
[[0.23089383 0.24102547]
[0.68326352 0.60999666]
[0.83319491 0.17336465]
[0.39106061 0.18223609]
[0.75536141 0.42515587]
[0.20794166 0.56770033]
[0.03131329 0.84228477]
[0.44975413 0.39515024]
[0.92665887 0.727272 ]
[0.32654077 0.57044397]]
Example: k-Nearest Neighbors
In[14]: X = [Link](10, 2)
In[15]: %matplotlib inline
import [Link] as plt
import seaborn; [Link]() # Plot styling
[Link](X[:, 0], X[:, 1], s=100);
Example: k-Nearest Neighbors
In[16]: dist_sq = [Link]((X[:,[Link],:] - X[[Link],:,:]) ** 2, axis=-
1)
In[17]: # for each pair of points, compute differences in their coordinates
differences = X[:, [Link], :] - X[[Link], :, :]
[Link]
Out[17]: (10, 10, 2)
[[[ 0. 0. ]
[-0.45236969 -0.36897119]
[-0.60230109 0.06766081]
[-0.16016678 0.05878938]
[-0.52446758 -0.18413041]
[ 0.02295216 -0.32667486]
[ 0.19958053 -0.60125931]
[-0.21886031 -0.15412477]
[-0.69576504 -0.48624653]
[-0.09564694 -0.32941851]]…….
Example: k-Nearest Neighbors
sq_differences = differences ** 2
sq_differences.shape
Out[18]: (10, 10, 2)
[[[0.00000000e+00 0.00000000e+00]
[2.04638339e-01 1.36139740e-01]
[3.62766598e-01 4.57798555e-03]
[2.56533980e-02 3.45619099e-03]…..
In[19]: # sum the coordinate differences to get the
squared distance
dist_sq = sq_differences.sum(-1)
dist_sq.shape
Out[19]: (10, 10)
Example: k-Nearest Neighbors
Example: k-Nearest Neighbors
In[21]: nearest = [Link](dist_sq, axis=1)
print(nearest)
[[0 3 9 7 1 4 2 5 6 8]
[1 4 7 9 3 6 8 5 0 2]
[2 1 4 6 3 0 8 9 7 5]
[3 9 7 0 1 4 5 8 6 2]
[4 1 8 5 6 7 9 3 0 2]
[5 8 6 4 1 7 9 3 2 0]
[6 8 5 4 1 7 9 3 2 0]
[7 9 3 1 4 0 5 8 6 2]
[8 5 6 4 1 7 9 3 2 0]
[9 7 3 0 1 4 5 8 6 2]]
Example: k-Nearest Neighbors
In[22]: K = 2
nearest_partition = [Link](dist_sq, K + 1,
axis=1)
In[23]: [Link](X[:, 0], X[:, 1], s=100)
# draw lines from each point to its two nearest
neighbors
K=2
for i in range([Link][0]):
for j in nearest_partition[i, :K+1]:
# plot a line from X[i] to X[j]
# use some zip magic to make it happen:
[Link](*zip(X[j], X[i]), color='black')
Example: k-Nearest Neighbors
Structured Data: NumPy’s
Structured Arrays
Structured Arrays
import numpy as np
name = ['Alice', 'Bob', 'Cathy', 'Doug']
age = [25, 45, 37, 19]
weight = [55.0, 85.5, 68.0, 61.5]
data = [Link](4, dtype={'names':('name', 'age',
'weight'),'formats':('U10', 'i4', 'f8')})
print([Link])
[('name', '<U10'), ('age', '<i4'), ('weight', '<f8')]
data['name'] = name
data['age'] = age
data['weight'] = weight
print(data)
[('Alice', 25, 55. ) ('Bob', 45, 85.5) ('Cathy', 37, 68. ) ('Doug',
19, 61.5)]
Structured Arrays
 In[6]: # Get all names
 data['name']
 Out[6]: array(['Alice', 'Bob', 'Cathy', 'Doug'],
dtype='<U10’ )
 In[7]: # Get first row of data
 data[0]
 Out[7]: ('Alice', 25, 55.0)
 In[8]: # Get the name from the last row
 data[-1]['name']
 Out[8]: 'Doug‘
 In[9]: # Get names where age is under 30
 data[data['age'] < 30]['name']
 Out[9]: array(['Alice', 'Doug'], dtype='<U10')
NumPy data types
More Advanced Compound Types
 In[14]: tp = [Link]([('id', 'i8'), ('mat', 'f8', (3,
3))])
 X = [Link](1, dtype=tp)
 print(X[0])
 print(X['mat'][0])
 (0, [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0,
0.0]])
 [[ 0. 0. 0.]
 [ 0. 0. 0.]
 [ 0. 0. 0.]]
RecordArrays: Structured Arrays with a
Twist
In[15]: data['age']
Out[15]: array([25, 45, 37, 19], dtype=int32)
If we view our data as a record array instead, we can
access this with slightly fewer keystrokes:
In[16]: data_rec = [Link]([Link])
data_rec.age
Out[16]: array([25, 45, 37, 19], dtype=int32)
In[17]: %timeit data['age']
%timeit data_rec['age']
%timeit data_rec.age 1
000000 loops, best of 3: 241 ns per loop
100000 loops, best of 3: 4.61 µs per loop
100000 loops, best of 3: 7.27 µs per loop

You might also like