Python Numpy入门基础(一)创建数组

news2024/11/14 3:25:50

入门基础(一)

创建数组

1- np.array()

参数众多,初学时只要关注基本用法。

array(...)
    array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0,
          like=None)
    
    Create an array.
    
    Parameters
    ----------
    object : array_like
        An array, any object exposing the array interface, an object whose
        __array__ method returns an array, or any (nested) sequence.
    dtype : data-type, optional
        The desired data-type for the array.  If not given, then the type will
        be determined as the minimum type required to hold the objects in the
        sequence.
    copy : bool, optional
        If true (default), then the object is copied.  Otherwise, a copy will
        only be made if __array__ returns a copy, if obj is a nested sequence,
        or if a copy is needed to satisfy any of the other requirements
        (`dtype`, `order`, etc.).
    order : {'K', 'A', 'C', 'F'}, optional
        Specify the memory layout of the array. If object is not an array, the
        newly created array will be in C order (row major) unless 'F' is
        specified, in which case it will be in Fortran order (column major).
        If object is an array the following holds.
    
        ===== ========= ===================================================
        order  no copy                     copy=True
        ===== ========= ===================================================
        'K'   unchanged F & C order preserved, otherwise most similar order
        'A'   unchanged F order if input is F and not C, otherwise C order
        'C'   C order   C order
        'F'   F order   F order
        ===== ========= ===================================================
    
        When ``copy=False`` and a copy is made for other reasons, the result is
        the same as if ``copy=True``, with some exceptions for 'A', see the
        Notes section. The default order is 'K'.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).
    ndmin : int, optional
        Specifies the minimum number of dimensions that the resulting
        array should have.  Ones will be pre-pended to the shape as
        needed to meet this requirement.
    like : array_like
        Reference object to allow the creation of arrays which are not
        NumPy arrays. If an array-like passed in as ``like`` supports
        the ``__array_function__`` protocol, the result will be defined
        by it. In this case, it ensures the creation of an array object
        compatible with that passed in via this argument.
    
        .. versionadded:: 1.20.0

元组、列表转换

>>> import numpy as np
>>> np.array((1,2,3))
array([1, 2, 3])
>>> np.array([3,2,3])
array([3, 2, 3])
>>> np.array([[3,2,3],[4,5,6]])
array([[3, 2, 3],
       [4, 5, 6]])

内置函数 range()

>>> import numpy as np
>>> np.array(range(5))
array([0, 1, 2, 3, 4])
>>> np.array(range(2,11,2))
array([ 2,  4,  6,  8, 10])
>>> np.array([range(1,5),range(5,9)])
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

数组副本copy,开辟一块新内存复制原数组

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array(a)
>>> b
array([1, 2, 3])
>>> a[0] = 3
>>> a,b
(array([3, 2, 3]), array([1, 2, 3]))

主要参数:
dtype=     数组元素的数据类型,可选
copy=      对象是否需要复制,可选
order=     创建数组的样式,C为行方向,F为列方向,A为任意方向(默认)
subok=    默认返回一个与基类类型一致的数组
ndmin=    指定生成数组的最小维度

>>> import numpy as np
>>> np.array([[1, 2, 3, 4]], dtype=float)
array([[1., 2., 3., 4.]])
>>> np.array([[1, 2], [3, 4]], dtype=complex)
array([[1.+0.j, 2.+0.j],
       [3.+0.j, 4.+0.j]])
>>> np.array([[1, 2, 3, 4]], dtype=np.int64)
array([[1, 2, 3, 4]], dtype=int64)
>>> np.array({1, 2, 3, 4})
array({1, 2, 3, 4}, dtype=object)
>>> np.array({1, 2, 3, 4}).dtype
dtype('O') #集合只能作一个整体,大写字母O,即object
>>> np.array([[1, 2, 3, 4]], dtype=np.int64).dtype
dtype('int64')
>>> np.array([[1, 2], [3, 4, 5]])
array([list([1, 2]), list([3, 4, 5])], dtype=object)
>>> np.array([[1, 2], [3, 4, 5]]).dtype
dtype('O')
>>> 

>>> np.array([1, 2, 3, 4, 5], ndmin =  1)
array([1, 2, 3, 4, 5])
>>> np.array([1, 2, 3, 4, 5], ndmin =  2)
array([[1, 2, 3, 4, 5]])
>>> np.array([1, 2, 3, 4, 5], ndmin =  3)
array([[[1, 2, 3, 4, 5]]])
>>>

