Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ndarray(numpython)赋值分配0的数组:为什么?_Python_Numpy - Fatal编程技术网

ndarray(numpython)赋值分配0的数组:为什么?

ndarray(numpython)赋值分配0的数组:为什么?,python,numpy,Python,Numpy,所以在这个简单的代码中 result[columnNumber] = column #this assignment fails for some reason! 失败了,特别是它只是分配了一个零数组,而不是它应该分配的,我不知道为什么!这是完整的代码: """Softmax.""" scores = [3.0, 1.0, 0.2] import numpy as np def softmax(x): """Compute softmax values for each

所以在这个简单的代码中

result[columnNumber] = column #this assignment fails for some reason!
失败了,特别是它只是分配了一个零数组,而不是它应该分配的,我不知道为什么!这是完整的代码:

    """Softmax."""

scores = [3.0, 1.0, 0.2]

import numpy as np

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    data=np.array(x)
    columnNumber=0
    result=data.copy()
    result=result.T

    for column in data.T:
        sumCurrentColumn=0
        try: #Since 'column' can potentially be just a double,and sum needs some iterable object
            sumCurrentColumn=sum(np.exp(column))
        except TypeError:
            sumCurrentColumn=np.exp(column)



        column=np.divide(np.exp(column),sumCurrentColumn)
        print(column)
        print('before assignment:'+str(result[columnNumber]))
        result[columnNumber] = column #this assignment fails for some reason!
        print('after assignment:'+str(result[columnNumber]))
        columnNumber+=1 

    result=result.T

    return result


scores = np.array([[1, 2, 3, 6],
                   [2, 4, 5, 6],
                   [3, 8, 7, 6]])

print(softmax(scores))
这是它的输出:

   [ 0.09003057  0.24472847  0.66524096]
before assignment:[1 2 3]
after assignment:[0 0 0]
[ 0.00242826  0.01794253  0.97962921]
before assignment:[2 4 8]
after assignment:[0 0 0]
[ 0.01587624  0.11731043  0.86681333]
before assignment:[3 5 7]
after assignment:[0 0 0]
[ 0.33333333  0.33333333  0.33333333]
before assignment:[6 6 6]
after assignment:[0 0 0]
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

在您的示例中,输入的
分数
都是整数,因此
数据
数组的数据类型是整数。因此,
result
也是一个整数数组。不能将浮点值赋给整数数组——numpy数组具有不能动态更改的同构数据类型。行
result[columnNumber]=column
列中的值截断为整数,由于它们都在0和1之间,所以分配的值都是0

尝试将
结果的创建更改为:

result = data.astype(float)

(默认情况下,
astype
方法创建一个副本,即使
数据
已经具有指定的类型。)

您的数组
结果
具有类型
int
,因此所有浮点值都会自动转换为
int
,在这种情况下
0
。使用这个
result=data.astype(float)

我感觉有点糟糕,我自己拿不到:)Thx人!顺便说一句,因为我不知道x是否是多维数组,我想我必须使用异常处理,因为sum()作为参数iterable,而double或int不是。但这感觉不对。你知道有什么更优雅的方法可以做到这一点吗?