NumPy 特殊数组与通用函数
# 来源:NumPy Cookbook 2e ch6
创建通用函数
from __future__ import print_function
import numpy as np
def double(a):
return 2 * a
ufunc = np.frompyfunc(double, 1, 1)
print("Result", ufunc(np.arange(4)))
勾股数
from __future__ import print_function
import numpy as np
# 勾股数是指满足 a ** 2 + b ** 2 == c ** 2 的三个数
# 我们使 a = m ** 2 - n ** 2,b = 2 * m * n
# c = m ** 2 + n ** 2,来寻找 a + b + c == 1000 的勾股数
# m 和 n 都取 0 ~ 32
m = np.arange(33)
n = np.arange(33)
# 计算 a,b 和 c
# outer 生成 a[i] op b[j] 为每个元素的矩阵
# 相当于 meshgrid 之后再逐元素操作
a = np.subtract.outer(m ** 2, n ** 2)
b = 2 * np.multiply.outer(m, n)
c = np.add.outer(m ** 2, n ** 2)
# 取符合我们条件的下标
# where 把布尔下标转换为位置下标
idx = np.where((a + b + c) == 1000)
# 验证并打印结果
np.testing.assert_equal(a[idx]**2 + b[idx]**2, c[idx]**2)
print(a[idx], b[idx], c[idx])
# [375] [200] [425]
CharArray 字符串操作
import urllib2
import numpy as np
import re
response = urllib2.urlopen('http://python.org/')
html = response.read()
html = re.sub(r'<.*?>', '', html)
carray = np.array(html).view(np.chararray)
carray = carray.expandtabs(1)
carray = carray.splitlines()
print(carray)
创建屏蔽数组
from __future__ import print_function
import numpy as np from scipy.misc
import lena
import matplotlib.pyplot as plt
lena = lena()
random_mask = np.random.randint(0, 2, size=lena.shape)
plt.subplot(221)
plt.title("Original")
plt.imshow(lena)
plt.axis('off')
masked_array = np.ma.array(lena, mask=random_mask)
print(masked_array)
plt.subplot(222)
plt.title("Masked")
plt.imshow(masked_array)
plt.axis('off')
!
忽略负数以及极值
from __future__ import print_function
import numpy as np
from matplotlib.finance
import quotes_historical_yahoo
from datetime import date
import matplotlib.pyplot as plt
def get_close(ticker):
today = date.today()
start = (today.year - 1, today.month, today.day)
quotes = quotes_historical_yahoo(ticker, start, today)
return np.array([q[4] for q in quotes])
close = get_close('AAPL')
triples = np.arange(0, len(close), 3)
print("Triples", triples[:10], "...")
signs = np.ones(len(close))
print("Signs", signs[:10], "...")
signs[triples] = -1
print("Signs", signs[:10], "...")
ma_log = np.ma.log(close * signs)
print("Masked logs", ma_log[:10], "...")
dev = close.std()
avg = close.mean()
inside = np.ma.masked_outside(close, avg - dev, avg + dev)
print("Inside", inside[:10], "...")
plt.subplot(311)
plt.title("Original")
plt.plot(close)
plt.subplot(312)
plt.title("Log Masked")
plt.plot(np.exp(ma_log))
plt.subplot(313)
plt.title("Not Extreme")
plt.plot(inside)
plt.tight_layout()
plt.show()
!
记录数组
from __future__ import print_function
import numpy as np from matplotlib.finance
import quotes_historical_yahoo
from datetime import date
tickers = ['MRK', 'T', 'VZ']
def get_close(ticker):
today = date.today()
start = (today.year - 1, today.month, today.day)
quotes = quotes_historical_yahoo(ticker, start, today)
return np.array([q[4] for q in quotes])
weights = np.recarray((len(tickers),), dtype=[('symbol', np.str_, 16),
('stdscore', float), ('mean', float), ('score', float)])
for i, ticker in enumerate(tickers):
close = get_close(ticker)
logrets = np.diff(np.log(close))
weights[i]['symbol'] = ticker
weights[i]['mean'] = logrets.mean()
weights[i]['stdscore'] = 1/logrets.std()
weights[i]['score'] = 0
for key in ['mean', 'stdscore']:
wsum = weights[key].sum()
weights[key] = weights[key]/wsum
weights['score'] = (weights['stdscore'] + weights['mean'])/2 weights['score'].sort()
for record in weights:
print("%s,mean=%.4f,stdscore=%.4f,score=%.4f" % (record['symbol'], record['mean'], record['stdscore'], record['score']))
'''
MRK,mean=0.8185,stdscore=0.2938,score=0.2177
T,mean=0.0927,stdscore=0.3427,score=0.2262
VZ,mean=0.0888,stdscore=0.3636,score=0.5561
'''