“Numpy”的版本间差异

来自Shiyin's note
跳到导航 跳到搜索
第42行: 第42行:
sys.stdout.write("\n[{0}{1}]".format('#' * n, ' ' * (30 - n)))
sys.stdout.write("\n[{0}{1}]".format('#' * n, ' ' * (30 - n)))


*savetxt
*ndarray.savetxt
可以把一个数组一次性写入某个文件,但是第一个参数貌似必须是文件名,而且不能续写,写完后文件就被关闭
可以把一个数组一次性写入某个文件,但是第一个参数貌似必须是文件名,而且不能续写,写完后文件就被关闭

*ndarray.tofile ndarray.fromfile
数组的简单存取,比较方便可以用于多维数组


==vectorize==
==vectorize==

2018年3月2日 (五) 08:12的版本

http://bigsec.net/b52/scipydoc/numpy_intro.html

ndarray

  • shape() #数组的形状,虽然len()可以运行
  • size() #数组的总元素
  • a=np,empty_like(b) #初始化一个和b数组一样shape的空数组
  • 产生序列数组 a=np.arange(20)
  • 合并两个数组 np.append(array1,array2)
  • 选择数组在某个范围之内 sel=np.where((wave < 6800) & (wave > 3800))
  • asarray
  • astype
  • clip(a,a_min,a_max)
array(a).clip(a_min.a_max)
  • reduce,reduceat
reduceat 有点复杂 参见[1]
  • unique
  • sort
  • roll #平移
  • array[::-1] 数组倒序

统计

  • percentile
wout=np.percentile(flatchain,[16,50,84],0) #flatchain 是二维数组,这是在一个维度上求其分布的范围

格式输出

x = np.random.random(10)
np.set_printoptions(precision=3, suppress=True):
print(x)
print(np.array_str(x, precision=2))
np.array2string(x, formatter={'float_kind':'{0:.3f}'.format})
print(np.vectorize("%.2f".__mod__)(x))
  • 另外一个方法
def ndprint(a, format_string ='{0:.2f}'):
   print [format_string.format(v,i) for i,v in enumerate(a)]

叠加标准输出:%r 不换行 %n 换行

for n in range(30):
   sys.stdout.write("\r[{0}{1}]".format('#' * n, ' ' * (30 - n))) 
for n in range(30):
   sys.stdout.write("\n[{0}{1}]".format('#' * n, ' ' * (30 - n)))
  • ndarray.savetxt
可以把一个数组一次性写入某个文件,但是第一个参数貌似必须是文件名,而且不能续写,写完后文件就被关闭
  • ndarray.tofile ndarray.fromfile
数组的简单存取,比较方便可以用于多维数组

vectorize

  • 可以将只能对数值计算的函数,变成可以对数组计算,比如积分
import scipy.integrate as integrate
  
    vec_expint=np.vectorize(expint) 
def expint(t1,t2):
    return integrate.quad(CSFH,t1,t2)[0]

随机数

  • random.rand(20,20)