NumPy

Post date: Mar 02, 2020 4:22:0 AM

NumPy is one of the fundamental packages for scientific computing in Python. It contains functionality for multidimensional arrays, high-level mathematical functions such as linear algebra operations and the Fourier transform, and pseudorandom number generators. In scikit-learn, the NumPy array is the fundamental data structure. scikit-learn takes in data in the form of NumPy arrays. Any data you’re using will have to be converted to a NumPy array. The core functionality of NumPy is the ndarray class, a multidimensional (n-dimensional) array. All elements of the array must be of the same type. A NumPy array looks like this:

In[2]:

import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])

print("x:\n{}".format(x))

Essential Libraries and Tools | 7

Out[2]:

x:

[[1 2 3]

[4 5 6]]

We will refer to objects of the NumPy ndarray class as “NumPy arrays” or just “arrays.

12 Basic Commands with NumPy Array

NumPy (pronounced as Num-pee or Num-pai) is one of the important python packages (other being SciPy) for scientific computing. NumPy offers fast and flexible data structures for multi-dimensional arrays and matrices with numerous mathematical functions/operations associated with it. Core data structure in NumPy is “ndarray”, short for n-dimesional array for storing numeric values. Let us get started with some basic commands with NumPy 1d-array (one-dimensional array).

How to import NumPy package?

1

2

# import numpy package with nickname "np"

>import numpy as np

How to create a one-dimensional array?

We can create a NumPy array using the function array with a list of numbers as argument.

1

2

3

4

# create a numpy array

>my_first_array = np.array([1, 2, 3,4,5])

>my_first_array

array([1, 2, 3, 4, 5])

How to find the length (or number of elements) of a 1d-array?

We can find the number of elements in a 1d-array or the length of the array using the function len.

1

2

3

# length of the array

>len(my_first_array)

5

How to sum all the elements in 1d-array?

If we want to sum all the elements in a 1d numpy array using the function sum. This is way faster than a manually using a for loop going through all elements in a 1d-array.

1

2

3

4

# sum of all elements in the array

>np.sum(my_first_array)

>my_first_array.sum()

15

How to find the maximum value in NumPy 1d-array?

We can find the maximum value stored in 1d-array using NumPy’s max function.

1

2

3

4

# maximum value the elements in the array

>np.max(my_first_array)

>my_first_array.max()

5

How to find the minimum value in NumPy 1d-array?

Similarly, we can find the minimum value stored in 1d-array using NumPy’s min function.

1

2

3

4

# minimum value the elements in the array

>np.min(my_first_array)

>my_first_array.min()

1

How to create NumPy 1d-array with 1s?

Sometimes you may want to create a numpy array with 1s in all elements. NumPy’s ones function can create 1d-array with 1s. We need to specify the length of NumPy array as argument.

1

2

3

# create a numpy array of 1s (of length 5)

>np.ones(5)

array([ 1., 1., 1., 1., 1.])

How to create NumPy 1d-array with 0s?

Similarly we can create 1d NumPy array with 0s in it using zeros function.

1

2

3

# create a numpy array of length 5 with 5 zeros

>np.zeros(5)

array([ 0., 0., 0., 0., 0.])

How to create empty NumPy 1d-array of specified length?

Sometime you may want to create an empty array with no values in it. We can use NumPy’s empty function to create empty NumPy array.

1

2

3

4

# create an empty numpy array of length 5

>np.empty(5)

array([ 1.49166815e-154, 1.49166815e-154, 3.03574399e-152,

2.64522460e+185, 1.45913207e-152])

How to create NumPy 1d-array with a range of numbers?

1

2

3

4

5

6

# create a numby array with a range of numbers 0 to 9

>np.arange(10)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# create a numpy array with a range of numbers and step size

>np.arange(0, 9, 2)

array([0, 2, 4, 6, 8])

How to create NumPy 1d-array with random numbers?

1

2

3

# create an array with random numbers

>np.random.random(5)

array([ 0.4648749 , 0.9236576 , 0.93804724, 0.86871356, 0.49829188])

How to create NumPy 1d-array with equally spaced numbers in an interval?

1

2

3

4

# Return evenly spaced numbers over a specified interval.

>np.linspace(0, 1, 10)

array([ 0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,

0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])

NumPy Cheat Sheet — Python for Data Science

NumPy is the library that gives Python its ability to work with data at speed. Originally, launched in 1995 as ‘Numeric,’ NumPy is the foundation on which many important Python data science libraries are built, including Pandas, SciPy and scikit-learn.

It’s common when first learning NumPy to have trouble remembering all the functions and methods that you need, and while at Dataquest we advocate getting used to consulting the NumPy documentation, sometimes it’s nice to have a handy reference, so we’ve put together this cheat sheet to help you out!

If you’re interested in learning NumPy, you can consult our NumPy tutorial blog post, or you can signup for free and start learning NumPy through our interactive Python data science course.

Key and Imports

In this cheat sheet, we use the following shorthand:

arr | A NumPy Array object

You’ll also need to import numpy to get started:

import numpy as np

