折叠
展开

numpy的常规操作大全

060: lambda 表达式

lambda的使用
def add(x,y):
    return x+y
add(2,4)
6
add_lambda = lambda x,y:x+y
add_lambda(3,4)
7
三元运算符
condition =True
1 if condition else 2
1

061: map 函数的应用

map函数的应用
list_=[1,2,3,4,5]
r = map(lambda x: x+x,list_)
list(r)
[2, 4, 6, 8, 10]
m1=map(lambda x,y:x*x+y ,[1,2,3,4,5],[1,2,3,43,1])
list(m1)
[2, 6, 12, 59, 26]

062: filter 过滤器

filter过滤器
def is_not_none(s):
    return s and len(s.strip())>0

list2 = ['','hello','word',None,'ai']
result = filter(is_not_none,list2)
list(result)
['hello', 'word', 'ai']

063: reduce 函数

reduce函数

from functools import reduce

f = lambda x,y:x+y
r = reduce(f,[1,2,3,4,5],100)
r

064: python 三大推导式#### 列表推导式

list1 = [1,2,3,4,5,6]
f = map(lambda x:x+x,list1)
print(list(f))

list2 = [i+1 for i in list1]
print(list2)

list3 = [i**3 for i in list1]
print(list3)

# 有选择的筛选
list4 = [i*i for i in list1 if i>3]
print(list4)
[2, 4, 6, 8, 10, 12]
[2, 3, 4, 5, 6, 7]
[1, 8, 27, 64, 125, 216]
[16, 25, 36]

字典推导式

s={
    'name':'zhansan',
    'age':21
}

s_key =[key for key,value in s.items()]
s_key
['name', 'age']
# 字典反转
ss={value:key for key,value in s.items()}
ss
{'zhansan': 'name', 21: 'age'}

065: 闭包

闭包

import time
def runtime():
    def now_time():
        print(time.time())
    return now_time
f = runtime()
f()
1584842636.908828
# 读出一个文件中带有关键字的行
def make_filter(keep):
    def the_filter(filename):
        file = open(filename)
        lines = file.readlines()
        file.close()
        filter_doc = [i for i in lines if keep in i]
        return filter_doc
    return the_filter
filter1 = make_filter('c')

filter_result = filter1('data.csv')
filter_result
['a,b,c,d,e\n']

066: 装饰器一

装饰器,语法糖,注解

# 获取每一个函数运行的时间
import time

def runtime(func):
    def get_time():
        print(time.time())
        func()
    return get_time
@runtime
def student_run():
    print("学生跑")
    
student_run()
1584843414.625161
学生跑

067: 装饰器二

# 带参数的
def runtime(func):
    def get_time(*args):
        print(time.time())
        func(*args)
    return get_time
@runtime
def student_run(i,j):
    print("学生跑")
    
@runtime
def student_run1(i):
    print("学生跑") 
    
student_run(1,3)
student_run1(34)
1584843680.2516952
学生跑
1584843680.252018
学生跑
# 关键字参数
def runtime(func):
    def get_time(*args,**kwargs):
        print(time.time())
        func(*args,**kwargs)
    return get_time
    
@runtime
def student_run(*args,**kwargs):
    print("学生跑") 
    
student_run(1,i=3)

1584843867.901767
学生跑

068: 初识 numpy

numpy其实就是一个多维数组

import numpy as np
data =[1,2,3,4,5,6]
n = np.array(data*10)
print(data)
print(n)
[1, 2, 3, 4, 5, 6]
[1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1
 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6]

069: numpy 数组的创建

每一个np的数组都有一个shape和dtype的方法

n.shape
(60,)
n.dtype
dtype('int64')

嵌套序列:由一组等长列表组成的列表

arr = [[1,2,3,4],[5,6,7,8]]
arr2=np.array(arr)
print(arr2)
print(arr2.ndim)
print(arr2.shape)
[[1 2 3 4]
 [5 6 7 8]]
2
(2, 4)

np对数据类型的一个判断

arr = [['1',2,'3',4],['5',6,'7',8]]
arr2=np.array(arr)
print(arr2)
print(arr2.ndim)
print(arr2.shape)
print(arr2.dtype)
# 但成员有一个为float时,那么numnpy将会推断称为float64
[['1' '2' '3' '4']
 ['5' '6' '7' '8']]
2
(2, 4)
<U1
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.ones((2,3))
array([[1., 1., 1.],
       [1., 1., 1.]])
