numpy学习(五)
练习篇(Part 5)
51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)
- arr = np.zeros(10,[('position',[('x',float,1),('y',float,1)]),
- ('color',[('r',float,1),('g',float,1),('b',float,1)])])
- print(arr)
运行结果:
[((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))]
52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
- arr = np.random.randint(1,4,(100,2))
- x,y = np.atleast_2d(arr[:,0],arr[:,1])
- print(arr)
- dist = np.sqrt((x-x.T)**2+(y-y.T)**2)
- print(dist)
运行结果:(太长)略
53. How to convert a float (32 bits) array into an integer (32 bits) in place?
- arr = np.arange(10,dtype = np.float)
- arr = arr.astype(np.int32)
- print(arr)
运行结果:[0 1 2 3 4 5 6 7 8 9]
54. How to read the following file? (★★☆)
1,2,3,4,5
6, , ,7,8
, ,9,10,11
- from io import StringIO
- s = StringIO("""1, 2, 3, 4, 5\n
- 6, , , 7, 8\n
- , , 9,10,11\n""")
- arr = np.genfromtxt(s,delimiter=",",dtype=np.int)
- print(arr)
运行结果:
[[ 1 2 3 4 5]
[ 6 -1 -1 7 8]
[-1 -1 9 10 11]]
55. What is the equivalent of enumerate for numpy arrays? (★★☆)
- arr = np.arange(9).reshape(3,3)
- for index, value in np.ndenumerate(arr):
- print(index, value)
- for index in np.ndindex(arr.shape):
- print(index, arr[index])
运行结果:
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
56. Generate a generic 2D Gaussian-like array (★★☆)
- x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
- d = np.sqrt(x*x+y*y)
- sigma,mu = 1.0,0.0
- g = np.exp(-(d-mu)**2/(2.0*sigma**2))
- print(g)
运行结果:(太长)略
57. How to randomly place p elements in a 2D array? (★★☆)
- arr = np.zeros((10,10))
- np.put(arr,np.random.choice(range(10*10),3,replace=False),25)
- print(arr)
运行结果:
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 25. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 25. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 25. 0. 0. 0. 0. 0. 0. 0. 0.]]
58. Subtract the mean of each row of a matrix (★★☆)
- arr = np.random.randint(1,5,(5,5))
- arr2 = arr - arr.mean(axis=1,keepdims=True)
- print(arr)
- print(arr2)
运行结果:
[[3 1 4 2 2]
[1 1 1 4 4]
[1 3 2 4 2]
[4 4 1 4 3]
[3 1 3 2 1]]
[[ 0.6 -1.4 1.6 -0.4 -0.4]
[-1.2 -1.2 -1.2 1.8 1.8]
[-1.4 0.6 -0.4 1.6 -0.4]
[ 0.8 0.8 -2.2 0.8 -0.2]
[ 1. -1. 1. 0. -1. ]]
59. How to sort an array by the nth column? (★★☆)
- arr = np.random.randint(1,10,(3,3))
- print(arr)
- arr = arr[arr[:,0].argsort()]
- print(arr)
运行结果:
[[3 6 4]
[8 6 1]
[9 1 9]]
[[3 6 4]
[8 6 1]
[9 1 9]]
60. How to tell if a given 2D array has null columns? (★★☆)
- arr = np.random.randint(0,6,(3,3))
- print(arr)
- print((~arr.any(axis=1)).any())
运行结果:
[[5 4 1]
[2 4 0]
[2 4 3]]
False
61. Find the nearest value from a given value in an array (★★☆)
- arr = np.random.uniform(0,1,10)
- print(arr)
- x= 0.5
- print(arr.flat[np.abs(arr - x).argmin()])
运行结果:
[0.90305224 0.48639632 0.27508478 0.54555147 0.71661301 0.21709767
0.03780985 0.90465381 0.22589984 0.42418026]
0.4863963171374911
62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)
- arr1 = np.arange(3).reshape(3,1)
- arr2 = np.arange(3).reshape(1,3)
- it = np.nditer([arr1,arr2,None])
- for x,y,z in it:
- z[...] = x + y
- print(it.operands[2])
运行结果:
[[0 1 2]
[1 2 3]
[2 3 4]]
63. Create an array class that has a name attribute (★★☆)
- class NamedArray(np.ndarray):
- def __new__(cls, array, name="no name"):
- obj = np.asarray(array).view(cls)
- obj.name = name
- return obj
- def __array_finalize__(self,obj):
- if obj is None:
- return
- self.info = getattr(obj,'name','no name')
- arr = NamedArray(np.arange(10),"range_10")
- print(arr.name)
运行结果:range_10
64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)
- arr1 = np.ones(10)
- arr2 = np.random.randint(1,10,20)
- np.add.at(arr1,arr2,1)
- print(arr2)
- print(arr1)
运行结果:
[5 8 2 2 5 4 4 4 7 1 3 8 5 4 9 3 7 3 1 3]
[1. 3. 3. 5. 5. 4. 1. 3. 3. 2.]
65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)
- arr1 = np.ones(10)
- arr2 = np.random.randint(1,10,10)
- print(arr2)
- print(np.bincount(arr2,arr1))
运行结果:
[7 4 4 2 6 9 3 1 1 7]
[0. 2. 1. 1. 2. 0. 1. 2. 0. 1.]
numpy学习(五)的更多相关文章
- numpy 学习笔记
numpy 学习笔记 导入 numpy 包 import numpy as np 声明 ndarray 的几种方法 方法一,从list中创建 l = [[1,2,3], [4,5,6], [7,8,9 ...
- Numpy学习笔记(下篇)
目录 Numpy学习笔记(下篇) 一.Numpy数组的合并与分割操作 1.合并操作 2.分割操作 二.Numpy中的矩阵运算 1.Universal Function 2.矩阵运算 3.向量和矩阵运算 ...
- numpy学习总结
Contents Numpy是一个用python实现的科学计算包,主要提供矩阵运算的功能,而矩阵运算在机器学习领域应用非常广泛,Numpy一般与Scrapy.matplotlib一起使用. Numpy ...
- TweenMax动画库学习(五)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- NumPy学习笔记 三 股票价格
NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...
- NumPy学习笔记 二
NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...
- 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 索引和切片 通过索引和切片可以访问以及修改数组元素的值 一维数组 程 ...
随机推荐
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- C# checked unchecked
static void CheckedUnCheckedDemo() { int i = int.MaxValue; try { //checked //{ // Console.WriteLine( ...
- JavaScript中基本数据类型之间的转换
在JavaScript中共有六种数据类型,其中有五种是基本数据类型,还有一种则是引用数据类型.五种基本数据类型分别是:Number 数值类型.String 字符串类型.Boolean 布尔类型, nu ...
- 为NuGet配置微软官方中国镜像
NuGet微软官方中国镜像地址: https://nuget.cdn.azure.cn/v3/index.json 打开Visual Studio => 工具 => NuGet包管理器 = ...
- SpringBoot安全管理--(一)SpringSecurity基本配置
简介: Spring Boot针对Spring Security提供了自动化配置方案,因此可以使Spring Security非常容易地整合进Spring Boot项目中,这也是在Spring Boo ...
- textarea输入文字限制个数
说明: w-count固定为数字部分的class textarea-active为超出最大输入文字个数报错信息的class html 部分: <div class="wrap wrap ...
- MySql概述及入门(五)
MySql概述及入门(五) MySQL集群搭建之读写分离 读写分离的理解 为解决单数据库节点在高并发.高压力情况下出现的性能瓶颈问题,读写分离的特性包括会话不开启事务,读语句直接发送到 salve 执 ...
- 【MVC+EasyUI实例】对数据网格的增删改查(上)
前言 此案例是针对之前做的一个小例子的后台框架的修改,从以前的三层框架改为现在的MVC框架,也是做了一次MVC和EasyUI的结合,分为2篇文章来阐述. 界面如下: 点击"添加"按 ...
- 一个抓猫的游戏 消遣GAME 持续更新中!
一个抓猫的游戏 版本 Catch_Cat_V0.30 https://files-cdn.cnblogs.com/files/send-off-a-friend/Catch_Cat_V0.3.rar ...
- opencv —— copyMakeBorder 扩充图像边界
扩充图像边界:copyMakeBorder 函数 在图像处理过程中,因为卷积算子有一定大小,所以就会导致图像一定范围的边界不能被处理,这时就需要将边界进行适当扩充. void copyMakeBord ...