Numpy study notes (1): array(), range(), arange() usage

Array function

Usage : np.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Function : Create an array.
Parameter description:
object: the array itself;
dtype: data type;
order: {'K','A','C','F'}, the default order is "K"
ndmin: specify the minimum dimension that the result array should have .

Actual code

import numpy as np
a = np.array([1,2,3])
b = np.array([[1,2,1],[2,3,2]]) # two dim array 两行三列
c = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[0,1,2]],[[3,4,5],[6,7,8]]]) # 包含了3个两行三列的二维数组的三维数组,x.shape[0]代表包含二维数组的个数,x.shape[1]表示二维数组的行数,x.shape[2]表示二维数组的列数。
# 二维数组里面b.shape[0]代表行数,b.shape[1]代表列数。
d = b.shape[0]
e = b.shape[1]
f = c.shape
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)

Code result:
Insert picture description here

Range function

Usage : range(start, stop[,step])
**Function:** Create a list of integers, generally used in a for loop.
Parameter description:
start: counting starts from start, the default is from 0
stop: counting to the end of stop, but not including stop.
step: step length, the default is 1.

Actual code:

"""range(5)
[0,1,2,3,4]
range(1,6)
[1,2,3,4,5]
range(0,20,5)
[0,5,10,15]
range(0,-5,-1)
[0,-1,-2,-3,-4]
range(0)
[]
"""
n = ' i love python '
for i in range(len(n)):
    print(n[i])

Insert picture description here

Arange function

Usage: The np.arange() function is divided into one parameter, two parameters, and three parameters.
Function : returns a fixed-step arrangement with end point and start point

Actual code:

import numpy as np
a = np.arange(6)
b = np.arange(1,6)
c = np.arange(0, 6, 0.1)
print(a)
print(b)
print(c)

Code result:
Insert picture description here
The difference between arange function and range function:
range() does not support the step size as a decimal, np.arange() supports the step size as a decimal

Guess you like

Origin blog.csdn.net/m0_51004308/article/details/112644756