logo

NumPy - Overview

Last Updated: 2024-02-13

SciPy Stack

http://www.scipy.org/

  • numpy:
    • core data structure: ndarray(n-dimensional array)
  • pandas:
    • built on top of numpy
    • Series(1-dimensional)
    • DataFrame(2-dimensional)
  • matplotlib:
    • 2D Plotting
  • scikit-learn
  • jupyter

Zip With Index

Pure Python

>>> [[i + 1,value] for (i, value) in enumerate(v)]
[[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]

Use numpy:

>>> np.c_[range(1,len(v)+1), v]
array([['1', 'a'],
       ['2', 'b'],
       ['3', 'c'],
       ['4', 'd']],
      dtype='<U21')

>>> type(np.c_[range(1,len(v)+1), v])
<class 'numpy.ndarray'>

Calculate Gradients

import numpy as np

x = np.array([1, 2, 4, 7, 11, 16], dtype=np.float)

print(np.gradient(x))

[ 1.   1.5  2.5  3.5  4.5  5. ]
  • 1 = 2 - 1
  • 1.5 = ((4 - 2) + (7 - 4)) / 2

Random

from standard normal distribution.

>>> np.random.randn(10)
array([ 0.79879596, -2.13756431, -0.56107906,  0.26990325, -1.1990176 ,
       -1.10211192, -0.72586982, -0.73188623,  1.08109614,  0.82507571])

from a uniform distribution over [0, 1).

>>> np.random.rand(10)
array([ 0.79237886,  0.4509385 ,  0.83720332,  0.87449912,  0.98571927,
        0.84808056,  0.88746248,  0.57405272,  0.15187069,  0.28217071])
>>> np.random.rand(10).astype(np.float32)
array([ 0.80928844,  0.96036869,  0.9398548 ,  0.24155894,  0.32953534,
        0.06660849,  0.61671036,  0.62121075,  0.70144385,  0.24093041], dtype=float32)