练习篇(Part 2)

11. Create a 3x3 identity matrix (★☆☆)

 arr = np.eye(3)
print(arr)

运行结果:[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]

12. Create a 3x3x3 array with random values (★☆☆)

 arr = np.random.random((3,3,3))
print(arr)

运行结果:略

13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)

 arr = np.random.random((10,10))
print('max:'+str(arr.max()))
print('min:'+str(arr.min()))

运行结果:

max:0.9966220981691146

min:0.0034603079973672957

14. Create a random vector of size 30 and find the mean value (★☆☆)

 arr = np.random.random(30)
print(arr.mean())

运行结果:0.49710820465862965

15. Create a 2d array with 1 on the border and 0 inside (★☆☆)

 arr = np.ones((10,10))
arr[1:9,1:9] = 0
print(arr)

运行结果:

[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]

16. How to add a border (filled with 0's) around an existing array? (★☆☆)

 arr = np.ones((10,10))
arr = np.pad(arr, pad_width=1, mode='constant', constant_values=0)
print(arr)

运行结果:

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

17. What is the result of the following expression? (★☆☆)

 0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1
 print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)

运行结果:nan False False nan True False

18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)

 arr = np.diag(1+np.arange(4),k=-1)
print(arr)

运行结果:

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

19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)

 arr = np.zeros((8,8))
arr[0::2,0::2]=1
arr[1::2,1::2]=1
print(arr)

运行结果:

[[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]
[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]
[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]
[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]]

20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?

 print(np.unravel_index(99,(6,7,8)))

运行结果:(1, 5, 3)

21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)

 arr = np.tile(np.array([[0,1],[1,0]]),(4,4))
print(arr)

运行结果:

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

22. Normalize a 5x5 random matrix (★☆☆)

 arr = np.random.random((5,5))
arr = (arr - np.min(arr))/(np.max(arr) - np.min(arr))
print(arr)

运行结果:

[[0.25182827 1. 0. 0.08239415 0.28511849]
[0.10300901 0.52930264 0.95743154 0.84571053 0.50581171]
[0.82070737 0.5720979 0.91581986 0.16283325 0.27075288]
[0.91480517 0.40637193 0.7032704 0.15695137 0.79951099]
[0.18035443 0.50388197 0.48436665 0.9828424 0.02296698]]

23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)

 color = np.dtype([
("r", np.ubyte, 1),
("g", np.ubyte, 1),
("b", np.ubyte, 1),
("a", np.ubyte, 1)])
print(color)

运行结果:[('r', 'u1'), ('g', 'u1'), ('b', 'u1'), ('a', 'u1')]

24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)

 arr1 = np.random.random((5,3))
arr2 = np.random.random((3,2))
arr3 = np.dot(arr1,arr2)
print(arr1)
print(arr2)
print(arr3)

运行结果:

[[0.51143733 0.22531681 0.71917393]
[0.81667839 0.1703277 0.1519645 ]
[0.15206992 0.370845 0.62943418]
[0.33517061 0.6224698 0.46285361]
[0.09370654 0.04592576 0.52768143]]
[[0.0785262 0.25563589]
[0.04088907 0.04818778]
[0.82035168 0.40391289]]
[[0.63934976 0.43208287]
[0.19575952 0.27836044]
[0.54346236 0.3109813 ]
[0.43147462 0.30262961]
[0.44212063 0.23930515]]

25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)

 arr = np.random.randint(1,10,10)
print(arr)
arr[(arr >=3) & (arr <= 8)] = 0
print(arr)

运行结果:

[8 1 9 5 5 4 3 1 1 3]
[0 1 9 0 0 0 0 1 1 0]
26. What is the output of the following script? (★☆☆)
 print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
 print(sum(range(5),-1))     #-1表示起始
from numpy import *
print(sum(range(5),-1)) #-1表示轴向

运行结果:9 10

27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)

 Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z
 z = np.random.randint(1,4,(5,5))
print(z)
print(z**z)
print(2<<z>>2)
print(z < -z)
print(1j*z) #1j表示复数的i
print(z/1/1)
print(z<z>z)

运行结果:略(只有最后一个会出错)

