ndarray对象基本属性

    科技2025-08-15  8

    ndarray对象属性的基本操作

    1.numpy对象的基本数据类型

    1.1基本数据类型
    类型表示方法类型字符码(简写)布尔型bool?有符号整形int8/int16/int32/int64i1/i2/i3/i4无符号整型uint8/uint16/uint32/uint64u1/u2/u3/u4浮点型float16/float32/float64f2/f4/f8复数型complex64/complex128c8/c16字符串型str,每个字符用32位Unicode编码表示M8[Y],M8[M],M8[D],M8[h],M8[m],M8[s]
    1.2数据对象的构造方式

    -np.array() -np.arrange(起始值,终止值,步长) -np.zeros() -np.ones() -np.zeros_like() -np.ones_like()

    1.3自定义复合类型的三种表示方法
    data = [('zs', [90, 70, 88], 15),('ls', [95, 76, 81], 16),('ww', [67, 79, 84], 17)] # 第一种设置dtype的格式 a = np.array(data, dtype='U2, 3int32, int32') print(a) print("ww age:", a[2][2]) print("ww grade:", a[2]["f1"]) # field # 若字段较多,则可以使用第二种设置dtype的方式 b = np.array(data, dtype=[("name", "str", 2),("scores", "int32", 3),("age", "int32",1)]) print(b) print("ww grade:", b[2]["scores"]) # 第三种设置dtype的格式 c = np.array(data, dtype={"names": ["name", "scores", "age"],"formats": ["U2", "3int32", "int32"]}) print(c) print("ls grade:", c[1]["scores"])
    1.3日期类型数组
    data = ["2011", "2011-01-02", "2012-01-01", "2012-02-01 10:10:10"] dates = np.array(data) print(dates, dates.dtype) #精确到Day的datatime64类型 dates = dates.astype("M8[D]") #M8[D], M8[M] print(dates, dates.dtype) print(dates[2] - dates[0])

    2.numpy属性的基本操作

    2.1基本属性如下
    基本属性表示方法维度shape元素类型dtype元素数量size维数ndim,len(shape)元素字节数itemsize总字节数nbytes=size*itemsize复数数组的实部数组real复数数组的虚部数组imag数组对象的转置视图T扁平迭代器flat a = np.array([[1+1j, 2+4j, 3+7j],[4+2j, 5+5j, 6+8j],[7+3j, 8+6j, 9+9j]]) print(a.shape) print(a.dtype) print(a.ndim) print(a.size) print(a.itemsize) print(a.nbytes) print(a.real, a.imag, sep="\n") print(a.T) print([elem for elem in a.flat]) b = a.tolist()#将a变为列表格式输出 print(b, type(b))
    2.2ndarray数组对象的维度操作
    #视图变维(数据共享):reshape()与ravel() a = np.arange(1, 9) print(a, a.shape) b = a.reshape(2, 4) print(b, b.shape) b[0, 0] = 10 print(a, a.shape) c = b.reshape(2, 2, 2) print(c, c.shape) d = c.ravel() print(d, d.shape) #复制变维(数据独立):flatten() copy() e = b.flatten()#默认按行的方向降维 print("e1:", e) b[0, 0] = 1 print("e2:", e) f = b.flatten("F") print(f) g = b.copy() print(g) #就地变维:直接改变原数组对象的维度,不返回新数组 a.shape a.resize() e.shape = (2, 2, 2) print(e) e.resize(9) #e.resize((9,)) print(e)

    3.ndarray数据切片操作

    3.1基本思想

    -数组对象切片的参数设置与列表切面参数类似 -步长+:默认切从首到尾 -步长-:默认切从尾到首 -数组对象[起始位置:终止位置:步长]

    3.2一维数组切片
    a = np.arange(1, 10) print(a) print(a[:3]) print(a[3:6]) print(a[6:]) print(a[::-1]) print(a[:-4:-1]) print(a[-4:-7:-1]) print(a[-7::-1]) print(a[::]) print(a[:]) print(a[::3]) print(a[1::3]) print(a[2::3])
    3.3多维数组切片
    a = np.arange(1, 28).reshape(3, 3, 3) print(a) print(a[:, :2, :2])
    3.4多维数组的组合与切分
    a = np.arange(1, 7).reshape(2, 3) b = np.arange(7, 13).reshape(2, 3) print(a) print(b) #垂直方向的组合与拆分 c = np.vstack((a, b)) d, e= np.vsplit(c, 2) print(c) print(d) print(e) #水平方向的组合与拆分 c = np.hstack((a, b)) d, e = np.hsplit(c, 2) print(c) print(d) print(e) #深度方向的组合与拆分 c = np.dstack((a, b)) d, e = np.dsplit(c, 2) print(c) print(d) print(e) ''' 0:垂直方向组合 1:水平方向组合 2:深度方向组合, a,b必须是三维数组 ''' c = np.concatenate((a, b), axis=1) print(c) d, e = np.split(c, 2, axis=0) print(d) print(e)
    3.5简单的一维数组组合方案
    a = np.arange(1, 9) b = np.arange(9, 17) #把两个数组摞在一起成两行 c = np.row_stack((a, b)) #把两个数组摞在一起成两列 d = np.column_stack((a, b)) print(c) print(d)

    4.ndarray数组的掩码操作

    a = np.arange(1, 100) b = [] mask = [True, False, True, False, True, False, True, False, True] #print(a[mask]) #使用掩码从大数据中截取子集 print(a[(a % 3 == 0) & (a % 7 == 0)]) #使用掩码把数组中的元素重新排列 b = np.array(["A", "B", "C", "D"]) mask = [3, 0, 2, 0, 0] print(b[mask])

    本博客仅为自学习使用,如有侵权请联系删除

    Processed: 0.009, SQL: 8