Basics
Importing NumPy
NumPy (short for “Numerical Python”) is a Python module used for numerical computing, creating arrays and matrices, and performing very fast operations on those data structures.
To use NumPy with Python, import it at the top of your file using the following line:
import numpy as np
Writing as np
allows us to use np
as a shorthand for NumPy, which saves us time when calling a NumPy function (less typing = fewer errors!)
NumPy Arrays
The core of NumPy is a multidimensional Array object.
Creating NumPy Arrays from list
The NumPy .array()
method is used to create new NumPy Arrays.
data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1
# array([ 6. , 7.5, 8. , 0. , 1. ])
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = np.array(data2)
arr2
# array([[1, 2, 3, 4],[5, 6, 7, 8]])
Creating NumPy Arrays from files
NumPy Arrays can be created from data in CSV files or other delimited text by using the np.genfromtxt()
method. The named parameter delimiter
is used to determine the delimiting character between values.
imported_csv = np.genfromtxt("filename.txt",
delimiter=",")
Creating special NumPy Arrays using functions
.arange()
: produce an array of a sequence from 0 to the input (excluded), similar torange()
..zeros()
: produce an array of all 0’s with the given shape and dtype..ones()
: produce an array of all 1’s with the given shape and dtype..empty()
: create new arrays by allocating new memory, but do not populate with any values like ones and zeros.eye(), .identity()
: create a square N x N identity matrix (1’s on the diagonal and 0’s elsewhere)
# create a sequence from 0 to 5
np.arange(6)
# array([ 0, 1, 2, 3, 4, 5])
# create all zero vector
np.zeros(10)
# array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
# create all one matrix
np.ones((2,3))
# array([[ 1., 1., 1.],
# [ 1., 1., 1.]])
# create empty 3-dimensional array
np.empty((2,3,4))
# array([[[ -1.72723371e-077, -3.11109840e+231, 2.25215972e-314,
# 2.25994200e-314],
# [ -1.72723371e-077, -1.72723371e-077, 5.43472210e-323,
# 0.00000000e+000],
# [ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
# 0.00000000e+000]],
#
# [[ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
# 0.00000000e+000],
# [ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
# 0.00000000e+000],
# [ -1.72723371e-077, -1.72723371e-077, -1.72723371e-077,
# 1.73060593e-077]]])
# create identity matrix
np.eye(3)
# array([[ 1., 0., 0.],
# [ 0., 1., 0.],
# [ 0., 0., 1.]])
# create identity matrix
np.identity(3)
# array([[ 1., 0., 0.],
# [ 0., 1., 0.],
# [ 0., 0., 1.]])
Two-Dimensional Arrays
In Python, we can create lists that are made up of other lists. Similarly, in NumPy we can create an array of arrays. If the arrays that make up our bigger array are all the same size, then it has a special name: a two-dimensional array.
# this is a two-dimensional NumPy array
np.array([[92, 94, 88, 91, 87],
[79, 100, 86, 93, 91],
[87, 85, 72, 90, 92]])
# this will run but it will not create
# a two-dimensional array
np.array([[29, 49, 6],
[77, 1]])
# create 1-dimension then reshape
arr = np.arange(32).reshape((8, 4))
# array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11],
# [12, 13, 14, 15],
# [16, 17, 18, 19],
# [20, 21, 22, 23],
# [24, 25, 26, 27],
# [28, 29, 30, 31]])
Dimension of NumPy Arrays
arr2.ndim
# 2
arr2.shape
# (2, 4)
Data Type in NumPy Arrays
# assign data type when create the nbarray
arr1 = np.array([1, 2, 3], dtype=np.float64)
arr2 = np.array([1, 2, 3], dtype=np.int32)
arr1.dtype
# dtype('float64')
arr2.dtype
# dtype('int32')
# convert from other data type
arr = np.array([1, 2, 3, 4, 5])
arr.dtype
# dtype('int64')
float_arr = arr.astype(np.float64)
float_arr.dtype
# dtype('float64')
# convert from string to float
numeric_strings = np.array(['1.25', '-9.6', '42'],
dtype=np.string_)
numeric_strings.astype(float)
# array([ 1.25, -9.6 , 42. ])
# use another array’s dtype attribute
int_array = np.arange(10)
calibers = np.array([.22, .270, .357, .380, .44, .50],
dtype=np.float64)
int_array.astype(calibers.dtype)
# array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
Note: Calling .astype()
always creates a new array (a copy of the data), even if the new dtype is the same as the old dtype.
Last updated
Was this helpful?