#简单好用的python(速度慢)+ 速度较快的numpy(基于C语言)
#在numpy中,创建二维数组的默认方式为“C-type”,以行序在内存中存储数据
#如果是“Fortran”方式创建,是以列序在内存中存储
import numpy as np
import pandas as pd
import time
row_major = np.zeros((10,10),order = 'C') #C-type,按行序合并(axis=0),较快
col_major = np.zeros((10,10),order = 'F') #Fortran,按列序合并(axis=1),较快
a = np.zeros((200,200),order ='C')
b = np.zeros((200,200),order ='F')
N = 9999
def f1(a):
for _ in range(N):
np.concatenate((a,a),axis = 0)
def f2(b):
for _ in range(N):
np.concatenate((b,b),axis = 0)
t0 = time.time()
f1(a)
t1 = time.time()
f2(b)
t2 = time.time()
print((t1-t0)/N)
print((t2-t1)/N)
#################
np.vstack((a,a))#与concatenate相比,较慢
np.concatenate((a,a),axis = 0) #相对于vstack而言,较快
#################
indices = np.random.randint(0,100,size = 10)
a[indices,:] #以行序存储数据,在行上创建数据要更快
a[:,indices] #(1)concatenate 优于 vstack (2)axis = 0,order = 'C'更优
#axis = 1, order = 'F'更优(3)行序,a[indices,:]更优
#列序,a[:,indices]更优
#view 优于copy,view是取源数据的索引部分,copy是将数据复制出来,存到内存的另一个地方
a = np.arange(1,7).reshape((3,2))
a_view = a[:2]
a_copy = a[:2].copy()
a_copy[1,1] = 0
print(a)#源数据不变
a_view[1,1] = 0
print(a)#源数据改变
a = np.zeros((1000,1000))
b = np.zeros((1000,1000))
N = 9999
def f1(a):
for _ in range(N):
a *= 2 #等同于a[:] *= 2
def f2(b):
for _ in range(N):
b = 2*b
t0 = time.time()
f1(a)
t1 = time.time()
f2(b)
t2 = time.time()
print('%f' %((t1-t0)/N)) #速度更快
print('%f' %((t2-t1)/N))
#在矩阵展平的时候,np.ravel()返回的是一个view索引,np.flatten()
#返回的是一个copy,ravel的速度更快
def f1(a):
for _ in range(N):
a.flatten()
def f2(b):
for _ in range(N):
b.ravel()
#以下都是使用view的方式
a_view1 = a[1:2,3:6] #切片slice
a_view2 = a[:100] #同上
a_view3 = a[::2] #跳步
a_view4 = a.ravel()#矩阵展平
#以下都是使用copy的方式
a_copy1 = a[[1,4,6],[2,4,6]] #用index选择
a_copy2 = a[[True,True],[False,True]] #用mask
a_copy3 = a[[1,2],:]
a_copy4 = a[a[1,:] != 0,:] #fancy indexing
a_copy5 = a[np.isnan(a),:] #fancy indexing
#用np.take()选择数据比用index要快
a = np.random.rand(1000000,10)
N = 99
indices = np.random.randint(0,1000000,size = 10000)
def f1(a):
for _ in range(N):
_ = np.take(a,indices,axis = 0) #快
def f2(b):
for _ in range(N):
_ = b[indices]
#用np.compress()代替mask
mask = a[:,0] < 0.5
def f1(a):
for _ in range(N)
_ = np.compress(mask,a,axis = 0)#快
def f2(b):
for _ in range(N):
_ = b[mask]
#在numpy中,创建二维数组的默认方式为“C-type”,以行序在内存中存储数据
#如果是“Fortran”方式创建,是以列序在内存中存储
import numpy as np
import pandas as pd
import time
row_major = np.zeros((10,10),order = 'C') #C-type,按行序合并(axis=0),较快
col_major = np.zeros((10,10),order = 'F') #Fortran,按列序合并(axis=1),较快
a = np.zeros((200,200),order ='C')
b = np.zeros((200,200),order ='F')
N = 9999
def f1(a):
for _ in range(N):
np.concatenate((a,a),axis = 0)
def f2(b):
for _ in range(N):
np.concatenate((b,b),axis = 0)
t0 = time.time()
f1(a)
t1 = time.time()
f2(b)
t2 = time.time()
print((t1-t0)/N)
print((t2-t1)/N)
#################
np.vstack((a,a))#与concatenate相比,较慢
np.concatenate((a,a),axis = 0) #相对于vstack而言,较快
#################
indices = np.random.randint(0,100,size = 10)
a[indices,:] #以行序存储数据,在行上创建数据要更快
a[:,indices] #(1)concatenate 优于 vstack (2)axis = 0,order = 'C'更优
#axis = 1, order = 'F'更优(3)行序,a[indices,:]更优
#列序,a[:,indices]更优
#view 优于copy,view是取源数据的索引部分,copy是将数据复制出来,存到内存的另一个地方
a = np.arange(1,7).reshape((3,2))
a_view = a[:2]
a_copy = a[:2].copy()
a_copy[1,1] = 0
print(a)#源数据不变
a_view[1,1] = 0
print(a)#源数据改变
a = np.zeros((1000,1000))
b = np.zeros((1000,1000))
N = 9999
def f1(a):
for _ in range(N):
a *= 2 #等同于a[:] *= 2
def f2(b):
for _ in range(N):
b = 2*b
t0 = time.time()
f1(a)
t1 = time.time()
f2(b)
t2 = time.time()
print('%f' %((t1-t0)/N)) #速度更快
print('%f' %((t2-t1)/N))
#在矩阵展平的时候,np.ravel()返回的是一个view索引,np.flatten()
#返回的是一个copy,ravel的速度更快
def f1(a):
for _ in range(N):
a.flatten()
def f2(b):
for _ in range(N):
b.ravel()
#以下都是使用view的方式
a_view1 = a[1:2,3:6] #切片slice
a_view2 = a[:100] #同上
a_view3 = a[::2] #跳步
a_view4 = a.ravel()#矩阵展平
#以下都是使用copy的方式
a_copy1 = a[[1,4,6],[2,4,6]] #用index选择
a_copy2 = a[[True,True],[False,True]] #用mask
a_copy3 = a[[1,2],:]
a_copy4 = a[a[1,:] != 0,:] #fancy indexing
a_copy5 = a[np.isnan(a),:] #fancy indexing
#用np.take()选择数据比用index要快
a = np.random.rand(1000000,10)
N = 99
indices = np.random.randint(0,1000000,size = 10000)
def f1(a):
for _ in range(N):
_ = np.take(a,indices,axis = 0) #快
def f2(b):
for _ in range(N):
_ = b[indices]
#用np.compress()代替mask
mask = a[:,0] < 0.5
def f1(a):
for _ in range(N)
_ = np.compress(mask,a,axis = 0)#快
def f2(b):
for _ in range(N):
_ = b[mask]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import numpy as np
np.add(a,1,out = a) #如果已经有了一个placeholder(比如a),没有必要再复制一个
#速度最快
a += 1 #速度次之
a = np.add(a,1) #速度再次之
a = a+1 #速度最慢
a = np.zeros(3,dtype = [('foo',np.int32),('bar',np.float16)])
b = pd.DataFrame(np.zeros((3,2),dtype=np.int32),columns = ['foo','bar'])
b['bar'] = b['bar'].astype(np.float16)
def f1(a):
for _ in range(N):
a['bar'] *= a['foo']#numpy比较快
def f2(b):
for _ in range(N):
b['bar'] *= b['foo']#pandas比较慢