NumPy学习笔记

array 结构

import numpy as np
arr=[1,2,3,4,5]
arr+1
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [2], in <cell line: 2>()
      1 arr=[1,2,3,4,5]
----> 2 arr+1


TypeError: can only concatenate list (not "int") to list
arr=np.array([1,2,3,4,5])
print(type(arr))
<class 'numpy.ndarray'>
arr2=arr+1
arr2
array([2, 3, 4, 5, 6])
arr2+arr
array([ 3,  5,  7,  9, 11])
arr2*arr
array([ 2,  6, 12, 20, 30])
arr[2]
3
arr2[4]
6
arr.shape
(5,)
tag_list=[1,2,3]
tag_list.shape
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

Input In [10], in <cell line: 2>()
      1 tag_list=[1,2,3]
----> 2 tag_list.shape


AttributeError: 'list' object has no attribute 'shape'
arr2.dtype
dtype('int64')

对于ndarray结构来说,里面所有的元素必须是同一类型的 如果不是的话,会自动向下进行转化

arr2[2]=2.3
print(arr2)
arr2.dtype
[2 3 2 5 6]





dtype('int64')
arr2.itemsize
8
arr2.size
5
arr2.ndim
1

矩阵格式(多维结构)

arr3=np.array([[1,2,3],
               [4,5,6],
              [7,8,9]])
arr3.ndim
2
arr3.size
9
arr3.shape
(3, 3)
arr3[1,2]
6
arr3[1]
array([4, 5, 6])
arr3[:,1]
array([2, 5, 8])
arr4=arr3
arr4
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
arr4[1,1]=100
arr4
array([[  1,   2,   3],
       [  4, 100,   6],
       [  7,   8,   9]])
arr3
array([[  1,   2,   3],
       [  4, 100,   6],
       [  7,   8,   9]])
arr4=arr3.copy()
arr4[1,1]=10
arr4
array([[ 1,  2,  3],
       [ 4, 10,  6],
       [ 7,  8,  9]])
arr3
array([[  1,   2,   3],
       [  4, 100,   6],
       [  7,   8,   9]])
tar_arr=np.arange(0,100,10)
tar_arr
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
mask=np.array([0,1,0,0,0,1,1,0,0,1],dtype=bool)
mask
array([False,  True, False, False, False,  True,  True, False, False,
        True])
mask[5]=5
mask
array([False,  True, False, False, False,  True,  True, False, False,
        True])
tar_arr[mask]
array([10, 50, 60, 90])
random_arr=np.random.rand(10)
random_arr
array([0.29601142, 0.62927564, 0.69287355, 0.51879573, 0.26011604,
       0.90627468, 0.28751362, 0.74263935, 0.58191364, 0.64113374])
mask=random_arr>0.5
mask
array([False,  True,  True,  True, False,  True, False,  True,  True,
        True])
random_arr[mask]
array([0.62927564, 0.69287355, 0.51879573, 0.90627468, 0.74263935,
       0.58191364, 0.64113374])
np.where(random_arr>0.5)
(array([1, 2, 3, 5, 7, 8, 9]),)
random_arr[np.where(random_arr>0.5)]
array([0.62927564, 0.69287355, 0.51879573, 0.90627468, 0.74263935,
       0.58191364, 0.64113374])

array 值类型操作

arr=np.array([1,2,3,4],dtype=np.float32)
arr
array([1., 2., 3., 4.], dtype=float32)
arr.dtype
dtype('float32')
arr=np.array([1,2,3.7,'ads'])
arr
array(['1', '2', '3.7', 'ads'], dtype='<U32')
arr=np.array([1,2,3.7,'ads'],dtype=np.object_)
arr
array([1, 2, 3.7, 'ads'], dtype=object)
arr*2
array([2, 4, 7.4, 'adsads'], dtype=object)
tag_arr=np.array([1,2,3,4])
tag_arr2=np.asarray(tag_arr,dtype=np.float32)
tag_arr2
array([1., 2., 3., 4.], dtype=float32)
tag_arr
array([1, 2, 3, 4])

array 数组的数值计算

tag_arr=np.array([[1,2,3],[4,5,6]])
tag_arr
array([[1, 2, 3],
       [4, 5, 6]])
np.sum(tag_arr)
21
  • 指定要进行的操作是沿着什么轴(维度)
