numpy学习(三)
练习篇(Part 3)
31. 略
32. Is the following expressions true? (★☆☆)
np.sqrt(-1) == np.emath.sqrt(-1)
print(np.sqrt(-1) == np.emath.sqrt(-1))
运行结果:False
33. How to get the dates of yesterday, today and tomorrow? (★☆☆)
yesterday = np.datetime64('today','D') - np.timedelta64(1,'D')
today = np.datetime64('today','D')
tomorrow = np.datetime64('today','D') + np.timedelta64(1,'D')
print("yesterday:"+str(yesterday))
print("today:"+str(today))
print("tomorrow:"+str(tomorrow))
运行结果:
yesterday:2019-09-24
today:2019-09-25
tomorrow:2019-09-26
34. How to get all the dates corresponding to the month of July 2016? (★★☆)
arr = np.arange('2016-07','2016-08',dtype='datetime64[D]')
print(arr)
运行结果:
['2016-07-01' '2016-07-02' '2016-07-03' '2016-07-04' '2016-07-05'
'2016-07-06' '2016-07-07' '2016-07-08' '2016-07-09' '2016-07-10'
'2016-07-11' '2016-07-12' '2016-07-13' '2016-07-14' '2016-07-15'
'2016-07-16' '2016-07-17' '2016-07-18' '2016-07-19' '2016-07-20'
'2016-07-21' '2016-07-22' '2016-07-23' '2016-07-24' '2016-07-25'
'2016-07-26' '2016-07-27' '2016-07-28' '2016-07-29' '2016-07-30'
'2016-07-31']
35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
arr1 = np.random.random((3,3))
arr2 = np.random.random((3,3))
print(arr1)
print(arr2)
arr3 = np.multiply(np.add(arr1,arr2),np.negative(np.divide(arr1,2)))
print(arr3)
运行结果:
[[0.93844098 0.64468962 0.39723495]
[0.40210752 0.55750482 0.00350184]
[0.09511603 0.95997034 0.77923869]]
[[0.94571561 0.30103345 0.4198415 ]
[0.88062036 0.38437861 0.28678044]
[0.57298281 0.24126303 0.89882227]]
[[-8.84084874e-01 -3.04848926e-01 -1.62285662e-01]
[-2.57897263e-01 -2.62552273e-01 -5.08260388e-04]
[-3.17734528e-02 -5.76574205e-01 -6.53805017e-01]]
36. Extract the integer part of a random array using 5 different methods(★★☆)
arr = np.random.uniform(3,8,10)
print(arr)
print(np.trunc(arr))
print(arr - arr%1)
print(np.floor(arr))
print(np.ceil(arr)-1)
print(arr.astype(int))
运行结果:
[7.31488564 7.18687183 6.17100343 4.79264848 4.71726774 5.95315196
5.29135106 4.35113601 4.78410156 4.56738764]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7. 7. 6. 4. 4. 5. 5. 4. 4. 4.]
[7 7 6 4 4 5 5 4 4 4]
37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
arr = np.zeros((5,5))
arr += np.arange(5)
print(arr)
运行结果:
[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
def generate():
for x in range(10):
yield x
arr = np.fromiter(generate(),dtype=float,count=-1)
print(arr)
运行结果:[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)
arr = np.linspace(0,1,11,endpoint=False)[1:]
print(arr)
运行结果:[0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091]
40. Create a random vector of size 10 and sort it (★★☆)
arr = np.random.randint(1,20,10)
print(arr)
print(np.sort(arr))
运行结果:
[ 2 15 13 14 16 18 8 18 1 8]
[ 1 2 8 8 13 14 15 16 18 18]
numpy学习(三)的更多相关文章
- Numpy学习三:数组运算
1.转置 #reshape(shape)函数改变数组形状,shape是一个元组,表示数组的形状 创建一个包含15个元素的一维数组,通过reshape函数调整数组形状为3行5列的二维数组arr = np ...
- NumPy学习笔记 三 股票价格
NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...
- NumPy学习笔记 一
NumPy学习笔记 一 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...
- 数据分析之Pandas和Numpy学习笔记(持续更新)<1>
pandas and numpy notebook 最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...
- NumPy学习(索引和切片,合并,分割,copy与deep copy)
NumPy学习(索引和切片,合并,分割,copy与deep copy) 目录 索引和切片 合并 分割 copy与deep copy 索引和切片 通过索引和切片可以访问以及修改数组元素的值 一维数组 程 ...
- NumPy学习(让数据处理变简单)
NumPy学习(一) NumPy数组创建 NumPy数组属性 NumPy数学算术与算数运算 NumPy数组创建 NumPy 中定义的最重要的对象是称为 ndarray 的 N 维数组类型. 它描述相同 ...
- numpy 学习笔记
numpy 学习笔记 导入 numpy 包 import numpy as np 声明 ndarray 的几种方法 方法一,从list中创建 l = [[1,2,3], [4,5,6], [7,8,9 ...
- Numpy学习1
NumPy学习(1) 参考资料: http://www.cnblogs.com/zhanghaohong/p/4854858.html http://linusp.github.io/2016/02/ ...
- Numpy学习笔记(下篇)
目录 Numpy学习笔记(下篇) 一.Numpy数组的合并与分割操作 1.合并操作 2.分割操作 二.Numpy中的矩阵运算 1.Universal Function 2.矩阵运算 3.向量和矩阵运算 ...
随机推荐
- RT-Thread can - STM32F103ZET6
SDK版本v4.0.2 目前,RT-Thread Studio还不能够自定义添加can设备.下面介绍手动添加过程: 使用RT-Thread Studio创建一个简单工程 使用RT-Thread env ...
- linux设置服务器时间
在 Linux 机器上有两种时钟: 由内核维持的软件时钟(又称系统时钟)和在机器关机后记录时间的(电池供电的)硬件时钟. 启动的时候, 内核会把系统时钟与硬件时钟同步. 之后, 两个时钟各自独立运行. ...
- 使用ffmpeg为影片添加字幕
ffmpeg -i infile.mp4 -i infile.srt -c copy -c:s mov_text outfile.mp4 The order of -c copy -c:s mov_t ...
- .NetCore 3.0迁移遇到的各种问题
错误集合 [错误]当前+.NET+SDK+不支持将+.NET+Core+3.0+设置为目标.请将+.NET+Core+2.2+或更低版 [解决方法]勾选上就可以了 2. [错误] add-migrat ...
- MySql 部分字段去重
select * from personal_question_answer where answer_id in ( select min(answer_id) from personal_ques ...
- 第十周CTF解答
第十周write-up解题答案及过程 隐写诶 直接用WinRAR查看就能看到其flag{0ca175b9c0f726a831d895e269332461 } 第一题 将后缀名改为 rar ,发现压缩包 ...
- go 算法与数据结构
数据结构 稀疏数组 package main import "fmt" /* 稀疏数组 案例:五子棋存盘与复盘 节省存储空间 */ type ValNode struct { ro ...
- 一、threejs————灯光阴影
threejs设置阴影有三个注意点 1.只有支持阴影的灯光才可以 pointLight,spotlight,directionallight 2.添加摄像机辅助器 THREE.CameraHelper ...
- 重启防火墙(iptables)命令#service iptable restart失效
Redirecting to /bin/systemctl restart iptables.ser linux下执行防火墙相关指令报错: Redirecting to /bin/systemctl ...
- 解决jmeter5.1高版本linux CPU,IO,Memory监控性能测试 java.lang.NoSuchMethodError: org.apache.jmeter.samplers.SampleSaveConfiguration.setFormatter(Ljava/t
jmeter中也可以监控服务器的CPU和内存使用情况,但是需要安装一些插件还需要在被监测服务器上开启服务. 安装性能监控插件(jmeter-plugins)后报如下错误,是由于jmeter版本过高jm ...