Reading 11
NumPy
Numpy is a data analysis package in Python. This package can simplify calculations ina variety of calculations.
2D Arrays (Matrix)
Note: In numpy, rows are considered the first axis, columns are considered the second axis.
Now, let’s import and use numpy:
import numpy as np
wines = np.array(wines[1:], dtype=np.float)
Now when you display “wines” you get:
array([[ 7.4 , 0.7 , 0. , ..., 0.56 , 9.4 , 5. ],
[ 7.8 , 0.88 , 0. , ..., 0.68 , 9.8 , 5. ],
[ 7.8 , 0.76 , 0.04 , ..., 0.65 , 9.8 , 5. ],
...,
[ 6.3 , 0.51 , 0.13 , ..., 0.75 , 11. , 6. ],
[ 5.9 , 0.645, 0.12 , ..., 0.71 , 10.2 , 5. ],
[ 6. , 0.31 , 0.47 , ..., 0.66 , 11. , 6. ]])
You can return the number of rows and columns using shape
wines.shape
returns
(1599,12)
Other useful Array Methods:
- np.zeros((2,3)) - creates matrix with 2 rows and 3 columns of zeros.
- np.random.rand(2,3) - creates matrix with 2 rows and 3 columns of random numbers
- np.genfromtxt(“file-name.csv”, delimiter=”;”, skip_header=1) - upacks data from a csv into a matrix
- data_name[:,3] - selects entire 4th row (0 indexed)
- data_name[3,:] - selects entire 4th column
- data_name[1:5] = “apple” - assigns item at column 1, row 5 to “apple”
- data_name[:,3] = “banana” - assigns entire column to “banana”
source: https://www.dataquest.io/blog/numpy-tutorial-python/