Numpy-ndarray
import numpy as npx = np.array([1,2,3])
print(type(x))
print(x)
print(x[0]) # 用索引方式取得或设定内容
print(x[1])
print(x[2])
x[1] = 10
print(x)
print(x.dtype) # 数组元素类型
print(x.itemsize) # 数组元素大小,int32,32位,4个字节
print(x.ndim) # 数组维度,1维数组
print(x.shape) # 数组外形
print(x.size) # 数组元素个数
x =np.array([2,4,6],dtype=np.int8) # 8位整数,1个字节
print(x.dtype)
print(x.itemsize)
y = np.array([1.1,2.3,3.6]) # 浮点数数组
print(y.dtype)
print(y)
ndarray.dtype:数组元素类型
ndarray.itemsize:数组元素数据类型大小(所占内存空间),字节
ndarray.ndim:数组的维度
ndarray.shape:数组维度元素个数的元组
ndarray.size:数组元素个数
import numpy as nprow1 = [1,2,3]
arr1 = np.array(row1, ndmin = 2)
print(arr1.ndim)
print(arr1.shape)
print(arr1.size)
print(arr1)
print('-'*70)
row2 = [4,5,6]
arr2 = np.array([row1,row2],ndmin=2)
print(arr2.ndim)
print(arr2.shape)
print(arr2.size)
print(arr2)
np.array(object, dtype, ndmin)
object:数组数据
dtype:数据类型,如果省略会使用可以容纳数据最省的类型
ndmin: 设定数组应具有的最小维度
import numpy as npx=np.array([[1,2,3],[4,5,6]])
print(x[0][2])
print(x[0,2])
import numpy as npx1 = np.zeros(3)
print(x1)
print('-'*50)
x2 = np.zeros((2,3),dtype=np.uint8)
print(x2)
np.zeros(shape, dtype=float)建立内容是0的数组
import numpy as np
x1=np.ones(3)
print(x1)
print('-'*50)
x2 = np.ones((2,3),dtype=np.uint8)
print(x2)
np.ones(object, dtype=None)建立内容是1的数组。
import numpy as np
x1 = np.empty(3)
print(x1)
print('-'*50)
x2 = np.empty((2,3), dtype=np.uint8)
print(x2)
np.empty(shape, dtype=float)建立指定形状与数据类型的数组,数组内容未初始化。
import numpy as npx1 = np.random.randint(10,20)
print('返回值是10(含)到20(不含)的单一随机数')
print(x1)
print('-'*50)
print('返回一维数组10个元素,值是1(含)到5(不含)的随机数')
x2 = np.random.randint(1,5,10)
print(x2)
print('-'*50)
print('返回3*5数组,值是0(含)到10(不含)的随机数')
x3 = np.random.randint(10,size = (3,5))
print(x3)
np.random.randint(low, high=none, size=None, dtype=int)
low随机数的最小值(含此值)
heigh:是可选项,有此值则代表随机数的最大值(不含此值),如果不含此参数,随机数是0~low
size:可选项,表示数组的维度。
dtype:默认整数数据类型
import numpy as npx = np.arange(16)
print(x)
np.arange(start, stop, step)
start:起始值,默认0
stop:结束值,不包含
step:相邻元素间距,默认1
import numpy as npx1 = np.arange(16)
print(x1)
print(np.reshape(x1,(2,8)))
np.reshape(a, newshape)
a:要更改的数组, newshap:新数组的外形,可以是数组或元组
有时候newshape的其中一个元素是-1,表示将依照另一个元素安排元素内容,自适应
import numpy as npx1 = np.arange(16)
print(x1)
print(np.reshape(x1,(-1,8)))
一维数组的运算与切片
import numpy as npx = np.array([1,2,3])
y = x+5
print(y)
y = np.array([10,20,30])
z = x+y
print(z)
z = x*y
print(z)
z = x/y
print(z)
import numpy as np
x = np.array([1,2,3])
y = np.array([10,20,30])
z = x>y
print(z)
z = x<y
print(z)
关系运算符:>,>=,<,<=,==,!=
import numpy as np
x = np.array([0,1,2,3,4,5,6,7,8,9])
print(f"x[-3:-7:-1]={x[-3:-7:-1]}")
print(f"x[::]={x[::]}")
print(f"x[:]={x[:]}")
print(f"x[-1]={x[-1]}")
import numpy as np
x1 = np.array([0,1,2,3,4,5])
x2 = np.array(x1, copy = True)
print(x1)
print(x2)
print('-'*50)
x2[0] = 9
print(f"x1:{x1}")
print(f"x2:{x2}")
np.array()函数的参数copy设为True,就可以复制数组,当内容修改时彼此不会互相影响。
import numpy as np
x1 = np.array([0,1,2,3,4,5])
x2 = x1.copy()
print(x1)
print(x2)
print('-'*50)
x2[0] = 9
print(f"x1:{x1}")
print(f"x2:{x2}")
x2 = x1.copy()与上一个等价
多维数组的索引与切片
在轴(axis)中,最小轴号代表数组的最外层。
import numpy as np
x1 = [0,1,2,3,4]
x2 = [5,6,7,8,9]
x3=[10,11,12,13,14]
x4 = np.array([x1,x2,x3])
x5 = np.array([x4,x4])
print(f"x5[0][2][1]={x5[0][2][1]}")
print(f"x5[0][1][3]={x5[0][1][3]}")
print(f"x5[1,0,1]={x5[1,0,1]}")
print(f"x5[1,1,4]={x5[1,1,4]}")
import numpy as npx1 = np.arange(4).reshape(2,2)
print(f'数组 1 \n{x1}')
x2 = np.arange(4,8).reshape(2,2)
print(f"数组 2 \n{x2}")
x = np.vstack((x1,x2))
print(f'合并结果 \n{x}')
x = np.vstack(tup)
tup:要垂直合并的两个数组
import numpy as np
x1 = np.arange(4).reshape(2,2)
print(f'数组 1 \n{x1}')
x2 = np.arange(4,8).reshape(2,2)
print(f"数组 2 \n{x2}")
x = np.hstack((x1,x2))
print(f'合并结果 \n{x}')
x = np.hstack(tup):水平合并数组