[Python] Calculation method with numpy

Calculation with numpy

Create an array from 0 to 10 using numpy.

import numpy as np

arr = np.arange(11)

arr

Execution result.


array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

You can calculate the square root of each number by using the sqrt method.

np.sqrt(arr)

Execution result.


array([ 0.48273727, -1.28739284,  1.52422575, -1.73666091, -0.25126809,
       -0.41952278, -0.75042054, -0.64585434, -0.86014472, -0.44542315])

You can calculate random numbers with a normal distribution (mean = 0, variance = 1) with random.randn ().

A = np.random.randn(10)

Execution result.


array([-0.94897439, -0.43075947,  0.088691  ,  0.37859721, -0.2141078 ,
        1.30327378,  0.41654781, -0.42907224, -1.61139916, -0.651694  ])

You can add by using the add method.

B = np.random.randn(10)

B

array([ 0.05623043,  1.97843447, -0.02581691,  0.70663108,  0.51213852,
       -0.70386143,  1.50729471, -0.00577632, -1.08456477, -0.38103167])

np.add(A,B)

Execution result.


array([ 0.5389677 ,  0.69104164,  1.49840884, -1.03002983,  0.26087043,
       -1.1233842 ,  0.75687417, -0.65163066, -1.94470949, -0.82645482])

You can subtract with the subtract method.

np.subtract(A,B)

Execution result.


array([ 0.42650684, -3.26582731,  1.55004266, -2.44329199, -0.76340661,
        0.28433865, -2.25771524, -0.64007803,  0.22442006, -0.06439147])

You can multiply with the multiply method.

np.multiply(A,B)

Execution result.


array([ 0.02714452, -2.54702237, -0.0393508 , -1.22717858, -0.12868407,
        0.2952859 , -1.1311049 ,  0.00373066,  0.93288266,  0.16972033])

You can divide with the divide method.

np.divide(A,A)

Execution result.


array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

Recommended Posts

[Python] Calculation method with numpy
Kernel Method with Python
python numpy array calculation
Numerical calculation with Python
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Implemented SMO with Python + NumPy
Python3 | Getting Started with numpy
My Numpy (Python)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Automatic update method with python Pyinstaller exe
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
#Python basics (#Numpy 1/2)
with syntax (Python)
#Python basics (#Numpy 2/2)
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Bingo with python
Zundokokiyoshi with python
Handle numpy with Cython (method by memoryview)
Python #Numpy basics
Johnson method (python)
Excel with Python
[Python] Numpy memo
[Python] Semi-Lagrange method
Microcomputer with Python
Cast with python
Debug with VS Code using boost python numpy
Track green objects with python + numpy (particle filter)
[Rust / Python] Handle numpy with PyO3 (August 2020 version)
Calculation of mutual information (continuous value) with numpy
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Python and numpy tips
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learn Python with ChemTHEATER
Run prepDE.py with python3
Collecting tweets with Python
3. 3. AI programming with Python
Non-blocking with Python + uWSGI
Python installation method Windows
Scraping with Python + PhantomJS
Python basics 8 numpy test