NUMPY PACKAGE
CW - Overall Understanding
Padhai_Numpy

Comparing performance within lists

In [ ]:
N = 100000000
In [ ]:
%%time
list_ = list(range(N))
for i in range(N):
    list_[i] = list_[i] * list_[i]
CPU times: user 28.1 s, sys: 2.25 s, total: 30.3 s
Wall time: 30.5 s

Finetune the for loop in the above code

In [ ]:
%%time
list_ = list(range(N))
list_ = [item * item for item in list_]
CPU times: user 9.77 s, sys: 4.23 s, total: 14 s
Wall time: 14 s

further., finetune the above list

In [ ]:
%%time
list_ = list(range(N))
list_ = map(lambda x: x * x, list_)
CPU times: user 1.55 s, sys: 1.45 s, total: 2.99 s
Wall time: 2.98 s

if, in case we want to add all the numbers in the list... !!

In [ ]:
%%time
list_ = list(range(N))
list_sum = 0
for item in list_:
    list_sum += item
CPU times: user 14.8 s, sys: 2 s, total: 16.8 s
Wall time: 16.8 s

finetune with in-built function for addition

In [ ]:
%%time
list_ = list(range(N))
list_sum = sum(list_)
CPU times: user 2.35 s, sys: 1.78 s, total: 4.13 s
Wall time: 4.11 s
In [ ]:
import numpy as np
In [ ]:
%%time
arr = np.arange(N)
arr = arr * arr
CPU times: user 1.06 s, sys: 1.74 ms, total: 1.06 s
Wall time: 1.09 s

finetune the above code with in-built function

In [ ]:
%%time
arr = np.arange(N)
arr_sum = np.sum(arr)
CPU times: user 355 ms, sys: 0 ns, total: 355 ms
Wall time: 375 ms

the above code and respective exection time conveys that numpy arrays are significantly faster than normal lists

Creating nd arrays (n-dimensional arrays)

In [ ]:
arr = np.arange(6)
In [ ]:
print(arr)
[0 1 2 3 4 5]
In [ ]:
print(arr.dtype)
int64
In [ ]:
print(arr.size, arr.dtype)
6 int64
In [ ]:
print(arr.itemsize)
8
In [ ]:
print(arr.ndim)
1
In [ ]:
arr2d = np.array([
                  [1,2,3],
                  [4,5,6]
])
In [ ]:
arr2d
Out[ ]:
array([[1, 2, 3],
       [4, 5, 6]])
In [ ]:
print(arr2d.dtype, arr2d.ndim)
int64 2
In [ ]:
arr2d.size
Out[ ]:
6
In [ ]:
arr2d.ndim
Out[ ]:
2
In [ ]:
arr2d.shape
Out[ ]:
(2, 3)
In [ ]:
arr2d = np.array([
                  [1,2,3],
                  [4,5,6]
])
In [ ]:
arr3d = np.array([
                  [[1,2,3],
                   [4,5,6]],
                  [[7,8,9],
                   [10,11,12]]
])
In [ ]:
arr3d
Out[ ]:
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
In [ ]:
arr3d = np.array([
                  [
                   [1,2,3],
                   [4,5,6]
                  ],
                  [
                   [7,8,9],
                  [10,11,12]
                    ]
])
In [ ]:
print(arr3d)
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]
In [ ]:
arr3d
Out[ ]:
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
In [ ]:
print(arr3d.size)
12
In [ ]:
print(arr3d.itemsize)
8
In [ ]:
print(arr3d.ndim)
3
In [ ]:
arr3d.shape
Out[ ]:
(2, 2, 3)
In [ ]:
np.ones((3,4))      # a two dimensional array - observe the number of square braces over here
Out[ ]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
In [ ]:
np.ones((2,3,4))  # a three dimensional array - observe the number of square braces over here
Out[ ]:
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]])
In [ ]:
np.ones((2,3,4,5))      # a four dimensional array - observe the number of square braces over here
Out[ ]:
array([[[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]],


       [[[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1.]]]])
