Following Python class learned by chemoinformatics, it is one of the representative libraries of Python with the theme of lipidomics (exhaustive analysis of lipids). I will explain about "NumPy". We will mainly explain practical examples of chemoinformatics, so if you want to check the basics, please read the following article before reading this article.
Pharmaceutical researcher summarized NumPy
By using NumPy, you can perform vector and matrix operations at high speed.
First, you can load the library with ʻimport library name. Furthermore, by adding ʻas abbreviation
, you can call (point to) the library with the abbreviation written here.
In the case of NumPy, it is customary to use np
.
import numpy as np
masses = np.array([12, 1.00783, 15.99491]) #An ndarray containing the precise masses of carbon, hydrogen, and oxygen atoms
numbers = np.array([16, 32, 2]) #Number of carbon, hydrogen and oxygen atoms in palmitic acid
print(masses * numbers) #Calculate precision mass for each element
print(sum((masses * numbers))) #Precision mass of palmitic acid
As shown above, you can create a vector with multiple elements by doing np.array (list)
.
When you perform four arithmetic operations, the operation is performed for each element.
You can also use sum
to find the sum of all the elements.
The vector created by np.array (list)
is very similar to a list, but the data type is numpy.ndarray
instead of list
.
Multiplying numpy.ndarray
s will return numpy.ndarray
multiplied by each element, but multiplying list
s will result in TypeError
and the operation cannot be performed.
import numpy as np
masses_ndarray = np.array([12, 1.00783, 15.99491])
print(type(masses_ndarray)) # numpy.ndarray
masses_list = [12, 1.00783, 15.99491]
print(type(masses_list)) # list
print(list(masses_ndarray)) # [12, 1.00783, 15.99491]
numbers_list = [16, 32, 2]
# print(masses_list * numbers_list) # TypeError
Next, consider finding the atomic weight based on the precise mass of the atom and its isotope and the natural abundance ratio.
import numpy as np
exact_mass_H = np.array([1.00783, 2.01410]) #Precision isotope mass
isotope_ratio_H = np.array([0.99989, 0.00011]) #Natural abundance of isotopes
molecular_weight_H = sum(exact_mass_H * isotope_ratio_H) #Calculate atomic weight
print(molecular_weight_H) #Atomic weight of hydrogen
Finally, let's find the precise masses of multiple fatty acid molecular species.
import numpy as np
numbers = np.array([[16, 32, 2], [18, 36, 2], [18, 34, 2]]) #Elemental composition of palmitic acid, stearic acid, oleic acid
print(masses * numbers) #Calculate the precise mass of each element of each molecular species
print(np.sum(masses * numbers, axis=1)) #Calculate the precise mass of each molecular species
Here, I explained about NumPy, focusing on practical knowledge that can be used in chemoinformatics. Let's review the main points again.
--By using NumPy, you can easily perform operations between vector elements. --Can be used to calculate atomic weight, molecular weight, and precise mass.
Next, I will explain about Pandas in the following article.
Pandas learning from chemoinformatics
What is the programming language Python? Can it be used for AI and machine learning?
Recommended Posts