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.向量和矩阵运算 ...
随机推荐
- PMP--1.3 项目环境
项目所处的环境可能对项目的开展产生有利或不利的影响.影响项目的环境因素==项目经理在项目期间需要考虑的因素.这些因素不需要死记硬背,需要有一定了解就可以,在项目开始前针对文中内容提前把环境了解清楚,并 ...
- Go Web爬虫并发实现
题目:Exercise: Web Crawler 直接参考了 https://github.com/golang/tour/blob/master/solutions/webcrawler.go 的实 ...
- JS中变量、作用域的本质,定义及使用方法
全局作用域和局部作用域 全局作用域 局部作用域:函数作用域 全局作用域在全局和局部都可以访问到,局部作用域只能在局部被访问到 var name="cyy"; function fn ...
- day19 几个模块的学习
# 模块本质上就是一个 .py 文件# 数据类型# 列表.元组# 字典# 集合.frozenset# 字符串# 堆栈:特点:先进后出# 队列:先进先出 FIFO # from collections ...
- EF Core For Oracle11中Find FirstOrDefault等方法执行失败
问题描述 最近在使用ef core连接oracle的发现Find.FirstOrDefault.Skip Task分页等等方法执行失败.使用的是docker安装的oracle11,错误如下图: 解决办 ...
- Essential C++ 笔记-1
本文作者为C++初学者,学习之中难免有误,该文章仅为参考 面向对象概述 继承:改变类之间的关系 多态:让基类的pointer或refence得以十分透明的指向基类的某个派生对象 继承 继承发生在对象与 ...
- nginx: [warn] conflicting server name "aaa.7yule.cn" on 0.0.0.0:80, ignored
故障现象: 修改nginx配置参数后,使用nginx -t检查配置,出现告警提示 nginx: [warn] conflicting server name "aaa.7yule.cn&qu ...
- 剑指offer-拓展训练-N皇后的问题-全排列
/* 题目: N皇后的问题. */ /* 思路: 全排列. 声明一个具有N个元素的数组curr,每个下标i(0>i>n)代表行,每个curr[i]代表列,所以初始化为curr[i] = i ...
- Tomcat + mysql + myeclipse 启动遇到的问题
1. 问题: Tomcat启动时报错如下:Table 'performance_schema.session_variables' doesn't exist 2. 网络上普遍找到的解决办法: 控制台 ...
- 在系统下使用命令安装gnome图形界面程序
yum groupinstall "GNOME Desktop" "Graphical Administration Tools" reboot 记得别忘了更新 ...