np.sum(tag_arr,axis=0)
array([5, 7, 9])
tag_arr.ndim
2
np.sum(tag_arr,axis=1)
array([ 6, 15])
np.sum(tag_arr,axis=-1)
array([ 6, 15])
tag_arr.sum(axis=1)
array([ 6, 15])
tag_arr.sum(axis=0)
array([5, 7, 9])
tag_arr.prod()
720
tag_arr.prod(axis=1)
array([  6, 120])
tag_arr.prod(axis=0)
array([ 4, 10, 18])
tag_arr.max()
6
tag_arr.min()
1
  • 找到索引位置
tag_arr.argmax()
5
tag_arr.argmin()
0
tag_arr.argmax(axis=1)
array([2, 2])
tag_arr.argmin(axis=3)
---------------------------------------------------------------------------

AxisError                                 Traceback (most recent call last)

Input In [59], in <cell line: 1>()
----> 1 tag_arr.argmin(axis=3)


AxisError: axis 3 is out of bounds for array of dimension 2

  • 均值
tag_arr.mean()
3.5
tag_arr.mean(axis=0)
array([2.5, 3.5, 4.5])
  • 标准差
tag_arr.std()
1.707825127659933
  • 方差计算
tag_arr.var()
2.9166666666666665
  • 对矩阵值进行限制,<最小值 则取最小值,>最大值 则取最大值
print(tag_arr)
tag_arr.clip(2,4)
[[1 2 3]
 [4 5 6]]





array([[2, 2, 3],
       [4, 4, 4]])
  • 对矩阵值四舍五入
tag_arr=np.array([[1.2,3.53,5.23,6.8]])
tag_arr.round()
array([[1., 4., 5., 7.]])
tag_arr.round(decimals=1)
array([[1.2, 3.5, 5.2, 6.8]])

排序操作

tag_arr=np.array([[1.5,2.1,1.1],
                  [2.0,3.4,1.0]])
tag_arr
array([[1.5, 2.1, 1.1],
       [2. , 3.4, 1. ]])
np.sort(tag_arr)
array([[1.1, 1.5, 2.1],
       [1. , 2. , 3.4]])
np.sort(tag_arr,axis=0)
array([[1.5, 2.1, 1. ],
       [2. , 3.4, 1.1]])
np.sort(tag_arr,axis=1)
array([[1.1, 1.5, 2.1],
       [1. , 2. , 3.4]])
tag_arr
array([[1.5, 2.1, 1.1],
       [2. , 3.4, 1. ]])
np.argsort(tag_arr)
array([[2, 0, 1],
       [2, 0, 1]])
np.argsort(tag_arr,axis=0)
array([[0, 0, 1],
       [1, 1, 0]])
np.argsort(tag_arr,axis=1)
array([[2, 0, 1],
       [2, 0, 1]])
tag_arr=np.linspace(0,10,10)
tag_arr
array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,
        5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])
values=np.array([3.5,6.5,9.3])
np.searchsorted(tag_arr,values)
array([4, 6, 9])
tag_arr=np.array([
    [1,0,6],
    [2,5,7],
    [3,2,0],
    [4,8,1]
])
tag_arr
array([[1, 0, 6],
       [2, 5, 7],
       [3, 2, 0],
       [4, 8, 1]])
index=np.lexsort([-1*tag_arr[:,0],tag_arr[:,2]])
index
array([2, 3, 0, 1])
tar_arr=tar_arr[index]
tar_arr
array([20, 30,  0, 10])

数组维度操作

tag_arr=np.arange(10)
tag_arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tag_arr.shape
(10,)
tag_arr.shape=2,5
tag_arr
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
tag_arr.reshape(1,10)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
tag_arr.shape=3,4
tag_arr
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Input In [84], in <cell line: 1>()
----> 1 tag_arr.shape=3,4
      2 tag_arr


ValueError: cannot reshape array of size 10 into shape (3,4)
tag_arr.shape
(2, 5)
tag_arr.reshape(1,10)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
tag_arr[np.newaxis,:]
tag_arr.shape
(2, 5)
tag_arr=np.arange(10)
tag_arr.shape
(10,)
tag_arr=tag_arr[:,np.newaxis]
tag_arr.shape
(10, 1)
tag_arr=tag_arr[:,np.newaxis,np.newaxis]
tag_arr.shape
(10, 1, 1, 1)
tag_arr=tag_arr.squeeze()
tag_arr.shape
(10,)
tag_arr.shape=2,5
tag_arr
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
tag_arr.T
array([[0, 5],
       [1, 6],
       [2, 7],
       [3, 8],
       [4, 9]])
  • 数组拼接
