Numpy 学习笔记


Numpy学习

此notebook只是跟着官方文档实现了一遍,在需要注意的部分代码后添加了简短的注释。

import numpy as np

Create a basic array

a = np.array([2, 0, 2, 3])
b = np.zeros(2)
c = np.ones(2)
d = np.empty(2)     # random values
e = np.arange(5)
f = np.linspace(0, 10, num=5)
a, b, c, d, e, f
(array([2, 0, 2, 3]),
 array([0., 0.]),
 array([1., 1.]),
 array([5.40721420e-285, 1.95812747e-306]),
 array([0, 1, 2, 3, 4]),
 array([ 0. ,  2.5,  5. ,  7.5, 10. ]))

Adding, removing and Sorting elements

arr = np.array([1, 9, 5, 6, 6])
np.sort(arr)
array([1, 5, 6, 6, 9])
arr = np.array([1, 9, 5, 6, 6])
np.argsort(arr)
array([0, 2, 3, 4, 1], dtype=int32)
arr = np.array([[0, 3],
                [2, 2]])
np.argsort(arr, axis=0) # sorts along first axis(down)
array([[0, 1],
       [1, 0]], dtype=int32)
arr = np.array([[0, 3],
                [2, 2]])
np.argsort(arr, axis=1) # sorts last first axis(across)
array([[0, 1],
       [0, 1]], dtype=int32)
a = np.array([1, 2, 3, 4, 5])
b = np.array([6, 7, 8, 9, 10])
np.concatenate((a, b))
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x, y), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x, y.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x, y.T), axis=None)
array([1, 2, 3, 4, 5, 6])

Get the shape and size of an array

array_example = np.array([[[0, 1, 2, 3],
                           [4, 5, 6, 7]],

                          [[0, 1, 2, 3],
                           [4, 5, 6, 7]],

                          [[0 ,1 ,2, 3],
                           [4, 5, 6, 7]]])
array_example.ndim
3
array_example.size
24
array_example.shape
(3, 2, 4)

Reshape the array

a = np.arange(6)
b = a.reshape((3, 2), order='C')    # C-style order 默认按行建立索引,同一行内存连续
b
array([[0, 1],
       [2, 3],
       [4, 5]])
a = np.arange(6)
b = a.reshape((3, 2), order='F') # Fortan order 默认按列建立索引,同一列内存连续
b
array([[0, 3],
       [1, 4],
       [2, 5]])

Add new axis to an array

a = np.arange(6)
a2 = a[np.newaxis, :]
a3 = a[:, np.newaxis]
a.shape, a2.shape, a3.shape
((6,), (1, 6), (6, 1))
b = np.arange(6)
b2 = np.expand_dims(b, axis=0)
b3 = np.expand_dims(b, axis=1)
b.shape, b2.shape, b3.shape
((6,), (1, 6), (6, 1))

Index and slicing

data = np.arange(3)
print(data)
data[0:2], data[1:], data[-2:]
[0 1 2]





(array([0, 1]), array([1, 2]), array([1, 2]))
a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a[a < 5]
array([1, 2, 3, 4])
a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a[a >= 6]
array([ 6,  7,  8,  9, 10, 11, 12])
a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a[a % 2 == 0]
array([ 2,  4,  6,  8, 10, 12])
a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a[(a > 2) & (a < 11)]
array([ 3,  4,  5,  6,  7,  8,  9, 10])
(a > 5) | (a % 2 ==0)
array([[False,  True, False,  True],
       [False,  True,  True,  True],
       [ True,  True,  True,  True]])
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
b = np.nonzero(a < 5)   # nonzero返回一个元组,表示每一维中各非零元素的索引
b, a[b]                 # 非零元素有4个,分别为(0,0), (0,1), (0,2), (0,3)。于是每一维返回一个元组,分别为([0, 0, 0, 0]), ([0, 1, 2, 3])
((array([0, 0, 0, 0], dtype=int32), array([0, 1, 2, 3], dtype=int32)),
 array([1, 2, 3, 4]))
list_of_coordinates= list(zip(b[0], b[1]))
for coord in list_of_coordinates:
    print(coord)
(0, 0)
(0, 1)
(0, 2)
(0, 3)

Create an array from existing data

a = np.array([1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
arr1 = a[3:8]
arr1
array([4, 5, 6, 7, 8])
a1 = np.array([[1, 1],
               [2, 2]])

a2 = np.array([[3, 3],
               [4, 4]])

np.vstack((a1, a2)), np.hstack((a1, a2))
(array([[1, 1],
        [2, 2],
        [3, 3],
        [4, 4]]),
 array([[1, 1, 3, 3],
        [2, 2, 4, 4]]))
x = np.arange(1, 25).reshape(2, 12)
print(x)
np.hsplit(x, 3)    # 平均分成3份
[[ 1  2  3  4  5  6  7  8  9 10 11 12]
 [13 14 15 16 17 18 19 20 21 22 23 24]]





[array([[ 1,  2,  3,  4],
        [13, 14, 15, 16]]),
 array([[ 5,  6,  7,  8],
        [17, 18, 19, 20]]),
 array([[ 9, 10, 11, 12],
        [21, 22, 23, 24]])]
x = np.arange(1, 25).reshape(2, 12)
np.hsplit(x, (2, 4, 8))   # 在第2, 4, 8列之后切开
[array([[ 1,  2],
        [13, 14]]),
 array([[ 3,  4],
        [15, 16]]),
 array([[ 5,  6,  7,  8],
        [17, 18, 19, 20]]),
 array([[ 9, 10, 11, 12],
        [21, 22, 23, 24]])]
# 注意:通过slice得到的数组会随着原数组变化而变化,只有copy()方法才会生成一个新数组
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
b1 = a[0, :]
print(b1)
b1[0] = 99
print(b1)
a
[1 2 3 4]
[99  2  3  4]





array([[99,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
# 注意:通过slice得到的数组会随着原数组变化而变化,只有copy()方法才会生成一个新数组
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
b2 = a[0, :].copy()
print(b2)
b2[0] = 99
print(b2)
a
[1 2 3 4]
[99  2  3  4]





array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

Basic array operations

data = np.array([1, 2])
ones = np.ones(2, dtype=int)
data + ones, data - ones, data * data, data / data
(array([2, 3]), array([0, 1]), array([1, 4]), array([1., 1.]))
a = np.array([1, 2, 3, 4])
a.sum()
10
b = np.array([[1, 2], [3, 4]])
b.sum(axis=0), b.sum(axis=1)
(array([4, 6]), array([3, 7]))
a = np.arange(4)
a**2
array([0, 1, 4, 9], dtype=int32)
np.sin(a)
array([0.        , 0.84147098, 0.90929743, 0.14112001])
A = np.array([[1, 1],
              [0, 1]])
B = np.array([[2, 0],
              [3, 4]])
print(A * B) # 逐元素相乘
print(A @ B) # 矩阵乘法
print(A.dot(B)) # 矩阵乘法
[[2 0]
 [0 4]]
[[5 4]
 [3 4]]
[[5 4]
 [3 4]]
rg = np.random.default_rng(1)   # 创造随机数生成器
a = np.ones((2, 3), dtype=int)
b = rg.random((2, 3))
print(b)
b += a
b
[[0.51182162 0.9504637  0.14415961]
 [0.94864945 0.31183145 0.42332645]]





array([[1.51182162, 1.9504637 , 1.14415961],
       [1.94864945, 1.31183145, 1.42332645]])
a += b
---------------------------------------------------------------------------

UFuncTypeError                            Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_10724\858184992.py in <module>
----> 1 a += b


UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
a = np.ones(3, dtype=np.int32)
b = np.linspace(0, 3.14, 3)
b.dtype.name
'float64'
c = a + b
c, c.dtype.name
(array([1.  , 2.57, 4.14]), 'float64')
d = np.exp(c * 1j)
d, d.dtype.name
(array([ 0.54030231+0.84147098j, -0.84104046+0.54097222j,
        -0.54164179-0.8406094j ]),
 'complex128')
b = np.arange(12).reshape(3, 4)
print(b)
b.min(), b.min(axis=0), b.min(axis=1)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]





(0, array([0, 1, 2, 3]), array([0, 4, 8]))
b = np.arange(12).reshape(3, 4)
print(b)
b.cumsum(axis=0), b.cumsum(axis=1)  # 求前缀和
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]





(array([[ 0,  1,  2,  3],
        [ 4,  6,  8, 10],
        [12, 15, 18, 21]], dtype=int32),
 array([[ 0,  1,  3,  6],
        [ 4,  9, 15, 22],
        [ 8, 17, 27, 38]], dtype=int32))
b = np.arange(3)
print(np.exp(b))
print(np.sqrt(b))
print(np.add(b, np.exp(b)))
[1.         2.71828183 7.3890561 ]
[0.         1.         1.41421356]
[1.         3.71828183 9.3890561 ]

Broadcasting

broadcasting 是指numpy如何计算维度不一致的数组计算,例如小数组与大数组的计算时会将小数组扩充(或者称为“广播”)到大数组的维度,使它们具有兼容、能够计算的尺寸。注意:在numpy实现中,这种扩充并不用重新生成一个新的数组,且底层用C实现,效率是有保障的。

data = np.array([1.0, 2.0])
data * 1.6
array([1.6, 3.2])

General Broadcasting Rules:

1. they are equal, or
2. one of them is 1.

例如:

Image  (3d array): 256 x 256 x 3
Scale  (1d array):             3
Result (3d array): 256 x 256 x 3
A      (4d array):  8 x 1 x 6 x 1
B      (3d array):      7 x 1 x 5
Result (4d array):  8 x 7 x 6 x 5

Get unique items and counts

a = np.array([100, 11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18, 19, 20])
np.unique(a)
array([ 11,  12,  13,  14,  15,  16,  17,  18,  19,  20, 100])
unique_value, indices = np.unique(a, return_index=True)
unique_value, indices
(array([ 11,  12,  13,  14,  15,  16,  17,  18,  19,  20, 100]),
 array([ 1,  3,  4,  5,  6,  7,  8, 13, 14, 15,  0], dtype=int32))
unique_value, indices = np.unique(a, return_counts=True)
unique_value, indices
(array([ 11,  12,  13,  14,  15,  16,  17,  18,  19,  20, 100]),
 array([3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]))
a_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [9, 10, 11, 12]])
unique_value, indices = np.unique(a_2d, return_index=True)
unique_value, indices
(array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),
 array([ 0,  1,  2,  3,  4,  5,  6,  7, 12, 13, 14, 15], dtype=int32))
a_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [9, 10, 11, 12]])
unique_row, indices, counts = np.unique(a_2d, axis=0, return_index=True, return_counts=True)
unique_row, indices, counts
(array([[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]]),
 array([0, 1, 3], dtype=int32),
 array([2, 1, 1]))

Transposing and reshaping the matrix

data = np.arange(1, 7)
data.reshape(2, 3), data.reshape(3, 2)
(array([[1, 2, 3],
        [4, 5, 6]]),
 array([[1, 2],
        [3, 4],
        [5, 6]]))
data = data.reshape(2, 3)
data.transpose(), data.T
(array([[1, 4],
        [2, 5],
        [3, 6]]),
 array([[1, 4],
        [2, 5],
        [3, 6]]))

Reverse an array

arr = np.arange(7)
reversed_arr = np.flip(arr)
reversed_arr
array([6, 5, 4, 3, 2, 1, 0])
arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
reversed_arr_2d = np.flip(arr_2d)
reversed_arr_2d
array([[12, 11, 10,  9],
       [ 8,  7,  6,  5],
       [ 4,  3,  2,  1]])
arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
reversed_arr_2d = np.flip(arr_2d, axis=0)
reversed_arr_2d
array([[ 9, 10, 11, 12],
       [ 5,  6,  7,  8],
       [ 1,  2,  3,  4]])
arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
reversed_arr_2d = np.flip(arr_2d, axis=1)
reversed_arr_2d
array([[ 4,  3,  2,  1],
       [ 8,  7,  6,  5],
       [12, 11, 10,  9]])

