When dividing elements of matrix i would like the result 0 instead of inf? - Printable Version +- Python Forum ( https://python-forum.io)+-- Forum: Python Coding ( https://python-forum.io/forum-7.html)+--- Forum: Data Science ( https://python-forum.io/forum-44.html)+--- Thread: When dividing elements of matrix i would like the result 0 instead of inf? ( /thread-19967.html) |

When dividing elements of matrix i would like the result 0 instead of inf? - Koczkodan - Jul-22-2019
Hi, While dividing element from first matrix by second matrix value 0 I need result to be 0 instead of inf. example: 1 1 2 6 divided by 1 0 4 3 should be 1 0(now got inf) 0,5 2 import numpy as np np.savetxt("result.txt", np.divide(np.loadtxt("1.txt"), np.loadtxt("2.txt"))) m1, m2 = np.loadtxt("1.txt"), np.loadtxt("2.txt") result = np.divide(m1, m2) RE: 0 instead of inf - scidam - Jul-22-2019
It is normal that division by zero yields infinity, i.e. inf (floating point). However, you can change all infs to zeros, e.g. result[np.isinf(result)] = 0.0 RE: 0 instead of inf - Koczkodan - Jul-22-2019
(Jul-22-2019, 09:41 AM)scidam Wrote: It is normal that division by zero yields infinity, i.e. inf (floating point). still got inf in result :/ RE: 0 instead of inf - DeaD_EyE - Jul-22-2019
You have to do this after the calculation. RE: 0 instead of inf - Koczkodan - Jul-22-2019
(Jul-22-2019, 11:19 AM)DeaD_EyE Wrote: You have to do this after the calculation. I think I did: import numpy as np np.savetxt("result.txt", np.divide(np.loadtxt("1.txt"), np.loadtxt("2.txt"))) m1, m2 = np.loadtxt("1.txt"), np.loadtxt("2.txt") result = np.divide(m1, m2) result[np.isinf(result)] = 0.0maybe sth else is wrong? I'm not a coder just need this to work so that you for your patience |