layout | element | title | language |
---|---|---|---|
page |
notes |
A brief introduction to Numpy |
Python |
Numpy is the fundamental library for scientific computing in Python. It contains list like objects that work like arrays, matrices, and data tables. This is how scientists typically expect data to behave. Numpy also provides linear algebra, Fourier transforms, random number generation, and tools for integrating C/C++ and Fortran code.
import numpy as np
example_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
example_array
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
example_array[1, 1]
5
example_array[:, 0]
array([1, 4, 7])
example_array[1, :]
array([4, 5, 6])
example_array[1:3, 1:3]
array([[5, 6]
[8, 9]])
array1 = np.array([1, 1, 1, 2, 2, 2, 1])
array2 = np.array([1, 2, 3, 4, 5, 6, 7])
array2[array1==1]
array([1, 2, 3, 7])
array3 = np.array(['a', 'a', 'a', 'b', 'b', 'b', 'b'])
array2[(array1==1) & (array3=='a')]
array([1, 2, 3])
named_array['column_name']
array1 * 2 + 1
array([3, 3, 3, 5, 5, 5, 3])
array1 + array2
array([2, 3, 4, 6, 7, 8, 8])
matrix1 = np.matrix([[1, 2, 3], [4, 5, 6]])
matrix2 = np.matrix([1, 2, 3])
matrix1 * matrix2.transpose()
matrix([[14], [32]])
The numpy function genfromtxt is a powerful way to import text data. It can use different delimiters, skip header rows, control the type of imported data, give columns of data names, and a number of other useful goodies. See the documentation for a full list of features of run help(np.genfromtxt) from the Python shell (after importing the module of course).
data = np.genfromtxt('C:pathtofiledatafile.csv', delimiter=',')
data = np.genfromtxt('C:pathtofiledatafile.csv', dtype=None, delimiter=',')
data = np.genfromtxt('C:pathtofiledatafile.csv', names=['column1', 'column2', 'column3'], delimiter=',')
data = np.genfromtxt('C:pathtofiledatafile.csv', names=True, delimiter=',')
np.savetxt('C:pathtofileoutputfile.csv', example_matrix, delimiter=',')
np.random.rand(rows, cols)
np.random.randn(rows, cols)
np.random.randint(min, max, [rows, cols])