Importing/exporting

np.loadtxt('file.txt') | From a text file

np.genfromtxt('file.csv',delimiter=',') | From a CSV file

np.savetxt('file.txt',arr,delimiter=' ') | Writes to a text file

np.savetxt('file.csv',arr,delimiter=',') | Writes to a CSV file

Creating Arrays

np.array([1,2,3]) | One dimensional array

np.array([(1,2,3),(4,5,6)]) | Two dimensional array

np.zeros(3) | 1D array of length 3 all values 0

np.ones((3,4)) | 3x4 array with all values 1

np.eye(5) | 5x5 array of 0 with 1 on diagonal (Identity matrix)

np.linspace(0,100,6) | Array of 6 evenly divided values from 0 to 100

np.arange(0,10,3) | Array of values from 0 to less than 10 with step 3 (eg [0,3,6,9])

np.full((2,3),8) | 2x3 array with all values 8

np.random.rand(4,5) | 4x5 array of random floats between 01

np.random.rand(6,7)*100 | 6x7 array of random floats between 0100

np.random.randint(5,size=(2,3)) | 2x3 array with random ints between 04

Inspecting Properties

arr.size | Returns number of elements in arr

arr.shape | Returns dimensions of arr (rows,columns)

arr.dtype | Returns type of elements in arr

arr.astype(dtype) | Convert arr elements to type dtype

arr.tolist() | Convert arr to a Python list

np.info(np.eye) | View documentation for np.eye

Copying/sorting/reshaping

np.copy(arr) | Copies arr to new memory

arr.view(dtype) | Creates view of arr elements with type dtype

arr.sort() | Sorts arr

arr.sort(axis=0) | Sorts specific axis of arr

two_d_arr.flatten() | Flattens 2D array two_d_arr to 1D

arr.T | Transposes arr (rows become columns and vice versa)

arr.reshape(3,4) | Reshapes arr to 3 rows, 4 columns without changing data

arr.resize((5,6)) | Changes arr shape to 5x6 and fills new values with 0

Adding/removing Elements

np.append(arr,values) | Appends values to end of arr

np.insert(arr,2,values) | Inserts values into arr before index 2

np.delete(arr,3,axis=0) | Deletes row on index 3 of arr

np.delete(arr,4,axis=1) | Deletes column on index 4 of arr

Combining/splitting

np.concatenate((arr1,arr2),axis=0) | Adds arr2 as rows to the end of arr1

np.concatenate((arr1,arr2),axis=1) | Adds arr2 as columns to end of arr1

np.split(arr,3) | Splits arr into 3 sub-arrays

np.hsplit(arr,5) | Splits arr horizontally on the 5th index

Indexing/slicing/subsetting

arr[5] | Returns the element at index 5

arr[2,5] | Returns the 2D array element on index [2][5]

arr[1]=4 | Assigns array element on index 1 the value 4

arr[1,3]=10 | Assigns array element on index [1][3] the value 10

arr[0:3] | Returns the elements at indices 0,1,2 (On a 2D array: returns rows 0,1,2)

arr[0:3,4] | Returns the elements on rows 0,1,2 at column 4

arr[:2] | Returns the elements at indices 0,1 (On a 2D array: returns rows 0,1)

arr[:,1] | Returns the elements at index 1 on all rows

arr<5 | Returns an array with boolean values

(arr1<3) & (arr2>5) | Returns an array with boolean values

~arr | Inverts a boolean array

arr[arr<5] | Returns array elements smaller than 5

Scalar Math

np.add(arr,1) | Add 1 to each array element

np.subtract(arr,2) | Subtract 2 from each array element

np.multiply(arr,3) | Multiply each array element by 3

np.divide(arr,4) | Divide each array element by 4 (returns np.nan for division by zero)

np.power(arr,5) | Raise each array element to the 5th power

Vector Math

np.add(arr1,arr2) | Elementwise add arr2 to arr1

np.subtract(arr1,arr2) | Elementwise subtract arr2 from arr1

np.multiply(arr1,arr2) | Elementwise multiply arr1 by arr2

np.divide(arr1,arr2) | Elementwise divide arr1 by arr2

np.power(arr1,arr2) | Elementwise raise arr1 raised to the power of arr2

np.array_equal(arr1,arr2) | Returns True if the arrays have the same elements and shape

np.sqrt(arr) | Square root of each element in the array

np.sin(arr) | Sine of each element in the array

np.log(arr) | Natural log of each element in the array

np.abs(arr) | Absolute value of each element in the array

np.ceil(arr) | Rounds up to the nearest int

np.floor(arr) | Rounds down to the nearest int

np.round(arr) | Rounds to the nearest int

Statistics

np.mean(arr,axis=0) | Returns mean along specific axis

arr.sum() | Returns sum of arr

arr.min() | Returns minimum value of arr

arr.max(axis=0) | Returns maximum value of specific axis

np.var(arr) | Returns the variance of array

np.std(arr,axis=1) | Returns the standard deviation of specific axis

arr.corrcoef() | Returns correlation coefficient of array