python的一些高级用法积累

一、矩阵数组运算相关的:

1. np.concatenate 数据连起来

x=np.array([[1,2],[3,4]])
y=np.concatenate(x)
[1,2,3,4]

2. np.flatnonzero

找出是那个class的index

classes=np.array([1,0,3,2,1,1,0,3])
np.flatnonzero(classes==1)
>>> array([0, 4, 5], dtype=int32)

3. np.hstack 数组加一列

x=np.array([[1,2],[2,3],[3,4]])
np.hstack([x,np.ones((len(x),1))])
>>> array([[ 1.,  2.,  1.],
       [ 2.,  3.,  1.],
       [ 3.,  4.,  1.]])

4. 正确率 np.array(x==y).sum()/len(x)

因为正确的为1,错误的为0,直接sum

num_correct=np.sum(y_label==y_test)
accuracy=float(num_correct)/n_test

直接用mean

accuracy=np.mean(y_label==y_test)

5. np.array_split(range(6),3)

range_split=np.array_split(range(6),3)
>>>[array([0, 1]), array([2, 3]), array([4, 5])]
x_folds=[ x[range_split[i] for i in range(3)]

>>>x
> [1,2,3,4,5,6]
> [[1,2],[3,4],[5,6]]

6. np.newaxis, A[range(len(A),y]

整个矩阵减去 一列*n_cols

score=np.array([[5,2,1],
               [1,1,3],
               [4,-2,3],
               [-5,1,2]])
y=np.array([2,0,0,1])
print("score=\n",score)
r=score[range(4),y]
print("\nr=",r,"\n")
print("r[:, np.newaxis]\n",r[:, np.newaxis],"\n")
print("score-r[:, np.newaxis]\n",score-r[:, np.newaxis])

score= [[ 5 2 1]
[ 1 1 3]
[ 4 -2 3]
[-5 1 2]]
r= [1 1 4 1]
r[:, np.newaxis]= [ [1]
[1]
[4]
[1]]
score-r[:, np.newaxis]=[[ 4 1 0]
[ 0 0 2]
[ 0 -6 -1]
[-6 0 1]]

二、for循环

1. for i,xx in emumerate(X)

classes=['dog','cat','fish']
for i,cls in enumerate(classes):
    i=0,1,2
    cls='dog','cat','fish'

这个比直接用for要快,下面看看例子:

x=np.linspace(1,1e5,1e7)
@time
def forf(x):
    for i in range(len(x)):
        a=x[i]
@time
def emur(x):
    for i,a in enumerate(x):
        b=a

forf(x),emur(x)
[Function forf] costs: 1.400562s
[Function emur] costs: 1.131064s

2. knn Counter(x).most_common(1)

from collections import Counter
labels=[1,2,2]
Counter(labels).most_common(1)
>>>[(2,2)]
predict_y=Counter(labels).most_common(1)[0][0]
>>>2

3. knn np.array.argsort()[:3]

np.argsort()[:3], 返回的是index

dist=np.array([3,2,5,1,2,4])
ind=dist.argsort()[:3]
>>> ind=array([3, 1, 4], dtype=int32)
print([dist[i] for i in ind])
>>>[1,2,2]

path.join(dir,fname)

os.path.join(dir,filename)

计时 time_function(f,*args)

def time_function(f,*args):
    import time
    tic=time.time()
    f(*args)
    toc=time.time()
    return toc-tic

 def add(x,y):
     for i in range(1000):
         z=np.exp(x**2)/y
         for j in range(200):
             z=np.tanh(z**2)*np.sin(x+y)
     return z

 print("time for add func: ",time_function(add,10.1,2.6))
 >>> time for add func: 0.422592639923095

np.random.choice(10,2,replace=False)

np.random.choice(10,2,replace=False)
>>> array([4,2])
从一个数组中随机挑选m个元素
>>> mask=np.random.choice(len(x),m,replace=False)
>>> x[mask]

函数参数

位置参数 f(a,b)
默认参数 f(x,n=None)
可变参数,关键字参数 fun(*args,**kw)

def op(*elem,**scale):
    '''
    不定参数 arguments:elem
    关键字参数keyword
    '''
    print(elem,scale)
    ret=sum(elem)
    if "s" in scale:
        ret*=scale["s"]
    return ret
op(1,2,3,4,s=10,a=2)

(1, 2, 3, 4) {’s’: 10, ‘a’: 2}

100

你可能感兴趣的:(python)