Numpy 简述

Numpy、Scipy、Pandas、Matplotlib 作为Python 进行数据分析的最常用的四个库,熟练操作对每一位数据分析或者深度学习的小伙伴都有非常大的帮助,本人就是其一,因此在最近的学习中打算进行整理分享出来,有需要的小伙伴记得关注收藏哟!

持续优化更新。。。

小序

标准的Python中使用列表保存一组数值,可以当作数组使用,但是列表的元素可以是任何对象,因此列表中实际上保存的是对象的指针。这样一来,为了保存一个简单的列表就需要一组指针 & 一组数值,对于数值运算来说,这种结构明显比较浪费内存和CPU的计算时间。

Numpy 的诞生弥补了这些不足,Numpy 提供了两种基本的对象:ndarray & ufunc 。

  • ndarray 是存储单一数据类型的 多维数组;
  • ufunc 则是能够对数组进行处理的相关函数;

1、np.random 随机值

1.1 np.random.rand

np.random.rand(d0, d1, ..., dn)

Random values in a given shape.
Create an array of the given shape and populate it with random samples from a uniform distribution over [0,1).

参数:

  • d0, d1, …, dn [int, optional] The dimensions of the returned array, should all be positive. If no argument is given a single Python float is returned.

Returns:

  • out [ndarray, shape (d0, d1, …, dn)] 随机参数值.
>>> np.random.rand(3,2)
array([[ 0.14022471, 0.96360618], #random
	   [ 0.37601032, 0.25528411], #random
	   [ 0.49313049, 0.94909878]]) #random

1.2 np.random.randn

np.random.randn(d0, d1, ..., dn)

Return a sample (or samples) from the “standard normal” distribution.If positive, int_like or int-convertible arguments are provided, randn generates an array of shape (d0, d1,…, dn), filled with random floats sampled from a univariate “normal” (Gaussian) distribution of mean 0 and variance 1 (if any of the 𝑒 𝑗 are floats, they are first converted to integers by truncation). A single float randomly sampled from the distribution is returned if no argument is provided.

This is a convenience function. If you want an interface that takes a tuple as the first argument, use numpy.
random.standard_normal instead.

参数:

  • d0, d1, …, dn [int, optional] The dimensions of the returned array, should be all positive. If no argument is given a single Python float is returned.

Returns:

  • Z [ndarray or float] A (d0, d1, …, dn)-shaped array of floating-point samples from the standard normal distribution, or a single such float if no parameters were supplied.

注意:
For random samples from N(μ,σ2)N( \mu ,\sigma^2 ), use:
sigma * np.random.randn(…) + mu

>>> np.random.randn()
2.1923875335537315 										   #random

>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random
	   [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random

1.3 np.random.random

np.random.random(size=None)

Return random floats in the half-open interval [0.0, 1.0).
Results are from the “continuous uniform” distribution over the stated interval. To sample 𝑉𝑜𝑗𝑔[𝑏,𝑐),𝑐 > 𝑏
multiply the output of random_sample by (b-a) and add a:
(b - a) * random_sample() + a

Parameters:

  • size [int or tuple of ints, optional] Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

Returns:

  • out [float or ndarray of floats] Array of random floats of shape size (unless size=None, in which case a single float is returned).

1.4 np.random.randint

np.random.randint(low, high=None, size=None, dtype=’l’)

Return random integers from low (inclusive) to high (exclusive).
Return random integers from the “discrete uniform”distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).

Parameters:

  • low [int] Lowest (signed) integer to be drawn from the distribution (unless high=None, in which case this parameter is one above the highest such integer).
  • high [int, optional] If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None).
  • size [int or tuple of ints, optional] Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
  • dtype [dtype, optional] Desired dtype of the result. All dtypes are determined by their name,i.e., ‘int64’, ‘int’, etc, so byteorder is not available and a specific precision may have different C types depending on the platform. The default value is ‘np.int’.
    New in version 1.11.0.

Returns:
out [int or ndarray of ints] size-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.

>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])

2、np.array

2.1 np.ones & np.zeros

