linalg
import numpy as np
a1 = np.ones((2,5))
a1
from scipy import linalg as la
arr = np.array([[1,2],[2,3]])
la.det(arr)
import numpy as np
np.linspace(1,2,10,endpoint=False)
np.fromfunction(lambda i,j:(i+1)*(j+1),(5,5))
arr = np.array([[1,2,3],[4,5,6]])
arr[0:2]
arr = np.array([[1,2,3],[4,5,6]])
arr[:,0:2]
for row in arr:
print(row)
reshape
and resize
¶arr.reshape(3,2)
arr
arr.resize(3,2)
arr
arr1 = np.arange(0,16).reshape(4,4)
arr1
arr1.reshape(2,-1)
arr.hstack
and arr.vstack
a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
np.vstack((a1,a2))
a3 = np.array([[4,5,6],[7,8,9]])
a1 + a3
a3+2
a3.sum(axis=0)
a3.argmax()
import math
import time
import numpy as np
x = np.arange(0,100,0.01)
tm1 = time.process_time()
for i,t in enumerate(x):
x[i] = math.pow(math.sin(t),2)
tm2 = time.process_time()
y = np.arange(0,100,0.01)
tn1 = time.process_time()
y = np.power(np.sin(y),2)
tn2 = time.process_time()
print('running time by math:', tm2-tm1)
print('running time by numpy:', tn2-tn1)
Series
in Pandas
¶import pandas as pd
s1 = pd.Series([1,2,3,4,'a'])
s1
s2 = pd.Series([1,2,3,'a'], index=[1,2,3,4])
s2
s3 = pd.Series([1,2,3,'SUFE'], index=['a','b','c','d'])
s3
s3['b']
data = {'B':88, 'A':90,'T':91}
index1 = ['B','A','T','H']
s4 = pd.Series(data,index1)
s4
pd.isnull(s4)
s5 = s4
s6 = s4
s4['H'] = 98
s5 + s6
data = {'names':['wang','li','yang','liu','zhou'],'salaries':[4000,5000,3000,6000,4400]}
df1 = pd.DataFrame(data)
df1
data = np.array([('wang',4000),('li',5000),('yang',3000),('liu',6000),('zhou',4400)])
df2 = pd.DataFrame(data,index=range(1,6),columns=['names','salaries'])
df2.index
df2.columns
df2.values
df2.salaries
df2['salaries']
df2.iloc[:,1]
df2.iloc[2,1]
df2.iloc[:2,1]
data = np.array([('wang',4000),('li',5000),('yang',3000),('liu',6000),('zhou',4400)])
df3 = pd.DataFrame(data,index=range(1,6),columns=['names','salaries'])
df3['names'] = 'admin'
df3
del df3['salaries']
df3
df2.salaries.min()
df2[df2.salaries>='5000']
np.eye(3)
np.eye(4,k=1)
np.eye(4,k=-1)
np.random.uniform(0,1,5)
x1 = np.random.rand(3,5)
x1
#x1.shape[0]
y1 = np.random.choice(np.arange(x1.shape[0]),2,replace=True)
y1
x1[y1]
x1[:,y1]
z1 = np.arange(5)
#z1<=2
x1[:,z1<=2]
np.where(z1<=2)
np.where(z1%2)