In [ ]:
arr5d = np.ones((2,3,4,5,6))        # a four dimensional array - observe the number of square braces over here
print(arr5d)
[[[[[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]]


  [[[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]]


  [[[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]]]



 [[[[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]]


  [[[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]]


  [[[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]

   [[1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]
    [1. 1. 1. 1. 1. 1.]]]]]
In [ ]:
1729 * np.ones((2,3,4))
Out[ ]:
array([[[1729., 1729., 1729., 1729.],
        [1729., 1729., 1729., 1729.],
        [1729., 1729., 1729., 1729.]],

       [[1729., 1729., 1729., 1729.],
        [1729., 1729., 1729., 1729.],
        [1729., 1729., 1729., 1729.]]])
In [ ]:
np.zeros((2,3,4))
Out[ ]:
array([[[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]],

       [[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]]])

Generating random numbers to create arrays

  • generally used in probability
  • also used in simulations, sampling...
  • randn from random generates a series of numbers with mean as 0 and variance as 1 - forms normal distribution
  • n in randn stands for normal distribution
In [ ]:
np.random.randn(2,3)
Out[ ]:
array([[ 0.57314445,  0.71712891,  0.42598204],
       [-1.1019094 ,  0.24444025,  0.08239929]])
In [ ]:
np.random.randn(3,3)
Out[ ]:
array([[-0.88843753, -0.0972612 , -0.87371097],
       [-0.6523051 ,  0.34056285,  0.77792798],
       [-1.03552801, -1.59169572, -2.71135498]])
  • to get numbers uniformly sampled between 0 and 1 - is rand
In [ ]:
np.random.rand(2,3)
Out[ ]:
array([[0.73865911, 0.37088827, 0.60850801],
       [0.96256262, 0.69262156, 0.57283244]])
  • to generate random numbers in a given interval, with a particular shape - use randint()
In [ ]:
np.random.randint(10, 80, (3,4))
Out[ ]:
array([[59, 11, 34, 45],
       [42, 61, 18, 21],
       [69, 22, 60, 39]])
In [ ]:
np.arange(2, 10, 7) # - with step size of 7
Out[ ]:
array([2, 9])
In [ ]:
np.arange(2, 30, 2)     # with step size of 2
Out[ ]:
array([ 2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
In [ ]:
np.arange(2, 40, 12)        # with step size of 12
Out[ ]:
array([ 2, 14, 26, 38])
In [ ]:
np.linspace(7, 70, 10) # linspace or linear space generates 10 numbers between 2 and 4
Out[ ]:
array([ 7., 14., 21., 28., 35., 42., 49., 56., 63., 70.])
In [ ]:
np.linspace(1,2, 20)    # twenty numbers between 1 and 2 inclusive of 2
Out[ ]:
array([1.        , 1.05263158, 1.10526316, 1.15789474, 1.21052632,
       1.26315789, 1.31578947, 1.36842105, 1.42105263, 1.47368421,
       1.52631579, 1.57894737, 1.63157895, 1.68421053, 1.73684211,
       1.78947368, 1.84210526, 1.89473684, 1.94736842, 2.        ])
In [ ]:
np.array([2.4,3,'and'])
Out[ ]:
array(['2.4', '3', 'and'], dtype='<U32')
In [ ]:
np.array(['and', '2.4', '5.654'])
Out[ ]:
array(['and', '2.4', '5.654'], dtype='<U5')
In [ ]:
np.array(['and', 2.3, 'dan'])
Out[ ]:
array(['and', '2.3', 'dan'], dtype='<U32')
In [ ]:
np.array(['and', 23, 'dan'])
Out[ ]:
array(['and', '23', 'dan'], dtype='<U21')
In [ ]:
np.array(['and', 2, 'dan'])
Out[ ]:
array(['and', '2', 'dan'], dtype='<U21')
In [ ]:
np.array(['and', 'dan', 'nad', 'adn'])
Out[ ]:
array(['and', 'dan', 'nad', 'adn'], dtype='<U3')
In [ ]:
print(arr3d)
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]

Indexing arrays

In [ ]:
arr3d = np.array([
                  [
                   [1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12]
                  ],
                  [
                  [13, 14, 15, 16],
                   [17, 18, 19, 20],
                   [21, 22, 23, 24]
                    ]
])
In [ ]:
print(arr3d.shape)
(2, 3, 4)
In [ ]:
arr3d[0,0,0]
Out[ ]:
1
In [ ]:
print(arr3d)
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

 [[13 14 15 16]
  [17 18 19 20]
  [21 22 23 24]]]
In [ ]:
print(arr3d[:,:,1])     # z, row, col
[[ 2  6 10]
 [14 18 22]]
In [ ]:
print(arr3d[:,1,:])
[[ 4  5  6]
 [10 11 12]]
In [ ]:
arr3d.shape
Out[ ]:
(2, 2, 3)
In [ ]:
print(arr3d)
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

 [[13 14 15 16]
  [17 18 19 20]
  [21 22 23 24]]]
In [ ]:
print(arr3d[:,:, 0:2])
[[[ 1  2]
  [ 5  6]
  [ 9 10]]

 [[13 14]
  [17 18]
  [21 22]]]

Operations on Numpy arrays

In [ ]:
import numpy as np
In [ ]:
arr1 = np.zeros((3,4))
arr2 = np.ones((3,4))
In [ ]:
arr1 + arr2
Out[ ]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
In [ ]:
arr1 = np.random.rand(3, 4)
arr2 = np.random.randn(3, 4)
In [ ]:
arr1
Out[ ]:
array([[0.90301469, 0.03032627, 0.33716493, 0.91555221],
       [0.59518678, 0.69582463, 0.760763  , 0.90040152],
       [0.59667863, 0.56478244, 0.17417988, 0.95759055]])
In [ ]:
arr2
Out[ ]:
array([[ 0.63064391, -0.28153414,  1.3572748 , -0.09055389],
       [-0.87526771, -0.90171283,  0.10549671, -1.20778381],
       [-0.26843387, -1.82902589, -1.29788103, -0.52565182]])
In [ ]:
arr1 + arr2
Out[ ]:
array([[ 1.5336586 , -0.25120787,  1.69443973,  0.82499832],
       [-0.28008093, -0.2058882 ,  0.86625971, -0.30738229],
       [ 0.32824475, -1.26424345, -1.12370116,  0.43193873]])
In [ ]:
arr1 - arr2
Out[ ]:
array([[ 0.27237078,  0.31186041, -1.02010987,  1.00610611],
       [ 1.47045449,  1.59753746,  0.65526629,  2.10818532],
       [ 0.8651125 ,  2.39380833,  1.47206091,  1.48324236]])
In [ ]:
arr1*arr2
Out[ ]:
array([[ 0.56948071, -0.00853788,  0.45762546, -0.08290682],
       [-0.52094777, -0.627434  ,  0.080258  , -1.08749037],
       [-0.16016876, -1.0330017 , -0.22606476, -0.50335921]])
In [ ]:
np.exp(arr1)
Out[ ]:
array([[2.46702924, 1.03079079, 1.40097011, 2.49815438],
       [1.81336962, 2.00536208, 2.13990834, 2.46059089],
       [1.81607691, 1.75906504, 1.19026965, 2.60541129]])
In [ ]:
np.log(arr1)
Out[ ]:
array([[-0.10201646, -3.49574101, -1.08718306, -0.08822789],
       [-0.51888   , -0.36265762, -0.2734334 , -0.10491448],
       [-0.51637662, -0.57131469, -1.74766672, -0.043335  ]])
In [ ]:
arr1
Out[ ]:
array([[0.90301469, 0.03032627, 0.33716493, 0.91555221],
       [0.59518678, 0.69582463, 0.760763  , 0.90040152],
       [0.59667863, 0.56478244, 0.17417988, 0.95759055]])
In [ ]:
np.log(np.exp(arr1))
Out[ ]:
array([[0.90301469, 0.03032627, 0.33716493, 0.91555221],
       [0.59518678, 0.69582463, 0.760763  , 0.90040152],
       [0.59667863, 0.56478244, 0.17417988, 0.95759055]])
In [ ]:
np.sqrt(arr1)
Out[ ]:
array([[0.95027085, 0.17414439, 0.58065905, 0.95684493],
       [0.7714835 , 0.83416103, 0.87221729, 0.94889489],
       [0.77244976, 0.75152009, 0.41734863, 0.97856556]])
In [ ]:
arr_inv = 1/arr1
In [ ]:
print(arr_inv)
[[ 1.1074017  32.97471352  2.96590752  1.092237  ]
 [ 1.68014484  1.43714372  1.31446982  1.11061563]
 [ 1.67594405  1.7705933   5.74119125  1.04428767]]
In [ ]:
15*arr1
Out[ ]:
array([[13.54522032,  0.45489402,  5.05747394, 13.73328317],
       [ 8.92780175, 10.43736948, 11.41144497, 13.50602278],
       [ 8.95017943,  8.47173659,  2.61269819, 14.36385819]])

following the example as given in numpy exercise

In [ ]:
ndim = 2
npoints = 1000000
In [ ]:
points = np.random.rand(npoints, ndim)
In [ ]:
dfo = np.zeros((npoints, 1))
outside_points = 0
In [ ]:
print(points[0:4])
[[0.77116362 0.48872236]
 [0.28309865 0.08381394]
 [0.41814343 0.93115249]
 [0.26433994 0.59130442]]
In [ ]:
%%time
dfo = np.zeros((npoints, 1))
outside_points = 0
for i in range(npoints):
    for j in range(ndim):
        dfo[i] += points[i, j] ** 2
    dfo[i] = np.sqrt(dfo[i])
    if dfo[i] > 1:
        outside_points += 1
CPU times: user 13.4 s, sys: 1.77 s, total: 15.2 s
Wall time: 13.2 s
In [ ]:
print("Fraction of points outside circle: ", outside_points / npoints)
Fraction of points outside circle:  0.21392
In [ ]:
1 - 3.14/4
Out[ ]:
0.21499999999999997
In [ ]:
point = 5
dim = 2
In [ ]:
apoint = np.random.rand(point, dim)
In [ ]:
apoint
Out[ ]:
array([[0.95741729, 0.53538333],
       [0.52708283, 0.56506885],
       [0.99470626, 0.96420744],
       [0.63138596, 0.80245294],
       [0.62784088, 0.94472876]])
In [ ]:
apoint * apoint
Out[ ]:
array([[0.91664788, 0.28663531],
       [0.27781631, 0.3193028 ],
       [0.98944054, 0.92969598],
       [0.39864823, 0.64393072],
       [0.39418417, 0.89251242]])
In [ ]:
print(np.random.randint(1, 10, 5))
[2 7 3 3 9]
In [ ]:
abc = np.arange(20).reshape(4,5)
In [ ]:
print(abc)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
In [ ]:
print( abc * abc)
[[  0   1   4   9  16]
 [ 25  36  49  64  81]
 [100 121 144 169 196]
 [225 256 289 324 361]]
In [ ]:
dfo = np.sum(abc)
In [ ]:
print(dfo)
190
In [ ]:
dfo = np.sum(abc, axis=1)
In [ ]:
print(dfo)
[10 35 60 85]
In [ ]:
print(np.sum(abc, axis= 0))
[30 34 38 42 46]
In [ ]:
print(sum(abc * abc))
[350 414 486 566 654]
In [ ]:
print(abc * abc)
[[  0   1   4   9  16]
 [ 25  36  49  64  81]
 [100 121 144 169 196]
 [225 256 289 324 361]]
In [ ]:
print(sum(abc))
[30 34 38 42 46]
In [ ]:
print(abc)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
In [ ]:
print(sum(abc, 1))
[31 35 39 43 47]
In [ ]:
print(np.sum(abc, 1))
[10 35 60 85]
In [ ]:
print(abc)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
In [ ]:
print(np.sum(abc, axis=1))
[10 35 60 85]
In [ ]:
print(np.sqrt(np.sum(abc, 1)))
[3.16227766 5.91607978 7.74596669 9.21954446]
In [ ]:
print(np.sqrt(10))
3.1622776601683795
In [ ]:
%%time
sp_points = points * points
CPU times: user 3.65 ms, sys: 6.34 ms, total: 9.99 ms
Wall time: 14.5 ms
In [ ]:
print(sp_points)
[[0.59469333 0.23884955]
 [0.08014484 0.00702478]
 [0.17484393 0.86704495]
 ...
 [0.24926367 0.03748581]
 [0.76865883 0.41678856]
 [0.21165453 0.43527623]]
In [ ]:
print(points)
[[0.41159862 0.24216339]
 [0.62946256 0.22748868]
 [0.72907476 0.31878666]
 ...
 [0.22009878 0.5619777 ]
 [0.99029206 0.05962467]
 [0.84914425 0.52705389]]
In [ ]:
sp_points = points * points
In [ ]:
print(sp_points)
[[0.16941342 0.05864311]
 [0.39622312 0.0517511 ]
 [0.53155001 0.10162494]
 ...
 [0.04844347 0.31581894]
 [0.98067837 0.0035551 ]
 [0.72104596 0.2777858 ]]
In [ ]:
points = np.random.rand(npoints, ndim)
In [ ]:
print(points)
[[0.5811624  0.11910859]
 [0.14466438 0.38295867]
 [0.52744828 0.05786606]
 ...
 [0.97070777 0.92684638]
 [0.80136454 0.0188756 ]
 [0.49745226 0.18554566]]
In [ ]:
sp_points = points * points
In [ ]:
print(sp_points)
[[3.37749738e-01 1.41868570e-02]
 [2.09277821e-02 1.46657341e-01]
 [2.78201691e-01 3.34848086e-03]
 ...
 [9.42273576e-01 8.59044214e-01]
 [6.42185127e-01 3.56288178e-04]
 [2.47458750e-01 3.44271902e-02]]
In [ ]:
sp_points = 0
In [ ]:
sp_points = points * points
dfo = np.sqrt(np.sum(sp_points, axis=1))
outside_points = np.sum(dfo > 1)
In [ ]:
print(outside_points/npoints)
0.21418

Broadcasting

In [ ]:
import numpy as np
In [ ]:
arr1 = np.arange(6)
In [ ]:
arr1
Out[ ]:
array([0, 1, 2, 3, 4, 5])
In [ ]:
arr1 = arr1.reshape((3, 2))
In [ ]:
print(arr1)
[[0 1]
 [2 3]
 [4 5]]
In [ ]:
arr2 = np.arange(6).reshape( 3, 2)
In [ ]:
print(arr2)
[[0 1]
 [2 3]
 [4 5]]
In [ ]:
np.reshape?
In [ ]:
arr1 + arr2
Out[ ]:
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])
In [ ]:
arr2[0].reshape(1,2)
Out[ ]:
array([[0, 1]])
In [ ]:
arr2[1].reshape(1,2)
Out[ ]:
array([[2, 3]])
In [ ]:
arr1
Out[ ]:
array([[0, 1],
       [2, 3],
       [4, 5]])
In [ ]:
arr1 + arr2[1]
Out[ ]:
array([[2, 4],
       [4, 6],
       [6, 8]])
In [ ]: