np.random.seed(44)
a = np.random.random_integers(-4, 4, 7)
print(a)
np.sign.at(a, [2, 4])
print(a)
np.random.seed(20)
a = np.random.random_integers(0, 7, 9)
print(a)
print(np.partition(a, 4))
np.random.seed(46)
a = np.random.randn(30)
estimates = np.zeros((len(a), 3))
for i in xrange(len(a)):
b = a.copy()
b[i] = np.nan
estimates[i,] = [np.nanmean(b), np.nanvar(b), np.nanstd(b)]
print("Estimator variance", estimates.var(axis=0))
print(np.full((1, 2), 7))
print(np.full((1, 2), 7, dtype=np.int))
array([[7, 7]])
a = np.linspace(0, 1, 5)
print(a)
print(np.full_like(a, 7))
print(np.full_like(a, 7, dtype=np.int))
np.random.choice 随机选取
N = 400
np.random.seed(28)
data = np.random.binomial(5, .5, size=N)
bootstrapped = np.random.choice(data, size=(N, 30))
means = bootstrapped.mean(axis=0)
plt.title('Bootstrapping demo')
plt.grid()
plt.boxplot(means)
plt.plot(3 * [data.mean()], lw=3, label='Original mean')
plt.legend(loc='best')
plt.show()
datetime64 类型
import numpy as np
# 由年月日构造
print(np.datetime64('2015-05-21'))
# numpy.datetime64('2015-05-21')
# 去掉横杠
print(np.datetime64('20150521'))
# 由年月构造
print(np.datetime64('2015-05'))
# numpy.datetime64('20150521')
# numpy.datetime64('2015-05')
# 由日期和时间构造
local = np.datetime64('1578-01-01T21:18')
print(local)
# numpy.datetime64('1578-01-01T21:18Z')
# 可以带上偏移
with_offset = np.datetime64('1578-01-01T21:18-0800')
print(with_offset)
# numpy.datetime64('1578-01-02T05:18Z')
# datetime64 作差会生成 timedelta64
print(local - with_offset)
# numpy.timedelta64(-480,'m')