python-基础语法-numpy

参考声明:https://blog.csdn.net/jningwei/article/details/78811259

 

1.numpy.reshape

numpy.reshape(a,newshape,order='C')

其中,newshape为 int or tuple of ints类型,其中一维可以是-1。

Parameters:

a : array_like

Array to be reshaped.

newshape : int or tuple of ints

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

order : {‘C’, ‘F’, ‘A’}, optional

Read the elements of a using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. ‘A’ means to read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

Returns:

reshaped_array : ndarray

This will be a new view object if possible; otherwise, it will be a copy. Note there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.

例如:

我们有一个2*3维度的数组,想要reshape成3*2维度的数组。

a = np.array([[1,2,3], [4,5,6]])
np.reshape(a,(3,-1))
print(a)
#result:
#[[1 2 3]
# [4 5 6]]

   其中,newshape的第二位指定为-1,这样reshape()函数会根据实际元素数量,自动计算出缺省维度。

 

2.numpy.asarray():

np.asarray(a, dtype=None, order=None)

将结构数据a,转化为ndarray。

Parameters:

a : array_like

Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.

dtype : data-type, optional

By default, the data-type is inferred from the input data.

order : {‘C’, ‘F’}, optional

Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to ‘C’.

Returns:

out : ndarray

Array interpretation of a. No copy is performed if the input is already an ndarray with matching dtype and order. If a is a subclass of ndarray, a base class ndarray is returned.

注意:np.asarray() 和 np.array()存在差别:

从定义上看:

def asarray(a, dtype=None, order=None):
    return array(a, dtype, copy=False, order=order)

def array(a, dtype=None, order=None):
    return array(a, dtype, copy=True, order=order)

主要区别在于 np.array (默认情况下)将会copy该对象,

而 np.asarray 除非必要,否则不会copy该对象。

但是,如果np.asarray(a, dtype=None, order=None)中,a 的类型与dtype指定的不匹配,这时候会copy对象。

# -*- coding:utf-8 -*-
import numpy as np

a = np.arange(5,dtype=np.float32)
b = np.array(a)
c = np.asarray(a)
d = np.asarray(a,dtype=np.float64)

print(b is a )
#result:False
print(c is a )
#result:True
print(d is a)
#result:False

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值