Skip to content

Instantly share code, notes, and snippets.

@wang-zhijun
Last active November 15, 2016 13:12
Show Gist options
  • Save wang-zhijun/c55f85ede4354989211c54a1e9557a7a to your computer and use it in GitHub Desktop.
Save wang-zhijun/c55f85ede4354989211c54a1e9557a7a to your computer and use it in GitHub Desktop.
NumPy

Install NumPy

brew install python3

# Get access to the scientific Python formulas
brew tap Homebrew/python

# Install Numpy and Matplotlib
brew install numpy --with-python3

pip3.4 install --upgrade pip
pip3 install matplotlib

If the optional parameter retstep is set, the function will also return the value of the spacing between adjacent values. So, the function will return a tuple ('samples', 'step'):

3つの数字は 1.18367347+ 18367347 = 1.36734694

>>> samples, spacing = np.linspace(1, 10, endpoint=True, retstep=True)
>>> print(spacing)
0.1836734693877551
>>> print(samples)
[  1.           1.18367347   1.36734694   1.55102041   1.73469388
   1.91836735   2.10204082   2.28571429   2.46938776   2.65306122
   2.83673469   3.02040816   3.20408163   3.3877551    3.57142857
   3.75510204   3.93877551   4.12244898   4.30612245   4.48979592
   4.67346939   4.85714286   5.04081633   5.2244898    5.40816327
   5.59183673   5.7755102    5.95918367   6.14285714   6.32653061
   6.51020408   6.69387755   6.87755102   7.06122449   7.24489796
   7.42857143   7.6122449    7.79591837   7.97959184   8.16326531
   8.34693878   8.53061224   8.71428571   8.89795918   9.08163265
   9.26530612   9.44897959   9.63265306   9.81632653  10.        ]

The syntax of arange:

arange([start,] stop[, step,], dtype=None)

>>> x = np.arange(10.4)
>>> print(x)
[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.]
>>> x = np.arange(0.5, 10.4, 0.8)
>>> print(x)
[  0.5   1.3   2.1   2.9   3.7   4.5   5.3   6.1   6.9   7.7   8.5   9.3
  10.1]

>>> import numpy as np
>>> cvalues = [25.3, 24.8, 26.9, 23.9]
>>> C = np.array(cvalues)
>>> print(C)
[ 25.3  24.8  26.9  23.9]
>>> print(C*9/5 + 32)
[ 77.54  76.64  80.42  75.02]

>>> print(np.linspace(1, 10))
[  1.           1.18367347   1.36734694   1.55102041   1.73469388
   1.91836735   2.10204082   2.28571429   2.46938776   2.65306122
   2.83673469   3.02040816   3.20408163   3.3877551    3.57142857
   3.75510204   3.93877551   4.12244898   4.30612245   4.48979592
   4.67346939   4.85714286   5.04081633   5.2244898    5.40816327
   5.59183673   5.7755102    5.95918367   6.14285714   6.32653061
   6.51020408   6.69387755   6.87755102   7.06122449   7.24489796
   7.42857143   7.6122449    7.79591837   7.97959184   8.16326531
   8.34693878   8.53061224   8.71428571   8.89795918   9.08163265
   9.26530612   9.44897959   9.63265306   9.81632653  10.        ]
>>> print(np.linspace(1, 10, 7))
[  1.    2.5   4.    5.5   7.    8.5  10. ]
>>> print(np.linspace(1, 10, 7, endpoint=False))
[ 1.          2.28571429  3.57142857  4.85714286  6.14285714  7.42857143
  8.71428571]

>>> import numpy as np
>>> a = np.array([2,3,4])
>>> a
array([2, 3, 4])
>>> a.dtype
dtype('int64')

>>> b = np.array([1.2, 3.5])
>>> b
array([ 1.2,  3.5])
>>> b.dtype
dtype('float64')

>>> b = np.array([(1.5,2,3), (4,5,6)])
>>> b
array([[ 1.5,  2. ,  3. ],
       [ 4. ,  5. ,  6. ]])
>>> b.dtype
dtype('float64')
>>> b[0][1]
2.0

>>> c = np.array([[1,2],[3,4]], dtype=complex)
>>> c
array([[ 1.+0.j,  2.+0.j],
       [ 3.+0.j,  4.+0.j]])
       
>>> np.zeros((3,4))
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

>>> np.ones((2,3,4), dtype=np.int16)
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]]], dtype=int16)
        
>>> np.arange(10, 30, 5)
array([10, 15, 20, 25])
>>>
>>> np.arange(0, 2, 0.3)
array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])

>>> from numpy import pi
>>> np.linspace( 0, 2, 9 )                 # 9 numbers from 0 to 2
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])
>>> x = np.linspace( 0, 2*pi, 100 )        # useful to evaluate function at lots of points
>>> f = np.sin(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment