[PYTHON] Pharmaceutical company researchers summarized NumPy

Introduction

NumPy is a library for high-speed vector and matrix calculations. Here, we will explain the methods that are often used in NumPy. It is supposed to use Python3 series.

import

To use NumPy, you first need to load the library. By convention, it is often np.

NumPy_1.py


import numpy as np

One-dimensional array

With NumPy, you can create an array like a list.

NumPy_2.py


import numpy as np


np_arr_1 = np.array([1, 2, 5])

print(np_arr_1)
print(type(np_arr_1)) #ndarray class
print(np_arr_1**2) #Square each element.


np_arr_2 = np.array([3, 5, 7])

print(np_arr_1 + np_arr_2) #Add for each element.


np_arr_3 = np.arange(10)

print(np_arr_3)

np_arr_3[0:2] = 100
print(np_arr_3)

The feature of NumPy is that you can easily calculate between elements. If you try to perform the above process without using NumPy, it will be as follows.

NumPy_3.py


arr_1 = [1, 2, 5]

for i, num in enumerate(arr_1):
    arr_1[i] = num ** 2
print(arr_1)

Note that in a NumPy array, updating the value of the element to which it is assigned will also update the value of the original array. To prevent the original array from being updated, use the copy () method.

NumPy_4.py


import numpy as np


np_arr_1 = np.array([1, 2, 5])
np_arr_2 = np_arr_1
np_arr_2[1] = 100
print(np_arr_1) # np_arr_1 has also changed.
print(np_arr_2)


np_arr_1 = np.array([1, 2, 5])
np_arr_2 = np_arr_1.copy()
np_arr_2[1] = 100
print(np_arr_1) # np_arr_1 does not change.
print(np_arr_2)

It is also possible to extract only the elements that satisfy certain conditions.

NumPy_5.py


import numpy as np


np_arr_1 = np.array([1, 2, 5])
print(np_arr_1[np_arr_1 % 2 == 1]) #Extract only odd elements.

NumPy provides a function called a "universal function" that returns the result of an operation on each element of an array.

NumPy_6.py


import numpy as np


np_arr_4 = np.array([-1, 2, -3])
print(np.abs(np_arr_4)) #Absolute value of each element

np_arr_5 = np.array([1, 9, 25])
print(np.sqrt(np_arr_5)) #Square root of each element

NumPy can also generate random numbers.

NumPy_7.py


import numpy as np


np_arr_6 = np.random.randint(0, 10, 5) #Randomly generate 5 integers between 0 and 9.
print(np_arr_6)

np_arr_7 = np.random.randint(5) #Randomly generate one integer from 0 to 4.
print(np_arr_7)

By rewriting the ʻimport` part, you can write it shorter and easier.

Numpy_8.py


from numpy.random import randint


np_arr_6 = randint(0, 10, 5)
print(np_arr_6)

np_arr_7 = randint(5)
print(np_arr_7)

A two-dimensional array

The methods for 2D arrays are as follows.

NumPy_9.py


import numpy as np


np_arr_8 = np.array([[1, 2, 3], [4, 5, 6]])
print(np_arr_8)
print(np_arr_8[1])
print(np_arr_8[1, 1])
print(np_arr_8.sum())
print(np_arr_8.sum(axis=0)) #Calculated column by column
print(np_arr_8.sum(axis=1)) #Calculated row by row
print(np_arr_8.shape)
print(np_arr_8.reshape(3, 2))
print(np_arr_8.T)
print(np.transpose(np_arr_8))
print(np_arr_8.mean()) #average
print(np.average(np_arr_8)) #average
print(np.max(np_arr_8)) #Maximum value
print(np.min(np_arr_8)) #minimum value
print(np.std(np_arr_8)) #standard deviation
print(np.var(np_arr_8)) #Distributed
print(np.argmax(np_arr_8)) #Index number of the maximum element
print(np.argmin(np_arr_8)) #Index number of the element with the lowest value

Summary

Here, we have introduced methods that are often used in NumPy. The best way to master it is to actually use it.

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python's data structures
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers have summarized the operators used in Python
How to install Python for pharmaceutical company researchers
A pharmaceutical company researcher summarized the basic description rules of Python