28. What are the result of the following expressions?

 np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)

运行结果:nan 0 [-2.14748365e+09]

29. How to round away from zero a float array ? (★☆☆)

 arr = np.random.random(5)*100
arr1 = np.trunc(arr+0.5)
print(arr)
print(arr1)

运行结果:

[69.37662327 99.0777984 93.6006897 45.79127547 51.93021804]
[69. 99. 94. 46. 52.]

30. How to find common values between two arrays? (★☆☆)

 arr1 = np.random.randint(1,5,10)
arr2 = np.random.randint(3,10,10)
print(arr1)
print(arr2)
print(np.intersect1d(arr1,arr2))

运行结果:

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

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

  1. Numpy学习二:数组的索引与切片

    1.一维数组索引与切片#创建一维数组arr1d = np.arange(10)print(arr1d) 结果:[0 1 2 3 4 5 6 7 8 9] #数组的索引从0开始,通过索引获取第三个元素a ...

  2. NumPy学习笔记 二

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

  3. NumPy学习笔记 一

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

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

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

  5. numpy 学习笔记

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

  6. numpy 学习总结

    numpy 学习总结 作者:csj更新时间:01.09 email:59888745@qq.com 说明:因内容较多,会不断更新 xxx学习总结: 回主目录:2017 年学习记录和总结 #生成数组/使 ...

  7. (转)Python数据分析之numpy学习

    原文:https://www.cnblogs.com/nxld/p/6058572.html https://morvanzhou.github.io/tutorials/data-manipulat ...

  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. http报文解析

    http报文结构 报文首部 起始行 请求报文的起始行: 方法(method) request-URL version(http协议版本) 响应报文的起始行 HTTP响应码 请求头 通用首部 请求首部 ...

  2. HTML连载70-相片墙、盒子阴影和文字阴影

    一. 制作一个相片墙 二. <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  3. linux中shell内置命令和外置命令

    shell内置命令 无法通过which或者whereis去查找命令的位置 例如cd,cp这些命令是shell解释器内置的命令 当shell内置命令传入shell解释器,shell解释器通过内核获取相关 ...

  4. 阿里Java架构师分享自己的成长经历,教你如何快速成长为架构师

    架构师是公司的“金领”,很少需要考虑生存的问题,从而有更多的精力思考关键技术,形成“强者愈强”的良性循环.当然,冰冻三尺非一日之寒,成为一名合格的架构师是一个漫长的积累过程.对于大部分的软件开发人员来 ...

  5. powersploit的两个信息收集的脚本

    0x00 简介 powersploit是基于powershell的渗透工具包,里面都是powershell的脚本工具文件.工具包地址:https://github.com/PowerShellMafi ...

  6. List泛型

    .Net自从2.0以后开始支持泛型. 泛型的作用:可以创建独立于被包含类型的类和方法.泛型类使用泛型类型,并可以根据需要使用特定的类型替换泛型类型.这就保证了类型安全性:如果某个类型不支持泛型类,编译 ...

  7. C#里面低消耗获取当前时间的思路

    Linux下有vsyscall来优化一些例如time(NULL), gettimeofday这种调用的消耗; 但是Windows下, 没有类似的东西, 但是思路还是有的 1. 程序启动的时候, 获取一 ...

  8. 回炉重造之重读Windows核心编程-006-线程

    线程也是有两部分组成的: 线程的内核对象,操作系统用来管理线程和统计线程信息的地方. 线程堆栈,用于维护现场在执行代码的时候用到的所有函数参数和局部变量. 进程是线程的容器,如果进程中有一个以上的线程 ...

  9. jQuery---jquery.ui实现新闻模块

    jquery.ui实现新闻模块 jquery也有ui,了解即可,用的不多,类似element ui 和bootstrap JQuery UI API: jquery.ui实现新闻模块 draggale ...

  10. 腾讯qlv视频转为MP4格式工具

    本文解决上一篇<优酷爱奇艺视频转换为MP4格式工具>留下的腾讯视频qlv转MP4格式问题,教程都是一步步亲手操作的,每一步都有配图.希望各位老板多转发分享,谢谢! 解压软件.(建议关闭所有 ...