numpy 数组中元素的乘法和除法给出整数结果 [英] Multiplication and Division of elements in a numpy array gives integer results

查看:67
本文介绍了numpy 数组中元素的乘法和除法给出整数结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import numpy as np
A = np.array([[2,1,-1,8],
         [-3,-1,2,-11],
         [-2,1,2,-3]])

B =  A[1]+A[0]* (-A[1][0]/A[0][0])
print(B) #B =[ 0.   0.5  0.5  1. ]
A[1] = A[1]+A[0]* (-A[1][0]/A[0][0])
print(A[1]) #A[1] = [0 0 0 1]

上述情况是怎么发生的,我该怎么办?

How does the above situation happen, and what can I do about it?

推荐答案

在主数组中使用 dtype=float.您的数组默认为整数.

Use dtype=float in main array. Your array is integer by default.

import numpy as np
A = np.array([[2,1,-1,8],
         [-3,-1,2,-11],
         [-2,1,2,-3]], dtype=float)
B =  A[1] + (A[0]*(-A[1,0]/A[0,0]))
print(B)
A[1] = A[1] + (A[0]*(-A[1,0]/A[0,0]))
print(A[1])
#Output:
#[ 0.   0.5  0.5  1. ]
#[ 0.   0.5  0.5  1. ]

这篇关于numpy 数组中元素的乘法和除法给出整数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