方法 描述
eye(N[M, k, dtype, order]) 返回一个二维数组,对角线上有一个,其他地方为零。
identity(n[ dtype]) 返回标识数组。
empty(shape[dtype, order]) 返回给定形状和类型的新数组,仅分配数组所需要的内存,不进行初始化操作。
empty_like(prototype[dtype, order, subok, …]) 返回形状和类型与给定数组相同的新数组。
ones(shape[dtype, order]) 返回给定形状和类型的新数组,并填充为1。
ones_like(a[dtype, order, subok, shape]) 返回形状与类型与给定数组相同的数组。
zeros(shape[dtype, order]) 返回给定形状和类型的新数组,并用零填充。
zeros_like(a[dtype, order, subok, shape]) 返回形状与类型与给定数组相同的零数组。
full(shape, fill_value[dtype, order]) 返回给定形状和类型的新数组,并用fill_value填充。
full_like(a, fill_value[dtype, order, …]) 返回形状和类型与给定数组相同的完整数组。

zeros_like()、ones_like()、empty_like() 等带 _like() 的函数创建与参数数组的形状及类型相同的数组。

frombuffer()、fromstring()、fromfile() 等函数可以从字节序列或文件创建数组

2.2 np.array

np.array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)
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. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.
  • 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.

Returns:
out [ndarray] An array object satisfying the specified requirements.

Examples:

>>> np.array([1, 2, 3])
array([1, 2, 3])

>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])

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

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

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

>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])

>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
[3, 4]])

>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
[3, 4]])

2.3 np.ndarray

np.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:

  • 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] Same as self.transpose(), except that self is returned if self.ndim < 2.
  • data [buffer] Python buffer object pointing to the start of the array’s data.
  • dtype [dtype object] Data-type of the array’s elements.
  • flags [dict] Information about the memory layout of the array.
  • flat [numpy.flatiter object] A 1-D iterator over the array.
  • imag [ndarray] The imaginary part of the array.
  • real [ndarray] The real part of the array.
  • size [int] Number of elements in the array.
  • itemsize [int] Length of one array element in bytes.
  • nbytes [int] Total bytes consumed by the elements of the array.
  • ndim [int] Number of array dimensions.
  • shape [tuple of ints] Tuple of array dimensions.
  • strides [tuple of ints] Tuple of bytes to step in each dimension when traversing an array.
  • ctypes [ctypes object] An object to simplify the interaction of the array with the ctypes module.
  • base [ndarray] Base object if memory is from some other object.

Examples:

>>> np.ndarray(shape=(2,2), dtype=float, order='F')
array([[ -1.13698227e+002, 4.25087011e-303],
[ 2.88528414e-306, 3.27025015e-309]]) #random

>>> 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])

一个 ndarray是具有相同类型和大小的项目的多维容器。
尺寸和数组中的项目的数量是由它的shape定义, 它是由N个非负整数组成的tuple(元组),用于指定每个维度的大小。

不同的是,ndarrays可以共享相同的数据, 因此在一个ndarray中进行的更改可能在另一个中可见。 也就是说,ndarray可以是另一个ndarray 的 “view” ,它所指的数据由 “base” ndarray处理。 ndarrays也可以是Python拥有的内存strings或实现 buffer 或数组接口的对象的视图。

2.4 数组属性

以下属性包含有关数组内存布局的信息:

方法 描述
ndarray.flags 有关数组内存布局的信息
ndarray.shape 数组维度的元组
ndarray.strides 遍历数组时每个维度中的字节元组
ndarray.ndim 数组维数
ndarray.data Python 缓冲区对象指向数组的数据的开头
ndarray.size 数组中的元素数
ndarray.itemsize 一个数组元素的长度,以字节为单位
ndarray.nbytes 数组元素消耗的总字节数
ndarray.base 如果内存来自其他对象,则为基础对象

3、创建矩阵

方法 描述
mat(data[, dtype]) 将输入解释为矩阵。
bmat(obj[, ldict, gdict]) 从字符串,嵌套序列或数组构建矩阵对象。
diag(v[, k]) 提取对角线或构造对角线数组。
diagflat(v[, k]) 使用展平的输入作为对角线创建二维数组。
tri(N[, M, k, dtype]) 在给定对角线处及以下且在其他位置为零的数组。
tril(m[, k]) 数组的下三角。
triu(m[, k]) 数组的上三角。
vander(x[, N, increasing]) 生成范德蒙矩阵。

4、np.arange

np.arange 通过制定开始值/种植/步长 创建 等差数列的一维数组

  • endpoint :
展开阅读全文
©️2020 CSDN 皮肤主题: 鲸 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值