Array creation routines

Ones and zeros


In [1]:
import numpy as np

Create a new array of 2*2 integers, without initializing entries.


In [2]:
np.array([[1, 2],[3, 4]])


Out[2]:
array([[1, 2],
       [3, 4]])

Let X = np.array([1,2,3], [4,5,6], np.int32). Create a new array with the same shape and type as X.


In [10]:
X = np.array([[1,2,3], [4,5,6]], np.int32)
X


Out[10]:
array([[1, 2, 3],
       [4, 5, 6]])

Create a 3-D array with ones on the diagonal and zeros elsewhere.


In [4]:
np.identity(3)


Out[4]:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

In [5]:
np.eye(3)


Out[5]:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

Create a new array of 3*2 float numbers, filled with ones.


In [7]:
np.ones((3,2))


Out[7]:
array([[1., 1.],
       [1., 1.],
       [1., 1.]])

Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X.


In [12]:
x = np.reshape(np.arange(4, dtype=np.int64), (2, 2))
x


Out[12]:
array([[0, 1],
       [2, 3]], dtype=int64)

Create a new array of 3*2 float numbers, filled with zeros.


In [9]:
np.ones((3, 2))


Out[9]:
array([[1., 1.],
       [1., 1.],
       [1., 1.]])

Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X.


In [14]:
x = np.reshape(np.arange(4, dtype=np.int64), (2,2))
x


Out[14]:
array([[0, 1],
       [2, 3]], dtype=int64)

Create a new array of 2*5 uints, filled with 6.


In [15]:
x = np.full((2, 4), 6)
print(x)


[[6 6 6 6]
 [6 6 6 6]]

Let x = np.arange(4, dtype=np.int64). Create an array of 6's with the same shape and type as X.


In [16]:
x = np.arange(4, dtype=np.int64)
print(x)
x.fill(6)
print(x)


[0 1 2 3]
[6 6 6 6]

From existing data

Create an array of [1, 2, 3].


In [17]:
np.array([1, 2, 3])


Out[17]:
array([1, 2, 3])

Let x = [1, 2]. Convert it into an array.


In [18]:
x = [1,2]
np.asarray(x)


Out[18]:
array([1, 2])

Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix.


In [20]:
X = np.array([[1, 2], [3, 4]])
np.asmatrix(X)


Out[20]:
matrix([[1, 2],
        [3, 4]])

Let x = [1, 2]. Conver it into an array of float.


In [21]:
x = [1, 2]
np.asarray(x, dtype=np.float)


Out[21]:
array([1., 2.])

Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30.


In [22]:
x = np.array([30])
np.asscalar(x)


Out[22]:
30

Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x.


In [24]:
x = np.array([1, 2, 3])
x2 = x.copy()
print(id(x), id(x2))


1743135762672 1743135763552

Numerical ranges

Create an array of 2, 4, 6, 8, ..., 100.


In [32]:
np.array(range(2, 101, 2))
np.arange(2, 101, 2)


Out[32]:
array([  2,   4,   6,   8,  10,  12,  14,  16,  18,  20,  22,  24,  26,
        28,  30,  32,  34,  36,  38,  40,  42,  44,  46,  48,  50,  52,
        54,  56,  58,  60,  62,  64,  66,  68,  70,  72,  74,  76,  78,
        80,  82,  84,  86,  88,  90,  92,  94,  96,  98, 100])

Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive.


In [35]:
np.linspace(3, 10, 50)


Out[35]:
array([ 3.        ,  3.14285714,  3.28571429,  3.42857143,  3.57142857,
        3.71428571,  3.85714286,  4.        ,  4.14285714,  4.28571429,
        4.42857143,  4.57142857,  4.71428571,  4.85714286,  5.        ,
        5.14285714,  5.28571429,  5.42857143,  5.57142857,  5.71428571,
        5.85714286,  6.        ,  6.14285714,  6.28571429,  6.42857143,
        6.57142857,  6.71428571,  6.85714286,  7.        ,  7.14285714,
        7.28571429,  7.42857143,  7.57142857,  7.71428571,  7.85714286,
        8.        ,  8.14285714,  8.28571429,  8.42857143,  8.57142857,
        8.71428571,  8.85714286,  9.        ,  9.14285714,  9.28571429,
        9.42857143,  9.57142857,  9.71428571,  9.85714286, 10.        ])

Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive.


In [36]:
np.logspace(3, 10, 50)


Out[36]:
array([1.00000000e+03, 1.38949549e+03, 1.93069773e+03, 2.68269580e+03,
       3.72759372e+03, 5.17947468e+03, 7.19685673e+03, 1.00000000e+04,
       1.38949549e+04, 1.93069773e+04, 2.68269580e+04, 3.72759372e+04,
       5.17947468e+04, 7.19685673e+04, 1.00000000e+05, 1.38949549e+05,
       1.93069773e+05, 2.68269580e+05, 3.72759372e+05, 5.17947468e+05,
       7.19685673e+05, 1.00000000e+06, 1.38949549e+06, 1.93069773e+06,
       2.68269580e+06, 3.72759372e+06, 5.17947468e+06, 7.19685673e+06,
       1.00000000e+07, 1.38949549e+07, 1.93069773e+07, 2.68269580e+07,
       3.72759372e+07, 5.17947468e+07, 7.19685673e+07, 1.00000000e+08,
       1.38949549e+08, 1.93069773e+08, 2.68269580e+08, 3.72759372e+08,
       5.17947468e+08, 7.19685673e+08, 1.00000000e+09, 1.38949549e+09,
       1.93069773e+09, 2.68269580e+09, 3.72759372e+09, 5.17947468e+09,
       7.19685673e+09, 1.00000000e+10])

Building matrices

Let X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]). Get the diagonal of X, that is, [0, 5, 10].


In [39]:
X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
# np.reshape(X, (3, 4))
np.diag(X)


Out[39]:
array([ 0,  5, 10])

Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere.


In [40]:
np.diagflat([1, 2, 3, 4])


Out[40]:
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])

Create an array which looks like below. array([[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.]])


In [42]:
np.tri(3, 5, -1)


Out[42]:
array([[0., 0., 0., 0., 0.],
       [1., 0., 0., 0., 0.],
       [1., 1., 0., 0., 0.]])

Create an array which looks like below. array([[ 0, 0, 0], [ 4, 0, 0], [ 7, 8, 0], [10, 11, 12]])


In [101]:



Out[101]:
array([[ 0,  0,  0],
       [ 4,  0,  0],
       [ 7,  8,  0],
       [10, 11, 12]])

Create an array which looks like below. array([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [ 0, 0, 12]])


In [102]:



Out[102]:
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 0,  8,  9],
       [ 0,  0, 12]])