np.empty((2,3,4))
array([[[-0.00000000e+000, -0.00000000e+000,  6.42285340e-323,
          0.00000000e+000],
        [ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,
          0.00000000e+000],
        [ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,
          0.00000000e+000]],

       [[ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,
          1.96087885e+243],
        [-0.00000000e+000, -2.68679727e+154,  2.16209973e+233,
          6.32672818e+180],
        [ 4.74483502e+170,  1.48344189e+295, -0.00000000e+000,
          2.32036341e+077]]])
np.arange(10) # arahen
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr = np.array([1,2,3,4,5,-2.3,-5.8])
print(arr)
print(arr.dtype)
print(arr.astype(np.int32))
[ 1.   2.   3.   4.   5.  -2.3 -5.8]
float64
[ 1  2  3  4  5 -2 -5]

numpy数组索引和切片

arr =np.arange(10)
print(arr)
print(arr[1])
print(arr[4:])
[0 1 2 3 4 5 6 7 8 9]
1
[4 5 6 7 8 9]
arr[0:4]=11
print(arr)
[11 11 11 11  4  5  6  7  8  9]
arr_copy=arr.copy()
arr_copy[4:]=100
print(arr)
print(arr_copy)
[11 11 11 11  4  5  6  7  8  9]
[ 11  11  11  11 100 100 100 100 100 100]
# 一个二维数组的访问方式
arr1= np.array([[1,2,3],[4,5,6]])
print(arr1[0][1])
2
names=np.array(['Tony','Jack','Robin'])
print(names=='Tony')
print((names=='Tony') & (names=='Jack'))  # and(嵌套第一个的鹅,在进行比较)
print((names=='Tony') | (names=='Jack'))  # or
[ True False False]
[False False False]
[ True  True False]

070: numpy 的矢量化运算

矢量化

arr1=np.array([1,2,3,4])
arr2=np.array([5,6,7,8])
arr1+arr2
array([ 6,  8, 10, 12])
arr1=np.array([[1,2,3,4],[1,2,3,4]])
arr2=np.array([[5,6,7,8],[5,6,7,8]])
arr1+arr2

array([[ 6,  8, 10, 12],
       [ 6,  8, 10, 12]])
arr1-arr2
array([[-4, -4, -4, -4],
       [-4, -4, -4, -4]])
arr1/arr2
array([[0.2       , 0.33333333, 0.42857143, 0.5       ],
       [0.2       , 0.33333333, 0.42857143, 0.5       ]])
5*arr1
array([[ 5, 10, 15, 20],
       [ 5, 10, 15, 20]])

071: numpy 的花式索引

花式索引(Fancy indexing)它是一个numpyz中术语,指的是利用整数数组进行选索引

arr=np.empty((4,6))
for i in range(4):
    arr[i]=i
arr
array([[0., 0., 0., 0., 0., 0.],
       [1., 1., 1., 1., 1., 1.],
       [2., 2., 2., 2., 2., 2.],
       [3., 3., 3., 3., 3., 3.]])
print(arr[[2,3,1]])
# 以一个特定的顺序来选取行中的子集,传入一个用于指定顺序的整数列表,或者数组,也可以用复数索引从末尾获取
[[2. 2. 2. 2. 2. 2.]
 [3. 3. 3. 3. 3. 3.]
 [1. 1. 1. 1. 1. 1.]]
arr=np.arange(32).reshape((8,4))
print(arr)
print(arr[[1,4,6,2]])
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]
 [24 25 26 27]
 [28 29 30 31]]
[[ 4  5  6  7]
 [16 17 18 19]
 [24 25 26 27]
 [ 8  9 10 11]]
print(arr[[1,4,6,2],[0,3,1,2]]) # 纵横交错的取值
print(arr[[1,4,6,2]][:,[0,3,1,2]]) # 取值
[ 4 19 25 10]
[[ 4  7  5  6]
 [16 19 17 18]
 [24 27 25 26]
 [ 8 11  9 10]]

072: numpy 数组转置和轴对换,

数组转置和🥣兑换

转置就是重塑数组的一个特殊的形式,常用的方式T和transpose

arr = np.arange(15).reshape(3,5)
arr
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
arr.transpose()
array([[ 0,  5, 10],
       [ 1,  6, 11],
       [ 2,  7, 12],
       [ 3,  8, 13],
       [ 4,  9, 14]])
arr.T
array([[ 0,  5, 10],
       [ 1,  6, 11],
       [ 2,  7, 12],
       [ 3,  8, 13],
       [ 4,  9, 14]])
