arr =np.array([[1,2,3],
[4,5,6]])
print(arr.ndim)
print(arr.shape)
print(arr.size)
arr =np.array([1,2,3],dtype=np.int/np.int32/np.float32/np.float64)
print(arr.dtype)
arr=np.zeros((2,3))
arr=np.ones((3,4),dtype=np.int16)
arr=np.empty((3,4))近似为0
arr= np.arange(10,20,2)
arr =np.arange(12).reshape((3,4))
arr = np.linspace(1,10,5)
arr = np.linspace(1,10,6).reshape((2,3))
import numpy as np
a = np.array([10,4,6,7])
b = np.arange(4)
c=a+b
c=b**2
print(a,b)
n.sin(a)
np.cos(a)
np.tan(a)
print(b<3)
a = np.array([[0,1]
,[1,0]])
b = np.arange(4).reshape((2,2))
c=a.b
c_dot=np.dot(a,b)
c_dot_2 =a.dot(b)
b = np.arange(2,14).reshape((3,4))
print(np.argmin(b))
print(np.argmax(b))
print(np.mean(b))
print(b.mean())
print(np.median(b))
print(np.cumsum(b))
print(np.diff(b))
print(np.nonzero(b))
print(np.sort(b))
print(np.transpose(b))
print(b.T)
print(b.T.dot(b))
print(np.clip(b,2,8))
print(np.mean(b,axis=0))
print(np.mean(b,axis=1))
a =np.arange(2,15).reshape((3,4))
print(a[2])
print(a[1][1])
print(a[1,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)
A= np.array([1,1,1])
print(A.shape())##(3,)
B= np.array([2,2,2])
print(np.vstack(A,B)) #vertical stack
print(np.hstack(A,B)) #horizontal stack
print(A[np.newaxis,:])
print(A[:,np.newaxis])
A= np.array([1,1,1])[:,np.newaxis]
B= np.array([2,2,2])[:,np.newaxis]
c=np.concatenate((A,B,B,A),axis=1)
a=np.arange(12).reshape((3,4))
pirnt(np.split(a,2,axis =1)) #对列进行操作
pirnt(np.split(a,3,axis =0)) #对行进行操作
pirnt(np.split(a,2,axis =1)) #对列进行操作,报错
pirnt(np.array_split(a,3,axis =1)) #对列进行操作,不等分割
print(np.vsplit(a,3))
print(np.hsplit(a,2))
a =np.arange(4)
b=a
c=a.copy()
a[0]=11
b和a一起改变(赋值一个变另一个也变)
c不变(a变c不变)
a中改变,b不变
import pandas as pd
import numpy as np
s=pd.Series([1,2,np.nan,5])
print(s)
date =pd.date_range(‘20160101’,periods=6)
print(date)
df = pd.DataFrame(np.arange(12).reshape((3,4)),index=date,columns=[‘a’,‘b’,‘c’,‘d’])
df = pd.DataFrame(np.random.randn(6,4),index=date,columns=[‘a’,‘b’,‘c’,‘d’])
df2 =pd.DataFrame({‘A’:1,
‘B’:pd.Timestamp(‘20130102’),
‘C’:pd.Series(1,index=list(range(4),dtype=‘float32’)),
‘D’:np.array([3]*4,dtype=‘int32’),
‘E’:pd.Categorical([‘test’,‘train’,‘test’,‘train’])
‘F’:‘foo’,
})