Reshaping and flattening muldimensional arrays

# flatten()会复制, ravel()不会复制
x = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
flatten_x = x.flatten()
ravel_x = x.ravel()
print(flatten_x, ravel_x)
x[0] = 99
print(flatten_x, ravel_x)
[ 1  2  3  4  5  6  7  8  9 10 11 12] [ 1  2  3  4  5  6  7  8  9 10 11 12]
[ 1  2  3  4  5  6  7  8  9 10 11 12] [99 99 99 99  5  6  7  8  9 10 11 12]

Access the docstring for more information

a = np.array([1, 2, 3])
help(a.view)
Help on built-in function view:

view(...) method of numpy.ndarray instance
    a.view([dtype][, type])
    
    New view of array with the same data.
    
    .. note::
        Passing None for ``dtype`` is different from omitting the parameter,
        since the former invokes ``dtype(None)`` which is an alias for
        ``dtype('float_')``.
    
    Parameters
    ----------
    dtype : data-type or ndarray sub-class, optional
        Data-type descriptor of the returned view, e.g., float32 or int16.
        Omitting it results in the view having the same data-type as `a`.
        This argument can also be specified as an ndarray sub-class, which
        then specifies the type of the returned object (this is equivalent to
        setting the ``type`` parameter).
    type : Python type, optional
        Type of the returned view, e.g., ndarray or matrix.  Again, omission
        of the parameter results in type preservation.
    
    Notes
    -----
    ``a.view()`` is used two different ways:
    
    ``a.view(some_dtype)`` or ``a.view(dtype=some_dtype)`` constructs a view
    of the array's memory with a different data-type.  This can cause a
    reinterpretation of the bytes of memory.
    
    ``a.view(ndarray_subclass)`` or ``a.view(type=ndarray_subclass)`` just
    returns an instance of `ndarray_subclass` that looks at the same array
    (same shape, dtype, etc.)  This does not cause a reinterpretation of the
    memory.
    
    For ``a.view(some_dtype)``, if ``some_dtype`` has a different number of
    bytes per entry than the previous dtype (for example, converting a
    regular array to a structured array), then the behavior of the view
    cannot be predicted just from the superficial appearance of ``a`` (shown
    by ``print(a)``). It also depends on exactly how ``a`` is stored in
    memory. Therefore if ``a`` is C-ordered versus fortran-ordered, versus
    defined as a slice or transpose, etc., the view may give different
    results.

    Examples
    --------
    >>> x = np.array([(1, 2)], dtype=[('a', np.int8), ('b', np.int8)])
    
    Viewing array data using a different type and dtype:
    
    >>> y = x.view(dtype=np.int16, type=np.matrix)
    >>> y
    matrix([[513]], dtype=int16)
    >>> print(type(y))
    <class 'numpy.matrix'>
    
    Creating a view on a structured array so it can be used in calculations
    
    >>> x = np.array([(1, 2),(3,4)], dtype=[('a', np.int8), ('b', np.int8)])
    >>> xv = x.view(dtype=np.int8).reshape(-1,2)
    >>> xv
    array([[1, 2],
           [3, 4]], dtype=int8)
    >>> xv.mean(0)
    array([2.,  3.])
    
    Making changes to the view changes the underlying array
    
    >>> xv[0,1] = 20
    >>> x
    array([(1, 20), (3,  4)], dtype=[('a', 'i1'), ('b', 'i1')])
    
    Using a view to convert an array to a recarray:
    
    >>> z = x.view(np.recarray)
    >>> z.a
    array([1, 3], dtype=int8)
    
    Views share data:
    
    >>> x[0] = (9, 10)
    >>> z[0]
    (9, 10)
    
    Views that change the dtype size (bytes per entry) should normally be
    avoided on arrays defined by slices, transposes, fortran-ordering, etc.:
    
    >>> x = np.array([[1,2,3],[4,5,6]], dtype=np.int16)
    >>> y = x[:, 0:2]
    >>> y
    array([[1, 2],
           [4, 5]], dtype=int16)
    >>> y.view(dtype=[('width', np.int16), ('length', np.int16)])
    Traceback (most recent call last):
        ...
    ValueError: To change to a dtype of a different size, the array must be C-contiguous
    >>> z = y.copy()
    >>> z.view(dtype=[('width', np.int16), ('length', np.int16)])
    array([[(1, 2)],
           [(4, 5)]], dtype=[('width', '<i2'), ('length', '<i2')])

a?
Type:        ndarray
String form: [1 2 3]
Length:      3
File:        c:\users\luo\appdata\local\programs\python\python37-32\lib\site-packages\numpy\__init__.py
Docstring:  
ndarray(shape, dtype=float, buffer=None, offset=0,
        strides=None, order=None)

An array object represents a multidimensional, homogeneous array
of fixed-size items.  An associated data-type object describes the
format of each element in the array (its byte-order, how many bytes it
occupies in memory, whether it is an integer, a floating point number,
or something else, etc.)

Arrays should be constructed using `array`, `zeros` or `empty` (refer
to the See Also section below).  The parameters given here refer to
a low-level method (`ndarray(...)`) for instantiating an array.

For more information, refer to the `numpy` module and examine the
methods and attributes of an array.

Parameters
----------
(for the __new__ method; see Notes below)

shape : tuple of ints
    Shape of created array.
dtype : data-type, optional
    Any object that can be interpreted as a numpy data type.
buffer : object exposing buffer interface, optional
    Used to fill the array with data.
offset : int, optional
    Offset of array data in buffer.
strides : tuple of ints, optional
    Strides of data in memory.
order : {'C', 'F'}, optional
    Row-major (C-style) or column-major (Fortran-style) order.

Attributes
----------
T : ndarray
    Transpose of the array.
data : buffer
    The array's elements, in memory.
dtype : dtype object
    Describes the format of the elements in the array.
flags : dict
    Dictionary containing information related to memory use, e.g.,
    'C_CONTIGUOUS', 'OWNDATA', 'WRITEABLE', etc.
flat : numpy.flatiter object
    Flattened version of the array as an iterator.  The iterator
    allows assignments, e.g., ``x.flat = 3`` (See `ndarray.flat` for
    assignment examples; TODO).
imag : ndarray
    Imaginary part of the array.
real : ndarray
    Real part of the array.
size : int
    Number of elements in the array.
itemsize : int
    The memory use of each array element in bytes.
nbytes : int
    The total number of bytes required to store the array data,
    i.e., ``itemsize * size``.
ndim : int
    The array's number of dimensions.
shape : tuple of ints
    Shape of the array.
strides : tuple of ints
    The step-size required to move from one element to the next in
    memory. For example, a contiguous ``(3, 4)`` array of type
    ``int16`` in C-order has strides ``(8, 2)``.  This implies that
    to move from element to element in memory requires jumps of 2 bytes.
    To move from row-to-row, one needs to jump 8 bytes at a time
    (``2 * 4``).
ctypes : ctypes object
    Class containing properties of the array needed for interaction
    with ctypes.
base : ndarray
    If the array is a view into another array, that array is its `base`
    (unless that array is also a view).  The `base` array is where the
    array data is actually stored.

See Also
--------
array : Construct an array.
zeros : Create an array, each element of which is zero.
empty : Create an array, but leave its allocated memory unchanged (i.e.,
        it contains "garbage").
dtype : Create a data-type.
numpy.typing.NDArray : A :term:`generic <generic type>` version
                       of ndarray.

Notes
-----
There are two modes of creating an array using ``__new__``:

1. If `buffer` is None, then only `shape`, `dtype`, and `order`
   are used.
2. If `buffer` is an object exposing the buffer interface, then
   all keywords are interpreted.

No ``__init__`` method is needed because the array is fully initialized
after the ``__new__`` method.

Examples
--------
These examples illustrate the low-level `ndarray` constructor.  Refer
to the `See Also` section above for easier ways of constructing an
ndarray.

First mode, `buffer` is None:

>>> np.ndarray(shape=(2,2), dtype=float, order='F')
array([[0.0e+000, 0.0e+000], # random
       [     nan, 2.5e-323]])

Second mode:

>>> np.ndarray((2,), buffer=np.array([1,2,3]),
...            offset=np.int_().itemsize,
...            dtype=int) # offset = 1*itemsize, i.e. skip first element
array([2, 3])

Save and load NumPy objects

a = np.array([1, 2, 3, 4, 5, 6])
np.save('filename', a)
np.savetxt('filename.csv', a)
b = np.load('filename.npy')
c = np.loadtxt('filename.csv')
b, c
(array([1, 2, 3, 4, 5, 6]), array([1., 2., 3., 4., 5., 6.]))

100 numpy exercises

以下是numpy100题练习。所有问题下的代码块如果第一行为# answer(xxx)表示此代码来自参考答案,否则就是我写的代码,我写的不少都是错误的,没有删去只是为了记录。

This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow
and in the numpy documentation. The goal of this collection is to offer a quick reference for both old
and new users but also to provide a set of exercises for those who teach.

If you find an error or think you’ve a better way to solve some of them, feel
free to open an issue at https://github.com/rougier/numpy-100.

File automatically generated. See the documentation to update questions/answers/hints programmatically.

Run the initialize.py module, then for each question you can query the
answer or an hint with hint(n) or answer(n) for n question number.

%run initialise.py

1. Import the numpy package under the name np (★☆☆)

import numpy as np

2. Print the numpy version and the configuration (★☆☆)

print(np.__version__)
np.show_config()
1.21.6
blas_mkl_info:
  NOT AVAILABLE
blis_info:
  NOT AVAILABLE
openblas_info:
    library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
    libraries = ['openblas_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
    libraries = ['openblas_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
  NOT AVAILABLE
openblas_lapack_info:
    library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
    libraries = ['openblas_lapack_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
    library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
    libraries = ['openblas_lapack_info']
    language = f77
    define_macros = [('HAVE_CBLAS', None)]
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2
    found = SSE3,SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
    not found = AVX512F,AVX512CD,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL

3. Create a null vector of size 10 (★☆☆)

np.empty(10)
array([6.23042070e-307, 8.45590538e-307, 6.23054972e-307, 8.45592576e-307,
       6.23054972e-307, 1.42419530e-306, 9.34604358e-307, 1.11261162e-306,
       7.23188049e-308, 1.35994435e-317])
# answer
Z = np.zeros(10)
print(Z)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

4. How to find the memory size of any array (★☆☆)

size(a)
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_1384\974405400.py in <module>
----> 1 size(a)


NameError: name 'size' is not defined
# answer(4)
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
800 bytes

5. How to get the documentation of the numpy add function from the command line? (★☆☆)

help(np.add)

6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)

a = np.zeros(10)
a[4] = 1
a
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])

7. Create a vector with values ranging from 10 to 49 (★☆☆)

np.arange(10, 50)
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
       44, 45, 46, 47, 48, 49])

8. Reverse a vector (first element becomes last) (★☆☆)

a = np.arange(10, 50)
a = np.flip(a)
a
array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
       32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
       15, 14, 13, 12, 11, 10])
# answer(8)
Z = np.arange(50)
Z = Z[::-1]
print(Z)
[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2
  1  0]

9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)

a = np.arange(9)
a.reshape(3, 3)
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)

a = np.array([1, 2, 0, 0, 4, 0])
np.nonzero(a)
(array([0, 1, 4], dtype=int32),)

11. Create a 3x3 identity matrix (★☆☆)

np.diag([1, 1, 1])
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]])
# answer
Z = np.eye(3)
print(Z)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

12. Create a 3x3x3 array with random values (★☆☆)

np.empty([3, 3,3])
array([[[7.63797880e-285, 0.00000000e+000, 2.04803837e+161],
        [1.94920553e-153, 1.05773468e-153, 6.03461190e+151],
        [8.48595369e-096, 1.67772392e+243, 1.16466606e-028]],

       [[4.90900643e+252, 1.94918511e-153, 1.16568549e+253],
        [7.49511203e+247, 1.12284826e+219, 1.94919754e-153],
        [8.42244189e+252, 1.46899937e+179, 1.05146958e-153]],

       [[1.94920675e-153, 2.59549710e+251, 4.91303153e-019],
        [5.67483605e-008, 6.77023429e+223, 1.94918965e-153],
        [2.59050139e-144, 1.16442250e-028, 6.79769037e-310]]])