arr1=np.array([[32,23,45],[123,23,34]])
arr1
array([[ 32,  23,  45],
       [123,  23,  34]])
arr2=np.array([[89,54,67],[13,54,365]])
arr2
array([[ 89,  54,  67],
       [ 13,  54, 365]])
c=np.concatenate((arr1,arr2))
c
array([[ 32,  23,  45],
       [123,  23,  34],
       [ 89,  54,  67],
       [ 13,  54, 365]])
c=np.concatenate((arr1,arr2),axis=0)
c
array([[ 32,  23,  45],
       [123,  23,  34],
       [ 89,  54,  67],
       [ 13,  54, 365]])
c=np.concatenate((arr1,arr2),axis=1)
c
array([[ 32,  23,  45,  89,  54,  67],
       [123,  23,  34,  13,  54, 365]])
np.vstack((arr1,arr2))
array([[ 32,  23,  45],
       [123,  23,  34],
       [ 89,  54,  67],
       [ 13,  54, 365]])
np.hstack((arr1,arr2))
array([[ 32,  23,  45,  89,  54,  67],
       [123,  23,  34,  13,  54, 365]])
arr1
array([[ 32,  23,  45],
       [123,  23,  34]])
arr1.flatten()
array([ 32,  23,  45, 123,  23,  34])
arr1
array([[ 32,  23,  45],
       [123,  23,  34]])
arr1.ravel()
array([ 32,  23,  45, 123,  23,  34])
  • 数组生成
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(2,20,2)
array([ 2,  4,  6,  8, 10, 12, 14, 16, 18])
np.linspace(0,10,50)
array([ 0.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,
        1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,
        2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,
        3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,
        4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,
        5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,
        6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,
        7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,
        8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,
        9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ])
np.logspace(0,1,5) #默认10 为底的 log
array([ 1.        ,  1.77827941,  3.16227766,  5.62341325, 10.        ])
help(np.logspace)
Help on function logspace in module numpy:

logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)
    Return numbers spaced evenly on a log scale.
    
    In linear space, the sequence starts at ``base ** start``
    (`base` to the power of `start`) and ends with ``base ** stop``
    (see `endpoint` below).
    
    .. versionchanged:: 1.16.0
        Non-scalar `start` and `stop` are now supported.
    
    Parameters
    ----------
    start : array_like
        ``base ** start`` is the starting value of the sequence.
    stop : array_like
        ``base ** stop`` is the final value of the sequence, unless `endpoint`
        is False.  In that case, ``num + 1`` values are spaced over the
        interval in log-space, of which all but the last (a sequence of
        length `num`) are returned.
    num : integer, optional
        Number of samples to generate.  Default is 50.
    endpoint : boolean, optional
        If true, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    base : array_like, optional
        The base of the log space. The step size between the elements in
        ``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
        Default is 10.0.
    dtype : dtype
        The type of the output array.  If `dtype` is not given, the data type
        is inferred from `start` and `stop`. The inferred type will never be
        an integer; `float` is chosen even if the arguments would produce an
        array of integers.
    axis : int, optional
        The axis in the result to store the samples.  Relevant only if start
        or stop are array-like.  By default (0), the samples will be along a
        new axis inserted at the beginning. Use -1 to get an axis at the end.
    
        .. versionadded:: 1.16.0
    
    
    Returns
    -------
    samples : ndarray
        `num` samples, equally spaced on a log scale.
    
    See Also
    --------
    arange : Similar to linspace, with the step size specified instead of the
             number of samples. Note that, when used with a float endpoint, the
             endpoint may or may not be included.
    linspace : Similar to logspace, but with the samples uniformly distributed
               in linear space, instead of log space.
    geomspace : Similar to logspace, but with endpoints specified directly.
    
    Notes
    -----
    Logspace is equivalent to the code
    
    >>> y = np.linspace(start, stop, num=num, endpoint=endpoint)
    ... # doctest: +SKIP
    >>> power(base, y).astype(dtype)
    ... # doctest: +SKIP
    
    Examples
    --------
    >>> np.logspace(2.0, 3.0, num=4)
    array([ 100.        ,  215.443469  ,  464.15888336, 1000.        ])
    >>> np.logspace(2.0, 3.0, num=4, endpoint=False)
    array([100.        ,  177.827941  ,  316.22776602,  562.34132519])
    >>> np.logspace(2.0, 3.0, num=4, base=2.0)
    array([4.        ,  5.0396842 ,  6.34960421,  8.        ])
    
    Graphical illustration:
    
    >>> import matplotlib.pyplot as plt
    >>> N = 10
    >>> x1 = np.logspace(0.1, 1, N, endpoint=True)
    >>> x2 = np.logspace(0.1, 1, N, endpoint=False)
    >>> y = np.zeros(N)
    >>> plt.plot(x1, y, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()
x=np.linspace(-10,10,5)
#y=np.linspace(-10,10,5)
y=np.copy(x)
y
array([-10.,  -5.,   0.,   5.,  10.])
x,y=np.meshgrid(x,y)#构造网格
x
array([[-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.]])
y
array([[-10., -10., -10., -10., -10.],
       [ -5.,  -5.,  -5.,  -5.,  -5.],
       [  0.,   0.,   0.,   0.,   0.],
       [  5.,   5.,   5.,   5.,   5.],
       [ 10.,  10.,  10.,  10.,  10.]])
np.r_[0:10:1]#构造行向量
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.c_[0:10:1]#构造列向量
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])
np.zeros(3
    )
