热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[Numpy]数组广播的两个例题:找零问题,最近邻居问题

找零问题25分分成10分,5分和1分怎么分呢?有多少种分法?Usingbroadcasting,computethenumberofwa

找零问题

25分分成10分,5分和1分怎么分呢?有多少种分法?
Using broadcasting, compute the number of ways a quarter (i.e., 25 cents) can be split into pennies, nickels, and dimes.

Hint: You can do this using loops as

n=0 # start counter
for n_d in range(0,3): # at most 2 dimes
for n_n in range(0,6): # at most 5 nickels
for n_p in range(0,26): # at most 25 pennies
value = n_d10+n_n5+n_p
if value == 25:
print (‘dimes=%d, nickels=%d, pennies=%d’%(n_d,n_n,n_p))
n+=1
Can you find a better way using broadcasting?
Can you do it in one-line?
What is the supportability argument against this?

import numpy as np#X as dimes, Y as nickles, Z as pennis
X=np.arange(3)*np.array([10])
Y=np.arange(6)*np.array([5])
Z=np.arange(26)#broadcasting
res = X[:,None,None]+Y[None,:,None]+Z[None,None,:]#Number of possible solutions 12
print(len(np.where(res==25)[0]))#Output solutions
for iter in np.array(np.where(res==25)).T:print(f'{iter[0]} dimes, {iter[1]} nickles, {iter[2]} pennis.')

# result:
12
0 dimes, 0 nickles, 25 pennis.
0 dimes, 1 nickles, 20 pennis.
0 dimes, 2 nickles, 15 pennis.
0 dimes, 3 nickles, 10 pennis.
0 dimes, 4 nickles, 5 pennis.
0 dimes, 5 nickles, 0 pennis.
1 dimes, 0 nickles, 15 pennis.
1 dimes, 1 nickles, 10 pennis.
1 dimes, 2 nickles, 5 pennis.
1 dimes, 3 nickles, 0 pennis.
2 dimes, 0 nickles, 5 pennis.
2 dimes, 1 nickles, 0 pennis.

最近邻居问题

如何求出二维直角坐标系上各点相距最近的点
Using broadcasting, can you compute the nearest neighbors to the following two-dimensional array,

array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
For a given row, your answer should be the indices of the other row that is closest to it (other than itself) using the square of the Euclidean distance metric

di,j=x2i,j+y2i,j
The answer for this exercise is the following,

array([1, 0, 1, 2])
So, the point (0,1) in the 0th row is closest to (2,3) which is 1st row. The 1st row is closest to the 0th row, the 2nd row is closest to the 1st row, and the 3rd row is closest to the 2nd row.

What if you had a three-dimensional array?

参考https://stackoverflow.com/questions/52366421/how-to-do-n-d-distance-and-nearest-neighbor-calculations-on-numpy-arrays/52366706

import numpy as npX = np.array([[0, 1],[2, 3],[4, 5],[6, 7]])# D is the np array of distance
D = np.sqrt(np.abs(np.sum((X[None,:,:] - X[:,None,:])**2, axis=2)))# Another way to write D
D_ds = np.sqrt( # (X - Y) ** 2
np.einsum('ij, ij ->i', X, X)[:, None] + # = X ** 2 \
np.einsum('ij, ij ->i', X, X) - # + X ** 2 \
2 * X.dot(X.T)) # - 2 * X * Y# print(D)
# argpartition: find the 2nd small distance on second axis and then do partition
print(D.argpartition(1,axis=1)[:,1])

# result:
# D
#[[0. 2.82842712 5.65685425 8.48528137]
# [2.82842712 0. 2.82842712 5.65685425]
# [5.65685425 2.82842712 0. 2.82842712]
# [8.48528137 5.65685425 2.82842712 0. ]]
[1 0 1 2]


推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • 基于词向量计算文本相似度1.测试数据:链接:https:pan.baidu.coms1fXJjcujAmAwTfsuTg2CbWA提取码:f4vx2.实验代码:imp ... [详细]
  • 动态多点××× 单云双HUB
    动态多点是一个高扩展的IPSEC解决方案传统的ipsecS2S有如下劣势1.中心站点配置量大,无论是采用经典ipsec***还是采用greoveripsec多一个分支 ... [详细]
  • 【原创】利用Python进行河流遥感处理的PyRIS软件开发
    今天开始着手改造pyris1.0.文章地址:https:doi.org10.1016J.ENVSOFT.2018.03.028Monegaglia,2 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • Centos7搭建ELK(Elasticsearch、Logstash、Kibana)教程及注意事项
    本文介绍了在Centos7上搭建ELK(Elasticsearch、Logstash、Kibana)的详细步骤,包括下载安装包、安装Elasticsearch、创建用户、修改配置文件等。同时提供了使用华为镜像站下载安装包的方法,并强调了保证版本一致的重要性。 ... [详细]
  • 本文介绍了一个Python函数same_set,用于判断两个相等长度的数组是否包含相同的元素。函数会忽略元素的顺序和重复次数,如果两个数组包含相同的元素,则返回1,否则返回0。文章还提供了函数的具体实现代码和样例输入输出。 ... [详细]
  • 在IDEA中运行CAS服务器的配置方法
    本文介绍了在IDEA中运行CAS服务器的配置方法,包括下载CAS模板Overlay Template、解压并添加项目、配置tomcat、运行CAS服务器等步骤。通过本文的指导,读者可以轻松在IDEA中进行CAS服务器的运行和配置。 ... [详细]
  • 本文介绍了一种求解最小权匹配问题的方法,使用了拆点和KM算法。通过将机器拆成多个点,表示加工的顺序,然后使用KM算法求解最小权匹配,得到最优解。文章给出了具体的代码实现,并提供了一篇题解作为参考。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了源码分析--ConcurrentHashMap与HashTable(JDK1.8)相关的知识,希望对你有一定的参考价值。  Concu ... [详细]
  • AstridDAO 专访:波卡稳定币黑马 BAI
    加入Pol ... [详细]
author-avatar
MINT米田
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有