numpy学习笔记 - numpy常用函数、向量化操作及基本数学统计方法

# -*- coding: utf-8 -*-
"""
主要记录代码,相关说明采用注释形势,供日常总结、查阅使用,不定时更新。
Created on Fri Aug 24 19:57:53 2018

@author: Dev
"""

import numpy as np
import random
 
# 常用函数
arr = np.arange(10)
print(np.sqrt(arr))    # 求平方根
print(np.exp(arr))    # 求e的次方
 
random.seed(200)    # 设置随机种子
x = np.random.normal(size=(8, 8))
y = np.random.normal(size=(8, 8))
np.maximum(x, y)    # 每一位上的最大值
 
arr = np.random.normal(size=(2, 4)) * 5
print(arr)
print(np.modf(arr))    # 将小数部分与整数部分分成两个单独的数组
 
# 向量化操作
# meshgrid的基本用法
points1 = np.arange(-5, 5, 1)     # [-5, 5]闭区间步长为1的10个点
points2 = np.arange(-4, 4, 1)
xs, ys = np.meshgrid(points1, points2)    # 返回一个由xs, ys构成的坐标矩阵
print(xs)   # points1作为行向量的len(points1) * len(points2)的矩阵
print(ys)   # points2作为列向量的len(points1) * len(points2)的矩阵
 
# 将坐标矩阵经过计算后生成灰度图
import matplotlib.pyplot as plt
points = np.arange(-5, 5, 0.01)     # 生成1000个点的数组
xs, ys = np.meshgrid(points, points)
z = np.sqrt(xs ** 2 + ys ** 2)  # 计算矩阵的平方和开根
print(z)
plt.imshow(z, cmap=plt.cm.gray)
plt.colorbar()
plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")
plt.show()
 
numpy学习笔记 - numpy常用函数、向量化操作及基本数学统计方法numpy学习笔记 - numpy常用函数、向量化操作及基本数学统计方法
 
# 将条件逻辑表达为数组运算
xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])
# 纯Python,用列表解析式做判断
result = [x if c else y for x, y, c in zip(xarr, yarr, cond)]
# zip()基本用法
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
for tupple_a in zip(a, b, c):   # python 3.x为了减少内存占用,每次zip()调用只返回一个元组对象
    print(tupple_a)
 
# np.where()
result = np.where(cond, xarr, yarr)
 
 
# np.where()的用法:
numpy学习笔记 - numpy常用函数、向量化操作及基本数学统计方法numpy学习笔记 - numpy常用函数、向量化操作及基本数学统计方法
 
#np.where()的几个例子
# condition, x, y均指定
np.where([[False, True], [True, False]],
[[1, 2], [3, 4]],
[[9, 8], [7, 6]])
# 只指定condition时,返回np.nonzero(),即非零元素的索引
x = np.arange(9.).reshape(3, 3)
np.where( x > 5 )
x[np.where( x > 3.0 )]  # 将索引值带入原数组,得到满足大于3条件的元素
 
arr = np.random.normal(size=(4,4))
print(arr)
np.where(arr > 0, 2, -2)
np.where(arr > 0, 2, arr)   # 只将大于0的元素设置为2
 
# 用np.where()进行多条件判断
# 例子: 对0~100范围内的数进行判断
# 纯python
sum1 = 0
for i in range(0, 100):
    if np.sqrt(i) > 3 and np.sqrt(i) < 5:   # 平方根在(3, 5)之间
        sum1 += 3
    elif np.sqrt(i) > 3:    # 平方根在[5, 10)之间
        sum1 += 2
    elif np.sqrt(i) < 5:    # 平方根在(0, 3]之间
        sum1 += 1
    else:
        sum1 -= 1
print(sum1)
注: 这个例子其实用的不好,最后一个sum -= 1实际上没有用到,只是用这个例子说明多条件判断。
 
