Numpy-arithmetic-operations
NumPy-算術演算
add()、subtract()、multiply()、divide()などの算術演算を実行するための入力配列は、同じ形状であるか、配列のブロードキャストルールに準拠する必要があります。
例
import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
print 'First array:'
print a
print '\n'
print 'Second array:'
b = np.array([10,10,10])
print b
print '\n'
print 'Add the two arrays:'
print np.add(a,b)
print '\n'
print 'Subtract the two arrays:'
print np.subtract(a,b)
print '\n'
print 'Multiply the two arrays:'
print np.multiply(a,b)
print '\n'
print 'Divide the two arrays:'
print np.divide(a,b)
それは次の出力を生成します-
First array:
[[Second array:
[10 10 10]
Add the two arrays:
[[Subtract the two arrays:
[[Multiply the two arrays:
[[Divide the two arrays:
[[Let us now discuss some of the other important arithmetic functions available in NumPy.
=== numpy.reciprocal()
This function returns the reciprocal of argument, element-wise. For elements with absolute values larger than 1, the result is always 0 because of the way in which Python handles integer division. For integer 0, an overflow warning is issued.
==== Example
[source,prettyprint,notranslate]
numpyをnp a = np.array([0.25、1.33、1、0、100])としてインポート
print 'Our array is:' print a '\ n'
print '逆関数を適用した後:' print np.reciprocal(a)print '\ n'
b = np.array([100]、dtype = int)print '2番目の配列は:' print b print '\ n'
print '相反関数を適用した後:' print np.reciprocal(b)
It will produce the following output −
[source,result,notranslate]
配列は次のとおりです。 [ 0.25 1.33 1. 0. 100. ]
相互関数を適用した後:main.py:9:RuntimeWarning:相互印刷でゼロ除算が発生しましたnp.reciprocal(a) [ 4. 0.7518797 1. inf 0.01 ]
2番目の配列は次のとおりです。
相互関数を適用した後:
=== numpy.power()
This function treats elements in the first input array as base and returns it raised to the power of the corresponding element in the second input array.
[source,prettyprint,notranslate]
numpyをnp a = np.array([10,100,1000])としてインポート
print 'Our array is:' print a '\ n'
print 'パワー関数の適用:' print np.power(a、2)print '\ n'
print '2番目の配列:' b = np.array([1,2,3])print b print '\ n'
print '電力関数の再適用:' print np.power(a、b)
It will produce the following output −
[source,result,notranslate]
配列は次のとおりです。 [ 10 100 1000]
べき関数の適用: [ 100 10000 1000000]
2番目の配列:
再び電源機能を適用する: [ 10 10000 1000000000]
=== numpy.mod()
This function returns the remainder of division of the corresponding elements in the input array. The function *numpy.remainder()* also produces the same result.
[source,prettyprint,notranslate]
numpyをnp a = np.array([10,20,30])b = np.array([3,5,7])としてインポート
印刷 '最初の配列:'印刷 '\ n'
print '2番目の配列:' print b print '\ n'
print 'mod()関数の適用:' print np.mod(a、b)print '\ n'
print 'Applying remaining()function:' print np.remainder(a、b)
It will produce the following output −
[source,result,notranslate]
最初の配列:
2番目の配列:
mod()関数の適用:
remaining()関数の適用:
The following functions are used to perform operations on array with complex numbers.
* *numpy.real()* − returns the real part of the complex data type argument.
* *numpy.imag()* − returns the imaginary part of the complex data type argument.
* *numpy.conj()* − returns the complex conjugate, which is obtained by changing the sign of the imaginary part.
* *numpy.angle()* − returns the angle of the complex argument. The function has degree parameter. If true, the angle in the degree is returned, otherwise the angle is in radians.
[source,prettyprint,notranslate]
numpyをnp a = np.array([-5.6j、0.2j、11。 、1 + 1j])
print 'Our array is:' print a '\ n'
print 'real()関数の適用:' print np.real(a)print '\ n'
print 'imag()関数の適用:' print np.imag(a)print '\ n'
print 'conj()関数の適用:' print np.conj(a)print '\ n'
print 'applying angle()function:' print np.angle(a)print '\ n'
print '再度angle()関数を適用(度で結果)' print np.angle(a、deg = True)
It will produce the following output −
[source,result,notranslate]
配列は次のとおりです。 [ 0.-5.6j 0.+0.2j 11.+0.j 1.+1.j ]
real()関数の適用: [ 0. 0. 11. 1.]
imag()関数の適用: [-5.6 0.2 0. 1. ]
conj()関数の適用: [ 0.+5.6j 0.-0.2j 11.-0.j 1.-1.j ]
angle()関数の適用: [-1.57079633 1.57079633 0. 0.78539816]
angle()関数を再度適用します(度で結果) [-90. 90. 0. 45.]