Numpy简介
Numpy对象是元素具有相同类型(通常为数字)并以以整数元组作为索引的一维或多维数组。数组的索引整数元组包含的整数个数被称为数组的阶。 举个例子,数组[1,2,1]的阶是1,因为索引该数组只需一个数字就行了。而其第0维的长度为3。而对于数组[[1,2],[3,4],[5,6]]而言阶为2,因为索引这个数组需要2个数字。第0维的长度为3,第1维的长度为2。 Numpy数组的类型被称为ndarray。注意,Numpy数组并不等同于标准的Python数组。 ndarrray对象常用的几个属性如下: ndarray.ndim:数组的阶。 ndarray.shape:数组的形状,将返回一个整数元组,整数元组第n(n<阶数)个数表示第n维的长度。 ndarray.size:数组包含的所有元素的个数。 ndarray.dtype:数组元素的类型。 ndarray.itemsize:数组每个元素所占字节数,例如int32为4个字节,float64为8个字节 ndarray.data:数组在内存中储存的地址。例如<memory at 0x1075c7120>
import numpy as np
a = np.arange(15).reshape(3, 5)
print(a)
'''
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
'''
print(a.shape) #(3, 5)
print(a.ndim) #2
print(a.dtype.name) #'int64'
print(a.itemsize) #8
print(a.size) #15
print(type(a)) #<type 'numpy.ndarray'>
b = np.array([6, 7, 8])
print(b) #array([6, 7, 8])
print(type(b)) #<type 'numpy.ndarray'>