数组属性:ndarray.ndim, ndarray.shape, ndarray.reshape
import numpy as np
#数组属性
#ndarray.ndim 用于返回数组的维数,等于秩。
a=np.arange(24)
print(a.ndim)
b=a.reshape(2,4,3)
print(b.ndim)
print(b)
#输出为:
# =============================================================================
# 1
# 3
# [[[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]]
#
# [[12 13 14]
# [15 16 17]
# [18 19 20]
# [21 22 23]]]
# =============================================================================
#ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
#比如,一个二维数组,其维度表示"行数"和"列数"。
a = np.array([[1,2,3],[4,5,6]])
print (a.shape)
#调整数组大小
a.shape=(3,2)
print(a)
#输出为:
# =============================================================================
# (2, 3)
# [[1 2]
# [3 4]
# [5 6]]
# =============================================================================
2.创建数组
import numpy as np
#creat a empty array
#numpy.empty 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组
a=np.empty((2,3),dtype=int,order="C")
print(a)
# numpy.zeros创建指定大小的数组,数组元素以 0 来填充:
b=np.zeros((2,3),dtype=np.int,order="C")
print(b)
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])
print(z)
x=np.ones((2,10),dtype=int)
print(x)
#输出为:
# =============================================================================
# [[0 0 0]
# [0 0 0]]
# [[0 0 0]
# [0 0 0]]
# [[(0, 0) (0, 0)]
# [(0, 0) (0, 0)]]
# [[1 1 1 1 1 1 1 1 1 1]
# [1 1 1 1 1 1 1 1 1 1]]
# =============================================================================
从已有数组创建数组
import numpy as np
#从已有数组创建数组
a=[1,2,3,4]
a1=np.asarray(a,dtype=float)
print(a1)
print(a1.reshape((2,2)))
#[1. 2. 3. 4.]
#[[1. 2.]
# [3. 4.]]
#numpy.frombuffer用于实现动态数组.接受 buffer 输入参数,以流的形式读入转化成 ndarray对象
s=b"hello"
a=np.frombuffer(s,dtype="S1")
print(a)
#[b'h' b'e' b'l' b'l' b'o']
#numpy.fromiter 方法从可迭代对象中建立 ndarray 对象,返回一维数组。
a=range(5)
print(a)
lista=iter(a)
print(lista)
b=np.fromiter(lista,dtype=int)
print(b)
#range(0, 5)
#<range_iterator object at 0x0000000009345790>
#[0 1 2 3 4]
从数值范围创建数组
import numpy as np
#从数值范围创建数组
#numpy.arange根据start与stop指定的范围以及step设定的步长,生成一个ndarray。
a=np.arange(10,20,2,dtype=float)
print(a)
#numpy.linspace函数用于创建一个一维数组,数组是一个等差数列构成的,格式如下:
#np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
b=np.linspace(1,20,dtype=float).reshape((5,-1))
print(b)
#等比数列
d=np.logspace(1,3,10,dtype=float).reshape((2,5))
print(d)
#numpy.logspace 函数用于创建一个于等比数列。格式如下:
e=np.logspace(1,3,10,base=2,dtype=float).reshape((2,5))
print(e)
#[10. 12. 14. 16. 18.]
#[[ 1. 1.3877551 1.7755102 2.16326531 2.55102041 2.93877551
# 3.32653061 3.71428571 4.10204082 4.48979592]
# [ 4.87755102 5.26530612 5.65306122 6.04081633 6.42857143 6.81632653
# 7.20408163 7.59183673 7.97959184 8.36734694]
# [ 8.75510204 9.14285714 9.53061224 9.91836735 10.30612245 10.69387755
# 11.08163265 11.46938776 11.85714286 12.24489796]
# [12.63265306 13.02040816 13.40816327 13.79591837 14.18367347 14.57142857
# 14.95918367 15.34693878 15.73469388 16.12244898]
# [16.51020408 16.89795918 17.28571429 17.67346939 18.06122449 18.44897959
# 18.83673469 19.2244898 19.6122449 20. ]]
#[[ 10. 16.68100537 27.82559402 46.41588834 77.42636827]
# [ 129.1549665 215.443469 359.38136638 599.48425032 1000. ]]
#[[2. 2.33305808 2.72158 3.1748021 3.70349885]
# [4.32023896 5.0396842 5.87893797 6.85795186 8. ]]
参考:https://www.runoob.com/numpy/numpy-array-from-numerical-ranges.html
以上,记录本人学习过程