Learn to Code via Tutorials on Repl.it!

← Back to all posts
NumPy For Beginners
dillonjoshua68 (73)

NumPy Basics

This contains 9 Topics
Basics/Intro, Creating arrays, Indexing, Slicing, Data types, Copy vs View, Array shape, and Array reshape

NumPy aims to provide an array object that is up to 50x faster that traditional Python lists.

The array object in NumPy is called ndarray

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
This behavior is called locality of reference on computer science.
In python version 3.4 and later Pip is already installed... it’s a package manager for python packages or modules.

import numpy
arr = numpy.array([1, 2, 3, 4, 5])

To check numpy version use the version attribute.

if we print the type of the array from above (arr) we get:

A dimension in arrays is one level of depth (nested arrays).
nested arrays - arrays that have arrays as their elements.

Dimensions in arrays:
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
import numpy as np
arr = np.array(42)

1-D array is an array that has 0-D arrays as its elements is called uni-dimensional or 1-D array
arr = np.array([1, 2, 3, 4, 5])

2-D arrays are arrays that have 1-D arrays as its elements. They are often used to represent matrix or 2nd order tensors.
arr = np.array([[1, 2, 3], [4, 5, 6]])

NumPy has a whole sub module dedicated towards matrix operations called numpy.mat

3-D array is an array that has 2-D arrays (matrices) as its elements. Often used to represent a 3rd order tensor.
arr = np.array([[1, 2, 3], [4, 5, 6]], [1, 2, 3], [4, 5, 6]])

To check the number of dimensions do this:
a = np.array(42) # this is the variable we’re checking
print(a.ndim) # this is how to check

To add more dimensions we have to do this:
arr = np.array([1, 2, 3,4], ndmin = 8)

pass an argument called ndmin.

specify the number of dimensions

NumPy arrays follow the same indexing rules as normal arrays/lists. Even negative indexing.

arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])

this gets the numbers in those indexes and adds them.

It also follows the same rules for slicing...[start:end:step].
If we don’t pass any parameter for each the default for start is 0, for step is 1, for end is the length of the array.

For 2-D and above we do this:
print(arr[1, 1:8]). This prints the values in the first array, and prints the elements in the positions 1-7, 8 is ignored.

print(arr[0:2, 2]) - this returns the second index from both elements(arrays inside the array).

NumPy has extra data types, and refer to data types with one character... i for integer, etc.

i - integer
b - bool
u - unsigned int
f - float
c - complex float
m - timedelta
M - datetime
O - object
S - string
U - Unicode string
V - fixed chunk of memory for other type (void).

To check the data type of an array do this
we can even create an array with a specific data type:
arr = np.array([1, 2, 3, 4], dtype = ‘S’)

For i, u, f, S, and U we can also define size:
arr = np.array([1, 2, 3, 4], dtype = ‘i4’)

To convert data type on existing arrays we do this:
We need to store it in a variable
The astype() methods creates a copy of the array, and Takes data type as a parameter.

Copy vs View: copy.. copies the array, and any changes made won’t affect the original. And if the original is changed it won’t affect the copy. Whereas View is just view of the original array, and any changes made to either will affect the other one.

NumPy has the attribute base that returns None if the array owns the data... otherwise it’ll refer to the original object. The Copy returns none. View return the original
x = arr.copy()
y = arr.view()
print(x.base, y.base)

The shape of an array is the number of elements in each dimension.

They have an attribute called shape that returns a tuple having the number of corresponding elements.
(2, 4) - means that the array has 2 elements, and each dimension has 4 elements.

Using the reshape method we can add or remove dimensions or change number of elements in each dimension.
newarr = arr.reshape(4,3) - the outermost array will have 4 dimensions and 3 elements in each.
Trying to convert a 1-D array with elements to a 2-D array with 3 elements in each dimension will raise an error. This applies to others as well.

You are allowed to have one “unknown” dimension... meaning that you don’t have to specify an exact number for one of the dimensions in the reshape method. Pass -1 as the value, and NumPy will calculate this number for you.
We cannot pass -1 to more than one dimension.

Flattening an array means converting a multidimensional array into a 1-D array. To flatten an array we can use reshape(-1)

DynamicSquid (4551)

Cool! But just a tip, you might wanna use some more markdown.

dillonjoshua68 (73)

Ya I should’ve done that, thanks for the recommendation @DynamicSquid