array([0., 0., 0.])
np.zeros((3,3))
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
np.ones((3,3))
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
np.empty(2)
array([5.e-324, 0.e+000])
help(np.empty)
Help on built-in function empty in module numpy:

empty(...)
    empty(shape, dtype=float, order='C', *, like=None)
    
    Return a new array of given shape and type, without initializing entries.
    
    Parameters
    ----------
    shape : int or tuple of int
        Shape of the empty array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        Desired output data-type for the array, e.g, `numpy.int8`. Default is
        `numpy.float64`.
    order : {'C', 'F'}, optional, default: 'C'
        Whether to store multi-dimensional data in row-major
        (C-style) or column-major (Fortran-style) order in
        memory.
    like : array_like
        Reference object to allow the creation of arrays which are not
        NumPy arrays. If an array-like passed in as ``like`` supports
        the ``__array_function__`` protocol, the result will be defined
        by it. In this case, it ensures the creation of an array object
        compatible with that passed in via this argument.
    
        .. versionadded:: 1.20.0
    
    Returns
    -------
    out : ndarray
        Array of uninitialized (arbitrary) data of the given shape, dtype, and
        order.  Object arrays will be initialized to None.
    
    See Also
    --------
    empty_like : Return an empty array with shape and type of input.
    ones : Return a new array setting values to one.
    zeros : Return a new array setting values to zero.
    full : Return a new array of given shape filled with value.
    
    
    Notes
    -----
    `empty`, unlike `zeros`, does not set the array values to zero,
    and may therefore be marginally faster.  On the other hand, it requires
    the user to manually set all the values in the array, and should be
    used with caution.
    
    Examples
    --------
    >>> np.empty([2, 2])
    array([[ -9.74499359e+001,   6.69583040e-309],
           [  2.13182611e-314,   3.06959433e-309]])         #uninitialized
    
    >>> np.empty([2, 2], dtype=int)
    array([[-1073741821, -1067949133],
           [  496041986,    19249760]])                     #uninitialized
arr1
array([[ 32,  23,  45],
       [123,  23,  34]])
np.zeros_like(arr1)
array([[0, 0, 0],
       [0, 0, 0]])
np.identity(5)#构造单位矩阵
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

运算

x=np.array([5,5])
y=np.array([2,3])
np.multiply(x,y)
array([ 8, 15])
np.dot(x,y)
23
np.dot(y,x)
23
x=np.array([1,2,3])
y=np.array([[4,5,6],[7,8,9]])
print(x*y)
[[ 4 10 18]
 [ 7 16 27]]
# y=np.array([1,2,3])
x==y
array([[False, False, False],
       [False, False, False]])

随机模块

np.random.rand(3,2)
array([[0.48216974, 0.23825872],
       [0.35964068, 0.40709199],
       [0.40782496, 0.9657435 ]])
np.random.randint(10,size=(5,4)) # 返回 5行 4列 10以内的随机整数矩阵
array([[7, 8, 9, 8],
       [9, 7, 6, 6],
       [1, 5, 5, 4],
       [3, 2, 7, 1],
       [6, 4, 3, 0]])
