练习篇(Part 5)

51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)

 arr = np.zeros(10,[('position',[('x',float,1),('y',float,1)]),
('color',[('r',float,1),('g',float,1),('b',float,1)])])
print(arr)

运行结果:

[((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))]

52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)

 arr = np.random.randint(1,4,(100,2))
x,y = np.atleast_2d(arr[:,0],arr[:,1])
print(arr)
dist = np.sqrt((x-x.T)**2+(y-y.T)**2)
print(dist)

运行结果:(太长)略

53. How to convert a float (32 bits) array into an integer (32 bits) in place?

 arr = np.arange(10,dtype = np.float)
arr = arr.astype(np.int32)
print(arr)

运行结果:[0 1 2 3 4 5 6 7 8 9]

54. How to read the following file? (★★☆)

1,2,3,4,5
6, , ,7,8
, ,9,10,11

 from io import StringIO
s = StringIO("""1, 2, 3, 4, 5\n
6, , , 7, 8\n
, , 9,10,11\n""")
arr = np.genfromtxt(s,delimiter=",",dtype=np.int)
print(arr)

运行结果:

[[ 1 2 3 4 5]
[ 6 -1 -1 7 8]
[-1 -1 9 10 11]]

55. What is the equivalent of enumerate for numpy arrays? (★★☆)

 arr = np.arange(9).reshape(3,3)
for index, value in np.ndenumerate(arr):
print(index, value)
for index in np.ndindex(arr.shape):
print(index, arr[index])

运行结果:

(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8

56. Generate a generic 2D Gaussian-like array (★★☆)

 x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
d = np.sqrt(x*x+y*y)
sigma,mu = 1.0,0.0
g = np.exp(-(d-mu)**2/(2.0*sigma**2))
print(g)

运行结果:(太长)略

57. How to randomly place p elements in a 2D array? (★★☆)

 arr = np.zeros((10,10))
np.put(arr,np.random.choice(range(10*10),3,replace=False),25)
print(arr)

运行结果:

[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 25. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 25. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 25. 0. 0. 0. 0. 0. 0. 0. 0.]]

58. Subtract the mean of each row of a matrix (★★☆)

 arr = np.random.randint(1,5,(5,5))
arr2 = arr - arr.mean(axis=1,keepdims=True)
print(arr)
print(arr2)

运行结果:

[[3 1 4 2 2]
[1 1 1 4 4]
[1 3 2 4 2]
[4 4 1 4 3]
[3 1 3 2 1]]
[[ 0.6 -1.4 1.6 -0.4 -0.4]
[-1.2 -1.2 -1.2 1.8 1.8]
[-1.4 0.6 -0.4 1.6 -0.4]
[ 0.8 0.8 -2.2 0.8 -0.2]
[ 1. -1. 1. 0. -1. ]]

59. How to sort an array by the nth column? (★★☆)

 arr = np.random.randint(1,10,(3,3))
print(arr)
arr = arr[arr[:,0].argsort()]
print(arr)

运行结果:

[[3 6 4]
[8 6 1]
[9 1 9]]
[[3 6 4]
[8 6 1]
[9 1 9]]

60. How to tell if a given 2D array has null columns? (★★☆)

 arr = np.random.randint(0,6,(3,3))
print(arr)
print((~arr.any(axis=1)).any())

运行结果:

[[5 4 1]
[2 4 0]
[2 4 3]]
False

61. Find the nearest value from a given value in an array (★★☆)

 arr = np.random.uniform(0,1,10)
print(arr)
x= 0.5
print(arr.flat[np.abs(arr - x).argmin()])

运行结果:

[0.90305224 0.48639632 0.27508478 0.54555147 0.71661301 0.21709767
0.03780985 0.90465381 0.22589984 0.42418026]
0.4863963171374911

62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)

 arr1 = np.arange(3).reshape(3,1)
arr2 = np.arange(3).reshape(1,3)
it = np.nditer([arr1,arr2,None])
for x,y,z in it:
z[...] = x + y
print(it.operands[2])

运行结果:

[[0 1 2]
[1 2 3]
[2 3 4]]

63. Create an array class that has a name attribute (★★☆)

 class NamedArray(np.ndarray):
def __new__(cls, array, name="no name"):
obj = np.asarray(array).view(cls)
obj.name = name
return obj
def __array_finalize__(self,obj):
if obj is None:
return
self.info = getattr(obj,'name','no name') arr = NamedArray(np.arange(10),"range_10")
print(arr.name)

运行结果:range_10

64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)

 arr1 = np.ones(10)
arr2 = np.random.randint(1,10,20)
np.add.at(arr1,arr2,1)
print(arr2)
print(arr1)

运行结果:

[5 8 2 2 5 4 4 4 7 1 3 8 5 4 9 3 7 3 1 3]
[1. 3. 3. 5. 5. 4. 1. 3. 3. 2.]

65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)

 arr1 = np.ones(10)
arr2 = np.random.randint(1,10,10)
print(arr2)
print(np.bincount(arr2,arr1))

运行结果:

[7 4 4 2 6 9 3 1 1 7]
[0. 2. 1. 1. 2. 0. 1. 2. 0. 1.]

最新文章

  1. Asp.Net WebApi核心对象解析(上篇)
  2. Crumpet – 使用很简单的响应式前端开发框架
  3. yii2.0-advanced 高级版项目搭建
  4. string-->wstring-->string
  5. c/c++编译时,指定程序运行时查找的动态链接库路径
  6. HashBiMap
  7. MVC中使用AuthorizeAttribute做身份验证操作【转】
  8. Disruptor——一种可替代有界队列完成并发线程间数据交换的高性能解决方案
  9. AngularJS <a> 超链接标签不起作用?
  10. shell判断进程是否存在
  11. ansible基础-Jinja2模版 | 测试
  12. pycharm(Python编辑器)的激活
  13. 【洛谷P3224】永无乡 并查集+Splay启发式合并
  14. Spring Hibernate Transaction示例
  15. Ubuntu 18.04 修改为静态IP
  16. spring boot打war包发布
  17. 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
  18. bzoj2263: Pku3889 Fractal Streets
  19. Mac版Mysql Workbench不展示Schema
  20. mysql四-2:多表查询

热门文章

  1. 【C#】写文件时如何去掉编码前缀
  2. mysql数据库批量执行sql文件对数据库进行操作【windows版本】
  3. [MySQL]mysql binlog回滚数据
  4. 有哪些「看似复杂,实则简单」的 PS 技巧?
  5. 基于Jupyter Notebooks的C# .NET Interactive安装与使用
  6. MySQL数据库root密码忘记丢失重置方法
  7. 基于SSM开发在线家教预约系统源码
  8. 【redisson】分布式锁与数据库事务
  9. python_函数笔记
  10. StringBuilder的性能