练习篇(Part 4)

41. How to sum a small array faster than np.sum? (★★☆)

 arr = np.arange(10)
print(np.add.reduce(arr))

运行结果:45

42. Consider two random array A and B, check if they are equal (★★☆)

 arr1 = np.random.randint(0,2,4).reshape(2,2)
arr2 = np.random.randint(0,2,4).reshape(2,2)
print(arr1)
print(arr2)
print(np.allclose(arr1,arr2))
print(np.array_equal(arr1,arr2))

运行结果:

[[0 1]
[1 1]]
[[1 0]
[1 1]]
False
False

43. Make an array immutable (read-only) (★★☆)

 arr = np.random.randint(1,2,(3,3))
arr.flags.writeable = False
arr[0][0] = 1

运行结果:

44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)

 arr = np.random.randint(1,10,(10,2))
x = arr[:,0]
y = arr[:,1]
R = np.sqrt(x**2+y**2)
T = np.arctan2(y,x)
print(R)
print(T)

运行结果:

[ 8.94427191 9.21954446 9.89949494 10.63014581 7.07106781 9.21954446
8.94427191 9.05538514 8.60232527 11.3137085 ]
[0.46364761 0.21866895 0.78539816 0.71883 0.78539816 0.86217005
1.10714872 0.11065722 0.62024949 0.78539816]

45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)

 arr = np.random.randint(1,10,10)
print(arr)
arr[arr.argmax()] = 0
print(arr)

运行结果:

[3 4 7 9 4 2 4 4 2 8]
[3 4 7 0 4 2 4 4 2 8]

46. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area (★★☆)

 arr = np.zeros((5,5),[('x',float),('y',float)])
arr['x'],arr['y'] = np.meshgrid(np.linspace(0,1,5),np.linspace(0,1,5))
print(arr)

运行结果:

[[(0. , 0. ) (0.25, 0. ) (0.5 , 0. ) (0.75, 0. ) (1. , 0. )]
[(0. , 0.25) (0.25, 0.25) (0.5 , 0.25) (0.75, 0.25) (1. , 0.25)]
[(0. , 0.5 ) (0.25, 0.5 ) (0.5 , 0.5 ) (0.75, 0.5 ) (1. , 0.5 )]
[(0. , 0.75) (0.25, 0.75) (0.5 , 0.75) (0.75, 0.75) (1. , 0.75)]
[(0. , 1. ) (0.25, 1. ) (0.5 , 1. ) (0.75, 1. ) (1. , 1. )]]

47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj))

 arr1 = np.random.randint(5,10,5)
arr2 = np.random.randint(1,5,5)
print(arr1)
print(arr2)
arr3 = 1.0/np.subtract.outer(arr1,arr2)
print(arr3)

运行结果:

[9 9 5 6 7]
[2 3 1 4 1]
[[0.14285714 0.16666667 0.125 0.2 0.125 ]
[0.14285714 0.16666667 0.125 0.2 0.125 ]
[0.33333333 0.5 0.25 1. 0.25 ]
[0.25 0.33333333 0.2 0.5 0.2 ]
[0.2 0.25 0.16666667 0.33333333 0.16666667]]

48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)

 for dtype in [np.int8, np.int32, np.int64]:
print(np.iinfo(dtype).min)
print(np.iinfo(dtype).max)
for dtype in [np.float32, np.float64]:
print(np.finfo(dtype).min)
print(np.finfo(dtype).max)

运行结果:

-128
127
-2147483648
2147483647
-9223372036854775808
9223372036854775807
-3.4028235e+38
3.4028235e+38
-1.7976931348623157e+308
1.7976931348623157e+308

49. How to print all the values of an array? (★★☆)

 arr = np.random.randint(1,10,9).reshape(3,3)
print(arr)

运行结果:

[[5 3 4]
[9 2 9]
[6 6 4]]

50. How to find the closest value (to a given scalar) in a vector? (★★☆)

 arr1 = np.arange(100)
