练习篇(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学习(三)的更多相关文章

  1. Numpy学习三:数组运算

    1.转置 #reshape(shape)函数改变数组形状,shape是一个元组,表示数组的形状 创建一个包含15个元素的一维数组,通过reshape函数调整数组形状为3行5列的二维数组arr = np ...

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

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

  3. NumPy学习笔记 一

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

  4. 数据分析之Pandas和Numpy学习笔记(持续更新)<1>

    pandas and numpy notebook        最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...

  5. NumPy学习(索引和切片,合并,分割,copy与deep copy)

    NumPy学习(索引和切片,合并,分割,copy与deep copy) 目录 索引和切片 合并 分割 copy与deep copy 索引和切片 通过索引和切片可以访问以及修改数组元素的值 一维数组 程 ...

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

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

  7. numpy 学习笔记

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

  8. Numpy学习1

    NumPy学习(1) 参考资料: http://www.cnblogs.com/zhanghaohong/p/4854858.html http://linusp.github.io/2016/02/ ...

  9. Numpy学习笔记(下篇)

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

随机推荐

  1. ubuntu18.04管理redis

    ubuntu下 redis的安装使用 安装 1. 进入redis安装路径 cd ~/installed/redis-5.0.7 2. 启动redis 启动服务端redis-server 启动客户端(必 ...

  2. 基于arduino的气象站

    bmp180的简介: • 压力范围:~1100hPa(海拔 米~- 米) • 电源电压:.8V~.6V(VDDA), .62V~.6V(VDDD) • 尺寸:.6mmx3.8x0.93mm • 低功耗 ...

  3. 03.JS运算符

    前言:   学习一门编程语言的基本步骤 (01)了解背景知识 (02)搭建开发环境 (03)语法规范 (04)常量和变量 (05)数据类型 (06)数据类型转换 (07)运算符7.运算符 表达式:由运 ...

  4. Java连载73-String方法简介

    一.字符串常用的方法 package com.bjpowernode.java_learning; ​ public class D73_StringMethodBriefIntroduction { ...

  5. 重构与动态为angularjs栏位赋值或获取值

    先来看下面一段html: 这个ng-model名称带有一定的规律带有序号. 先来实现数据绑定,从数据取到数据后,为ng-model绑定相对应的值: var c = response.data $sco ...

  6. Github无法访问的解决办法

    #github 192.30.253.113 github.com 192.30.253.113 github.com 192.30.253.118 gist.github.com 192.30.25 ...

  7. css选择器四大类:基本、组合、属性、伪类

    什么是选择器?选择器的作用是通过它可以找到元素,把css样式传递给元素!css选择器主要分为:基本选择器.属性选择器.组合选择器与伪类选择器四个大类! css基本选择器 基本选择器又分为:*通配符.标 ...

  8. Notes writer Pro

    Notes writer Pro pen钢笔和Pencil铅笔发现没啥区别 笔记软件使用说明书: https://www.lanzous.com/i777i3c

  9. PMP--1. PMBOK框架部分目录

    1.1 PMBOK体系框架描述https://www.cnblogs.com/hemukg/p/11821210.html 1.2 PMBOK指南组成部分https://www.cnblogs.com ...

  10. 基于axios的万能封装

    一 . 命名axios.js import axios from 'axios'; export default function ajax(url = '', params = {}, type = ...