A brief introduction to Numpy
What is Numpy
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.
Numpy Arrays
Creating a Numpy array
[4, 5, 6],
[7, 8, 9]])
Indexing an array
Slicing an array
Subsetting a Numpy array
Selecting a column if columns are named
>>> named_array['column_name']
Math
Arrays let you do basic math on every element in the array
Linear algebra can be done using matrices
[32]])
Importing data
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).
Basic
>>> data = np.genfromtxt('C:\path\to\file\datafile.csv', delimiter=',')
Auto-detect data types by column
>>> data = np.genfromtxt('C:\path\to\file\datafile.csv', dtype=None, delimiter=',')
Naming columns
>>> data = np.genfromtxt('C:\path\to\file\datafile.csv', names=['column1', 'column2', 'column3'], delimiter=',')
Get column names from header row
>>> data = np.genfromtxt('C:\path\to\file\datafile.csv', names=True, delimiter=',')
Exporting data
>>> np.savetxt('C:\path\to\file\outputfile.csv', example_matrix, delimiter=',')
Random number generation
Random uniform (0 to 1)
>>> np.random.rand(rows, cols)
Random normal (mean = 0, stdev = 1)
>>> np.random.randn(rows, cols)
Random integers
>>> np.random.randint(min, max, [rows, cols])