arr = np.arange(24).reshape(2,3,4)
print(arr)
print(arr.transpose((2,0,1)))
print(arr.T)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
[[[ 0  4  8]
  [12 16 20]]

 [[ 1  5  9]
  [13 17 21]]

 [[ 2  6 10]
  [14 18 22]]

 [[ 3  7 11]
  [15 19 23]]]
[[[ 0 12]
  [ 4 16]
  [ 8 20]]

 [[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]]

073: 条件逻辑转数组

条件逻辑转数组

np.where等同于x if condition else y 矢量化的一个版本 x if condition else y表示的意思是当条件condition成立时,表达式的返回值x,不成立时返回y

x_arr = np.array([1.1,1.2,1.3])
y_arr=np.array([2.1,2.2,2.3])
condition=np.array([True,False,True])
result = [(x if c else y) for x,y,c in zip(x_arr,y_arr,condition)]
result
[1.1, 2.2, 1.3]
r=np.where(condition,x_arr,y_arr)
r
array([1.1, 2.2, 1.3])

074: 数学运算与排序

# 值替换
arr=np.random.randn(4,4)
print(arr)
np.where(arr>0,2,-2)
[[-0.03000592 -0.35160803 -0.82858383 -1.71698228]
 [ 1.22366972 -0.46774521 -0.51781653 -1.76051147]
 [ 0.20241468  0.40203276  0.65073942  1.21851974]
 [-1.00625164 -1.04714857  0.12790317  0.81810531]]





array([[-2, -2, -2, -2],
       [ 2, -2, -2, -2],
       [ 2,  2,  2,  2],
       [-2, -2,  2,  2]])
np.where(arr>0,2,arr)
array([[-0.03000592, -0.35160803, -0.82858383, -1.71698228],
       [ 2.        , -0.46774521, -0.51781653, -1.76051147],
       [ 2.        ,  2.        ,  2.        ,  2.        ],
       [-1.00625164, -1.04714857,  2.        ,  2.        ]])

075: numpy 文件处理

numpy的文件操作

numpy可以读写磁盘上的文本数据或者二进制数据,主要用用的函数就是np.save()和np.load,默认是为压缩的二进制数据

import numpy as np
arr=np.arange(10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.save('array',arr)
np.load('array.npy')
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

保存压缩文件

arr=np.arange(100)
np.savez('array_tar',a=arr) # 保存成一个压缩文件,数组以相关键字参数的形式传入
np.load('array_tar.npz')['a']
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
np.savetxt('array.txt',arr,delimiter=',')
np.loadtxt('array.txt')
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
       13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.,
       26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38.,
       39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51.,
       52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64.,
       65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77.,
       78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90.,
       91., 92., 93., 94., 95., 96., 97., 98., 99.])

076: 线性代数函数和随机漫步例子

numpy的文件操作

numpy可以读写磁盘上的文本数据或者二进制数据,主要用用的函数就是np.save()和np.load,默认是为压缩的二进制数据

import numpy as np
arr=np.arange(10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.save('array',arr)
np.load('array.npy')
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

保存压缩文件

arr=np.arange(100)
np.savez('array_tar',a=arr) # 保存成一个压缩文件,数组以相关键字参数的形式传入
np.load('array_tar.npz')['a']
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
np.savetxt('array.txt',arr,delimiter=',')
np.loadtxt('array.txt')
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
       13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.,
       26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38.,
       39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51.,
       52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64.,
       65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77.,
       78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90.,
       91., 92., 93., 94., 95., 96., 97., 98., 99.])

线性代数

dot矩阵的惩罚运算

trace矩阵计算对角线元素和

det计算矩阵的行列式

eig计算特征和向量

inv矩阵的逆

x=np.array([[1,2,3],[4,5,6]])
y=np.array([[1,2],[3,4],[5,6]])
x.dot(y)
array([[22, 28],
       [49, 64]])

随机漫步的例子

# 求什么时候第一次, 距离初始点10步远
position=0 # 初始化一个位置的值
walk = [position] # 步值初始化
steps = 1000 # 步数设置
for i in range(steps):
    step = 1 if np.random.randint(0,2) else -1
    position+=step
    walk.append(position)
    
from matplotlib import pyplot as plt
plt.plot(walk)
[<matplotlib.lines.Line2D at 0x1185b3a50>]

(np.abs(walk)>10).argmax()
69

参考https://note.youdao.com/ynoteshare1/index.html?id=802ae07676c08efb7247b510d9678391&type=note

posted on 2020-03-22 16:03  TuringEmmy  阅读(159)  评论(0编辑  收藏  举报

导航