2.1- 基本属性 .shape  .ndim .dtype .size等

>>> a = np.array(range(2,11,2))
>>> b = np.array([range(1,5),range(5,9)])
>>> a.shape
(5,)
>>> b.shape
(2, 4)
>>> a.ndim, b.ndim
(1, 2)
>>> np.array(1)
array(1)
>>> np.array(1).ndim
0 #常数为0维
>>> a.dtype.name, b.dtype.name
('int32', 'int32')
>>> a.size, b.size
(5, 8)
>>> type(a), type(b)
(<class 'numpy.ndarray'>, <class 'numpy.ndarray'>)
>>> a
array([ 2,  4,  6,  8, 10])
>>> b
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
>>> print(a)
[ 2  4  6  8 10]
>>> print(b)
[[1 2 3 4]
 [5 6 7 8]]

.ndim      秩,即轴的数量或维度的数量
.shape    数组的维度,对于矩阵,n 行 m 列
.size       数组元素的总个数,相当于 .shape 中 n*m 的值
.dtype     对象的元素类型
.itemsize     对象中每个元素的大小,以字节为单位
.flags      对象的内存信息
.real       元素的实部
.imag     元素的虚部
.data      包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。 

2.2- 与属性同名的方法

除.itemsize .flags .data外者有同名方法,其它有方法的参数都为ndarray,dtype()除外。

>>> a = np.array([*range(5)],dtype=complex)
>>> np.ndim(a)
1
>>> np.shape(a)
(5,)
>>> np.size(a)
5
>>> np.real(a)
array([0., 1., 2., 3., 4.])
>>> np.imag(a)
array([0., 0., 0., 0., 0.])
>>> np.dtype(int)
dtype('int32')
>>> np.dtype(complex)
dtype('complex128')
>>> np.dtype(float)
dtype('float64')
>>> a.itemsize
16
>>> a.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

>>> a.data
<memory at 0x0000000002D79DC0>

3- np.arange()

arange(...)
    arange([start,] stop[, step,], dtype=None, *, like=None)
    
    Return evenly spaced values within a given interval.
    
    Values are generated within the half-open interval ``[start, stop)``
    (in other words, the interval including `start` but excluding `stop`).
    For integer arguments the function is equivalent to the Python built-in
    `range` function, but returns an ndarray rather than a list.
    
    When using a non-integer step, such as 0.1, the results will often not
    be consistent.  It is better to use `numpy.linspace` for these cases.
    
    Parameters
    ----------
    start : integer or real, optional
        Start of interval.  The interval includes this value.  The default
        start value is 0.
    stop : integer or real
        End of interval.  The interval does not include this value, except
        in some cases where `step` is not an integer and floating point
        round-off affects the length of `out`.
    step : integer or real, optional
        Spacing between values.  For any output `out`, this is the distance
        between two adjacent values, ``out[i+1] - out[i]``.  The default
        step size is 1.  If `step` is specified as a position argument,
        `start` must also be given.
    dtype : dtype
        The type of the output array.  If `dtype` is not given, infer the data
        type from the other input arguments.
    like : array_like
        Reference object to allow the creation of arrays which are not
        NumPy arrays. If an array-like passed in as ``like`` supports
        the ``__array_function__`` protocol, the result will be defined
        by it. In this case, it ensures the creation of an array object
        compatible with that passed in via this argument.
    
        .. versionadded:: 1.20.0

np.arange() 与 np.array(range()) 类似,但前者允许用浮点数

