小编use*_*487的帖子

python如何用零填充numpy数组

我想知道如何使用python 2.6.6和numpy 1.5.0版本用零填充2D numpy数组.抱歉! 但这些是我的局限.所以我不能用np.pad.例如,我想a用零填充,使其形状匹配b.我之所以这样做是因为我能做到:

b-a
Run Code Online (Sandbox Code Playgroud)

这样的

>>> a
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
>>> b
array([[ 3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.,  3.]])
>>> c
array([[1, 1, 1, 1, 1, 0],
       [1, 1, 1, 1, 1, 0],
       [1, 1, 1, …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy pad

57
推荐指数
3
解决办法
11万
查看次数

如何在numpy数组中找到唯一的非nan值?

我想知道是否有一种干净的方式来处理numpy中的nan.

my_array1=np.array([5,4,2,2,4,np.nan,np.nan,6])
print my_array1
#[  5.   4.   2.   2.   4.  nan  nan   6.]
print set(my_array1)
#set([nan, nan, 2.0, 4.0, 5.0, 6.0])
Run Code Online (Sandbox Code Playgroud)

我原本以为它应该返回最多1纳米的值.为什么它会返回多个nan值?我想知道我在numpy数组中有多少独特的非纳米值.

谢谢

python numpy nan

10
推荐指数
2
解决办法
1万
查看次数

具有较少参数和可选参数的函数

我经常编写看起来像这样的代码,并且正在寻找更好的建议.基本上,我通常创建一些通用函数myfuc_general来处理我需要的所有参数,通常使用可选参数.但是,我经常运行2个(可能更多)特定功能.在这种情况下,除了其中一个参数不同之外,一切都是相同的a.我经常运行它们以至于我实际上更喜欢只有两个附加功能,所以我不必记住可选参数需要的内容.

因此myfunct_specific1,我正在跑步,a=10而且myfunct_specific2,a=20.还有比这更好的事情吗?这似乎很草率,如果我需要更改myfuct_general调用,它有缺点,然后我必须更改所有其他功能.

def myfunc_general(constant, a=1,b=2):
    return constant+a+b

def myfunct_specific1(constant,b=2):
    a=10
    return myfunc_general(constant,a,b=2)

def myfunct_specific2(constant,b=2):
    a=20
    return myfunc_general(constant,a,b=2)

print myfunct_specific1(3) #15
print myfunct_specific2(3) #25
Run Code Online (Sandbox Code Playgroud)

编辑(补充):

iCodez感谢您的建议.我有这种特殊情况,它给我一个错误.救命?再次感谢

def myfunc_general(constant, constant2, a=0,b=2):
    return constant+constant2+b+a

import functools
myfunct_specific=functools.partial(myfunc_general,constant2=30)

print myfunct_specific
print myfunct_specific(3,5,b=3)


Traceback (most recent call last):
  File "C:/Python27/test", line 8, in <module>
    print myfunct_specific(3,5,b=3)
TypeError: myfunc_general() got multiple values for keyword argument 'constant2'
Run Code Online (Sandbox Code Playgroud)

python arguments function

3
推荐指数
1
解决办法
1135
查看次数

标签 统计

python ×3

numpy ×2

arguments ×1

arrays ×1

function ×1

nan ×1

pad ×1