# answer(12)
Z = np.random.random((3,3,3))
print(Z)
[[[0.70087058 0.28796343 0.36558073]
  [0.27845395 0.39957787 0.78124915]
  [0.32276857 0.31235655 0.88624314]]

 [[0.05004362 0.66781526 0.268062  ]
  [0.16450486 0.21165735 0.03606096]
  [0.76860487 0.27800734 0.5495332 ]]

 [[0.41797256 0.69033408 0.56868624]
  [0.77320771 0.0215807  0.55539528]
  [0.9414058  0.52934815 0.96923912]]]

13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)

a = np.random.random((10, 10))
a.min(), a.max()
(0.0019321122943093405, 0.9901264460013606)

14. Create a random vector of size 30 and find the mean value (★☆☆)

a = np.random.random(30)
a.mean()
0.5296207190020304

15. Create a 2d array with 1 on the border and 0 inside (★☆☆)

a = np.zeros((5, 5))
a[0, :] = a[-1, :] =  a[:, 0] = a[:, -1] = 1
a
array([[1., 1., 1., 1., 1.],
       [1., 0., 0., 0., 1.],
       [1., 0., 0., 0., 1.],
       [1., 0., 0., 0., 1.],
       [1., 1., 1., 1., 1.]])
# answer(15)
Z = np.ones((5,5))
Z[1:-1,1:-1] = 0
print(Z)
[[1. 1. 1. 1. 1.]
 [1. 0. 0. 0. 1.]
 [1. 0. 0. 0. 1.]
 [1. 0. 0. 0. 1.]
 [1. 1. 1. 1. 1.]]

16. How to add a border (filled with 0’s) around an existing array? (★☆☆)

a = np.arange(1, 10).reshape(3, 3)
b = np.zeros(a.shape[0])
a = np.vstack([b, a, b])
b = np.zeros((a.shape[0], 1))
a = np.hstack([b, a, b])
a
array([[0., 0., 0., 0., 0.],
       [0., 1., 2., 3., 0.],
       [0., 4., 5., 6., 0.],
       [0., 7., 8., 9., 0.],
       [0., 0., 0., 0., 0.]])
# answer(16) pad用法: https://numpy.org/doc/stable/reference/generated/numpy.pad.html#numpy.pad
Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print(Z)
[[0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 1. 1. 1. 1. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]

17. What is the result of the following expression? (★☆☆)

0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1
0 * np.nan
nan
np.nan == np.nan
False
np.inf > np.nan
False
np.nan in set([np.nan])
True
0.3 == 3 * 0.1
False

18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)

# 看错题目了,看成将对角线以上部分设置为0。这是ChatGPT的答案
a = np.random.randint(1, 5, size=(5, 5))
for i in range(5):
    a[i, i:] = 0
a
array([[0, 0, 0, 0, 0],
       [1, 0, 0, 0, 0],
       [2, 4, 0, 0, 0],
       [2, 1, 3, 0, 0],
       [2, 4, 1, 2, 0]])
# 看错题目了,看成将对角线以上部分设置为0。这是ChatGPT的答案
a = np.random.randint(1, 5, size=(5, 5))
a[np.triu_indices(5, k=0)] = 0
a
array([[0, 0, 0, 0, 0],
       [4, 0, 0, 0, 0],
       [4, 3, 0, 0, 0],
       [4, 1, 2, 0, 0],
       [4, 2, 1, 1, 0]])
# answer(18)
Z = np.diag(1+np.arange(4),k=-1)
print(Z)
[[0 0 0 0 0]
 [1 0 0 0 0]
 [0 2 0 0 0]
 [0 0 3 0 0]
 [0 0 0 4 0]]

19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)

a = np.zeros((8, 8), dtype=int)
a[::2, :] ^= 1
a[:, ::2] ^= 1
a
array([[0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0]])
# answer(19)
Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
print(Z)
[[0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]]

20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)

# 100 // 56 = 1
# so x = 1
# 100 - 56 = 44
# 44 // 8 = 5
# so y = 5
# 44 - 5 * 8 = 4
# so z = 4
# (1, 5, 4) is the 100th element
# answer(20)
print(np.unravel_index(99,(6,7,8)))
# 由于下标从0开始,z应该为3
(1, 5, 3)

21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)

a = np.array([[0, 1], [1, 0]])
np.tile(a, (4, 4))
array([[0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0]])

22. Normalize a 5x5 random matrix (★☆☆)

a = np.random.random((5, 5))
b = (a - a.mean()) / a.var()
b
array([[-1.3798781 , -4.36389211,  4.11418485,  0.35176577,  1.5933597 ],
       [-3.24896395, -3.08960552, -3.55428361, -0.93392942,  2.51877532],
       [-3.72227166,  6.07774641,  1.72588352,  5.41533313,  5.24830033],
       [-0.84673006, -2.93181892,  4.60308857,  1.70238601, -1.03432252],
       [-2.94486225, -1.88638923,  2.74315646, -4.12051686, -2.03651583]])
# answer(22)
Z = (a - np.mean (a)) / (np.std (a))
print(Z)
# 注意normalize是除以标准差,不是除以方差
[[-0.42264644 -1.33662782  1.26014434  0.10774325  0.48803427]
 [-0.99513359 -0.94632328 -1.08865074 -0.28605566  0.77148222]
 [-1.14010423  1.8615687   0.52862534  1.65867642  1.60751551]
 [-0.25934714 -0.89799442  1.40989193  0.52142822 -0.31680533]
 [-0.90198949 -0.57778705  0.8402085  -1.26208379 -0.62376971]]

23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)

# answer(23)
color = np.dtype([("r", np.ubyte),
                  ("g", np.ubyte),
                  ("b", np.ubyte),
                  ("a", np.ubyte)])
color
dtype([('r', 'u1'), ('g', 'u1'), ('b', 'u1'), ('a', 'u1')])

24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)

a = np.random.random((5, 3))
b = np.random.random((3, 2))
c = a.dot(b)
c
array([[0.65294461, 0.86079746],
       [1.02281891, 1.02220412],
       [0.76990053, 0.50680679],
       [0.83424076, 0.52723765],
       [0.96983981, 0.77543137]])
# answer(24)
Z = np.ones((5,3)) @ np.ones((3,2))
print(Z)
[[3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]
 [3. 3.]]

25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)

a = np.arange(10)
a[(a > 3) & (a < 8)] *= -1
a
array([ 0,  1,  2,  3, -4, -5, -6, -7,  8,  9])

26. What is the output of the following script? (★☆☆)

# Author: Jake VanderPlas

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
# 第一次输出 0 + 1 + 2 + 3 + 4 - 1 = 9
# 第二次输出 0 + 1 + 2 + 3 + 4 = 10
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z
# legal: 3, 4, 5, 6, 7
# answer(27)
Z = np.arange(3)
print(Z**Z)    # Z**Z 表示向量 Z 中的每个元素分别自己的幂运算。例如,如果 Z 包含 [2, 3, 4],那么 Z**Z 将产生 [2**2, 3**3, 4**4],即 [4, 27, 256]
print(2 << Z >> 2)
print(Z <- Z)
print(1j*Z)
print(Z/1/1)
print(Z<Z>Z)   # 在NumPy中,Z < Z > Z 表达式是不合法的,它将引发一个 ValueError。这是因为该表达式在语法上是不清晰的,并且不符合合理的运算规则。
[1 1 4]
[0 1 2]
[False False False]
[0.+0.j 0.+1.j 0.+2.j]
[0. 1. 2.]



---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_19920\1230403439.py in <module>
      6 print(1j*Z)
      7 print(Z/1/1)
----> 8 print(Z<Z>Z)   # 在NumPy中,Z < Z > Z 表达式是不合法的,它将引发一个 ValueError。这是因为该表达式在语法上是不清晰的,并且不符合合理的运算规则。


ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

28. What are the result of the following expressions? (★☆☆)

np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
np.array(0) / np.array(0)
C:\Users\Luo\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide
  """Entry point for launching an IPython kernel.





nan
np.array(0) // np.array(0)
C:\Users\Luo\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in floor_divide
  """Entry point for launching an IPython kernel.





0
print(np.array([np.nan]).astype(int).astype(float))
[-2.14748365e+09]

29. How to round away from zero a float array ? (★☆☆)

a = np.random.uniform(-10,+10,10)
print(a)
np.round(a)
[-9.57598132 -5.89442506  8.30063859  4.97022455  1.31123822 -0.97261964
 -6.96652433 -2.86466292 -4.55880481  8.30586822]





array([-10.,  -6.,   8.,   5.,   1.,  -1.,  -7.,  -3.,  -5.,   8.])
# answer(29)
# 问题是说,将浮点数组按照向0远离的方向取为整数,意思是正数向上取整,负数向下取整。
print(np.copysign(np.ceil(np.abs(a)), a))
# Author: Charles R Harris

Z = np.random.uniform(-10,+10,10)
print(np.copysign(np.ceil(np.abs(Z)), Z))

# More readable but less efficient
print(np.where(Z>0, np.ceil(Z), np.floor(Z)))
[-10.  -6.   9.   5.   2.  -1.  -7.  -3.  -5.   9.]

30. How to find common values between two arrays? (★☆☆)

a = np.array([1, 4, 9, 10])
b = np.array([4, 5, 7, 9])
c, counts = np.unique(np.concatenate((a, b)), return_counts=True)
print(c[counts > 1])
[4 9]
# answer(30)
print(np.intersect1d(a,b))
[4 9]
# answer(31)
# Suicide mode on
defaults = np.seterr(all="ignore")
Z = np.ones(1) / 0

# Back to sanity
_ = np.seterr(**defaults)

# Equivalently with a context manager
with np.errstate(all="ignore"):
    np.arange(3) / 0

32. Is the following expressions true? (★☆☆)

np.sqrt(-1) == np.emath.sqrt(-1)
print(np.sqrt(-1))
print(np.emath.sqrt(-1))
nan
1j