np.random.randint(0,10,3)
array([1, 3, 1])
np.random.rand()
0.31758529771887456
np.random.random_sample()
0.12935900005867884
np.set_printoptions(precision=2) #设置浮点数精度
n=np.random.normal(0,0.1,10) #高斯分布
n
array([-0.14, -0.1 ,  0.11,  0.03, -0.16,  0.13, -0.11, -0.03,  0.09,
       -0.04])
arr=np.arange(10)
arr

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.random.shuffle(arr)  # 洗牌
arr
array([0, 8, 1, 5, 9, 2, 6, 3, 7, 4])

文件读写

%%writefile tang.txt
1 2 3 4 5 6
2 3 4 5 6 7
Writing tang.txt
data=[]
with open('tang.txt') as f:
    for line in f:
        fileds=line.split()
        cur_data=[float(x) for x in fileds]
        data.append(cur_data)
data=np.array(data)
data
array([[1., 2., 3., 4., 5., 6.],
       [2., 3., 4., 5., 6., 7.]])
data2=np.loadtxt('tang.txt')
data2
array([[1., 2., 3., 4., 5., 6.],
       [2., 3., 4., 5., 6., 7.]])
%%writefile tang2.txt
1,2,3,4,5,6
2,3,4,5,6,7
Writing tang2.txt
data2=np.loadtxt('tang2.txt',delimiter=',')
data2
array([[1., 2., 3., 4., 5., 6.],
       [2., 3., 4., 5., 6., 7.]])
%%writefile tang2.txt
x,c,v,b,n,m
1,2,3,4,5,6
2,3,4,5,6,7
Overwriting tang2.txt
data2=np.loadtxt('tang2.txt',delimiter=',')
data2
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Input In [197], in <cell line: 1>()
----> 1 data2=np.loadtxt('tang2.txt',delimiter=',')
      2 data2


File /opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py:1148, in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows, like)
   1143 # read data in chunks and fill it into an array via resize
   1144 # over-allocating and shrinking the array later may be faster but is
   1145 # probably not relevant compared to the cost of actually reading and
   1146 # converting the data
   1147 X = None
-> 1148 for x in read_data(_loadtxt_chunksize):
   1149     if X is None:
   1150         X = np.array(x, dtype)


File /opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py:999, in loadtxt.<locals>.read_data(chunk_size)
    995     raise ValueError("Wrong number of columns at line %d"
    996                      % line_num)
    998 # Convert each value according to its column and store
--> 999 items = [conv(val) for (conv, val) in zip(converters, vals)]
   1001 # Then pack it according to the dtype's nesting
   1002 items = pack_items(items, packing)


File /opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py:999, in <listcomp>(.0)
    995     raise ValueError("Wrong number of columns at line %d"
    996                      % line_num)
    998 # Convert each value according to its column and store
--> 999 items = [conv(val) for (conv, val) in zip(converters, vals)]
   1001 # Then pack it according to the dtype's nesting
   1002 items = pack_items(items, packing)


File /opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py:736, in _getconv.<locals>.floatconv(x)
    734 if '0x' in x:
    735     return float.fromhex(x)
--> 736 return float(x)


ValueError: could not convert string to float: 'x'
data2=np.loadtxt('tang2.txt',delimiter=',',skiprows=1) # skiprows=1 取掉几行
data2
array([[1., 2., 3., 4., 5., 6.],
       [2., 3., 4., 5., 6., 7.]])
print(x)
print(y)
z=np.vstack((x,y))
z
[1 2 3]
[[4 5 6]
 [7 8 9]]





array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
np.savetxt('tang.txt',z)
np.savetxt('tang.txt',z,fmt='%d')
np.savetxt('tang.txt',z,fmt='%d',delimiter=',')
np.savetxt('tang.txt',z,fmt='%2f',delimiter=',')
  • 读写array结构
np.save('z_val.npy',z)
t=np.load('z_val.npy')
t
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
tarr=np.arange(10)
tarr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.savez('tarr_val.npz',a=z,b=tarr) ## 包含a.npy b.npy的压缩文件
data=np.load('tarr_val.npz')

data.keys()
KeysView(<numpy.lib.npyio.NpzFile object at 0x7fb371f30070>)
data['a']
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
data['b']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值