>>> np.arange(12)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> np.arange(0,1.1,0.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
>>> np.arange(2,5,0.3)
array([2. , 2.3, 2.6, 2.9, 3.2, 3.5, 3.8, 4.1, 4.4, 4.7])

4- np.reshape()

reshape(a, newshape, order='C')
    Gives a new shape to an array without changing its data.
    
    Parameters
    ----------
    a : array_like
        Array to be reshaped.
    newshape : int or tuple of ints
        The new shape should be compatible with the original shape. If
        an integer, then the result will be a 1-D array of that length.
        One shape dimension can be -1. In this case, the value is
        inferred from the length of the array and remaining dimensions.
    order : {'C', 'F', 'A'}, optional
        Read the elements of `a` using this index order, and place the
        elements into the reshaped array using this index order.  'C'
        means to read / write the elements using C-like index order,
        with the last axis index changing fastest, back to the first
        axis index changing slowest. 'F' means to read / write the
        elements using Fortran-like index order, with the first index
        changing fastest, and the last index changing slowest. Note that
        the 'C' and 'F' options take no account of the memory layout of
        the underlying array, and only refer to the order of indexing.
        'A' means to read / write the elements in Fortran-like index
        order if `a` is Fortran *contiguous* in memory, C-like order
        otherwise.
    
    Returns
    -------
    reshaped_array : ndarray
        This will be a new view object if possible; otherwise, it will
        be a copy.  Note there is no guarantee of the *memory layout* (C- or
        Fortran- contiguous) of the returned array.
>>> a = np.arange(8)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> np.reshape(a,(2,4))
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> np.reshape(a,(4,2))
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])
>>> np.reshape(a,(8,1))
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7]])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a.reshape(2,4)
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a.reshape(4,2)
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])

5- 数据类型

dtype对应的类型除了内置的int,float,complex等,可以用 np.bool_, np.int8, np.uint64:

bool_    布尔型数据类型(True 或者 False)
int_    默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
intc    与 C 的 int 类型一样,一般是 int32 或 int 64
intp    用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
int8    字节(-128 to 127)
int16    整数(-32768 to 32767)
int32    整数(-2147483648 to 2147483647)
int64    整数(-9223372036854775808 to 9223372036854775807)
uint8    无符号整数(0 to 255)
uint16    无符号整数(0 to 65535)
uint32    无符号整数(0 to 4294967295)
uint64    无符号整数(0 to 18446744073709551615)
float_    float64 类型的简写
float16    半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
float32    单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
float64    双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
complex_    complex128 类型的简写,即 128 位复数
complex64    复数,表示双 32 位浮点数(实数部分和虚数部分)
complex128    复数,表示双 64 位浮点数(实数部分和虚数部分)

每个内建类型都有一个唯一定义它的字符代码:

b    布尔型
i    (有符号) 整型
u    无符号整型 integer
f    浮点型
c    复数浮点型
m    timedelta(时间间隔)
M    datetime(日期时间)
O    (Python) 对象
S, a    (byte-)字符串
U    Unicode
V    原始数据 (void)

int8, int16, int32, int64 -- i1, i2, i4, i8
uint8,uint16,uint32,uint64 -- u1, u2, u4, u8
float16,float32,float64,float128 -- f2, f4, f8, f16
或: float32,float64,float128 -- f, d, g
complex64,complex128,complex256 -- c8,c16,c32
bool -- ?

