Numpy教程笔记:
import numpy as np
#numpy属性
print('#numpy属性')
array = np.array([[1,2,3],
[2,3,4]])
print(array)
print('number of dim',array.ndim)#number of dim 2
print('shape:',array.shape)#shape: (2, 3)
print('size',array.size)#size 6
print()
#numpy的创建array
print('numpy的创建array')
a=np.array([2,23,4],dtype=np.float32)
print(a)#[ 2. 23. 4.]
a=np.array([[2,23,4],
[2,32,4]])
print(a)
a=np.zeros((3,4))
print(a)
a=np.ones((3,4),dtype=np.int16)
print(a)
a=np.empty((3,4))
print(a)
a=np.arange(10,20,2)
print(a)
a=np.arange(12).reshape((3,4))
print(a)
a=np.linspace(1,10,5)
print(a)
a=np.linspace(1,10,6).reshape((2,3))
print(a)
print()
#numpy的基础运算
print('#numpy的基础运算')
a=np.array([10,20,30,40])
b=np.arange(4)
print(a,b)
c=a-b
print(c)
c=a+b
print(c)
c=a*b
print(c)
c=b**2
print(c)
c=10*np.sin(a)
print(c)
print(b)
print(b<3)
print(b==3)
a=np.array([[1,1],
[0,1]])
b=np.arange(4).reshape(2,2)
print(a)
print(b)
c=a*b
c_dot=np.dot(a,b)
c_dot_2=a.dot(b)
print(c)
print(c_dot)
print(c_dot_2)
a=np.random.random((2,4))
print(a)
print(np.sum(a))
print(np.min(a))
print(np.max(a))
print(np.sum(a,axis=1))#axis=1是行,axis=0是列
print(np.min(a,axis=0))
print(np.max(a,axis=1))
print()
#numpy的基础运算2
print('#numpy的基础运算2')
A=np.arange(14,2,-1).reshape((3,4))
print(A)
print(np.argmin(A))
print(np.argmax(A))
print(np.mean(A))
print(A.mean())
print(np.average(A))
print(np.median(A))
print(np.cumsum(A))#累和
print(np.diff(A))#累差
print(np.nonzero(A))#返回非零的行和列索引
print(np.sort(A))#逐行排序
print(np.transpose(A))
print((A.T).dot(A))
print(np.clip(A,5,9))#所有比5小的数变为5,比9大的数变为9,中间的数不变
print(np.mean(A,axis=0))#axis=1是行,axis=0是列
print()
#numpy的索引
print('#numpy的索引')
A=np.arange(3,15).reshape((3,4))
print(A)
print(A[2])
print(A[2][1])
print(A[2,1])
print(A[2,:])
print(A[:,1])
print(A[1,1:3])
for row in A:
print(row)
for column in A.T:
print(column)
print(A.flatten())
for item in A.flat:
print(item)
print()
#numpy的array合并
print('#numpy的array合并')
A=np.array([1,1,1])#注意:这里是3行一列!!!(3,)
B=np.array([2,2,2])#注意:这里是3行一列!!!(3,)
print(A.shape)#注意:这里是3行一列!!!(3,)
print(np.vstack((A,B)))#vertical stack,即竖方向拼接
C=np.vstack((A,B))
D=np.hstack((A,B))#horizontal stack,即横方向拼接
print(D)
print(A.shape,C.shape,D.shape)
print(A.T.shape)
print(A[np.newaxis,:].shape)
print(A[:,np.newaxis].shape)
print(A[:,np.newaxis])
A=np.array([1,1,1])[:,np.newaxis]#A=np.array([1,1,1]).reshape((3,1))
B=np.array([2,2,2])[:,np.newaxis]
print(np.hstack((A,B,A)))
C = np.concatenate((A,B,B,A),axis=0)#纵向合并
print(C)
print()
#numpy的array分割
print('#numpy的array分割')
A=np.arange(12).reshape((3,4))
print(A)
print(np.split(A,2,axis=1))#分割时axis=1分成3x2的两大块
print(np.array_split(A,3,axis=1))
print(np.vsplit(A,3))
print(np.hsplit(A,2))
print()
#numpy的copy & deep copy
print('#numpy的copy & deep copy')
a=np.arange(4)
print(a)
b=a
c=a
d=b
a[0]=0.3
print(a)
a[0]=11
print(a)
print(b)
print(b is a)
d[1:3]=[22,33]
print(a)
print(d)
print(c)
b=a.copy()#deep copy
print(b)
a[3]=44
print(b)
运行结果:
Pandas教程(待续):