简体   繁体   English

从 numpy“数组”的每个元素中减去 0.5

[英]Subtract 0.5 from every element of a numpy "array"

With "a" a numpy array, sometimes使用“a”是一个numpy数组,有时

a = a - 0.5 

works, sometimes it doesn't.有效,有时无效。 There are several variations on an array that I don't understand.我不明白的数组有几种变体。 When I print it out somehow it has gotten into this form.当我以某种方式打印出来时,它已经变成了这种形式。

[list([0.5, 0.5, 0.2, 1])]

I've got to either force it into this form我必须要么强迫它变成这种形式

[[0.5, 0.5, 0.2, 1]]

or find some way to subtract 0.5 from each element that works on both of these variants.或者找到某种方法从适用于这两种变体的每个元素中减去 0.5。


NEXT, I want to multiply each element times 2.接下来,我想将每个元素乘以 2。

Here is code that produces an error.这是产生错误的代码。

TypeError: unsupported operand type(s) for -: 'list' and 'float'类型错误:不支持 - 的操作数类型:'list' 和 'float'

import numpy as np
r = np.array(
          [
              [[.5,.5,2.,1.],0.,2.5]
          ]
        )
p = r[:,0]
print(p)            #output is [list([0.5, 0.5, 2.0, 1.0])]
r = (p - 1/2) * 2    #fail
import numpy as np

def flatten(l):
    if l == []:
        return l
    if isinstance(l[0], list):
        return flatten(l[0]) + flatten(l[1:])
    return l[:1] + flatten(l[1:])

inp = [[[.5,.5,2.,1.],0.,2.5]]
flat = flatten(inp)


r = np.array(flat)
p = r.copy()
print(p)
r = (p - 1/2) * 2

Just had to flatten your list, recursive只需要将您的列表展平,递归


EDIT:编辑:

Following your comment关注你的评论

I've got r = np.array( [ [[.5,.5,2.,1.],0.,2.5] [[.5,.5,2.,1.],0.,2.5] ] ) I want to extract the first column and get the form [[.5,.5,2.,1.] [.5,.5,2.,1.] ]我有r = np.array( [ [[.5,.5,2.,1.],0.,2.5] [[.5,.5,2.,1.],0.,2.5] ] )我想提取第一列并得到形式[[.5,.5,2.,1.] [.5,.5,2.,1.] ]

just do做就是了

flat = inp[0]

and the rest is the same其余的都是一样的

Your problem is understanding exactly what a numpy array is.您的问题是准确理解 numpy 数组是什么。 Check out some of the docs but essentially a numpy array is a specific data type that allows efficient vectorised operations over the dimensions of the array.查看一些文档,但本质上 numpy 数组是一种特定的数据类型,它允许对数组的维度进行有效的矢量化操作。 So that means every element of the array needs to be of the same type AND the array must have pre-defined dimensions.所以这意味着数组的每个元素都需要是相同的类型,并且数组必须具有预定义的维度。 A python list is a much more flexible container that can accept arbitrary objects into its elements but the downside of this is that you can't perform the efficient vectorised operations as you can in numpy (and as you wish to do). python 列表是一个更灵活的容器,它可以接受任意对象到它的元素中,但它的缺点是你不能像在 numpy 中那样执行有效的向量化操作(以及你希望做的)。

So essentially you are asking a numpy array to contain the following:所以基本上你要求一个 numpy 数组包含以下内容:

  • a list of floats: [.5,.5,2.,1.]浮点数列表: [.5,.5,2.,1.]
  • a float: 0.浮点数: 0.
  • a float: 2.5浮点数: 2.5

These are not all the same types (one is a list and two are floats).这些不是所有相同的类型(一个是列表,两个是浮点数)。

You need to make sure that these are the same types (AND exactly the same dimensions) otherwise numpy will not be able to create the correct typed array.您需要确保这些类型相同(并且维度完全相同),否则 numpy 将无法创建正确的类型化数组。

So the following will work:所以以下将起作用:

a = np.array([.5,.5,2.,1.,0.,2.5])
a -= 0.5

Or if you need a 3 x 3 matrix then:或者,如果您需要一个3 x 3矩阵,则:

a = np.array([
        [.5, .5, 2.],
        [1., 0., 2.5],
        [0, 0, 0]
    ])
a -= 0.5

Notice in the above example how each element is a list with three floats in it.请注意在上面的示例中,每个元素都是一个包含三个浮点数的列表。 If you change one of those lists to contain 2 or 4 floats then you are breaking the idea that the numpy array must have a fixed dimensionality and it will no longer work.如果您将其中一个列表更改为包含 2 个或 4 个浮点数,那么您就打破了 numpy 数组必须具有固定维度并且将不再起作用的想法。

Here's what works.这是有效的。

 R = np.matrix(
              [   #w   x   y  z     height    aspect-ratio
                  [.5,.5,2.,1.,     2.,        2.5]   
              ] 
            )
  P = R[:,0:4]    #get the first four columns  
  r = (P - 1/2)* 2 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM