Numpy是数据分析的基础性第三方库,对于python的学习来说是必备的。Pandas是在Numpy的基础上,对数据再进行封装。
函数:
属性:arr是一个ndarray数组
对于python所支持的不同基础类型数据data:列表、元组,都可以用转化成Numpy形式的ndarray数据结构。
data1 = [5, 7, 9, 20] #列表
arr1 = np.array(data1)
data2 = (5, 7, 9, 20) #元组
arr2 = np.array(data2)
print(arr1)
print(arr2)
#====结果====
array([ 5, 7, 9, 20])
array([ 5, 7, 9, 20])
data3 = [[1, 2, 3, 4],[5, 6, 7, 8]] #多维数组
arr3 = np.array(data3)
print(arr3)
print(type(arr3))
#====结果====
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
numpy.adarray
data4 = np.arange(24).reshape(2,3,4)
print(data4)
#====结果====
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
print(arr3.shape)
print(arr3.dtype)
#====结果====
(2,3)
dtype('int32')
np.zeros(8)
np.zeros((3,4))
np.ones(4)
np.empty((2, 2, 2)) #非常小的值
#====结果====
array([ 0., 0., 0., 0., 0., 0., 0., 0.])
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
array([ 1., 1., 1., 1.])
array([[[0.00000000e+000, 0.00000000e+000],
[0.00000000e+000, 0.00000000e+000]],
[[0.00000000e+000, 4.07110092e-321],
[9.02193261e+217, 6.19941833e-071]]])
np.arange(10)
np.arange(5,10)
#====结果====
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array([5, 6, 7, 8, 9])
arr = np.arange(9)
arr.reshape((3,3))
print(arr)
#====结果====
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
arr = np.array([[3, 4, 5],[1, 2, 3]])
print(arr.shape)
print(arr.reshape((3,2))) #reshape()不改变原数据维度
arr
#====结果====
(2, 3)
[[3 4]
[5 1]
[2 3]]
array([[3, 4, 5],
[1, 2, 3]])
arr1 = np.arange(12)
arr2 = np.arange(10).reshape((5,2))
print(arr1.reshape((3,-1)))
print(arr2)
#====结果====
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
arr1 = np.arange(12).reshape(3,4)
arr2 = np.arange(12,24).reshape(3,4)
np.concatenate([arr1, arr2],axis=0)
#====结果====
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
np.concatenate([arr1, arr2],axis=1)
#====结果====
array([[ 0, 1, 2, 3, 12, 13, 14, 15],
[ 4, 5, 6, 7, 16, 17, 18, 19],
[ 8, 9, 10, 11, 20, 21, 22, 23]])
np.vstack((arr1,arr2))
#====结果====
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
np.hstack((arr1, arr2))
#====结果====
array([[ 0, 1, 2, 3, 12, 13, 14, 15],
[ 4, 5, 6, 7, 16, 17, 18, 19],
[ 8, 9, 10, 11, 20, 21, 22, 23]])
arr = np.arange(12).reshape((6,2))
#arr
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
np.split(arr,[2, 4])
#====结果====
[array([[0, 1],
[2, 3]]),
array([[4, 5],
[6, 7]]),
array([[ 8, 9],
[10, 11]])]
np.split(arr,[2,5])
#====结果====
[array([[0, 1],
[2, 3]]),
array([[4, 5],
[6, 7],
[8, 9]]),
array([[10, 11]])]
np.split(arr,[4,])
[array([[0, 1],
[2, 3],
[4, 5],
[6, 7]]),
array([[ 8, 9],
[10, 11]])]
#====结果====
#arr
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
arr.transpose((1,0))
#====结果====
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
arr = np.arange(24).reshape((2,3,4))
#arr
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
arr.transpose(2,0,1) #原数组是2*3*4,转换之后是4*2*3
array([[[ 0, 4, 8],
[12, 16, 20]],
[[ 1, 5, 9],
[13, 17, 21]],
[[ 2, 6, 10],
[14, 18, 22]],
[[ 3, 7, 11],
[15, 19, 23]]])