# 使用np.where()
num_list = np.arange(0, 100)
cond1 = np.sqrt(num_list) > 3
cond2 = np.sqrt(num_list) < 5
result = np.where(cond1 & cond2, 3, np.where(cond1, 2, np.where(cond2 < 5, 1, -1)))
print(sum(result))
 
 
# 数学与统计方法
arr = np.random.normal(size=(5, 4))
arr.mean()  # 平均值
np.mean(arr)
arr.sum()   # 求和
arr.mean(axis=1)    # 对行求平均值
arr.sum(0)  # 对每列求和
arr.sum(axis=0)
 
arr = np.arange(9).reshape(3, 3)
arr.cumsum(0)   # 每列的累计和
arr.cumprod(1) # 每行的累计积
 
注: 关于numpy中axis的问题
axis=1可理解为跨列操作
axis=0可理解为跨行操作
 
# 布尔型数组
arr = np.random.normal(size=(10, 10))
(arr > 0).sum() # 正值的数量
bools = np.array([False, False, True, False])
bools.any() # 有一个为True,则结果为True
bools.all() # 必须全为True,结果才为True
 
# 排序
arr = np.random.normal(size=(4, 4))
print(arr)
arr.sort()  # 对每行元素进行排序
 
arr = np.random.normal(size=(5, 3))
print(arr)
arr.sort(0) # 对每列元素进行排序
# 求25%分位数(排序后根据索引位置求得)
num_arr = np.random.normal(size=(1000, 1000))
num_arr.sort()
print(num_arr[0, int(0.25 * len(num_arr))])
 
# 求唯一值
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
print(np.unique(names))
# 纯Python
print(sorted(set(names)))
# 判断values中的列表元素是否在数组list_a中
arr_a = np.array([6, 0, 0, 3, 2, 5, 6])
values = [2, 3, 6]
np.in1d(arr_a, values)
 
# 线性代数相关的函数
x = np.array([[1., 2., 3.], [4., 5., 6.]])
y = np.array([[6., 23.], [-1, 7], [8, 9]])
x.dot(y)    # 矩阵的乘法
np.dot(x, y)
np.dot(x, np.ones(3))
 
np.random.seed(12345)
from numpy.linalg import inv, qr
X = np.random.normal(size=(5, 5))
mat = X.T.dot(X)    # 矩阵X转置后再与原X相乘
inv(mat)    # 求逆矩阵
mat.dot(inv(mat))   # 与逆矩阵相乘
 
 
# 随机数
samples = np.random.normal(size=(4, 4))
samples
from random import normalvariate
# normalvariate(mu,sigma)
# mu: 均值
# sigma: 标准差
# mu = 0, sigma=1: 标准正态分布
 
# 比较纯Python与numpy生成指定数量的随机数的速度
N = 1000000    # 设置随机数的数量
get_ipython().magic(u'timeit samples = [normalvariate(0, 1) for _ in range(N)]')
get_ipython().magic(u'timeit np.random.normal(size=N)')
结果:
818 ms ± 9.87 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
34.5 ms ± 164 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
 
 
# 范例:随机漫步
import random
 
# 纯Python
position = 0
walk = [position]
steps = 1000
for i in range(steps):
    step = 1 if random.randint(0, 1) else -1
    position += step
    walk.append(position)
 
# np
np.random.seed(12345)
nsteps = 1000
draws = np.random.randint(0, 2, size=nsteps)    # 取不到2
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum()   # 求累计和
walk.min()
walk.max()
 
(np.abs(walk) >= 10).argmax()   # 首次到达10的索引数
 
# 一次模拟多个随机漫步
nwalks = 5000   # 一次生成5000组漫步数据
nsteps = 1000
draws = np.random.randint(0, 2, size=(nwalks, nsteps))
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum(1) # 将5000个样本中每一步的值进行累积求和
print(walks)
 
# 计算首次到达30
hits30 = (np.abs(walks) >= 30).any(1)   # 在列方向上进行对比
print(hits30)
print(hits30.sum()) # 到达+/-30的个数
# 查看每一步中首次到达30的步数
crossing_times = (np.abs(walks[hits30]) >= 30).argmax(1)
# 求到达30的平均步数
crossing_times.mean()
# 标准正态分布
steps = np.random.normal(loc=0, scale=0.25, size=(nwalks, nsteps))
 
 
上一篇:Winfrom BackgroundWorker


下一篇:css 样式 设置图片成为圆形