分页: 1 / 1

100道练习带你玩转Numpy

发表于 : 周四 7月 30, 2020 9:58 pm
BG6RSH
Numpy是用Python做数据分析所必须要掌握的基础库之一,它可以用来存储和处理大型矩阵,并且Numpy提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库,专为进行严格的数字处理而产生。

本文内容由科赛网翻译整理自Github开源项目(部分题目保留了原文作参考),建议读者完成科赛网 Numpy快速上手指南 --- 基础篇 和 Numpy快速上手指南 --- 进阶篇 这两篇教程的学习之后,再对此教程进行调试学习。



以下为100道Numpy习题及答案
  1. 1. 导入numpy库并简写为 np
  2.  
  3. (提示: importas)
  4.  
  5. In [ ]:
  6.  
  7. # import numpy as np
  8.  
  9. 2. 打印numpy的版本和配置说明
  10.  
  11. (提示: np.__version__, np.show_config)
  12.  
  13. In [ ]:
  14.  
  15. # print(np.__version__)
  16.  
  17. # np.show_config()
  18.  
  19. 3. 创建一个长度为10的空向量
  20.  
  21. (提示: np.zeros)
  22.  
  23. In [ ]:
  24.  
  25. # Z = np.zeros(10)
  26.  
  27. # print(Z)
  28.  
  29. 4. 如何找到任何一个数组的内存大小?
  30.  
  31. (提示: size, itemsize)
  32.  
  33. In [ ]:
  34.  
  35. # Z = np.zeros((10,10))
  36.  
  37. # print("%d bytes" % (Z.size * Z.itemsize))
  38.  
  39. 5. 如何从命令行得到numpy中add函数的说明文档?
  40.  
  41. (提示: http://np.info)
  42.  
  43. In [ ]:
  44.  
  45. # http://numpy.info(numpy.add)
  46.  
  47. 6. 创建一个长度为10并且除了第五个值为1的空向量
  48.  
  49. (提示: array[4])
  50.  
  51. In [ ]:
  52.  
  53. # Z = np.zeros(10)
  54.  
  55. # Z[4] = 1
  56.  
  57. # print(Z)
  58.  
  59. 7. 创建一个值域范围从1049的向量
  60.  
  61. (提示: np.arange)
  62.  
  63. In [ ]:
  64.  
  65. # Z = np.arange(10,50)
  66.  
  67. # print(Z)
  68.  
  69. 8. 反转一个向量(第一个元素变为最后一个)
  70.  
  71. (提示: array[::-1])
  72.  
  73. In [ ]:
  74.  
  75. # Z = np.arange(50)
  76.  
  77. # Z = Z[::-1]
  78.  
  79. # print(Z)
  80.  
  81. 9. 创建一个 3x3 并且值从08的矩阵
  82.  
  83. (提示: reshape)
  84.  
  85. In [ ]:
  86.  
  87. # Z = np.arange(9).reshape(3,3)
  88.  
  89. # print(Z)
  90.  
  91. 10. 找到数组[1,2,0,0,4,0]中非0元素的位置索引
  92.  
  93. (提示: np.nonzero)
  94.  
  95. In [ ]:
  96.  
  97. # nz = np.nonzero([1,2,0,0,4,0])
  98.  
  99. # print(nz)
  100.  
  101. 11. 创建一个 3x3 的单位矩阵
  102.  
  103. (提示: np.eye)
  104.  
  105. In [ ]:
  106.  
  107. # Z = np.eye(3)
  108.  
  109. # print(Z)
  110.  
  111. 12. 创建一个 3x3x3的随机数组
  112.  
  113. (提示: np.random.random)
  114.  
  115. In [ ]:
  116.  
  117. # Z = np.random.random((3,3,3))
  118.  
  119. # print(Z)
  120.  
  121. 13. 创建一个 10x10 的随机数组并找到它的最大值和最小值
  122.  
  123. (提示: min, max)
  124.  
  125. In [ ]:
  126.  
  127. # Z = np.random.random((10,10))
  128.  
  129. # Zmin, Zmax = Z.min(), Z.max()
  130.  
  131. # print(Zmin, Zmax)
  132.  
  133. 14. 创建一个长度为30的随机向量并找到它的平均值
  134.  
  135. (提示: mean)
  136.  
  137. In [ ]:
  138.  
  139. # Z = np.random.random(30)
  140.  
  141. # m = Z.mean()
  142.  
  143. # print(m)
  144.  
  145. 15.创建一二维数组,其中边界值为1,其余值为0
  146.  
  147. (提示: array[1:-1, 1:-1])
  148.  
  149. In [ ]:
  150.  
  151. # Z = np.ones((10,10))
  152.  
  153. # Z[1:-1,1:-1] = 0
  154.  
  155. # print(Z)
  156.  
  157. 16. 对于一个存在在数组,如何添加一个用0填充的边界?
  158.  
  159. (提示: np.pad)
  160.  
  161. In [ ]:
  162.  
  163. # Z = np.ones((5,5))
  164.  
  165. # Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
  166.  
  167. # print(Z)
  168.  
  169. 17. 以下表达式运行的结果分别是什么?
  170.  
  171. (提示: NaN = not a number, inf = infinity)
  172.  
  173. 0*np.nan
  174.  
  175. np.nan==np.nan
  176.  
  177. np.inf>np.nan
  178.  
  179. np.nan-np.nan
  180.  
  181. 0.3==3*0.1
  182.  
  183. In [ ]:
  184.  
  185. # print(0 * np.nan)
  186.  
  187. In [ ]:
  188.  
  189. # print(np.nan == np.nan)
  190.  
  191. In [ ]:
  192.  
  193. # print(np.inf > np.nan)
  194.  
  195. In [ ]:
  196.  
  197. # print(np.nan - np.nan)
  198.  
  199. In [ ]:
  200.  
  201. # print(0.3 == 3 * 0.1)
  202.  
  203. 18. 创建一个 5x5的矩阵,并设置值1,2,3,4落在其对角线下方位置
  204.  
  205. (提示: np.diag)
  206.  
  207. In [ ]:
  208.  
  209. # Z = np.diag(1+np.arange(4),k=-1)
  210.  
  211. # print(Z)
  212.  
  213. 19. 创建一个8x8 的矩阵,并且设置成棋盘样式
  214.  
  215. (提示: array[::2])
  216.  
  217. In [ ]:
  218.  
  219. # Z = np.zeros((8,8),dtype=int)
  220.  
  221. #Z[1::2,::2] = 1
  222.  
  223. # Z[::2,1::2] = 1
  224.  
  225. # print(Z)
  226.  
  227. 20. 考虑一个 (6,7,8) 形状的数组,其第100个元素的索引(x,y,z)是什么?
  228.  
  229. (提示: np.unravel_index)
  230.  
  231. In [ ]:
  232.  
  233. # print(np.unravel_index(100,(6,7,8)))
  234.  
  235. 21. 用tile函数去创建一个 8x8的棋盘样式矩阵
  236.  
  237. (提示: np.tile)
  238.  
  239. In [ ]:
  240.  
  241. # Z = np.tile( np.array([[0,1],[1,0]]), (4,4))
  242.  
  243. # print(Z)
  244.  
  245. 22. 对一个5x5的随机矩阵做归一化
  246.  
  247. (提示: (x - min) / (max - min))
  248.  
  249. In [ ]:
  250.  
  251. # Z = np.random.random((5,5))
  252.  
  253. # Zmax, Zmin = Z.max(), Z.min()
  254.  
  255. # Z = (Z - Zmin)/(Zmax - Zmin)
  256.  
  257. # print(Z)
  258.  
  259. 23. 创建一个将颜色描述为(RGBA)四个无符号字节的自定义dtype?
  260.  
  261. (提示: np.dtype)
  262.  
  263. In [ ]:
  264.  
  265. # color = np.dtype([("r", np.ubyte, 1),
  266.  
  267. # ("g", np.ubyte, 1),
  268.  
  269. # ("b", np.ubyte, 1),
  270.  
  271. # ("a", np.ubyte, 1)])
  272.  
  273. # color
  274.  
  275. 24. 一个5x3的矩阵与一个3x2的矩阵相乘,实矩阵乘积是什么?
  276.  
  277. (提示: np.dot | @)
  278.  
  279. In [ ]:
  280.  
  281. # Z = np.dot(np.ones((5,3)), np.ones((3,2)))
  282.  
  283. # print(Z)
  284.  
  285. 25. 给定一个一维数组,对其在38之间的所有元素取反
  286.  
  287. (提示: >, <=)
  288.  
  289. In [ ]:
  290.  
  291. # Z = np.arange(11)
  292.  
  293. # Z[(3 < Z) & (Z <= 8)] *= -1
  294.  
  295. # print(Z)
  296.  
  297. 26. 下面脚本运行后的结果是什么?
  298.  
  299. (提示: np.sum)
  300.  
  301. In [ ]:
  302.  
  303. # print(sum(range(5),-1))
  304.  
  305. In [ ]:
  306.  
  307. # from numpy import *
  308.  
  309. # print(sum(range(5),-1))
  310.  
  311. 27. 考虑一个整数向量Z,下列表达合法的是哪个?
  312.  
  313. Z**Z
  314.  
  315. 2<<Z>>2
  316.  
  317. Z<-Z
  318.  
  319. 1j*Z
  320.  
  321. Z/1/1
  322.  
  323. Z<Z>Z
  324.  
  325. In [ ]:
  326.  
  327. # Z = np.arange(5)
  328.  
  329. # Z ** Z # legal
  330.  
  331. In [ ]:
  332.  
  333. # Z = np.arange(5)
  334.  
  335. # 2 << Z >> 2 # false
  336.  
  337. In [ ]:
  338.  
  339. # Z = np.arange(5)
  340.  
  341. # Z <- Z # legal
  342.  
  343. In [ ]:
  344.  
  345. # Z = np.arange(5)
  346.  
  347. # 1j*Z # legal
  348.  
  349. In [ ]:
  350.  
  351. # Z = np.arange(5)
  352.  
  353. # Z/1/1 # legal
  354.  
  355. In [ ]:
  356.  
  357. # Z = np.arange(5)
  358.  
  359. # Z<Z>Z # false
  360.  
  361. 28. 下列表达式的结果分别是什么?
  362.  
  363. np.array(0) /np.array(0)
  364.  
  365. np.array(0) //np.array(0)
  366.  
  367. np.array([np.nan]).astype(int).astype(float)
  368.  
  369. In [ ]:
  370.  
  371. # print(np.array(0) / np.array(0))
  372.  
  373. In [ ]:
  374.  
  375. # print(np.array(0) // np.array(0))
  376.  
  377. In [ ]:
  378.  
  379. # print(np.array([np.nan]).astype(int).astype(float))
  380.  
  381. 29. 如何从零位对浮点数组做舍入 ?
  382.  
  383. (提示: np.uniform, np.copysign, np.ceil, np.abs)
  384.  
  385. In [ ]:
  386.  
  387. # Z = np.random.uniform(-10,+10,10)
  388.  
  389. # print (np.copysign(np.ceil(np.abs(Z)), Z))
  390.  
  391. 30. 如何找到两个数组中的共同元素?
  392.  
  393. (提示: np.intersect1d)
  394.  
  395. In [ ]:
  396.  
  397. # Z1 = np.random.randint(0,10,10)
  398.  
  399. # Z2 = np.random.randint(0,10,10)
  400.  
  401. # print(np.intersect1d(Z1,Z2))
  402.  
  403. 31. 如何忽略所有的 numpy 警告(尽管不建议这么做)?
  404.  
  405. (提示: np.seterr, np.errstate)
  406.  
  407. # Suicide mode on
  408.  
  409. defaults=np.seterr(all="ignore")
  410.  
  411. Z=np.ones(1) /0
  412.  
  413. # Back to sanity
  414.  
  415. _=np.seterr(**defaults)
  416.  
  417. Anequivalentway, withacontextmanager:
  418.  
  419. withnp.errstate(divide='ignore'):
  420.  
  421. Z=np.ones(1) /0
  422.  
  423. 32. 下面的表达式是正确的吗?
  424.  
  425. (提示: imaginary number)
  426.  
  427. np.sqrt(-1) ==np.emath.sqrt(-1)
  428.  
  429. In [ ]:
  430.  
  431. # np.sqrt(-1) == np.emath.sqrt(-1) # False
  432.  
  433. 33. 如何得到昨天,今天,明天的日期?
  434.  
  435. (提示: np.datetime64, np.timedelta64)
  436.  
  437. In [ ]:
  438.  
  439. # yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
  440.  
  441. # today = np.datetime64('today', 'D')
  442.  
  443. # tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
  444.  
  445. # print ("Yesterday is " + str(yesterday))
  446.  
  447. # print ("Today is " + str(today))
  448.  
  449. # print ("Tomorrow is "+ str(tomorrow))
  450.  
  451. 34. 如何得到所有与20167月对应的日期?
  452.  
  453. (提示: np.arange(dtype=datetime64['D']))
  454.  
  455. In [ ]:
  456.  
  457. # Z = np.arange('2016-07', '2016-08', dtype='datetime64[D]')
  458.  
  459. # print(Z)
  460.  
  461. 35. 如何直接在位计算(A+B)\*(-A/2)(不建立副本)?
  462.  
  463. (提示: np.add(out=), np.negative(out=), np.multiply(out=), np.divide(out=))
  464.  
  465. In [ ]:
  466.  
  467. # A = np.ones(3)*1
  468.  
  469. # B = np.ones(3)*2
  470.  
  471. # C = np.ones(3)*3
  472.  
  473. # np.add(A,B,out=B)
  474.  
  475. In [ ]:
  476.  
  477. # np.divide(A,2,out=A)
  478.  
  479. In [ ]:
  480.  
  481. # np.negative(A,out=A)
  482.  
  483. In [ ]:
  484.  
  485. # np.multiply(A,B,out=A)
  486.  
  487. 36. 用五种不同的方法去提取一个随机数组的整数部分
  488.  
  489. (提示: %, np.floor, np.ceil, astype, np.trunc)
  490.  
  491. In [ ]:
  492.  
  493. # Z = np.random.uniform(0,10,10)
  494.  
  495. # print (Z - Z%1)
  496.  
  497. In [ ]:
  498.  
  499. # print (np.floor(Z))
  500.  
  501. In [ ]:
  502.  
  503. # print (np.ceil(Z)-1)
  504.  
  505. In [ ]:
  506.  
  507. # print (Z.astype(int))
  508.  
  509. In [ ]:
  510.  
  511. # print (np.trunc(Z))
  512.  
  513. 37. 创建一个5x5的矩阵,其中每行的数值范围从04
  514.  
  515. (提示: np.arange)
  516.  
  517. In [ ]:
  518.  
  519. # Z = np.zeros((5,5))
  520.  
  521. # Z += np.arange(5)
  522.  
  523. # print (Z)
  524.  
  525. 38. 通过考虑一个可生成10个整数的函数,来构建一个数组
  526.  
  527. (提示: np.fromiter)
  528.  
  529. In [ ]:
  530.  
  531. # def generate():
  532.  
  533. # for x in range(10):
  534.  
  535. # yield x
  536.  
  537. # Z = np.fromiter(generate(),dtype=float,count=-1)
  538.  
  539. # print (Z)
  540.  
  541. 39. 创建一个长度为10的随机向量,其值域范围从01,但是不包括01
  542.  
  543. (提示: np.linspace)
  544.  
  545. In [ ]:
  546.  
  547. # Z = np.linspace(0,1,11,endpoint=False)[1:]
  548.  
  549. # print (Z)
  550.  
  551. 40. 创建一个长度为10的随机向量,并将其排序
  552.  
  553. (提示: sort)
  554.  
  555. In [ ]:
  556.  
  557. # Z = np.random.random(10)
  558.  
  559. # Z.sort()
  560.  
  561. # print (Z)
  562.  
  563. 41.对于一个小数组,如何用比 np.sum更快的方式对其求和?
  564.  
  565. (提示: np.add.reduce)
  566.  
  567. In [ ]:
  568.  
  569. # Z = np.arange(10)
  570.  
  571. # np.add.reduce(Z)
  572.  
  573. 42. 对于两个随机数组A和B,检查它们是否相等
  574.  
  575. (提示: np.allclose, np.array_equal)
  576.  
  577. In [ ]:
  578.  
  579. # A = np.random.randint(0,2,5)
  580.  
  581. # B = np.random.randint(0,2,5)
  582.  
  583. # # Assuming identical shape of the arrays and a tolerance for the comparison of values
  584.  
  585. # equal = np.allclose(A,B)
  586.  
  587. # print(equal)
  588.  
  589. In [ ]:
  590.  
  591. # # 方法2
  592.  
  593. # # Checking both the shape and the element values, no tolerance (values have to be exactly equal)
  594.  
  595. # equal = np.array_equal(A,B)
  596.  
  597. # print(equal)
  598.  
  599. 43. 创建一个只读数组(read-only)
  600.  
  601. (提示: flags.writeable)
  602.  
  603. # 使用如下过程实现
  604.  
  605. Z=np.zeros(10)
  606.  
  607. Z.flags.writeable=False
  608.  
  609. Z[0] =1
  610.  
  611. ---------------------------------------------------------------------------
  612.  
  613. ValueErrorTraceback(mostrecentcalllast)
  614.  
  615. <ipython-input-54-6fd4c6570dd1>in<module>()
  616.  
  617. 1Z=np.zeros(10)
  618.  
  619. 2Z.flags.writeable=False
  620.  
  621. ---->3Z[0] =1
  622.  
  623. ValueError: assignmentdestinationisread-only
  624.  
  625. 44. 将笛卡尔坐标下的一个10x2的矩阵转换为极坐标形式
  626.  
  627. (hint: np.sqrt, np.arctan2)
  628.  
  629. In [ ]:
  630.  
  631. # Z = np.random.random((10,2))
  632.  
  633. # X,Y = Z[:,0], Z[:,1]
  634.  
  635. # R = np.sqrt(X**2+Y**2)
  636.  
  637. # T = np.arctan2(Y,X)
  638.  
  639. # print (R)
  640.  
  641. # print (T)
  642.  
  643. 45. 创建一个长度为10的向量,并将向量中最大值替换为1
  644.  
  645. (提示: argmax)
  646.  
  647. In [ ]:
  648.  
  649. # Z = np.random.random(10)
  650.  
  651. # Z[Z.argmax()] = 0
  652.  
  653. # print (Z)
  654.  
  655. 46. 创建一个结构化数组,并实现 x 和 y 坐标覆盖 [0,1]x[0,1] 区域
  656.  
  657. (提示: np.meshgrid)
  658.  
  659. In [ ]:
  660.  
  661. # Z = np.zeros((5,5), [('x',float),('y',float)])
  662.  
  663. # Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,5),
  664.  
  665. # np.linspace(0,1,5))
  666.  
  667. # print(Z)
  668.  
  669. 47. 给定两个数组X和Y,构造Cauchy矩阵C (Cij =1/(xi - yj))
  670.  
  671. (提示: np.subtract.outer)
  672.  
  673. In [ ]:
  674.  
  675. # X = np.arange(8)
  676.  
  677. # Y = X + 0.5
  678.  
  679. # C = 1.0 / np.subtract.outer(X, Y)
  680.  
  681. # print(np.linalg.det(C))
  682.  
  683. 48. 打印每个numpy标量类型的最小值和最大值?
  684.  
  685. (提示: np.iinfo, np.finfo, eps)
  686.  
  687. In [ ]:
  688.  
  689. # for dtype in [np.int8, np.int32, np.int64]:
  690.  
  691. # print(np.iinfo(dtype).min)
  692.  
  693. # print(np.iinfo(dtype).max)
  694.  
  695. # for dtype in [np.float32, np.float64]:
  696.  
  697. # print(np.finfo(dtype).min)
  698.  
  699. # print(np.finfo(dtype).max)
  700.  
  701. # print(np.finfo(dtype).eps)
  702.  
  703. 49. 如何打印一个数组中的所有数值?
  704.  
  705. (提示: np.set_printoptions)
  706.  
  707. In [ ]:
  708.  
  709. # np.set_printoptions(threshold=np.nan)
  710.  
  711. # Z = np.zeros((16,16))
  712.  
  713. # print (Z)
  714.  
  715. 50. 给定标量时,如何找到数组中最接近标量的值?
  716.  
  717. (提示: argmin)
  718.  
  719. In [ ]:
  720.  
  721. # Z = np.arange(100)
  722.  
  723. # v = np.random.uniform(0,100)
  724.  
  725. # index = (np.abs(Z-v)).argmin()
  726.  
  727. # print (Z[index])
  728.  
  729. 51. 创建一个表示位置(x,y)和颜色(r,g,b)的结构化数组
  730.  
  731. (提示: dtype)
  732.  
  733. In [ ]:
  734.  
  735. # Z = np.zeros(10, [ ('position', [ ('x', float, 1),
  736.  
  737. # ('y', float, 1)]),
  738.  
  739. # ('color', [ ('r', float, 1),
  740.  
  741. # ('g', float, 1),
  742.  
  743. # ('b', float, 1)])])
  744.  
  745. # print (Z)
  746.  
  747. 52. 对一个表示坐标形状为(100,2)的随机向量,找到点与点的距离
  748.  
  749. (提示: np.atleast_2d, T, np.sqrt)
  750.  
  751. In [ ]:
  752.  
  753. # Z = np.random.random((10,2))
  754.  
  755. # X,Y = np.atleast_2d(Z[:,0], Z[:,1])
  756.  
  757. # D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
  758.  
  759. # print (D)
  760.  
  761. In [ ]:
  762.  
  763. # # 方法2
  764.  
  765. # # Much faster with scipy
  766.  
  767. # import scipy
  768.  
  769. # # Thanks Gavin Heverly-Coulson (#issue 1)
  770.  
  771. # import scipy.spatial
  772.  
  773. # D = scipy.spatial.distance.cdist(Z,Z)
  774.  
  775. # print (D)
  776.  
  777. 53. 如何将32位的浮点数(float)转换为对应的整数(integer)?
  778.  
  779. (提示: astype(copy=False))
  780.  
  781. In [ ]:
  782.  
  783. # Z = np.arange(10, dtype=np.int32)
  784.  
  785. # Z = Z.astype(np.float32, copy=False)
  786.  
  787. # print (Z)
  788.  
  789. 54. 如何读取以下文件?
  790.  
  791. (提示: np.genfromtxt)
  792.  
  793. 1, 2, 3, 4, 5
  794.  
  795. 6, , , 7, 8
  796.  
  797. , , 9,10,11
  798.  
  799. 参考链接
  800.  
  801. 55. 对于numpy数组,enumerate的等价操作是什么?
  802.  
  803. (提示: np.ndenumerate, np.ndindex)
  804.  
  805. In [ ]:
  806.  
  807. # Z = np.arange(9).reshape(3,3)
  808.  
  809. # for index, value in np.ndenumerate(Z):
  810.  
  811. # print (index, value)
  812.  
  813. # for index in np.ndindex(Z.shape):
  814.  
  815. # print (index, Z[index])
  816.  
  817. 56. 生成一个通用的二维Gaussian-like数组
  818.  
  819. (提示: np.meshgrid, np.exp)
  820.  
  821. In [ ]:
  822.  
  823. # X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
  824.  
  825. # D = np.sqrt(X*X+Y*Y)
  826.  
  827. # sigma, mu = 1.0, 0.0
  828.  
  829. # G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
  830.  
  831. # print (G)
  832.  
  833. 57. 对一个二维数组,如何在其内部随机放置p个元素?
  834.  
  835. (提示: np.put, np.random.choice)
  836.  
  837. In [ ]:
  838.  
  839. # n = 10
  840.  
  841. # p = 3
  842.  
  843. # Z = np.zeros((n,n))
  844.  
  845. # np.put(Z, np.random.choice(range(n*n), p, replace=False),1)
  846.  
  847. # print (Z)
  848.  
  849. 58. 减去一个矩阵中的每一行的平均值
  850.  
  851. (提示: mean(axis=,keepdims=))
  852.  
  853. In [ ]:
  854.  
  855. # X = np.random.rand(5, 10)
  856.  
  857. # # Recent versions of numpy
  858.  
  859. # Y = X - X.mean(axis=1, keepdims=True)
  860.  
  861. # print(Y)
  862.  
  863. In [ ]:
  864.  
  865. # # 方法2
  866.  
  867. # # Older versions of numpy
  868.  
  869. # Y = X - X.mean(axis=1).reshape(-1, 1)
  870.  
  871. # print (Y)
  872.  
  873. 59. 如何通过第n列对一个数组进行排序?
  874.  
  875. (提示: argsort)
  876.  
  877. In [ ]:
  878.  
  879. # Z = np.random.randint(0,10,(3,3))
  880.  
  881. # print (Z)
  882.  
  883. # print (Z[Z[:,1].argsort()])
  884.  
  885. 60. 如何检查一个二维数组是否有空列?
  886.  
  887. (提示: any, ~)
  888.  
  889. In [ ]:
  890.  
  891. # Z = np.random.randint(0,3,(3,10))
  892.  
  893. # print ((~Z.any(axis=0)).any())
  894.  
  895. 61. 从数组中的给定值中找出最近的值
  896.  
  897. (提示: np.abs, argmin, flat)
  898.  
  899. In [ ]:
  900.  
  901. # Z = np.random.uniform(0,1,10)
  902.  
  903. # z = 0.5
  904.  
  905. # m = Z.flat[np.abs(Z - z).argmin()]
  906.  
  907. # print (m)
  908.  
  909. 62. 如何用迭代器(iterator)计算两个分别具有形状(1,3)(3,1)的数组?
  910.  
  911. (提示: np.nditer)
  912.  
  913. In [ ]:
  914.  
  915. # A = np.arange(3).reshape(3,1)
  916.  
  917. # B = np.arange(3).reshape(1,3)
  918.  
  919. # it = np.nditer([A,B,None])
  920.  
  921. # for x,y,z in it:
  922.  
  923. # z[...] = x + y
  924.  
  925. # print (it.operands[2])
  926.  
  927. 63. 创建一个具有name属性的数组类
  928.  
  929. (提示: class方法)
  930.  
  931. In [ ]:
  932.  
  933. # class NamedArray(np.ndarray):
  934.  
  935. # def __new__(cls, array, name="no name"):
  936.  
  937. # obj = np.asarray(array).view(cls)
  938.  
  939. # obj.name = name
  940.  
  941. # return obj
  942.  
  943. # def __array_finalize__(self, obj):
  944.  
  945. # if obj is None: return
  946.  
  947. # http://self.info = getattr(obj, 'name', "no name")
  948.  
  949. # Z = NamedArray(np.arange(10), "range_10")
  950.  
  951. # print (Z.name)
  952.  
  953. 64. 考虑一个给定的向量,如何对由第二个向量索引的每个元素加1(小心重复的索引)?
  954.  
  955. (提示: np.bincount | np.add.at)
  956.  
  957. In [ ]:
  958.  
  959. # Z = np.ones(10)
  960.  
  961. # I = np.random.randint(0,len(Z),20)
  962.  
  963. # Z += np.bincount(I, minlength=len(Z))
  964.  
  965. # print(Z)
  966.  
  967. In [ ]:
  968.  
  969. # # 方法2
  970.  
  971. # np.add.at(Z, I, 1)
  972.  
  973. # print(Z)
  974.  
  975. 65. 根据索引列表(I),如何将向量(X)的元素累加到数组(F)?
  976.  
  977. (提示: np.bincount)
  978.  
  979. In [ ]:
  980.  
  981. # X = [1,2,3,4,5,6]
  982.  
  983. # I = [1,3,9,3,4,1]
  984.  
  985. # F = np.bincount(I,X)
  986.  
  987. # print (F)
  988.  
  989. 66. 考虑一个(dtype=ubyte)(w,h,3)图像,计算其唯一颜色的数量
  990.  
  991. (提示: np.unique)
  992.  
  993. In [ ]:
  994.  
  995. # w,h = 16,16
  996.  
  997. # I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
  998.  
  999. # #Note that we should compute 256*256 first.
  1000.  
  1001. # #Otherwise numpy will only promote F.dtype to 'uint16' and overfolw will occur
  1002.  
  1003. # F = I[...,0]*(256*256) + I[...,1]*256 +I[...,2]
  1004.  
  1005. # n = len(np.unique(F))
  1006.  
  1007. # print (n)
  1008.  
  1009. 67. 考虑一个四维数组,如何一次性计算出最后两个轴(axis)的和?
  1010.  
  1011. (提示: sum(axis=(-2,-1)))
  1012.  
  1013. In [ ]:
  1014.  
  1015. # A = np.random.randint(0,10,(3,4,3,4))
  1016.  
  1017. # # solution by passing a tuple of axes (introduced in numpy 1.7.0)
  1018.  
  1019. # sum = A.sum(axis=(-2,-1))
  1020.  
  1021. # print (sum)
  1022.  
  1023. In [ ]:
  1024.  
  1025. # # 方法2
  1026.  
  1027. # sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)
  1028.  
  1029. # print (sum)
  1030.  
  1031. 68. 考虑一个一维向量D,如何使用相同大小的向量S来计算D子集的均值?
  1032.  
  1033. (提示: np.bincount)
  1034.  
  1035. In [ ]:
  1036.  
  1037. # D = np.random.uniform(0,1,100)
  1038.  
  1039. # S = np.random.randint(0,10,100)
  1040.  
  1041. # D_sums = np.bincount(S, weights=D)
  1042.  
  1043. # D_counts = np.bincount(S)
  1044.  
  1045. # D_means = D_sums / D_counts
  1046.  
  1047. # print (D_means)
  1048.  
  1049. In [ ]:
  1050.  
  1051. # # 方法2
  1052.  
  1053. # import pandas as pd
  1054.  
  1055. # print(pd.Series(D).groupby(S).mean())
  1056.  
  1057. 69. 如何获得点积 dot prodcut的对角线?
  1058.  
  1059. (提示: np.diag)
  1060.  
  1061. In [ ]:
  1062.  
  1063. # A = np.random.uniform(0,1,(5,5))
  1064.  
  1065. # B = np.random.uniform(0,1,(5,5))
  1066.  
  1067. # # slow version
  1068.  
  1069. # np.diag(np.dot(A, B))
  1070.  
  1071. In [ ]:
  1072.  
  1073. ## 方法2
  1074.  
  1075. # # Fast version
  1076.  
  1077. # np.sum(A * B.T, axis=1)
  1078.  
  1079. In [ ]:
  1080.  
  1081. ## 方法3
  1082.  
  1083. # # Faster version
  1084.  
  1085. # np.einsum("ij,ji->i", A, B)
  1086.  
  1087. 70. 考虑一个向量[1,2,3,4,5],如何建立一个新的向量,在这个新向量中每个值之间有3个连续的零?
  1088.  
  1089. (提示: array[::4])
  1090.  
  1091. In [ ]:
  1092.  
  1093. # Z = np.array([1,2,3,4,5])
  1094.  
  1095. # nz = 3
  1096.  
  1097. # Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
  1098.  
  1099. # Z0[::nz+1] = Z
  1100.  
  1101. # print (Z0)
  1102.  
  1103. 71. 考虑一个维度(5,5,3)的数组,如何将其与一个(5,5)的数组相乘?
  1104.  
  1105. (提示: array[:, :, None])
  1106.  
  1107. In [ ]:
  1108.  
  1109. # A = np.ones((5,5,3))
  1110.  
  1111. # B = 2*np.ones((5,5))
  1112.  
  1113. # print (A * B[:,:,None])
  1114.  
  1115. 72. 如何对一个数组中任意两行做交换?
  1116.  
  1117. (提示: array[[]] = array[[]])
  1118.  
  1119. In [ ]:
  1120.  
  1121. # A = np.arange(25).reshape(5,5)
  1122.  
  1123. # A[[0,1]] = A[[1,0]]
  1124.  
  1125. # print (A)
  1126.  
  1127. 73. 考虑一个可以描述10个三角形的triplets,找到可以分割全部三角形的line segment
  1128.  
  1129. (提示: repeat, np.roll, np.sort, view, np.unique)
  1130.  
  1131. In [ ]:
  1132.  
  1133. # faces = np.random.randint(0,100,(10,3))
  1134.  
  1135. # F = np.roll(faces.repeat(2,axis=1),-1,axis=1)
  1136.  
  1137. # F = F.reshape(len(F)*3,2)
  1138.  
  1139. # F = np.sort(F,axis=1)
  1140.  
  1141. # G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )
  1142.  
  1143. # G = np.unique(G)
  1144.  
  1145. # print (G)
  1146.  
  1147. 74. 给定一个二进制的数组C,如何产生一个数组A满足np.bincount(A)==C
  1148.  
  1149. (提示: np.repeat)
  1150.  
  1151. In [ ]:
  1152.  
  1153. # C = np.bincount([1,1,2,3,4,4,6])
  1154.  
  1155. # A = np.repeat(np.arange(len(C)), C)
  1156.  
  1157. # print (A)
  1158.  
  1159. 75. 如何通过滑动窗口计算一个数组的平均数?
  1160.  
  1161. (提示: np.cumsum)
  1162.  
  1163. In [ ]:
  1164.  
  1165. # def moving_average(a, n=3) :
  1166.  
  1167. # ret = np.cumsum(a, dtype=float)
  1168.  
  1169. # ret[n:] = ret[n:] - ret[:-n]
  1170.  
  1171. # return ret[n - 1:] / n
  1172.  
  1173. # Z = np.arange(20)
  1174.  
  1175. # print(moving_average(Z, n=3))
  1176.  
  1177. 76. 考虑一维数组Z,构建一个二维数组,其第一行是(Z [0],Z [1],Z [2]),每个后续行移1(最后一行应该是( Z [-3],Z [-2],Z [-1]
  1178.  
  1179. (提示: from numpy.lib import stride_tricks)
  1180.  
  1181. In [ ]:
  1182.  
  1183. # from numpy.lib import stride_tricks
  1184.  
  1185. # def rolling(a, window):
  1186.  
  1187. # shape = (a.size - window + 1, window)
  1188.  
  1189. # strides = (a.itemsize, a.itemsize)
  1190.  
  1191. # return stride_tricks.as_strided(a, shape=shape, strides=strides)
  1192.  
  1193. # Z = rolling(np.arange(10), 3)
  1194.  
  1195. # print (Z)
  1196.  
  1197. 77. 如何对布尔值取反,或者原位(in-place)改变浮点数的符号(sign)
  1198.  
  1199. (提示: np.logical_not, np.negative)
  1200.  
  1201. In [ ]:
  1202.  
  1203. # Z = np.random.randint(0,2,100)
  1204.  
  1205. # np.logical_not(Z, out=Z)
  1206.  
  1207. In [ ]:
  1208.  
  1209. # Z = np.random.uniform(-1.0,1.0,100)
  1210.  
  1211. # np.negative(Z, out=Z)
  1212.  
  1213. 78. 考虑两组点集P0和P1去描述一组线(二维)和一个点p,如何计算点p到每一条线 i (P0[i],P1[i])的距离?
  1214.  
  1215. In [ ]:
  1216.  
  1217. # def distance(P0, P1, p):
  1218.  
  1219. # T = P1 - P0
  1220.  
  1221. # L = (T**2).sum(axis=1)
  1222.  
  1223. # U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L
  1224.  
  1225. # U = U.reshape(len(U),1)
  1226.  
  1227. # D = P0 + U*T - p
  1228.  
  1229. # return np.sqrt((D**2).sum(axis=1))
  1230.  
  1231. # P0 = np.random.uniform(-10,10,(10,2))
  1232.  
  1233. # P1 = np.random.uniform(-10,10,(10,2))
  1234.  
  1235. # p = np.random.uniform(-10,10,( 1,2))
  1236.  
  1237. # print (distance(P0, P1, p))
  1238.  
  1239. 79.考虑两组点集P0和P1去描述一组线(二维)和一组点集P,如何计算每一个点 j(P[j]) 到每一条线 i (P0[i],P1[i])的距离?
  1240.  
  1241. In [ ]:
  1242.  
  1243. # # based on distance function from previous question
  1244.  
  1245. # P0 = np.random.uniform(-10, 10, (10,2))
  1246.  
  1247. # P1 = np.random.uniform(-10,10,(10,2))
  1248.  
  1249. # p = np.random.uniform(-10, 10, (10,2))
  1250.  
  1251. # print (np.array([distance(P0,P1,p_i) for p_i in p]))
  1252.  
  1253. 80.考虑一个任意数组,写一个函数,提取一个固定形状的子部分,并以给定元素为中心(fill必要时填充一个值)
  1254.  
  1255. (提示: minimum, maximum)
  1256.  
  1257. In [ ]:
  1258.  
  1259. # Z = np.random.randint(0,10,(10,10))
  1260.  
  1261. # shape = (5,5)
  1262.  
  1263. # fill = 0
  1264.  
  1265. # position = (1,1)
  1266.  
  1267. # R = np.ones(shape, dtype=Z.dtype)*fill
  1268.  
  1269. # P = np.array(list(position)).astype(int)
  1270.  
  1271. # Rs = np.array(list(R.shape)).astype(int)
  1272.  
  1273. # Zs = np.array(list(Z.shape)).astype(int)
  1274.  
  1275. # R_start = np.zeros((len(shape),)).astype(int)
  1276.  
  1277. # R_stop = np.array(list(shape)).astype(int)
  1278.  
  1279. # Z_start = (P-Rs//2)
  1280.  
  1281. # Z_stop = (P+Rs//2)+Rs%2
  1282.  
  1283. # R_start = (R_start - np.minimum(Z_start,0)).tolist()
  1284.  
  1285. # Z_start = (np.maximum(Z_start,0)).tolist()
  1286.  
  1287. # R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()
  1288.  
  1289. # Z_stop = (np.minimum(Z_stop,Zs)).tolist()
  1290.  
  1291. # r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
  1292.  
  1293. # z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
  1294.  
  1295. # R[r] = Z[z]
  1296.  
  1297. # print (Z)
  1298.  
  1299. # print (R)
  1300.  
  1301. 81. 考虑一个数组Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14],如何生成一个数组R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ...,[11,12,13,14]]?
  1302.  
  1303. (提示: stride_tricks.as_strided)
  1304.  
  1305. In [ ]:
  1306.  
  1307. # Z = np.arange(1,15,dtype=np.uint32)
  1308.  
  1309. # R = stride_tricks.as_strided(Z,(11,4),(4,4))
  1310.  
  1311. # print (R)
  1312.  
  1313. 82. 计算一个矩阵的秩
  1314.  
  1315. (提示: np.linalg.svd)
  1316.  
  1317. In [ ]:
  1318.  
  1319. # Z = np.random.uniform(0,1,(10,10))
  1320.  
  1321. # U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
  1322.  
  1323. # rank = np.sum(S > 1e-10)
  1324.  
  1325. # print (rank)
  1326.  
  1327. 83. 如何找到一个数组中出现频率最高的值?
  1328.  
  1329. (提示: np.bincount, argmax)
  1330.  
  1331. In [ ]:
  1332.  
  1333. # Z = np.random.randint(0,10,50)
  1334.  
  1335. # print (np.bincount(Z).argmax())
  1336.  
  1337. 84. 从一个10x10的矩阵中提取出连续的3x3区块
  1338.  
  1339. (提示: stride_tricks.as_strided)
  1340.  
  1341. In [ ]:
  1342.  
  1343. # Z = np.random.randint(0,5,(10,10))
  1344.  
  1345. # n = 3
  1346.  
  1347. # i = 1 + (Z.shape[0]-3)
  1348.  
  1349. # j = 1 + (Z.shape[1]-3)
  1350.  
  1351. # C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)
  1352.  
  1353. # print (C)
  1354.  
  1355. 85. 创建一个满足 Z[i,j] == Z[j,i]的子类
  1356.  
  1357. (提示: class 方法)
  1358.  
  1359. In [ ]:
  1360.  
  1361. # class Symetric(np.ndarray):
  1362.  
  1363. # def __setitem__(self, index, value):
  1364.  
  1365. # i,j = index
  1366.  
  1367. # super(Symetric, self).__setitem__((i,j), value)
  1368.  
  1369. # super(Symetric, self).__setitem__((j,i), value)
  1370.  
  1371. # def symetric(Z):
  1372.  
  1373. # return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)
  1374.  
  1375. # S = symetric(np.random.randint(0,10,(5,5)))
  1376.  
  1377. # S[2,3] = 42
  1378.  
  1379. # print (S)
  1380.  
  1381. 86. 考虑p个 nxn 矩阵和一组形状为(n,1)的向量,如何直接计算p个矩阵的乘积(n,1)
  1382.  
  1383. (提示: np.tensordot)
  1384.  
  1385. In [ ]:
  1386.  
  1387. # p, n = 10, 20
  1388.  
  1389. # M = np.ones((p,n,n))
  1390.  
  1391. # V = np.ones((p,n,1))
  1392.  
  1393. # S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])
  1394.  
  1395. # print (S)
  1396.  
  1397. # It works, because:
  1398.  
  1399. # M is (p,n,n)
  1400.  
  1401. # V is (p,n,1)
  1402.  
  1403. # Thus, summing over the paired axes 0 and 0 (of M and V independently),
  1404.  
  1405. # and 2 and 1, to remain with a (n,1) vector.
  1406.  
  1407. 87. 对于一个16x16的数组,如何得到一个区域(block-sum)的和(区域大小为4x4)?
  1408.  
  1409. (提示: np.add.reduceat)
  1410.  
  1411. In [ ]:
  1412.  
  1413. # Z = np.ones((16,16))
  1414.  
  1415. # k = 4
  1416.  
  1417. # S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0),
  1418.  
  1419. # np.arange(0, Z.shape[1], k), axis=1)
  1420.  
  1421. # print (S)
  1422.  
  1423. 88. 如何利用numpy数组实现Game of Life?
  1424.  
  1425. (提示: Game of Life)
  1426.  
  1427. In [ ]:
  1428.  
  1429. # def iterate(Z):
  1430.  
  1431. # # Count neighbours
  1432.  
  1433. # N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
  1434.  
  1435. # Z[1:-1,0:-2] + Z[1:-1,2:] +
  1436.  
  1437. # Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:])
  1438.  
  1439. # # Apply rules
  1440.  
  1441. # birth = (N==3) & (Z[1:-1,1:-1]==0)
  1442.  
  1443. # survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1)
  1444.  
  1445. # Z[...] = 0
  1446.  
  1447. # Z[1:-1,1:-1][birth | survive] = 1
  1448.  
  1449. # return Z
  1450.  
  1451. # Z = np.random.randint(0,2,(50,50))
  1452.  
  1453. # for i in range(100): Z = iterate(Z)
  1454.  
  1455. # print (Z)
  1456.  
  1457. 89. 如何找到一个数组的第n个最大值?
  1458.  
  1459. (提示: np.argsort | np.argpartition)
  1460.  
  1461. In [ ]:
  1462.  
  1463. # Z = np.arange(10000)
  1464.  
  1465. # np.random.shuffle(Z)
  1466.  
  1467. # n = 5
  1468.  
  1469. # # Slow
  1470.  
  1471. # print (Z[np.argsort(Z)[-n:]])
  1472.  
  1473. In [ ]:
  1474.  
  1475. # # 方法2
  1476.  
  1477. # # Fast
  1478.  
  1479. # print (Z[np.argpartition(-Z,n)[:n]])
  1480.  
  1481. 90. 给定任意个数向量,创建笛卡尔积(每一个元素的每一种组合)
  1482.  
  1483. (提示: np.indices)
  1484.  
  1485. In [ ]:
  1486.  
  1487. # def cartesian(arrays):
  1488.  
  1489. # arrays = [np.asarray(a) for a in arrays]
  1490.  
  1491. # shape = (len(x) for x in arrays)
  1492.  
  1493. # ix = np.indices(shape, dtype=int)
  1494.  
  1495. # ix = ix.reshape(len(arrays), -1).T
  1496.  
  1497. # for n, arr in enumerate(arrays):
  1498.  
  1499. # ix[:, n] = arrays[n][ix[:, n]]
  1500.  
  1501. # return ix
  1502.  
  1503. # print (cartesian(([1, 2, 3], [4, 5], [6, 7])))
  1504.  
  1505. 91. 如何从一个正常数组创建记录数组(record array)?
  1506.  
  1507. (提示: np.core.records.fromarrays)
  1508.  
  1509. In [ ]:
  1510.  
  1511. # Z = np.array([("Hello", 2.5, 3),
  1512.  
  1513. # ("World", 3.6, 2)])
  1514.  
  1515. # R = np.core.records.fromarrays(Z.T,
  1516.  
  1517. # names='col1, col2, col3',
  1518.  
  1519. # formats = 'S8, f8, i8')
  1520.  
  1521. # print (R)
  1522.  
  1523. 92. 考虑一个大向量Z, 用三种不同的方法计算它的立方
  1524.  
  1525. (提示: np.power, \*, np.einsum)
  1526.  
  1527. In [ ]:
  1528.  
  1529. # x = np.random.rand()
  1530.  
  1531. # np.power(x,3)
  1532.  
  1533. In [ ]:
  1534.  
  1535. ## 方法2
  1536.  
  1537. # x*x*x
  1538.  
  1539. In [ ]:
  1540.  
  1541. ## 方法3
  1542.  
  1543. # np.einsum('i,i,i->i',x,x,x)
  1544.  
  1545. 93. 考虑两个形状分别为(8,3)(2,2)的数组A和B. 如何在数组A中找到满足包含B中元素的行?(不考虑B中每行元素顺序)
  1546.  
  1547. (提示: np.where)
  1548.  
  1549. In [ ]:
  1550.  
  1551. # A = np.random.randint(0,5,(8,3))
  1552.  
  1553. # B = np.random.randint(0,5,(2,2))
  1554.  
  1555. # C = (A[..., np.newaxis, np.newaxis] == B)
  1556.  
  1557. # rows = np.where(C.any((3,1)).all(1))[0]
  1558.  
  1559. # print (rows)
  1560.  
  1561. 94. 考虑一个10x3的矩阵,分解出有不全相同值的行 ([2,2,3])
  1562.  
  1563. In [ ]:
  1564.  
  1565. # Z = np.random.randint(0,5,(10,3))
  1566.  
  1567. # print (Z)
  1568.  
  1569. # # solution for arrays of all dtypes (including string arrays and record arrays)
  1570.  
  1571. # E = np.all(Z[:,1:] == Z[:,:-1], axis=1)
  1572.  
  1573. # U = Z[~E]
  1574.  
  1575. # print (U)
  1576.  
  1577. In [ ]:
  1578.  
  1579. # # 方法2
  1580.  
  1581. # # soluiton for numerical arrays only, will work for any number of columns in Z
  1582.  
  1583. # U = Z[Z.max(axis=1) != Z.min(axis=1),:]
  1584.  
  1585. # print (U)
  1586.  
  1587. 95. 将一个整数向量转换为matrix binary的表现形式
  1588.  
  1589. (提示: np.unpackbits)
  1590.  
  1591. In [ ]:
  1592.  
  1593. # I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])
  1594.  
  1595. # B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
  1596.  
  1597. # print(B[:,::-1])
  1598.  
  1599. In [ ]:
  1600.  
  1601. # # 方法2
  1602.  
  1603. # print (np.unpackbits(I[:, np.newaxis], axis=1))
  1604.  
  1605. 96. 给定一个二维数组,如何提取出唯一的(unique)行?
  1606.  
  1607. (提示: np.ascontiguousarray)
  1608.  
  1609. In [ ]:
  1610.  
  1611. # Z = np.random.randint(0,2,(6,3))
  1612.  
  1613. # T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))
  1614.  
  1615. # _, idx = np.unique(T, return_index=True)
  1616.  
  1617. # uZ = Z[idx]
  1618.  
  1619. # print (uZ)
  1620.  
  1621. 97. 考虑两个向量A和B,写出用einsum等式对应的inner, outer, sum, mul函数
  1622.  
  1623. (提示: np.einsum)
  1624.  
  1625. In [ ]:
  1626.  
  1627. # A = np.random.uniform(0,1,10)
  1628.  
  1629. # B = np.random.uniform(0,1,10)
  1630.  
  1631. # print ('sum')
  1632.  
  1633. # print (np.einsum('i->', A))# np.sum(A)
  1634.  
  1635. In [ ]:
  1636.  
  1637. # print ('A * B')
  1638.  
  1639. # print (np.einsum('i,i->i', A, B)) # A * B
  1640.  
  1641. In [ ]:
  1642.  
  1643. # print ('inner')
  1644.  
  1645. # print (np.einsum('i,i', A, B)) # np.inner(A, B)
  1646.  
  1647. In [ ]:
  1648.  
  1649. # print ('outer')
  1650.  
  1651. # print (np.einsum('i,j->ij', A, B)) # np.outer(A, B)
  1652.  
  1653. 98. 考虑一个由两个向量描述的路径(X,Y),如何用等距样例(equidistant samples)对其进行采样(sample)?
  1654.  
  1655. (提示: np.cumsum, np.interp)
  1656.  
  1657. In [ ]:
  1658.  
  1659. # phi = np.arange(0, 10*np.pi, 0.1)
  1660.  
  1661. # a = 1
  1662.  
  1663. # x = a*phi*np.cos(phi)
  1664.  
  1665. # y = a*phi*np.sin(phi)
  1666.  
  1667. # dr = (np.diff(x)**2 + np.diff(y)**2)**.5 # segment lengths
  1668.  
  1669. # r = np.zeros_like(x)
  1670.  
  1671. # r[1:] = np.cumsum(dr) # integrate path
  1672.  
  1673. # r_int = np.linspace(0, r.max(), 200) # regular spaced path
  1674.  
  1675. # x_int = np.interp(r_int, r, x) # integrate path
  1676.  
  1677. # y_int = np.interp(r_int, r, y)
  1678.  
  1679. 99. 给定整数n和2D数组X,从X中选择可以解释为具有n度的多项分布的绘制的行,即,仅包含整数并且总和为n的行。
  1680.  
  1681. (提示: np.logical_and.reduce, np.mod)
  1682.  
  1683. In [ ]:
  1684.  
  1685. # X = np.asarray([[1.0, 0.0, 3.0, 8.0],
  1686.  
  1687. # [2.0, 0.0, 1.0, 1.0],
  1688.  
  1689. # [1.5, 2.5, 1.0, 0.0]])
  1690.  
  1691. # n = 4
  1692.  
  1693. # M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1)
  1694.  
  1695. # M &= (X.sum(axis=-1) == n)
  1696.  
  1697. # print (X[M])
  1698.  
  1699. 100. 计算1D阵列X的平均值的自举95%置信区间(即,对替换N次的阵列的元素进行重新采样,计算每个样本的平均值,然后计算均值上的百分位数)。
  1700.  
  1701. In [ ]:
  1702.  
  1703. # X = np.random.randn(100) # random 1D array
  1704.  
  1705. # N = 1000 # number of bootstrap samples
  1706.  
  1707. # idx = np.random.randint(0, X.size, (N, X.size))
  1708.  
  1709. # means = X[idx].mean(axis=1)
  1710.  
  1711. # confint = np.percentile(means, [2.5, 97.5])
  1712.  
  1713. # print (confint)