Array definition of data mining numpy

Numpy array

The main objects of NumPy are multidimensional arrays of the same elements. This is a table of elements where all elements are of one type, indexed by a tuple of positive integers (usually the elements are numbers). In NumPy, dimensions are called axes, and the number of axes is called rank.

NumPy's array class is called ndarray.

ndarray object properties

  • ndarray.ndim

    The number of array axes, in the python world, the number of axes is called the rank.

  • ndarray.shape

    The dimension of the array. This is a tuple of integers indicating the size of the array in each dimension. For example, for a matrix with n rows and m columns, its shape attribute will be (2,3), and the length of this tuple is obviously the rank, that is, the dimension or ndim attribute.

  • ndarray.size

    The total number of array elements, equal to the product of the tuple elements in the shape attribute.

  • ndarray.dtype

    An object that describes the type of the elements in the array. Standard Python types can be used by creating or specifying dtype. Additionally NumPy provides its own data types.

  • ndarray.itemsize

    The size in bytes of each element in the array. For example, an array with an element type of float64 has an itemiz property value of 8 (=64/8), and another example, an array with an element type of complex32 has an item property value of 4 (=32/8).

  • ndarray.data

    The buffer that contains the actual array elements, usually we don't need to use this property because we always use the elements in the array by index.

from numpy import *
a = arange(15).reshape(3, 5)
# arange(n) 返回长度为n的数组(array对象)
# reshape() 数组新的shape属性应该要与原来的配套。
# 如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值。
print("数组的值:\n {}".format(a))
print("数组维度: {}".format(a.shape))
print("数组的秩: {}".format(a.ndim))
print("数组中元素类型: {}".format(a.dtype))
print("数组中元素大小(字节为单位): {}".format(a.itemsize))
print("数组中元素个数: {}".format(a.size))
"D:\Python 3.6.2\python.exe" E:/PyPro/DM/np.py
数组的值:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
数组维度: (3, 5)
数组的秩: 2
数组中元素类型: int32
数组中元素大小(字节为单位): 4
数组中元素个数: 15

Process finished with exit code 0

array creation

array function

from numpy import *
# array()创建
a = array([
    1, 2, 3, 4
])
print(a)

# 序列转化多维数组
b = array([
    (1, 2, 3, 4),
    (5, 4, 3, 2)
])
print(b)

# 创建时指定数组类型
c = array([
    [1, 2],
    [2, 3],
    [0.2, 4]
], dtype=complex)
print(c)
print("c的元素类型为: {}".format(c.dtype))
"D:\Python 3.6.2\python.exe" E:/PyPro/DM/np.py
[1 2 3 4]
[[1 2 3 4]
 [5 4 3 2]]
[[ 1.0+0.j  2.0+0.j]
 [ 2.0+0.j  3.0+0.j]
 [ 0.2+0.j  4.0+0.j]]
c的元素类型为: complex128

function to create array

The function zeros creates an array of all zeros;

The function ones creates an array of all 1s;

The function empty creates an array whose content is random and depends on the memory state;

The array type (dtype) created by default is float64.

from numpy import *
'''
函数 zeros 创建一个全是0的数组;
函数 ones 创建一个全1的数组;
函数 empty 创建一个内容随机并且依赖与内存状态的数组;
默认创建的数组类型(dtype)都是float64。
'''
a = zeros((3, 4))
print(a)

b = ones((2, 8))
print(b)

c = empty((3, 2))
"D:\Python 3.6.2\python.exe" E:/PyPro/DM/np.py
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.]]

NumPy provides an arange-like function that returns an array instead of a list. arange(m, n, k) means to create an array with step size k in mn (including m but not function n).

When arange uses a floating-point argument, the number of elements obtained is often unpredictable due to the limited floating-point precision. Therefore, it is better to use the function linspace to receive the number of elements we want instead of specifying the step size with range.

from numpy import *
a = arange(10, 20, 5)
print(type(a))
print(a)
'''
当 arange 使用浮点数参数时,由于有限的浮点数精度,通常无法预测获得的元素个数。
因此,最好使用函数 linspace 去接收我们想要的元素个数来代替用range来指定步长。
'''
b = arange(0, 2, 0.3)
print(b)
"D:\Python 3.6.2\python.exe" E:/PyPro/DM/np.py
<class 'numpy.ndarray'>
[10 15]
[ 0.   0.3  0.6  0.9  1.2  1.5  1.8]

其它函数array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, rand, randn, fromfunction, fromfile参考: NumPy示例

array print

Prints an array, and NumPy displays it like a nested list, but in the following layout: The last axis is printed left to right The next axis is printed top-down The remaining axes are printed top-down, each slice Separate from the next by a blank line.

from numpy import *
'''
打印一个数组,NumPy以类似嵌套列表的形式显示它,但是呈以下布局:
最后的轴从左到右打印
次后的轴从顶向下打印
剩下的轴从顶向下打印,每个切片通过一个空行与下一个隔开
'''
a = arange(24).reshape(2, 3, 4)
print(a)
'''
如果一个数组用来打印太大了,NumPy自动省略中间部分而只打印角落
禁用NumPy的这种行为并强制打印整个数组,你可以设置printoptions参数来更改打印选项
set_printoptions(threshold='nan')
'''

b = arange(5000)
print(b)
"D:\Python 3.6.2\python.exe" E:/PyPro/DM/np.py
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
[   0    1    2 ..., 4997 4998 4999]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631111&siteId=291194637