arr2 = np.random.uniform(0,100)
index = (np.abs(arr1-arr2)).argmin()
print(arr1)
print(arr2)
print(arr1[index])

运行结果:

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
96 97 98 99]
46.27162981393338
46

numpy学习(四)的更多相关文章

  1. Numpy学习四:numpy.power()用法

    numpy.power(n, x) 对数组n的元素分别求x次方.x可以是数字,也可以是数组,但是n和x的列数要相同.

  2. NumPy学习笔记 三 股票价格

    NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...

  3. NumPy学习笔记 二

    NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...

  4. NumPy学习笔记 一

    NumPy学习笔记 一 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...

  5. NumPy学习(让数据处理变简单)

    NumPy学习(一) NumPy数组创建 NumPy数组属性 NumPy数学算术与算数运算 NumPy数组创建 NumPy 中定义的最重要的对象是称为 ndarray 的 N 维数组类型. 它描述相同 ...

  6. numpy 学习笔记

    numpy 学习笔记 导入 numpy 包 import numpy as np 声明 ndarray 的几种方法 方法一,从list中创建 l = [[1,2,3], [4,5,6], [7,8,9 ...

  7. Numpy学习笔记(下篇)

    目录 Numpy学习笔记(下篇) 一.Numpy数组的合并与分割操作 1.合并操作 2.分割操作 二.Numpy中的矩阵运算 1.Universal Function 2.矩阵运算 3.向量和矩阵运算 ...

  8. Numpy学习笔记(上篇)

    目录 Numpy学习笔记(上篇) 一.Jupyter Notebook的基本使用 二.Jpuyter Notebook的魔法命令 1.%run 2.%timeit & %%timeit 3.% ...

  9. numpy学习总结

    Contents Numpy是一个用python实现的科学计算包,主要提供矩阵运算的功能,而矩阵运算在机器学习领域应用非常广泛,Numpy一般与Scrapy.matplotlib一起使用. Numpy ...

随机推荐

  1. Android Intent用法总结

    Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...

  2. 邓 【PHP大全】

    获取对应的时间戳(只保存月底的时间戳) function getTimeDate($timeType, $time, $count) { switch ($timeType) { case 'MONT ...

  3. JAVA面向对象 - 抽象类、接口

    抽象类 用abstract关键字来修饰一个类时,这个类就叫抽象类,用abstract关键字来修饰一个方式时,这个方法就是抽象方法.当一个类继承的父类是抽象类的话,需要我们把抽象类中的所有抽象方法全部实 ...

  4. YARN安装和使用

    简介 Yet Another Resource Negotiator ,负责整个集群资源的调度,和管理,支持多框架资源统一调度(HIVE spark flink) 开启yarn 安装hadoop,可以 ...

  5. js增删class的方法

    接下来我来介绍两种方法 我们先来一段HTMl代码 <div id="bb"> 你好呀 </div> 接下来我们再来一段css样式 .ob { color:r ...

  6. Dev-C++如何创建源代码模板?

    Dev-C++如何创建源代码模板? 预览图片 按下Ctrl+N或者点击新建源代码,就会自动出现这些代码了 以下是操作步骤 编写你的模板 这里有我的样例: #include<iostream> ...

  7. beego的请求数据处理

    我们经常需要获取用户传递的数据,包括 Get.POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据: GetString(key string) string Get ...

  8. Vue项目中实现tab栏和步骤条的数据联动

    也就是tab栏切换步骤条随之变化 <template>   <div>     <!-- 面包屑导航  -->     <el-breadcrumb sepa ...

  9. Redis入门-02-CentOS7环境搭建

    CentOS7下redis安装过程,安装后需要开启端口号6379 #下载 wget http://download.redis.io/releases/redis-3.2.4.tar.gz #解压 t ...

  10. STL-优先级队列-priority_queue

    头文件是<queue> 操作很简单 #include <iostream> #include <cstdio> #include <queue> usi ...