>>> import numpy as np
>>> np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
dtype([('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
>>> import numpy as np
>>> student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
>>> student
dtype([('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
>>> np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
array([(b'abc', 21, 50.), (b'xyz', 18, 75.)],
      dtype=[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
>>> a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
>>> print(a)
[(b'abc', 21, 50.) (b'xyz', 18, 75.)]

6- np.asarray()

asarray(...)
    asarray(a, dtype=None, order=None, *, like=None)
    
    Convert the input to an array.
    
    Parameters
    ----------
    a : array_like
        Input data, in any form that can be converted to an array.  This
        includes lists, lists of tuples, tuples, tuples of tuples, tuples
        of lists and ndarrays.
    dtype : data-type, optional
        By default, the data-type is inferred from the input data.
    order : {'C', 'F', 'A', 'K'}, optional
        Memory layout.  'A' and 'K' depend on the order of input array a.
        'C' row-major (C-style),
        'F' column-major (Fortran-style) memory representation.
        'A' (any) means 'F' if `a` is Fortran contiguous, 'C' otherwise
        'K' (keep) preserve input order
        Defaults to 'C'.
    like : array_like
        Reference object to allow the creation of arrays which are not
        NumPy arrays. If an array-like passed in as ``like`` supports
        the ``__array_function__`` protocol, the result will be defined
        by it. In this case, it ensures the creation of an array object
        compatible with that passed in via this argument.
    
        .. versionadded:: 1.20.0
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.asarray(a)
>>> a,b
(array([1, 2, 3]), array([1, 2, 3]))
>>> a[0]=3
>>> a,b
(array([3, 2, 3]), array([3, 2, 3]))

注意 b=asarray(a)  与 b=array(a) 的区别,前者两数组指向同一内存地址。

7- np.fromiter()

fromiter(...)
    fromiter(iter, dtype, count=-1, *, like=None)
    
    Create a new 1-dimensional array from an iterable object.
    
    Parameters
    ----------
    iter : iterable object
        An iterable object providing data for the array.
    dtype : data-type
        The data-type of the returned array.
    count : int, optional
        The number of items to read from *iterable*.  The default is -1,
        which means all data is read.
    like : array_like
        Reference object to allow the creation of arrays which are not
        NumPy arrays. If an array-like passed in as ``like`` supports
        the ``__array_function__`` protocol, the result will be defined
        by it. In this case, it ensures the creation of an array object
        compatible with that passed in via this argument.
    
        .. versionadded:: 1.20.0
    
    Returns
    -------
    out : ndarray
        The output array.
    
    Notes
    -----
    Specify `count` to improve performance.  It allows ``fromiter`` to
    pre-allocate the output array, instead of resizing it on demand.
>>> import numpy as np
>>> np.fromiter(range(5),dtype=int)
array([0, 1, 2, 3, 4])
>>> np.fromiter(range(5),dtype=float)
array([0., 1., 2., 3., 4.])
>>> iterable = (x*x for x in range(5))
>>> np.fromiter(iterable, float)
array([ 0.,  1.,  4.,  9., 16.])
>>> np.fromiter({1,2,3,4}, float)
array([1., 2., 3., 4.])
>>> np.array({1,2,3,4})
array({1, 2, 3, 4}, dtype=object)
#注意:array()不能从集合中取出元素,只能作为一个整体
>>> np.fromiter('Hann Yang',dtype='S1')
array([b'H', b'a', b'n', b'n', b' ', b'Y', b'a', b'n', b'g'], dtype='|S1')
>>> np.fromiter(b'Hann Yang',dtype=np.uint8)
array([ 72,  97, 110, 110,  32,  89,  97, 110, 103], dtype=uint8)
#注意:字节串b''与字符串str的区别

8- np.frombuffer()

流的形式读入转化成 ndarray 对象,还可以分批读入。

frombuffer(...)
    frombuffer(buffer, dtype=float, count=-1, offset=0, *, like=None)
    
    Interpret a buffer as a 1-dimensional array.
    
    Parameters
    ----------
    buffer : buffer_like
        An object that exposes the buffer interface.
    dtype : data-type, optional
        Data-type of the returned array; default: float.
    count : int, optional
        Number of items to read. ``-1`` means all data in the buffer.
    offset : int, optional
        Start reading the buffer from this offset (in bytes); default: 0.
    like : array_like
        Reference object to allow the creation of arrays which are not
        NumPy arrays. If an array-like passed in as ``like`` supports
        the ``__array_function__`` protocol, the result will be defined
        by it. In this case, it ensures the creation of an array object
        compatible with that passed in via this argument.
    
        .. versionadded:: 1.20.0
>>> np.frombuffer('Hann Yang',dtype='S1')
Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    np.frombuffer('Hann Yang',dtype='S1')
TypeError: a bytes-like object is required, not 'str'
>>> np.frombuffer(b'Hann Yang',dtype='S1')
array([b'H', b'a', b'n', b'n', b' ', b'Y', b'a', b'n', b'g'], dtype='|S1')
>>> np.frombuffer(b'Hann Yang',dtype=int)
Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    np.frombuffer(b'Hann Yang',dtype=int)
ValueError: buffer size must be a multiple of element size
>>> np.frombuffer(b'Hann Yang',dtype=np.uint8)
array([ 72,  97, 110, 110,  32,  89,  97, 110, 103], dtype=uint8)
>>> np.frombuffer(b'Hann Yang',dtype='S1')
array([b'H', b'a', b'n', b'n', b' ', b'Y', b'a', b'n', b'g'], dtype='|S1')
>>> np.frombuffer(b'Hann Yang',dtype=np.uint8)
array([ 72,  97, 110, 110,  32,  89,  97, 110, 103], dtype=uint8)
>>> np.frombuffer(b'Hann Yang',dtype=np.uint8,count=4)
array([ 72,  97, 110, 110], dtype=uint8)
>>> np.frombuffer(b'Hann Yang',dtype=np.uint8,count=4,offset=4)
array([ 32,  89,  97, 110], dtype=uint8)
>>> np.frombuffer(b'Hann Yang',dtype=np.uint8,count=-1,offset=8)
array([103], dtype=uint8)

9.1- np.linspace()

以等差数列创建数组

linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
    Return evenly spaced numbers over a specified interval.
    
    Returns `num` evenly spaced samples, calculated over the
    interval [`start`, `stop`].
    
    The endpoint of the interval can optionally be excluded.
    
    .. versionchanged:: 1.16.0
        Non-scalar `start` and `stop` are now supported.
    
    .. versionchanged:: 1.20.0
        Values are rounded towards ``-inf`` instead of ``0`` when an
        integer ``dtype`` is specified. The old behavior can
        still be obtained with ``np.linspace(start, stop, num).astype(int)``
    
    Parameters
    ----------
    start : array_like
        The starting value of the sequence.
    stop : array_like
        The end value of the sequence, unless `endpoint` is set to False.
        In that case, the sequence consists of all but the last of ``num + 1``
        evenly spaced samples, so that `stop` is excluded.  Note that the step
        size changes when `endpoint` is False.
    num : int, optional
        Number of samples to generate. Default is 50. Must be non-negative.
    endpoint : bool, optional
        If True, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    retstep : bool, optional
        If True, return (`samples`, `step`), where `step` is the spacing
        between samples.
    dtype : dtype, optional
        The type of the output array.  If `dtype` is not given, the data type
        is inferred from `start` and `stop`. The inferred dtype will never be
        an integer; `float` is chosen even if the arguments would produce an
        array of integers.
    
        .. versionadded:: 1.9.0
    
    axis : int, optional
        The axis in the result to store the samples.  Relevant only if start
        or stop are array-like.  By default (0), the samples will be along a
        new axis inserted at the beginning. Use -1 to get an axis at the end.
    
        .. versionadded:: 1.16.0

创建区间可以是全开区间,也可以前开后闭区间。 

>>> np.linspace(2.0, 3.0, num=5)
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([2. ,  2.2,  2.4,  2.6,  2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)
>>> np.linspace(1, 1, 10, dtype=int)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

9.2- np.logspace() 

以对数数列创建数组

logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)
    Return numbers spaced evenly on a log scale.
    
    In linear space, the sequence starts at ``base ** start``
    (`base` to the power of `start`) and ends with ``base ** stop``
    (see `endpoint` below).
    
    .. versionchanged:: 1.16.0
        Non-scalar `start` and `stop` are now supported.
    
    Parameters
    ----------
    start : array_like
        ``base ** start`` is the starting value of the sequence.
    stop : array_like
        ``base ** stop`` is the final value of the sequence, unless `endpoint`
        is False.  In that case, ``num + 1`` values are spaced over the
        interval in log-space, of which all but the last (a sequence of
        length `num`) are returned.
    num : integer, optional
        Number of samples to generate.  Default is 50.
    endpoint : boolean, optional
        If true, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    base : array_like, optional
        The base of the log space. The step size between the elements in
        ``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
        Default is 10.0.
    dtype : dtype
        The type of the output array.  If `dtype` is not given, the data type
        is inferred from `start` and `stop`. The inferred type will never be
        an integer; `float` is chosen even if the arguments would produce an
        array of integers.
    axis : int, optional
        The axis in the result to store the samples.  Relevant only if start
        or stop are array-like.  By default (0), the samples will be along a
        new axis inserted at the beginning. Use -1 to get an axis at the end.
    
        .. versionadded:: 1.16.0
>>> np.logspace(2.0, 3.0, num=4)
array([ 100.        ,  215.443469  ,  464.15888336, 1000.        ])
>>> np.logspace(2.0, 3.0, num=4, endpoint=False)
array([100.        ,  177.827941  ,  316.22776602,  562.34132519])
>>> np.logspace(2.0, 3.0, num=4, base=2.0)
array([4.        ,  5.0396842 ,  6.34960421,  8.        ])

9.3- np.geomspace() 

geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0)
    Return numbers spaced evenly on a log scale (a geometric progression).
    
    This is similar to `logspace`, but with endpoints specified directly.
    Each output sample is a constant multiple of the previous.
    
    .. versionchanged:: 1.16.0
        Non-scalar `start` and `stop` are now supported.
    
    Parameters
    ----------
    start : array_like
        The starting value of the sequence.
    stop : array_like
        The final value of the sequence, unless `endpoint` is False.
        In that case, ``num + 1`` values are spaced over the
        interval in log-space, of which all but the last (a sequence of
        length `num`) are returned.
    num : integer, optional
        Number of samples to generate.  Default is 50.
    endpoint : boolean, optional
        If true, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    dtype : dtype
        The type of the output array.  If `dtype` is not given, the data type
        is inferred from `start` and `stop`. The inferred dtype will never be
        an integer; `float` is chosen even if the arguments would produce an
        array of integers.
    axis : int, optional
        The axis in the result to store the samples.  Relevant only if start
        or stop are array-like.  By default (0), the samples will be along a
        new axis inserted at the beginning. Use -1 to get an axis at the end.
    
        .. versionadded:: 1.16.0
>>> np.geomspace(1, 1000, num=4)
array([    1.,    10.,   100.,  1000.])
>>> np.geomspace(1, 1000, num=3, endpoint=False)
array([   1.,   10.,  100.])
>>> np.geomspace(1, 1000, num=4, endpoint=False)
array([   1.        ,    5.62341325,   31.6227766 ,  177.827941  ])
>>> np.geomspace(1, 256, num=9)
array([   1.,    2.,    4.,    8.,   16.,   32.,   64.,  128.,  256.])
    
#Note that the above may not produce exact integers:
    
>>> np.geomspace(1, 256, num=9, dtype=int)
array([  1,   2,   4,   7,  16,  32,  63, 127, 256])
>>> np.around(np.geomspace(1, 256, num=9)).astype(int)
array([  1,   2,   4,   8,  16,  32,  64, 128, 256])
    
#Negative, decreasing, and complex inputs are allowed:
    
>>> np.geomspace(1000, 1, num=4)
array([1000.,  100.,   10.,    1.])
>>> np.geomspace(-1000, -1, num=4)
array([-1000.,  -100.,   -10.,    -1.])
>>> np.geomspace(1j, 1000j, num=4)  # Straight line
array([0.   +1.j, 0.  +10.j, 0. +100.j, 0.+1000.j])
>>> np.geomspace(-1+0j, 1+0j, num=5)  # Circle
array([-1.00000000e+00+1.22464680e-16j, -7.07106781e-01+7.07106781e-01j,
            6.12323400e-17+1.00000000e+00j,  7.07106781e-01+7.07106781e-01j,
            1.00000000e+00+0.00000000e+00j])

10.1- 常量np.pi np.e np.nan np.inf 等

>>> np.pi
3.141592653589793
>>> np.e
2.718281828459045
>>> np.nan
nan
>>> np.inf
inf
>>> np.Inf
inf
>>> np.Infinity
inf
>>> np.PINF
inf
>>> np.NINF
-inf
>>> np.PZERO
0.0
>>> np.NZERO
-0.0

10.2- 常量数组 zeros() ones() empty()

>>> np.zeros((2,5))
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
>>> np.zeros((2,5),dtype=int)
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])
>>> np.linspace(0, 0, 10, dtype=int).reshape((2,5))
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])
>>> 
>>> np.ones((3,4))
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> np.ones((3,4),dtype=int)
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
>>> np.linspace(1, 1, 12, dtype=int).reshape((3,4))
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
>>>
>>> np.linspace(1, 1, 12, dtype=int).reshape((3,4))*3
array([[3, 3, 3, 3],
       [3, 3, 3, 3],
       [3, 3, 3, 3]])
>>> np.linspace(1, 1, 12, dtype=int).reshape((3,4))*np.pi
array([[3.14159265, 3.14159265, 3.14159265, 3.14159265],
       [3.14159265, 3.14159265, 3.14159265, 3.14159265],
       [3.14159265, 3.14159265, 3.14159265, 3.14159265]])

10.3- 常量数组 zeros_like() ones_like() empty_like()

>>> arr = np.ones((3,4))
>>> np.zeros_like(arr)
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

10.4- 单位矩阵 np.eye() 或 np.identity() 对角线为1,其余为0

>>> np.eye(4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
>>> np.identity(4, dtype=int)
array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]])

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/806788.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CAN通信协议

CAN 物理电平 以高速CAN为例 有电压差&#xff08;2.5V&#xff09;为显性&#xff0c;逻辑0无电压差为隐性&#xff0c;逻辑1 帧结构 SOF 恒为显性&#xff0c;逻辑0 仲裁段 当有多个设备发送数据&#xff0c;产生总线冲突时&#xff0c;来判断一个先后顺序由于总线是线与…

重学C++系列之友元

一、什么是友元 在C中&#xff0c;为了提高程序的效率&#xff0c;在一些场景下&#xff0c;引入友元&#xff0c;但同时类的封装性就会被破坏。 二、怎么实现友元 友元关键字&#xff08;friend&#xff09; // 在类中声明另一个类的成员函数来做为友元函数 // 以关键字&…

golangd\pycharm-ai免费代码助手安装使用gpt4-免费使用--[推荐]

golangd-ai免费代码助手安装使用,pycharm可以使用&#xff0c;估计只要是xx的ide都是可以使用这个插件 目前GPT4以及gpt的大规模使用&#xff0c;如何快速掌握以及在ide中快速使用的办法&#xff0c;今天安装一款golangd编辑器的插件已经使用 一、安装以及使用 1.在golangd中…

texshop mac中文版-TeXShop for Mac(Latex编辑预览工具)

texshop for mac是一款可以在苹果电脑MAC OS平台上使用的非常不错的Mac应用软件&#xff0c;texshop for mac是一个非常有用的工具&#xff0c;广泛使用在数学&#xff0c;计算机科学&#xff0c;物理学&#xff0c;经济学等领域的合作&#xff0c;这些程序的标准tetex分布特产…

PKG内容查看工具:Suspicious Package for Mac安装教程

Suspicious Package Mac版是一款Mac平台上的查看 PKG 程序包内信息的应用&#xff0c;Suspicious Package Mac版支持查看全部包内全部文件&#xff0c;比如需要运行的脚本&#xff0c;开发者&#xff0c;来源等等。 suspicious package mac使用简单&#xff0c;只需在选择pkg安…

Druid连接池与Mybatis-Plus非Spring环境整合:报错异常:NoClassDefFoundError

最近在搞一个Netty与扫描枪通信项目;通过调研框架使用Mybatis-PlusDruid作为获取数据库数据,结果整合报了个错,顺便记录下: 一&#xff0c;配置文件: driverClassNamecom.mysql.cj.jdbc.Driver urljdbc:mysql://127.0.0.1:3306/xxxxxxx?useUnicodetrue&characterEncodin…

SQL编译优化原理

最近在团队的OLAP引擎上做了一些SQL编译优化的工作&#xff0c;整理到了语雀上&#xff0c;也顺便发在博客上了。SQL编译优化理论并不复杂&#xff0c;只需要掌握一些关系代数的基础就比较好理解&#xff1b;比较困难的在于reorder算法部分。 文章目录 基础概念关系代数等价 j…

面试 | 校招字符串相关高频算法题汇总【C++实现】

文章目录 1、反转字符串2、反转字符串||3、字符串最后一个单词的长度4、找字符串中第一个只出现一次的字符5、仅仅反转字母6、验证一个字符串是否是回文7、反转字符串中的单词【⭐】&#xff08;1&#xff09;移除给出字符串中的多余空格&#xff08;2&#xff09;反转整个字符…

Linux下安装VirtualBox虚拟机

1. 简介 VirtualBox是一款强大的x86和AMD64/Intel64虚拟化产品&#xff0c;适用于企业和家庭。VirtualBox不仅是为企业客户提供的一款功能丰富、高性能的产品&#xff0c;它也是根据GNU通用公共许可证(GPL)版本3条款作为开放源码软件免费提供的唯一专业解决方案。有关VirtualBo…

Modbus TCP使用例程

一、Modbus介绍 关于Modbus的介绍可参考前面的文章<modbus tcp协议介绍及分析>和<modbus rtu通信格式测试解析>这2篇文章。 二、Agile Modbus软件包介绍 Agile Modbus软件包的链接地址&#xff1a; https://gitee.com/RT-Thread-Mirror/agile_modbus Agile Modbus的…

Day46 算法记录| 动态规划 13(子序列)

这里写目录标题 300.最长递增子序列 674. 最长连续递增序列718. 最长重复子数组 300.最长递增子序列 视频解析&#xff1a; 第一层for循环遍历每一个元素&#xff0c; ------- 第二层for循环找到当前元素前面有几个小于该值的元素 结尾需要统计最多的个数 class Solution {pu…

如何有效地使用ChatGPT写小说讲故事?

​构思故事情节&#xff0c;虽有趣但耗时&#xff0c;容易陷入写作瓶颈。ChatGPT可提供灵感&#xff0c;帮你解决写作难题。要写出引人入胜的故事&#xff0c;关键在于抓住八个要素——主题、人物、视角、背景、情节、语气、冲突和解决办法。 直接给出故事模板&#xff0c;你可…

无线温湿度信息收集点模块的组成和工作状态及编程与组网建议

在传感技术与物联网的不断发展下&#xff0c;无线温湿度信息收集点模块作为一种重要的终端设备&#xff0c;被广泛应用于各个领域。本文将详细介绍该模块的组成和工作状态&#xff0c;并给出编程和组网的建议。 一、组成 该无线温湿度信息收集点模块由以下几个核心组成部分构成…

Banana Pi BPI-KVM – 基于 Rockchip RK3568 SoC 的 KVM over IP 解决方案

Banana Pi 已经开始开发基于 Rockchip RK3568 SoC 的 BPI-KVM 盒&#xff0c;但它不是迷你 PC&#xff0c;而是 KVM over IP 解决方案&#xff0c;旨在远程控制另一台计算机或设备&#xff0c;就像您在现场一样&#xff0c;例如能够打开和关闭连接的设备、访问 BIOS 等。 商业…

vagrant centos7 根目录扩容

目录 1 创建 centos7 虚拟机 2 扩容根目录 我知道的扩容方式有两种&#xff1a;1 直接扩容分区 &#xff1b;2 扩容逻辑卷。 我没找到为根目录设置到逻辑卷的方法&#xff0c;所以使用直接扩容分区。 1 创建 centos7 虚拟机&#xff0c; vagrant up vagrant ssh 查看磁盘…

Git-分支管理

文章目录 1.分支管理2.合并冲突3.合并模式4.补充 1.分支管理 Git分支管理是指在Git版本控制系统中&#xff0c;使用分支来管理项目的不同开发线路和并行开发的能力。通过分支&#xff0c;开发者可以在独立的环境中进行功能开发、bug修复等工作&#xff0c;而不会影响到主分支上…

文章详情页 - 评论功能的实现

目录 1. 准备工作 1.1 创建评论表 1.2 创建评论实体类 1.3 创建 mapper 层评论接口和对应的 xml 实现 1.4 准备评论的 service 层 1.5 准备评论的 controller 层 2. 总的初始化详情页 2.1 加载评论列表 2.1.1 实现前端代码 2.1.2 实现后端代码 2.2 查询当前登录用户的…

SFP6012A-ASEMI代理海矽美快恢复二极管参数、尺寸、规格

编辑&#xff1a;ll SFP6012A-ASEMI代理海矽美快恢复二极管参数、尺寸、规格 型号&#xff1a;SFP6012A 品牌&#xff1a;ASEMI 封装&#xff1a;TO-247AC 恢复时间&#xff1a;100ns 正向电流&#xff1a;60A 反向耐压&#xff1a;1200V 芯片大小&#xff1a;102MIL*2…

苍穹外卖day-04

苍穹外卖day-04 本项目学自黑马程序员的《苍穹外卖》项目&#xff0c;是瑞吉外卖的Plus版本 功能更多&#xff0c;更加丰富。 结合资料&#xff0c;和自己对学习过程中的一些看法和问题解决情况上传课件笔记 视频&#xff1a;https://www.bilibili.com/video/BV1TP411v7v6/?sp…

一、window安装vagrant

篇章一、window安装vagrant 前言 在日常的学习中&#xff0c;需要在Window中学习Linux相关的操作命令&#xff0c;在本地熟悉Linux服务器环境&#xff0c;因此需要在电脑中安装Vagrant虚拟机来管理所需安装的Linux系统&#xff08;也就是后续的Centos-7&#xff09;。 1、下…