C:\Users\Luo\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt
  """Entry point for launching an IPython kernel.

33. How to get the dates of yesterday, today and tomorrow? (★☆☆)

# answer(33)
yesterday = np.datetime64('today') - np.timedelta64(1)
today     = np.datetime64('today')
tomorrow  = np.datetime64('today') + np.timedelta64(1)
yesterday, today, tomorrow
(numpy.datetime64('2023-12-12'),
 numpy.datetime64('2023-12-13'),
 numpy.datetime64('2023-12-14'))

34. How to get all the dates corresponding to the month of July 2016? (★★☆)

# answer(34)
start_date = np.datetime64('2016-07')
end_date = np.datetime64('2016-08')
dates = np.arange(start_date, end_date, dtype='datetime64[D]')
dates
array(['2016-07-01', '2016-07-02', '2016-07-03', '2016-07-04',
       '2016-07-05', '2016-07-06', '2016-07-07', '2016-07-08',
       '2016-07-09', '2016-07-10', '2016-07-11', '2016-07-12',
       '2016-07-13', '2016-07-14', '2016-07-15', '2016-07-16',
       '2016-07-17', '2016-07-18', '2016-07-19', '2016-07-20',
       '2016-07-21', '2016-07-22', '2016-07-23', '2016-07-24',
       '2016-07-25', '2016-07-26', '2016-07-27', '2016-07-28',
       '2016-07-29', '2016-07-30', '2016-07-31'], dtype='datetime64[D]')

35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)

a = np.arange(1, 11)
b = np.arange(2, 12)
a = (a + b) * (-a / 2)
a
array([  -1.5,   -5. ,  -10.5,  -18. ,  -27.5,  -39. ,  -52.5,  -68. ,
        -85.5, -105. ])
# answer(35)
a = np.arange(1, 11, dtype=float)   
b = np.arange(2, 12, dtype=float)
np.add(a, b, out=b)
np.divide(a, 2, out=a)
np.negative(a, out=a)
np.multiply(a, b, out=a)
a
array([  -1.5,   -5. ,  -10.5,  -18. ,  -27.5,  -39. ,  -52.5,  -68. ,
        -85.5, -105. ])

36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)

a = np.random.uniform(1, 10, size=10)
print(a)
# method 1
b = a.astype(int)
print(b)
# method 2
c = np.floor(a)
print(c)
[7.50387502 1.35810221 4.50311126 2.91663656 2.51391239 8.2568266
 6.22778107 7.31209577 8.49089112 9.23073519]
[7 1 4 2 2 8 6 7 8 9]
[7. 1. 4. 2. 2. 8. 6. 7. 8. 9.]
# answer(36)
Z = np.random.uniform(0, 10, size=10)
print(Z - Z%1)
print(Z // 1)
print(np.floor(Z))
print(Z.astype(int))
print(np.trunc(Z))
[4. 4. 0. 5. 0. 3. 6. 3. 9. 0.]
[4. 4. 0. 5. 0. 3. 6. 3. 9. 0.]
[4. 4. 0. 5. 0. 3. 6. 3. 9. 0.]
[4 4 0 5 0 3 6 3 9 0]
[4. 4. 0. 5. 0. 3. 6. 3. 9. 0.]

37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)

a = np.zeros((5, 5))
b = np.arange(5)
a + b
array([[0., 1., 2., 3., 4.],
       [0., 1., 2., 3., 4.],
       [0., 1., 2., 3., 4.],
       [0., 1., 2., 3., 4.],
       [0., 1., 2., 3., 4.]])
# answer(37)

# without broadcasting
Z = np.tile(np.arange(0, 5), (5,1))
print(Z)
[[0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]]

38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)

# answer(38)
def generate():
    for x in range(10):
        yield x
Z = np.fromiter(generate(),dtype=float,count=-1)
print(Z)
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]

39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)

np.random.uniform(0, 1, size=10)    # uniform 区间为[a, b)
array([0.18453329, 0.06884179, 0.17844744, 0.83769568, 0.05977437,
       0.49090446, 0.11438209, 0.82276114, 0.16048214, 0.50674783])
# answer(39)
Z = np.linspace(0,1,11,endpoint=False)[1:]
print(Z)
[0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455
 0.63636364 0.72727273 0.81818182 0.90909091]

40. Create a random vector of size 10 and sort it (★★☆)

a = np.random.randint(1, 100, 10)
a = np.sort(a)
a
array([ 4, 16, 34, 43, 52, 58, 64, 65, 70, 83])
# answer(40)
Z = np.random.random(10)
Z.sort()
print(Z)
[0.04135744 0.24125819 0.29859906 0.4794082  0.54876639 0.64261729
 0.66737038 0.73548868 0.9690323  0.98726125]

41. How to sum a small array faster than np.sum? (★★☆)

# answer(41)
Z = np.arange(10)
np.add.reduce(Z)
45

42. Consider two random array A and B, check if they are equal (★★☆)

A = np.random.randint(0, 10, 5)
B = np.random.randint(0, 10, 5)
A == B
array([False, False, False, False, False])
# answer(42)
# Assuming identical shape of the arrays and a tolerance for the comparison of values
equal = np.allclose(A,B)
print(equal)

# Checking both the shape and the element values, no tolerance (values have to be exactly equal)
equal = np.array_equal(A, B)
print(equal)
False
False

43. Make an array immutable (read-only) (★★☆)

# answer(43)
Z = np.zeros(10)
Z.flags.writeable=False
Z[0] = 1
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_18256\2218019560.py in <module>
      2 Z = np.zeros(10)
      3 Z.flags.writeable=False
----> 4 Z[0] = 1


ValueError: assignment destination is read-only

44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)

a = np.random.uniform(-100, 100, size=(10, 2))
print('array a\n', a)
b = np.sqrt(np.sum(a ** 2, axis = 1))
print('array b\n', b)
c = np.arctan2(a[:, 1], a[:, 0])
print('array c\n', c)
array a
 [[-66.2118548  -89.72357686]
 [-80.84337819   4.2570688 ]
 [-93.87098186   5.59585266]
 [-21.2024823   94.78903591]
 [ 15.54133336 -14.51308278]
 [-83.89270299   3.18708499]
 [ 99.42197245 -39.15432335]
 [-25.75859288  87.90028052]
 [-90.99731926  -6.56081742]
 [ 80.46487577  56.61020499]]
array b
 [111.50932679  80.95538544  94.0376244   97.13138825  21.26411564
  83.95321987 106.85405768  91.59674897  91.23352694  98.38349222]
array c
 [-2.20654319  3.08898302  3.08205095  1.79085473 -0.75119854  3.1036209
 -0.37516713  1.85585872 -3.06961818  0.61309741]
# answer(44)
Z = a
X,Y = Z[:,0], Z[:,1]
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)
[111.50932679  80.95538544  94.0376244   97.13138825  21.26411564
  83.95321987 106.85405768  91.59674897  91.23352694  98.38349222]
[-2.20654319  3.08898302  3.08205095  1.79085473 -0.75119854  3.1036209
 -0.37516713  1.85585872 -3.06961818  0.61309741]

45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)

a = np.random.random(10)
print('array a:\n', a)
b = a.max()
a[a == b] = 0
print('array a\':\n', a)
array a:
 [0.54162012 0.69781853 0.89350992 0.90543023 0.3287079  0.42707876
 0.54219439 0.00236117 0.66811187 0.75041092]
array a':
 [0.54162012 0.69781853 0.89350992 0.         0.3287079  0.42707876
 0.54219439 0.00236117 0.66811187 0.75041092]
# answer(45)
Z = a
Z[Z.argmax()] = 0
print(Z)
[0.54162012 0.69781853 0.         0.         0.3287079  0.42707876
 0.54219439 0.00236117 0.66811187 0.        ]

46. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area (★★☆)

a = np.linspace(0, 1, 5)
b = np.linspace(0, 1, 5)
c = np.array([[a[i // 5], b[i % 5]] for i in range(25)])
c
array([[0.  , 0.  ],
       [0.  , 0.25],
       [0.  , 0.5 ],
       [0.  , 0.75],
       [0.  , 1.  ],
       [0.25, 0.  ],
       [0.25, 0.25],
       [0.25, 0.5 ],
       [0.25, 0.75],
       [0.25, 1.  ],
       [0.5 , 0.  ],
       [0.5 , 0.25],
       [0.5 , 0.5 ],
       [0.5 , 0.75],
       [0.5 , 1.  ],
       [0.75, 0.  ],
       [0.75, 0.25],
       [0.75, 0.5 ],
       [0.75, 0.75],
       [0.75, 1.  ],
       [1.  , 0.  ],
       [1.  , 0.25],
       [1.  , 0.5 ],
       [1.  , 0.75],
       [1.  , 1.  ]])
# answer(46)
Z = np.zeros((5, 5), [('x', float), ('y', float)])
Z['x'], Z['y'] = np.meshgrid(np.linspace(0, 1, 5), np.linspace(0, 1, 5))
Z
array([[(0.  , 0.  ), (0.25, 0.  ), (0.5 , 0.  ), (0.75, 0.  ),
        (1.  , 0.  )],
       [(0.  , 0.25), (0.25, 0.25), (0.5 , 0.25), (0.75, 0.25),
        (1.  , 0.25)],
       [(0.  , 0.5 ), (0.25, 0.5 ), (0.5 , 0.5 ), (0.75, 0.5 ),
        (1.  , 0.5 )],
       [(0.  , 0.75), (0.25, 0.75), (0.5 , 0.75), (0.75, 0.75),
        (1.  , 0.75)],
       [(0.  , 1.  ), (0.25, 1.  ), (0.5 , 1.  ), (0.75, 1.  ),
        (1.  , 1.  )]], dtype=[('x', '<f8'), ('y', '<f8')])

47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)

X = np.arange(1, 6).reshape(5, -1)
Y = np.arange(20, 30, 2)
Z = np.zeros((5, 5))
Z = 1 / (Z + X - Y)
print(Z)
[[-0.05263158 -0.04761905 -0.04347826 -0.04       -0.03703704]
 [-0.05555556 -0.05       -0.04545455 -0.04166667 -0.03846154]
 [-0.05882353 -0.05263158 -0.04761905 -0.04347826 -0.04      ]
 [-0.0625     -0.05555556 -0.05       -0.04545455 -0.04166667]
 [-0.06666667 -0.05882353 -0.05263158 -0.04761905 -0.04347826]]
# answer(47)
X = np.arange(1, 6)
Y = np.arange(20, 30, 2)
C = 1.0 / np.subtract.outer(X, Y)
C, np.linalg.det(C)
(array([[-0.05263158, -0.04761905, -0.04347826, -0.04      , -0.03703704],
        [-0.05555556, -0.05      , -0.04545455, -0.04166667, -0.03846154],
        [-0.05882353, -0.05263158, -0.04761905, -0.04347826, -0.04      ],
        [-0.0625    , -0.05555556, -0.05      , -0.04545455, -0.04166667],
        [-0.06666667, -0.05882353, -0.05263158, -0.04761905, -0.04347826]]),
 -9.99502026076863e-26)

48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)

# answer(48)

for dtype in [np.int8, np.int32, np.int64]:
    print(np.iinfo(dtype).min)
    print(np.iinfo(dtype).max)
for dtype in [np.float32, np.float64]:
    print(np.finfo(dtype).min)
    print(np.finfo(dtype).max)
    print(np.finfo(dtype).eps)
-128
127
-2147483648
2147483647
-9223372036854775808
9223372036854775807
-3.4028235e+38
3.4028235e+38
1.1920929e-07
-1.7976931348623157e+308
1.7976931348623157e+308
2.220446049250313e-16

49. How to print all the values of an array? (★★☆)

# answer(49)
np.set_printoptions(threshold=float("inf"))
Z = np.zeros((40, 40))
print(Z)
[[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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.
  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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.]
 [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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 0. 0. 0. 0. 0.]]

50. How to find the closest value (to a given scalar) in a vector? (★★☆)

a = np.arange(0, 100, 3)
v = np.random.uniform(0, 100)
print(v)
a[np.argmin(np.abs(a - v))]
68.38557326847669





69

51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)

# answer(51)
Z = np.zeros(10, [ ('position', [ ('x', float),
                                  ('y', float)]),
                   ('color',    [ ('r', int),
                                  ('g', int),
                                  ('b', int)])])
print(Z)
[((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, 0))
 ((0., 0.), (0, 0, 0)) ((0., 0.), (0, 0, 0)) ((0., 0.), (0, 0, 0))
 ((0., 0.), (0, 0, 0))]

52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)

a = np.random.random((10, 2))
Z = np.zeros((10, 10))
for i in range(10):
    for j in range(10):
        Z[i, j] = np.sqrt(np.sum((a[i] - a[j]) ** 2))
Z
array([[0.        , 0.3297834 , 0.75829338, 0.53937734, 0.577428  ,
        0.51379811, 0.97950188, 0.26578591, 0.68912321, 0.63464354],
       [0.3297834 , 0.        , 0.46143514, 0.30423405, 0.32313377,
        0.22766164, 0.65562846, 0.39451746, 0.35972699, 0.38600695],
       [0.75829338, 0.46143514, 0.        , 0.58338834, 0.56012704,
        0.47828917, 0.27472009, 0.85376686, 0.23298445, 0.17537242],
       [0.53937734, 0.30423405, 0.58338834, 0.        , 0.04676737,
        0.10914907, 0.65665831, 0.42972434, 0.37089343, 0.59543802],
       [0.577428  , 0.32313377, 0.56012704, 0.04676737, 0.        ,
        0.10486404, 0.6181793 , 0.47609704, 0.34031258, 0.58526591],
       [0.51379811, 0.22766164, 0.47828917, 0.10914907, 0.10486404,
        0.        , 0.57572464, 0.46112764, 0.27687383, 0.48646399],
       [0.97950188, 0.65562846, 0.27472009, 0.65665831, 0.6181793 ,
        0.57572464, 0.        , 1.01972382, 0.30655871, 0.4500344 ],
       [0.26578591, 0.39451746, 0.85376686, 0.42972434, 0.47609704,
        0.46112764, 1.01972382, 0.        , 0.7131795 , 0.77519002],
       [0.68912321, 0.35972699, 0.23298445, 0.37089343, 0.34031258,
        0.27687383, 0.30655871, 0.7131795 , 0.        , 0.32446444],
       [0.63464354, 0.38600695, 0.17537242, 0.59543802, 0.58526591,
        0.48646399, 0.4500344 , 0.77519002, 0.32446444, 0.        ]])
# answer(52)
Z = a
X,Y = np.atleast_2d(Z[:,0], Z[:,1])
D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
print(D)

# Much faster with scipy
import scipy
# Thanks Gavin Heverly-Coulson (#issue 1)
import scipy.spatial

Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z)
print(D)
[[0.         0.3297834  0.75829338 0.53937734 0.577428   0.51379811
  0.97950188 0.26578591 0.68912321 0.63464354]
 [0.3297834  0.         0.46143514 0.30423405 0.32313377 0.22766164
  0.65562846 0.39451746 0.35972699 0.38600695]
 [0.75829338 0.46143514 0.         0.58338834 0.56012704 0.47828917
  0.27472009 0.85376686 0.23298445 0.17537242]
 [0.53937734 0.30423405 0.58338834 0.         0.04676737 0.10914907
  0.65665831 0.42972434 0.37089343 0.59543802]
 [0.577428   0.32313377 0.56012704 0.04676737 0.         0.10486404
  0.6181793  0.47609704 0.34031258 0.58526591]
 [0.51379811 0.22766164 0.47828917 0.10914907 0.10486404 0.
  0.57572464 0.46112764 0.27687383 0.48646399]
 [0.97950188 0.65562846 0.27472009 0.65665831 0.6181793  0.57572464
  0.         1.01972382 0.30655871 0.4500344 ]
 [0.26578591 0.39451746 0.85376686 0.42972434 0.47609704 0.46112764
  1.01972382 0.         0.7131795  0.77519002]
 [0.68912321 0.35972699 0.23298445 0.37089343 0.34031258 0.27687383
  0.30655871 0.7131795  0.         0.32446444]
 [0.63464354 0.38600695 0.17537242 0.59543802 0.58526591 0.48646399
  0.4500344  0.77519002 0.32446444 0.        ]]
[[0.         0.42315767 0.36506628 0.92471152 0.77999097 0.3752647
  0.79760858 0.83295175 0.76169433 0.45692723]
 [0.42315767 0.         0.52546539 0.56581209 0.37567734 0.06941747
  0.51906377 0.48056132 0.43943745 0.3506988 ]
 [0.36506628 0.52546539 0.         1.09116702 0.74915612 0.52580797
  0.61152103 0.71593721 0.62614425 0.74610735]
 [0.92471152 0.56581209 1.09116702 0.         0.54316297 0.5761888
  0.87084928 0.72778174 0.76311399 0.53267689]
 [0.77999097 0.37567734 0.74915612 0.54316297 0.         0.44225426
  0.33255259 0.18561258 0.22637808 0.66695903]
 [0.3752647  0.06941747 0.52580797 0.5761888  0.44225426 0.
  0.58407124 0.54990225 0.50728715 0.2918012 ]
 [0.79760858 0.51906377 0.61152103 0.87084928 0.33255259 0.58407124
  0.         0.16895359 0.10773714 0.8682109 ]
 [0.83295175 0.48056132 0.71593721 0.72778174 0.18561258 0.54990225
  0.16895359 0.         0.09177957 0.81058214]
 [0.76169433 0.43943745 0.62614425 0.76311399 0.22637808 0.50728715
  0.10773714 0.09177957 0.         0.78283229]
 [0.45692723 0.3506988  0.74610735 0.53267689 0.66695903 0.2918012
  0.8682109  0.81058214 0.78283229 0.        ]]

53. How to convert a float (32 bits) array into an integer (32 bits) in place?

a = np.random.uniform(0, 100, 10).astype(np.float32)
print(a)
a.astype(np.int32)
[88.91767   84.35517   74.78657   45.005787  97.85284   23.466541
 18.971066  86.05179    3.6458135 38.86691  ]





array([88, 84, 74, 45, 97, 23, 18, 86,  3, 38])
# answer(53)
# Thanks Vikas (https://stackoverflow.com/a/10622758/5989906)
# & unutbu (https://stackoverflow.com/a/4396247/5989906)
Z = (np.random.rand(10)*100).astype(np.float32)
Y = Z.view(np.int32)
Y[:] = Z
print(Y)
[42 10  5 42 33  7 36 45 17 21]

54. How to read the following file? (★★☆)

1, 2, 3, 4, 5
6,  ,  , 7, 8
 ,  , 9,10,11
# answer(54)
from io import StringIO

#Fake file
s = StringIO('''1, 2, 3, 4, 5
             6,  ,  , 7, 8
             ,  , 9,10,11
             ''')
Z = np.genfromtxt(s, delimiter=",", dtype=np.int32)
Z
array([[ 1,  2,  3,  4,  5],
       [ 6, -1, -1,  7,  8],
       [-1, -1,  9, 10, 11]])

55. What is the equivalent of enumerate for numpy arrays? (★★☆)

# answer(55)

Z = np.arange(9).reshape(3, 3)
for index, value in np.ndenumerate(Z):
    print(index, value)
for index in np.ndindex(Z.shape):
    print(index, Z[index])
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8

56. Generate a generic 2D Gaussian-like array (★★☆)

# answer(56)
X, Y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
D = np.sqrt(X*X + Y*Y)
sigma, mu = 1.0, 0.0
G = np.exp(-(D - mu) ** 2 / (2 * sigma ** 2))
G
array([[0.36787944, 0.44822088, 0.51979489, 0.57375342, 0.60279818,
        0.60279818, 0.57375342, 0.51979489, 0.44822088, 0.36787944],
       [0.44822088, 0.54610814, 0.63331324, 0.69905581, 0.73444367,
        0.73444367, 0.69905581, 0.63331324, 0.54610814, 0.44822088],
       [0.51979489, 0.63331324, 0.73444367, 0.81068432, 0.85172308,
        0.85172308, 0.81068432, 0.73444367, 0.63331324, 0.51979489],
       [0.57375342, 0.69905581, 0.81068432, 0.89483932, 0.9401382 ,
        0.9401382 , 0.89483932, 0.81068432, 0.69905581, 0.57375342],
       [0.60279818, 0.73444367, 0.85172308, 0.9401382 , 0.98773022,
        0.98773022, 0.9401382 , 0.85172308, 0.73444367, 0.60279818],
       [0.60279818, 0.73444367, 0.85172308, 0.9401382 , 0.98773022,
        0.98773022, 0.9401382 , 0.85172308, 0.73444367, 0.60279818],
       [0.57375342, 0.69905581, 0.81068432, 0.89483932, 0.9401382 ,
        0.9401382 , 0.89483932, 0.81068432, 0.69905581, 0.57375342],
       [0.51979489, 0.63331324, 0.73444367, 0.81068432, 0.85172308,
        0.85172308, 0.81068432, 0.73444367, 0.63331324, 0.51979489],
       [0.44822088, 0.54610814, 0.63331324, 0.69905581, 0.73444367,
        0.73444367, 0.69905581, 0.63331324, 0.54610814, 0.44822088],
       [0.36787944, 0.44822088, 0.51979489, 0.57375342, 0.60279818,
        0.60279818, 0.57375342, 0.51979489, 0.44822088, 0.36787944]])

57. How to randomly place p elements in a 2D array? (★★☆)

a = np.random.uniform(1, 10, size=(5,5))
position = np.random.choice(25, 3)
p = np.array([1, 2 ,3])
print(position)
a.flat[position] = p
a
[ 3 10 15]





array([[7.96288151, 3.93028479, 9.4225756 , 1.        , 4.83921758],
       [7.52191194, 8.72634187, 2.8661822 , 7.35366251, 5.06752821],
       [2.        , 1.40107052, 9.14497376, 8.09267119, 3.77901104],
       [3.        , 6.99270062, 8.19191184, 3.08880068, 8.04496636],
       [3.95537903, 9.23532846, 8.44552518, 5.06618062, 6.31904907]])
# answer(57)
n = 10
p = 3
Z = np.zeros((n,n))
np.put(Z, np.random.choice(range(n*n), p, replace=False),1)
print(Z)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 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. 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. 0. 0. 0. 0. 0. 0. 0. 0.]]

58. Subtract the mean of each row of a matrix (★★☆)

a = np.random.random(size=(5, 5))
row_mean = np.mean(a, axis=1, keepdims=True)
a -= row_mean
a
array([[ 0.08310891,  0.01701224,  0.06478933,  0.05965835, -0.22456883],
       [-0.06046683, -0.45703049,  0.37357525,  0.02326705,  0.12065502],
       [ 0.18840979, -0.01519799, -0.09461293,  0.17515316, -0.25375203],
       [-0.15672251,  0.2062965 , -0.47548568,  0.11847163,  0.30744006],
       [ 0.26314857,  0.28564864,  0.2454288 , -0.66345414, -0.13077187]])

59. How to sort an array by the nth column? (★★☆)

# answer(59)
a = np.random.randint(0, 9, size=(5, 5))
print(a)
n = 1
print(a[a[:, n].argsort()])
[[3 3 4 7 2]
 [8 5 5 5 8]
 [5 7 6 7 8]
 [3 0 5 5 3]
 [8 2 4 1 7]]
[[3 0 5 5 3]
 [8 2 4 1 7]
 [3 3 4 7 2]
 [8 5 5 5 8]
 [5 7 6 7 8]]

60. How to tell if a given 2D array has null columns? (★★☆)

# answer(60)

# null: 0
Z = np.random.randint(0, 3, (3, 10))
print(Z)
print((~Z.any(axis=0)).any())

# null: nan
Z = np.array([[0, 1, np.nan],
             [1, 2, np.nan],
             [4, 5, np.nan]])
print(np.isnan(Z).all(axis=0))
[[1 1 2 2 2 2 0 1 0 0]
 [1 2 2 2 2 1 2 0 0 2]
 [2 1 2 1 0 0 0 0 0 0]]
True
[False False  True]

61. Find the nearest value from a given value in an array (★★☆)

a = np.random.randint(0, 100, 10)
b = np.random.uniform(0, 100)
print(a, '\n', b)
a[np.argmin(np.abs(a - b))]
[72 67 63 74 36  2 56 80 65 28] 
 57.827865990016804





56
# answer(61)
Z = np.random.uniform(0,1,10)
z = 0.5
m = Z.flat[np.abs(Z - z).argmin()]
print(m)
0.4374302103462948

62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)

# answer(62)
A = np.arange(3).reshape(3,1)
B = np.arange(3).reshape(1,3)
it = np.nditer([A, B, None])
for x, y, z in it: z[...] = x + y
print(it.operands[2])
[[0 1 2]
 [1 2 3]
 [2 3 4]]

63. Create an array class that has a name attribute (★★☆)

# answer(63)

class NamedArray(np.ndarray):
    def __new__(cls, array, name="no name"):
        obj = np.asarray(array).view(cls)
        obj.name = name
        return obj
    
    def __array_finalize__(self, obj):
        if obj is None: return
        self.name = getattr(obj, 'name', "no name")

Z = NamedArray(np.arange(10), "range_10")
print(Z.name)
range_10

64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)

a = np.arange(10)
b = np.random.randint(0, 5, 10)
print(a, b)
for i in b:
    a[i] += 1
a
[0 1 2 3 4 5 6 7 8 9] [3 2 4 3 4 3 3 1 0 0]





array([2, 2, 3, 7, 6, 5, 6, 7, 8, 9])
# answer(64)
a = np.arange(10)
b = np.random.randint(0, 5, 10)
c = a + np.bincount(b, minlength=len(a))

np.add.at(a, b, 1)
print(c, a)
[1 4 4 3 8 5 6 7 8 9] [1 4 4 3 8 5 6 7 8 9]

65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)

# answer(65)
X = [1, 2, 3, 4, 5, 6]
I = [1, 3, 9, 3, 4, 1]
F = np.bincount(I, X)
F
array([0., 7., 0., 6., 5., 0., 0., 0., 0., 3.])

66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)

print(np.iinfo(np.ubyte).min, np.iinfo(np.ubyte).max)

w = h = 256
A = np.random.randint(0, 4, size=(w, h, 3)).astype(np.ubyte)
unique_colors = np.unique(A)
len(unique_colors)
# 题意理解错了,这段代码不对
0 255





4
# answer(66)

# Author: Fisher Wang
colors = np.unique(A.reshape(-1, 3), axis=0)
print(len(colors))

# Faster version
# Author: Mark Setchell
# https://stackoverflow.com/a/59671950/2836621
I24 = np.dot(A.astype(np.uint32), [1, 256, 65536])
print(len(np.unique(I24)))
64
64

67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)

a = np.random.randint(0, 256, size=(2,3,4,5))
np.sum(a, axis=(2, 3))
array([[2605, 2411, 2889],
       [2383, 2163, 2561]])
answer(67)

# solution by passing a tuple of axes (introduced in numpy 1.7.0)
sum = a.sum(axis=(-2, -1))
print(sum)

# (useful for functions that don't accept tuples for axis argument)
sum = a.reshape(a.shape[:-2] + (-1, )).sum(axis=-1)
print(sum)
A = np.random.randint(0,10,(3,4,3,4))
# solution by passing a tuple of axes (introduced in numpy 1.7.0)
sum = A.sum(axis=(-2,-1))
print(sum)
# solution by flattening the last two dimensions into one
# (useful for functions that don't accept tuples for axis argument)
sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)
print(sum)
[[2605 2411 2889]
 [2383 2163 2561]]
[[2605 2411 2889]
 [2383 2163 2561]]

68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)

D = np.random.uniform(0,1,100)
S = np.random.randint(0,10,100)
print(D, S)
np.mean(D[S])
# 这段代码不对,题目理解错误
[0.12475313 0.06831297 0.08277464 0.63989228 0.72198058 0.25316853
 0.50394462 0.99179143 0.88172453 0.96781144 0.24182706 0.51735159
 0.56280616 0.56483328 0.83364523 0.87786151 0.16855808 0.59066631
 0.32397804 0.98058749 0.2466403  0.84080913 0.42057434 0.37739748
 0.10610893 0.31900729 0.84186107 0.29943082 0.49751049 0.96961858
 0.456281   0.05311887 0.3529408  0.09871068 0.34619873 0.66818691
 0.70513117 0.84662567 0.42385116 0.26383855 0.48349887 0.75946387
 0.44504003 0.83183817 0.84101185 0.52061717 0.59542918 0.98881908
 0.87089116 0.87126882 0.62826451 0.92019434 0.24636341 0.09255505
 0.61571474 0.34045692 0.3140061  0.1073132  0.67259987 0.42578538
 0.77689002 0.30782286 0.7519799  0.62168474 0.98128834 0.83802508
 0.44918991 0.05899669 0.00862738 0.53988708 0.27648262 0.0449583
 0.01753611 0.54413121 0.06818303 0.33804793 0.09642105 0.0110936
 0.38627451 0.32285911 0.21356018 0.23132036 0.51773463 0.25482529
 0.09608666 0.59077329 0.21356451 0.21747279 0.3022142  0.90177431
 0.43852577 0.81162865 0.60741074 0.36760377 0.64218605 0.86333009
 0.25015403 0.26770076 0.66307866 0.07897235] [2 4 2 9 5 4 8 2 7 4 8 4 3 5 9 4 5 4 8 6 4 3 8 5 5 8 7 5 2 4 8 2 0 7 2 9 1
 8 2 0 5 0 2 7 0 2 1 2 3 1 0 0 2 8 0 2 7 2 2 5 1 8 2 3 5 2 1 3 0 5 1 1 1 0
 8 4 6 5 4 3 0 5 1 0 3 7 1 0 0 0 3 0 3 2 2 8 8 9 8 5]





0.4260669992327882
# answer(68)
D_sums = np.bincount(S, weights=D)
D_counts = np.bincount(S)
D_means = D_sums / D_counts
D_means
array([0.40344696, 0.47181515, 0.49268271, 0.60365966, 0.56885146,
       0.41085248, 0.55872365, 0.53358509, 0.5127976 , 0.53623043])

69. How to get the diagonal of a dot product? (★★★)

A = np.random.random((5, 5))
B = np.random.random((5, 5))
np.diag(A.dot(B))
array([1.20595107, 1.54921828, 1.31636838, 2.15405644, 0.63604674])
# answer(69)

# Fast version
print(np.sum(A * B.T, axis=1))

# Faster version
print(np.einsum("ij, ji->i", A, B))
[1.20595107 1.54921828 1.31636838 2.15405644 0.63604674]
[1.20595107 1.54921828 1.31636838 2.15405644 0.63604674]

70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)

# answer(70)
Z = np.array([1, 2, 3, 4, 5])
nz = 3
Z0 = np.zeros(len(Z) + (len(Z)-1)*nz)
Z0[::nz+1] = Z
Z0
array([1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4., 0., 0., 0., 5.])

71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)

a = np.arange(75).reshape(5, 5, 3)
b = np.random.rand(5, 5)
a * b.reshape(5, 5, -1)
array([[[ 0.        ,  0.54915337,  1.09830674],
        [ 0.36214351,  0.48285802,  0.60357252],
        [ 5.59609058,  6.52877235,  7.46145411],
        [ 3.18001162,  3.53334624,  3.88668087],
        [11.18825139, 12.12060567, 13.05295995]],

       [[12.09903274, 12.90563493, 13.71223711],
        [ 9.83457909, 10.38094459, 10.9273101 ],
        [14.35872418, 15.04247295, 15.72622172],
        [22.74756776, 23.69538308, 24.6431984 ],
        [14.93223224, 15.48527788, 16.03832352]],

       [[16.95124017, 17.51628151, 18.08132285],
        [19.10590761, 19.68487451, 20.2638414 ],
        [30.13025076, 30.96720217, 31.80415358],
        [35.54607592, 36.45751376, 37.3689516 ],
        [17.88184214, 18.30760028, 18.73335843]],

       [[10.54890774, 10.78332792, 11.01774809],
        [ 4.01370405,  4.09732288,  4.18094172],
        [48.10051071, 49.04365798, 49.98680525],
        [ 1.0094935 ,  1.02818782,  1.04688215],
        [24.7292995 , 25.16314685, 25.59699421]],

       [[55.41305737, 56.33660832, 57.26015928],
        [54.00334316, 54.86053908, 55.717735  ],
        [58.3413572 , 59.22531716, 60.10927712],
        [40.3067605 , 40.89091645, 41.4750724 ],
        [43.16992448, 43.76950677, 44.36908905]]])
# answer(71)
a * b[:, :, None]
array([[[ 0.        ,  0.54915337,  1.09830674],
        [ 0.36214351,  0.48285802,  0.60357252],
        [ 5.59609058,  6.52877235,  7.46145411],
        [ 3.18001162,  3.53334624,  3.88668087],
        [11.18825139, 12.12060567, 13.05295995]],

       [[12.09903274, 12.90563493, 13.71223711],
        [ 9.83457909, 10.38094459, 10.9273101 ],
        [14.35872418, 15.04247295, 15.72622172],
        [22.74756776, 23.69538308, 24.6431984 ],
        [14.93223224, 15.48527788, 16.03832352]],

       [[16.95124017, 17.51628151, 18.08132285],
        [19.10590761, 19.68487451, 20.2638414 ],
        [30.13025076, 30.96720217, 31.80415358],
        [35.54607592, 36.45751376, 37.3689516 ],
        [17.88184214, 18.30760028, 18.73335843]],

       [[10.54890774, 10.78332792, 11.01774809],
        [ 4.01370405,  4.09732288,  4.18094172],
        [48.10051071, 49.04365798, 49.98680525],
        [ 1.0094935 ,  1.02818782,  1.04688215],
        [24.7292995 , 25.16314685, 25.59699421]],

       [[55.41305737, 56.33660832, 57.26015928],
        [54.00334316, 54.86053908, 55.717735  ],
        [58.3413572 , 59.22531716, 60.10927712],
        [40.3067605 , 40.89091645, 41.4750724 ],
        [43.16992448, 43.76950677, 44.36908905]]])

72. How to swap two rows of an array? (★★★)

a = np.arange(25).reshape(5, 5)
print(a)
i = 0
j = 3
a[(i, j),:] = a[(j, i), :]
print(a)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
[[15 16 17 18 19]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [ 0  1  2  3  4]
 [20 21 22 23 24]]
# answer(72)
a[[i, j]] = a[[j, i]]
a
array([[15, 16, 17, 18, 19],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [ 0,  1,  2,  3,  4],
       [20, 21, 22, 23, 24]])

73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)

# answer(73)
faces = np.random.randint(0,100,(10,3))
F = np.roll(faces.repeat(2, axis=1), -1, axis=1)
F = F.reshape(len(F) * 3, 2)
F = np.sort(F, axis=1)
G = F.view(dtype=[('p0', F.dtype), ('p1', F.dtype)])    # 创建一个新字段用于之后的去重
G
array([[(46, 86)],
       [(46, 93)],
       [(86, 93)],
       [(37, 96)],
       [( 1, 37)],
       [( 1, 96)],
       [(52, 66)],
       [(52, 78)],
       [(66, 78)],
       [(29, 35)],
       [( 0, 29)],
       [( 0, 35)],
       [(25, 64)],
       [(22, 25)],
       [(22, 64)],
       [(79, 98)],
       [(25, 79)],
       [(25, 98)],
       [(49, 89)],
       [(87, 89)],
       [(49, 87)],
       [(67, 70)],
       [(45, 67)],
       [(45, 70)],
       [(32, 68)],
       [(22, 68)],
       [(22, 32)],
       [(67, 99)],
       [(62, 67)],
       [(62, 99)]], dtype=[('p0', '<i4'), ('p1', '<i4')])

74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)

# answer(74)
C = np.bincount([1, 1, 2, 3, 4, 4, 6])
A = np.repeat(np.arange(len(C)), C)
A
array([1, 1, 2, 3, 4, 4, 6])

75. How to compute averages using a sliding window over an array? (★★★)

a = np.arange(10)
b = np.cumsum(a)
b = np.concatenate(([0], b))
(b[3:] - b[:-3]) / 3
array([1., 2., 3., 4., 5., 6., 7., 8.])
answer(75)
from numpy.lib.stride_tricks import sliding_window_view
sliding_window_view(a, window_shape=3).mean(axis=-1)
# Author: Jaime Fernández del Río

def moving_average(a, n=3) :
    ret = np.cumsum(a, dtype=float)
    ret[n:] = ret[n:] - ret[:-n]
    return ret[n - 1:] / n
Z = np.arange(20)
print(moving_average(Z, n=3))

# Author: Jeff Luo (@Jeff1999)
# make sure your NumPy >= 1.20.0

from numpy.lib.stride_tricks import sliding_window_view

Z = np.arange(20)
print(sliding_window_view(Z, window_shape=3).mean(axis=-1))





array([1., 2., 3., 4., 5., 6., 7., 8.])

76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)

Z = np.arange(10)
from numpy.lib.stride_tricks import sliding_window_view
sliding_window_view(Z, window_shape=3)
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6],
       [5, 6, 7],
       [6, 7, 8],
       [7, 8, 9]])
answer(76)

from numpy.lib import stride_tricks
def rolling(a, window):
    shape = (a.size - window + 1, window)
    strides = (a.strides[0], a.strides[0])
    print(a.strides)
    return stride_tricks.as_strided(a, shape = shape, strides = strides)

rolling(Z, 3)
# Author: Joe Kington / Erik Rigtorp
from numpy.lib import stride_tricks

def rolling(a, window):
    shape = (a.size - window + 1, window)
    strides = (a.strides[0], a.strides[0])
    return stride_tricks.as_strided(a, shape=shape, strides=strides)
Z = rolling(np.arange(10), 3)
print(Z)

# Author: Jeff Luo (@Jeff1999)

Z = np.arange(10)
print(sliding_window_view(Z, window_shape=3))
(4,)
(4,)





array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6],
       [5, 6, 7],
       [6, 7, 8],
       [7, 8, 9]])

77. How to negate a boolean, or to change the sign of a float inplace? (★★★)

a = np.random.choice([True, False], size=10)
print(a, ~a)
b = np.random.uniform(-1, 1, 10)
b *= -1
b
# 这段代码没有实现inplace
[ True False False  True False False False False  True  True] [False  True  True False  True  True  True  True False False]





array([-0.85167359, -0.54536241,  0.70904615,  0.83138658, -0.14649344,
        0.52269721, -0.37187051, -0.71181299, -0.51968463,  0.0612906 ])
# answer(77)
Z = np.random.randint(0, 2, 10)
print(np.logical_not(Z, out=Z))

Z = np.random.uniform(-1, 1, 10)
np.negative(Z)
[0 1 0 1 1 1 1 1 0 1]





array([-0.33563875,  0.81622025, -0.68167123,  0.8485686 , -0.96364976,
        0.69912579, -0.53939199, -0.64928643,  0.98387248,  0.02449191])

78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★)

P0 = np.random.rand(10, 2)
P1 = np.random.rand(10, 2)
p = np. random.rand(2)
L1 = P1 - P0
L2 = p - P0
np.abs(np.cross(L1, L2, axis=-1)) / np.linalg.norm(L1, axis=1)
array([0.20647981, 0.2318803 , 0.1928034 , 0.22777146, 0.13557924,
       0.4374719 , 0.12303806, 0.08367477, 0.01987133, 0.26243178])
# answer(78)
def distance(P0, P1, p):
    T = P1 - P0
    L = (T ** 2).sum(axis=1)
    U = -((P0[:, 0] - p[..., 0]) * T[:, 0] + (P0[:, 1] - p[..., 1]) * T[:, 1]) / L
    U = U.reshape(len(U), 1)
    D = P0 + U * T - p
    return np.sqrt((D ** 2).sum(axis=1))
distance(P0, P1, p)
array([0.20647981, 0.2318803 , 0.1928034 , 0.22777146, 0.13557924,
       0.4374719 , 0.12303806, 0.08367477, 0.01987133, 0.26243178])

79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)

P0 = np.random.rand(10, 2)
P1 = np.random.rand(10, 2)
P = np. random.rand(5, 2)
L1 = (P1 - P0).reshape(10, 1, 2)
L2 = np.subtract(P1.reshape(10, 1, 2), P.reshape(1, 5, 2))
np.abs(np.cross(L1, L2, axis=-1)) / np.linalg.norm(L1, axis=-1)
array([[0.09530455, 0.02615035, 0.13777474, 0.54078345, 0.57684666],
       [0.01744725, 0.24230459, 0.24503059, 0.35697847, 0.4284491 ],
       [0.08201217, 0.02079011, 0.17404426, 0.19328169, 0.1796406 ],
       [0.18234492, 0.39611478, 0.38482848, 0.21438036, 0.28406465],
       [0.27130313, 0.4998965 , 0.50796243, 0.09379229, 0.16578742],
       [0.24086303, 0.46082542, 0.45707302, 0.14426586, 0.21498444],
       [0.5036613 , 0.4282135 , 0.27698513, 0.18779366, 0.19295231],
       [0.37555939, 0.55868774, 0.5169615 , 0.05845961, 0.12216972],
       [0.08941255, 0.33535463, 0.37756659, 0.20489558, 0.27815554],
       [0.07031965, 0.1774065 , 0.26854469, 0.2239561 , 0.29165927]])
# answer(79)
np.array([distance(P0, P1, p_i) for p_i in P])
array([[0.09530455, 0.01744725, 0.08201217, 0.18234492, 0.27130313,
        0.24086303, 0.5036613 , 0.37555939, 0.08941255, 0.07031965],
       [0.02615035, 0.24230459, 0.02079011, 0.39611478, 0.4998965 ,
        0.46082542, 0.4282135 , 0.55868774, 0.33535463, 0.1774065 ],
       [0.13777474, 0.24503059, 0.17404426, 0.38482848, 0.50796243,
        0.45707302, 0.27698513, 0.5169615 , 0.37756659, 0.26854469],
       [0.54078345, 0.35697847, 0.19328169, 0.21438036, 0.09379229,
        0.14426586, 0.18779366, 0.05845961, 0.20489558, 0.2239561 ],
       [0.57684666, 0.4284491 , 0.1796406 , 0.28406465, 0.16578742,
        0.21498444, 0.19295231, 0.12216972, 0.27815554, 0.29165927]])

80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary) (★★★)

# answer(80)
# Author: Nicolas Rougier

Z = np.random.randint(0,10,(10,10))
shape = (5,5)
fill  = 0
position = (1,1)

R = np.ones(shape, dtype=Z.dtype)*fill
P  = np.array(list(position)).astype(int)
Rs = np.array(list(R.shape)).astype(int)
Zs = np.array(list(Z.shape)).astype(int)

R_start = np.zeros((len(shape),)).astype(int)
R_stop  = np.array(list(shape)).astype(int)
Z_start = (P-Rs//2)
Z_stop  = (P+Rs//2)+Rs%2

R_start = (R_start - np.minimum(Z_start,0)).tolist()
R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()
Z_start = (np.maximum(Z_start,0)).tolist()
Z_stop = (np.minimum(Z_stop,Zs)).tolist()

r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
print(r, z)
R[r] = Z[z]
print(Z)
print(R)
[slice(1, 5, None), slice(1, 5, None)] [slice(0, 4, None), slice(0, 4, None)]
[[4 9 0 5 6 8 9 9 4 5]
 [7 5 4 2 1 8 2 1 9 9]
 [4 5 0 3 7 1 5 2 8 2]
 [6 8 7 2 5 1 8 9 2 8]
 [4 4 0 7 3 3 0 3 1 5]
 [9 2 8 7 5 9 5 9 3 6]
 [2 0 5 1 6 1 1 7 8 5]
 [9 9 6 1 5 9 8 5 5 6]
 [9 9 1 7 0 6 7 2 4 0]
 [8 1 3 7 6 0 6 5 4 4]]
[[0 0 0 0 0]
 [0 4 9 0 5]
 [0 7 5 4 2]
 [0 4 5 0 3]
 [0 6 8 7 2]]


C:\Users\Luo\AppData\Roaming\Python\Python37\site-packages\ipykernel_launcher.py:27: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.

81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], …, [11,12,13,14]]? (★★★)

Z = np.arange(1, 15)
from numpy.lib.stride_tricks import sliding_window_view
sliding_window_view(Z, window_shape=4)
array([[ 1,  2,  3,  4],
       [ 2,  3,  4,  5],
       [ 3,  4,  5,  6],
       [ 4,  5,  6,  7],
       [ 5,  6,  7,  8],
       [ 6,  7,  8,  9],
       [ 7,  8,  9, 10],
       [ 8,  9, 10, 11],
       [ 9, 10, 11, 12],
       [10, 11, 12, 13],
       [11, 12, 13, 14]])

82. Compute a matrix rank (★★★)

Z = np.random.rand(10, 10)
np.linalg.matrix_rank(Z)
10

83. How to find the most frequent value in an array?

Z = np.random.randint(0, 5, 10)
Y = np.bincount(Z)

Z, Y.argmax()
(array([3, 0, 0, 1, 4, 4, 0, 2, 2, 3]), 0)

84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)

Z = np.random.randint(0, 10, size=(10, 10))
from numpy.lib.stride_tricks import sliding_window_view
sliding_window_view(Z, (3, 3))
array([[[[4, 0, 1],
         [7, 6, 5],
         [8, 2, 7]],

        [[0, 1, 8],
         [6, 5, 5],
         [2, 7, 5]],

        [[1, 8, 6],
         [5, 5, 6],
         [7, 5, 0]],

        [[8, 6, 4],
         [5, 6, 7],
         [5, 0, 2]],

        [[6, 4, 7],
         [6, 7, 0],
         [0, 2, 6]],

        [[4, 7, 6],
         [7, 0, 1],
         [2, 6, 4]],

        [[7, 6, 3],
         [0, 1, 7],
         [6, 4, 7]],

        [[6, 3, 3],
         [1, 7, 0],
         [4, 7, 3]]],

       [[[7, 6, 5],
         [8, 2, 7],
         [5, 1, 6]],

        [[6, 5, 5],
         [2, 7, 5],
         [1, 6, 7]],

        [[5, 5, 6],
         [7, 5, 0],
         [6, 7, 6]],

        [[5, 6, 7],
         [5, 0, 2],
         [7, 6, 5]],

        [[6, 7, 0],
         [0, 2, 6],
         [6, 5, 0]],

        [[7, 0, 1],
         [2, 6, 4],
         [5, 0, 4]],

        [[0, 1, 7],
         [6, 4, 7],
         [0, 4, 1]],

        [[1, 7, 0],
         [4, 7, 3],
         [4, 1, 5]]],

       [[[8, 2, 7],
         [5, 1, 6],
         [9, 6, 2]],

        [[2, 7, 5],
         [1, 6, 7],
         [6, 2, 6]],

        [[7, 5, 0],
         [6, 7, 6],
         [2, 6, 1]],

        [[5, 0, 2],
         [7, 6, 5],
         [6, 1, 5]],

        [[0, 2, 6],
         [6, 5, 0],
         [1, 5, 3]],

        [[2, 6, 4],
         [5, 0, 4],
         [5, 3, 1]],

        [[6, 4, 7],
         [0, 4, 1],
         [3, 1, 0]],

        [[4, 7, 3],
         [4, 1, 5],
         [1, 0, 2]]],

       [[[5, 1, 6],
         [9, 6, 2],
         [9, 2, 4]],

        [[1, 6, 7],
         [6, 2, 6],
         [2, 4, 4]],

        [[6, 7, 6],
         [2, 6, 1],
         [4, 4, 6]],

        [[7, 6, 5],
         [6, 1, 5],
         [4, 6, 3]],

        [[6, 5, 0],
         [1, 5, 3],
         [6, 3, 1]],

        [[5, 0, 4],
         [5, 3, 1],
         [3, 1, 4]],

        [[0, 4, 1],
         [3, 1, 0],
         [1, 4, 3]],

        [[4, 1, 5],
         [1, 0, 2],
         [4, 3, 0]]],

       [[[9, 6, 2],
         [9, 2, 4],
         [4, 5, 3]],

        [[6, 2, 6],
         [2, 4, 4],
         [5, 3, 0]],

        [[2, 6, 1],
         [4, 4, 6],
         [3, 0, 4]],

        [[6, 1, 5],
         [4, 6, 3],
         [0, 4, 7]],

        [[1, 5, 3],
         [6, 3, 1],
         [4, 7, 7]],

        [[5, 3, 1],
         [3, 1, 4],
         [7, 7, 3]],

        [[3, 1, 0],
         [1, 4, 3],
         [7, 3, 6]],

        [[1, 0, 2],
         [4, 3, 0],
         [3, 6, 3]]],

       [[[9, 2, 4],
         [4, 5, 3],
         [7, 0, 5]],

        [[2, 4, 4],
         [5, 3, 0],
         [0, 5, 2]],

        [[4, 4, 6],
         [3, 0, 4],
         [5, 2, 1]],

        [[4, 6, 3],
         [0, 4, 7],
         [2, 1, 8]],

        [[6, 3, 1],
         [4, 7, 7],
         [1, 8, 2]],

        [[3, 1, 4],
         [7, 7, 3],
         [8, 2, 2]],

        [[1, 4, 3],
         [7, 3, 6],
         [2, 2, 5]],

        [[4, 3, 0],
         [3, 6, 3],
         [2, 5, 0]]],

       [[[4, 5, 3],
         [7, 0, 5],
         [8, 9, 1]],

        [[5, 3, 0],
         [0, 5, 2],
         [9, 1, 6]],

        [[3, 0, 4],
         [5, 2, 1],
         [1, 6, 3]],

        [[0, 4, 7],
         [2, 1, 8],
         [6, 3, 6]],

        [[4, 7, 7],
         [1, 8, 2],
         [3, 6, 6]],

        [[7, 7, 3],
         [8, 2, 2],
         [6, 6, 6]],

        [[7, 3, 6],
         [2, 2, 5],
         [6, 6, 7]],

        [[3, 6, 3],
         [2, 5, 0],
         [6, 7, 7]]],

       [[[7, 0, 5],
         [8, 9, 1],
         [7, 2, 7]],

        [[0, 5, 2],
         [9, 1, 6],
         [2, 7, 0]],

        [[5, 2, 1],
         [1, 6, 3],
         [7, 0, 2]],

        [[2, 1, 8],
         [6, 3, 6],
         [0, 2, 4]],

        [[1, 8, 2],
         [3, 6, 6],
         [2, 4, 0]],

        [[8, 2, 2],
         [6, 6, 6],
         [4, 0, 2]],

        [[2, 2, 5],
         [6, 6, 7],
         [0, 2, 3]],

        [[2, 5, 0],
         [6, 7, 7],
         [2, 3, 6]]]])

85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)

# answer(85)

class Symetric(np.ndarray):
    def __setitem__(self, key, value):
        i, j = key
        super(Symetric, self).__setitem__((i, j), value)
        super(Symetric, self).__setitem__((j, i), value)

def symetric(Z):
    return np.array(Z + Z.T) - np.diag(Z.diagonal()).view(Symetric)

S = symetric(np.random.randint(0, 10, (5, 5)))
Z = np.random.randint(0, 10, (5, 5))
S[2, 3] = 42
S
Symetric([[ 6, 10, 10, 14,  8],
          [10,  7,  6,  4, 10],
          [10,  6,  5, 42, 14],
          [14,  4, 42,  0,  3],
          [ 8, 10, 14,  3,  8]])

86. Consider a set of p matrices with shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)

# answer(86)
p, n = 10, 20
M = np.ones((p, n, n))
V = np.ones((p, n, 1))
S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])
S, S.shape

# It works, because:
# M is (p,n,n)
# V is (p,n,1)
# Thus, summing over the paired axes 0 and 0 (of M and V independently),
# and 2 and 1, to remain with a (n,1) vector.
(array([[200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.],
        [200.]]),
 (20, 1))

87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)

Z = np.random.randint(0, 10, (16, 16))
from numpy.lib.stride_tricks import sliding_window_view
Y = sliding_window_view(Z, window_shape=(4, 4))
np.sum(Y[::4, ::4], axis=(-1, -2))
array([[80, 69, 86, 93],
       [48, 66, 79, 88],
       [67, 64, 62, 73],
       [54, 85, 79, 73]])
answer(87)
k = 4
S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0),
                  np.arange(0, Z.shape[1], k), axis=1)
S
# Author: Robert Kern

Z = np.ones((16,16))
k = 4
S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0),
                                       np.arange(0, Z.shape[1], k), axis=1)
print(S)

# alternative solution:
# Author: Sebastian Wallkötter (@FirefoxMetzger)

Z = np.ones((16,16))
k = 4

windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
S = windows[::k, ::k, ...].sum(axis=(-2, -1))

# Author: Jeff Luo (@Jeff1999)

Z = np.ones((16, 16))
k = 4
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))





array([[80, 69, 86, 93],
       [48, 66, 79, 88],
       [67, 64, 62, 73],
       [54, 85, 79, 73]], dtype=int32)

88. How to implement the Game of Life using numpy arrays? (★★★)

# answer(88)

def iterate(Z):
    # Count neighbours
    N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
         Z[1:-1,0:-2]                + Z[1:-1,2:] +
         Z[2:  ,0:-2] + Z[2:  ,1:-1] + Z[2:  ,2:])

    # Apply rules
    birth = (N==3) & (Z[1:-1,1:-1]==0)
    survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1)
    Z[...] = 0
    Z[1:-1,1:-1][birth | survive] = 1
    return Z

Z = np.random.randint(0,2,(50,50))
for i in range(100): Z = iterate(Z)
print(Z)
[[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 0]
 [0 0 0 ... 0 0 0]]

89. How to get the n largest values of an array (★★★)

a = np.random.randint(0, 10000, size=(10000))
a.sort()
print(a)
n = 5
a[-n:]
[   0    0    0 ... 9995 9996 9997]





array([9993, 9994, 9995, 9996, 9997])
# answer(89)
# Slow
print(a[np.argsort(a)[-n:]])
# Fast
print(a[np.argpartition(-a, n)[:n]])
[9993 9994 9995 9996 9997]
[9997 9996 9995 9994 9993]

90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)

# answer(90)
# Author: Stefan Van der Walt

def cartesian(arrays):
    arrays = [np.asarray(a) for a in arrays]
    shape = (len(x) for x in arrays)

    ix = np.indices(shape, dtype=int)
    ix = ix.reshape(len(arrays), -1).T

    for n, arr in enumerate(arrays):
        ix[:, n] = arrays[n][ix[:, n]]

    return ix

print (cartesian(([1, 2, 3], [4, 5], [6, 7])))
[[1 4 6]
 [1 4 7]
 [1 5 6]
 [1 5 7]
 [2 4 6]
 [2 4 7]
 [2 5 6]
 [2 5 7]
 [3 4 6]
 [3 4 7]
 [3 5 6]
 [3 5 7]]

91. How to create a record array from a regular array? (★★★)

# answer(91)

Z = np.array([("Hello", 2.5, 3),
              ("World", 3.6, 2)])
R = np.core.records.fromarrays(Z.T,
                               names='col1, col2, col3',
                               formats = 'S8, f8, i8')
print(R)
[(b'Hello', 2.5, 3) (b'World', 3.6, 2)]

92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)

x = np.random.rand(int(5e4))
%timeit x ** 3
1.65 ms ± 7.68 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
# answer(92)
%timeit np.power(x,3)
%timeit x*x*x
%timeit np.einsum('i,i,i->i',x,x,x)
1.66 ms ± 42.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
45.9 µs ± 3.31 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
110 µs ± 4.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★)

# answer(93)
A = np.random.randint(0,5,(8,3))
B = np.random.randint(0,5,(2,2))
C = (A[..., np.newaxis, np.newaxis] == B)
rows = np.where(C.any((3, 1)).all(1))[0]
rows
array([3, 5, 6, 7], dtype=int32)

94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)

Z = np.random.randint(0, 3, size=(10,3))
print(Z)
Y = np.equal(Z, Z[:,0, np.newaxis])
X = np.equal(Z, Z[:,0, np.newaxis])
Z[~((X & Y).all(1))]
[[1 2 2]
 [2 2 2]
 [2 2 2]
 [2 2 2]
 [2 2 0]
 [2 0 0]
 [0 1 2]
 [0 2 0]
 [0 1 1]
 [2 1 2]]





array([[1, 2, 2],
       [2, 2, 0],
       [2, 0, 0],
       [0, 1, 2],
       [0, 2, 0],
       [0, 1, 1],
       [2, 1, 2]])
# answer(94)

# solution for arrays of all dtypes (including string arrays and record arrays)
E = np.all(Z[:,1:] == Z[:,:-1], axis=1)
U = Z[~E]
print(U)

# soluiton for numerical arrays only, will work for any number of columns in Z
U = Z[Z.max(axis=1) != Z.min(axis=1),:]
print(U)
[[1 2 2]
 [2 2 0]
 [2 0 0]
 [0 1 2]
 [0 2 0]
 [0 1 1]
 [2 1 2]]
[[1 2 2]
 [2 2 0]
 [2 0 0]
 [0 1 2]
 [0 2 0]
 [0 1 1]
 [2 1 2]]

95. Convert a vector of ints into a matrix binary representation (★★★)

# answer(95)

# Author: Warren Weckesser
I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])
B = ((I.reshape(-1, 1) & (2 ** np.arange(8))) != 0).astype(int)
print(B[:, ::-1])

# Author: Daniel T. McDonald
I = I.astype(np.uint8)
print(np.unpackbits(I[:, np.newaxis], axis=1))
[[0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1]
 [0 0 0 0 0 0 1 0]
 [0 0 0 0 0 0 1 1]
 [0 0 0 0 1 1 1 1]
 [0 0 0 1 0 0 0 0]
 [0 0 1 0 0 0 0 0]
 [0 1 0 0 0 0 0 0]
 [1 0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1]
 [0 0 0 0 0 0 1 0]
 [0 0 0 0 0 0 1 1]
 [0 0 0 0 1 1 1 1]
 [0 0 0 1 0 0 0 0]
 [0 0 1 0 0 0 0 0]
 [0 1 0 0 0 0 0 0]
 [1 0 0 0 0 0 0 0]]

96. Given a two dimensional array, how to extract unique rows? (★★★)

Z = np.random.randint(0,2, (6, 3))
print(Z)
np.unique(Z, axis=0)
[[1 1 1]
 [0 1 1]
 [1 1 1]
 [0 1 0]
 [1 1 1]
 [1 0 1]]





array([[0, 1, 0],
       [0, 1, 1],
       [1, 0, 1],
       [1, 1, 1]])
# answer(96)
T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))
_, idx = np.unique(T, return_index=True)
Z[idx]
array([[0, 1, 0],
       [0, 1, 1],
       [1, 0, 1],
       [1, 1, 1]])

97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)

A = np.arange(5)
B = np.arange(3, 8)
print('inner: ', np.einsum('i,i', A, B))
print('outer: ', np.einsum('i,j', A, B))
print('sum: ', np.einsum('i->', A))
print('mul: ', np.einsum('i,i->i', A, B))
inner:  60
outer:  [[ 0  0  0  0  0]
 [ 3  4  5  6  7]
 [ 6  8 10 12 14]
 [ 9 12 15 18 21]
 [12 16 20 24 28]]
sum:  10
mul:  [ 0  4 10 18 28]

98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?

answer(98)

phi = np.arange(0, 10 * np.pi, 0.1)
a = 1
x = a * phi * np.cos(phi)
y = a * phi * np.sin(phi)
dr = (np.diff(x) ** 2 + np.diff(y) ** 2) ** .5
r = np.zeros_like(x)
r[1:] = np.cumsum(dr)
r_int = np.linspace(0, r.max(), 200)
x_int = np.interp(r_int, r, x)
y_int = np.interp(r_int, r, y)
# Author: Bas Swinckels

phi = np.arange(0, 10*np.pi, 0.1)
a = 1
x = a*phi*np.cos(phi)
y = a*phi*np.sin(phi)

dr = (np.diff(x)**2 + np.diff(y)**2)**.5 # segment lengths
r = np.zeros_like(x)
r[1:] = np.cumsum(dr)                # integrate path
r_int = np.linspace(0, r.max(), 200) # regular spaced path
x_int = np.interp(r_int, r, x)       # integrate path
y_int = np.interp(r_int, r, y)

99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★)

# answer(99)
X = np.asarray([[1.0, 0.0, 3.0, 8.0],
                [2.0, 0.0, 1.0, 1.0],
                [1.5, 2.5, 1.0, 0.0]])
n = 4
M = np.logical_and.reduce(np.mod(X, 1) == 0, axis = -1)
M &= (X.sum(axis=-1) == n)
X[M]
array([[2., 0., 1., 1.]])

100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★)

# answer(100)
X = np.random.randn(100)
N = 1000
idx = np.random.randint(0, X.size, (N, x.size))
means = X[idx].mean(axis=1)
confint = np.percentile(means, [2.5, 97.5])
print(confint)
[-0.07902213  0.15337207]

文章作者: Knowledge
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Knowledge !
  目录