0% found this document useful (0 votes)
19 views

Numpy - Basics

Uploaded by

saraga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Numpy - Basics

Uploaded by

saraga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

In [ ]:

# NumPy
NumPy is a Python library used for working with arrays.
It has support for large, multi-dimensional arrays and matrices,
along with a large collection of high-level mathematical functions to opera

In [ ]:
# import numpy
-> By adding the import keyword
-> Create an alias with the as keyword while importing

In [4]:
import numpy as np

In [ ]:
# Create a NumPy ndarray Object:

NumPy is used to work with arrays.


The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array() function.
To create an ndarray, we can pass a list, tuple or any array-like object in
and it will be converted into an ndarray.

In [6]:
import numpy as np

arr1 = np.array([1, 2, 3, 4, 5]) # passing list

print(arr1)

print(type(arr1))

arr2 = np.array((10, 9, 8, 7, 6)) # passing tuple

print(arr2)

print(type(arr2))

[1 2 3 4 5]
<class 'numpy.ndarray'>
[10 9 8 7 6]
<class 'numpy.ndarray'>

In [ ]:
# Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).

In [ ]:
# 0-D Arrays
0-D arrays, or Scalars, are the elements in an array.
Each value in an array is a 0-D array.

In [7]:
import numpy as np

arr = np.array(42)

print(arr)

42
In [ ]:
# 1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1

These are the most common and basic arrays.

In [9]:
#Create a 1-D array containing the values 1,2,3,4,5:

import numpy as np

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

print(arr)

[1 2 3 4 5]

In [ ]:
# 2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

In [10]:
#Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:

import numpy as np

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

print(arr)

[[1 2 3]
[4 5 6]]

In [ ]:
# 3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array

These are often used to represent a 3rd order tensor.

In [11]:
#Create a 3-D array with two 2-D arrays, both containing two arrays with th

import numpy as np

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

print(arr)

[[[1 2 3]
[4 5 6]]

[[1 2 3]
[4 5 6]]]

In [ ]:
# Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells
the array have.

In [13]:
#Check how many dimensions the arrays have:

import numpy as np

a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)

0
1
2
3

In [ ]:
# Higher Dimensional Arrays
An array can have any number of dimensions.

When the array is created, you can define the number of dimensions by using

In [14]:
#Create an array with 5 dimensions and verify that it has 5 dimensions:
import numpy as np

arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr)
print('number of dimensions :', arr.ndim)

[[[[[1 2 3 4]]]]]
number of dimensions : 5

In [ ]:
# NumPy Array Indexing
Access Array Elements
Array indexing is the same as accessing an array element.

You can access an array element by referring to its index number.

The indexes in NumPy arrays start with 0, meaning that the first element ha
and the second has index 1 etc.

In [15]:
#Get the first element from the following array:

import numpy as np

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

print(arr[0])

#Get the second element.


print(arr[1])

#Get third and fourth elements


print(arr[2] + arr[3])

1
2
7

In [ ]:
# Access 2-D Arrays
To access elements from 2-D arrays we can use comma separated integers repr
and the index of the element.

Think of 2-D arrays like a table with rows and columns,


where the row represents the dimension and the index represents the column

In [1]:
import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

#Access the element on the first row, second column:


print('2nd element on 1st row: ', arr[0, 1])

#Access the element on the 2nd row, 5th column:

print('5th element on 2nd row: ', arr[1, 4])

2nd element on 1st row: 2


5th element on 2nd row: 10

In [ ]:
# Access 3-D Arrays
To access elements from 3-D arrays we can use comma separated integers repr
and the index of the element.

In [16]:
#Access the third element of the second array of the first array:

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(arr[0, 1, 2])

In [ ]:
# Negative Indexing
Use negative indexing to access an array from the end.

In [20]:
#Print the last element from the 2nd dim:

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('Last element from 2nd dim: ', arr[1, -1])

Last element from 2nd dim: 10

In [ ]:
# NumPy Array Slicing
Slicing arrays
Slicing in python means taking elements from one given index to another giv

We pass slice instead of index like this: [start:end].


We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

In [19]:
#Slice elements from index 1 to index 5 from the following array:

import numpy as np

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

#The result includes the start index, but excludes the end index.
print(arr[1:5])
#Slice elements from index 4 to the end of the array:
print(arr[4:])

#Slice elements from the beginning to index 4 (not included):


print(arr[:4])

#Negative Slicing
#Use the minus operator to refer to an index from the end:
#Slice from the index 3 from the end to index 1 from the end:
print(arr[-3:-1])

#STEP
#Use the step value to determine the step of the slicing:
#Return every other element from index 1 to index 5:
print(arr[1:5:2])

#Return every other element from the entire array:


print(arr[::2])

[2 3 4 5]
[5 6 7]
[1 2 3 4]
[5 6]
[2 4]
[1 3 5 7]

Slicing 2-D Arrays


In [18]:
#From the second element, slice elements from index 1 to index 4 (not incl

import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr[1, 1:4])

#From both elements, return index 2:


print(arr[0:2, 2])

#From both elements, slice index 1 to index 4 (not included), this will re
print(arr[0:2, 1:4])
[7 8 9]
[3 8]
[[2 3 4]
[7 8 9]]

In [ ]:
# Data Types in NumPy
NumPy has some extra data types, and refer to data types with one character

Below is a list of all data types in NumPy and the characters used to repre

i - integer
b - boolean
u - unsigned integer
f - float
c - complex float
M - datetime
S - string
U - unicode string

In [ ]:
# Checking the Data Type of an Array
The NumPy array object has a property called dtype that returns the data ty

In [2]:
#Get the data type of an array object:

import numpy as np

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

print(arr.)

int32

In [34]:
#Get the data type of an array containing strings:

import numpy as np

arr = np.array(['apple', 'bananaa', 'cherry'])

print(arr.dtype)

<U7

In [ ]:
# Creating Arrays With a Defined Data Type
We use the array() function to create arrays,
this function can take an optional argument: dtype that allows us to define
of the array elements:

In [4]:
#Create an array with data type string:

import numpy as np

arr = np.array([1, 2, 3, 4], dtype='S')

print(arr)
print(arr.dtype)
[b'1' b'2' b'3' b'4']
|S1

In [ ]:
# Converting Data Type on Existing Arrays
The best way to change the data type of an existing array,
is to make a copy of the array with the astype() method.
The astype() function creates a copy of the array,
and allows you to specify the data type as a parameter.

The data type can be specified using a string, like 'f' for float,
'i' for integer etc. or you can use the data type directly like float for f

In [4]:
#Change data type from float to integer by using 'i' as parameter value:

import numpy as np

arr = np.array([1.1, 2.1, 3.1])

newarr = arr.astype('i')
print(arr)
print(arr.dtype)
print(newarr)
print(newarr.dtype)

[1.1 2.1 3.1]


float64
[1 2 3]
int32

In [6]:
#Change data type from float to integer by using int as parameter value:

import numpy as np

arr = np.array([1.1, 2.1, 3.1])

newarr = arr.astype(int)

print(newarr)
print(newarr.dtype)

[1 2 3]
int32

In [ ]:
# NumPy Array Copy vs View
The Difference Between Copy and View
The main difference between a copy and a view of an array is that the copy
and the view is just a view of the original array.

The copy owns the data and any changes made to the copy will not affect ori
and any changes made to the original array will not affect the copy.

The view does not own the data and any changes made to the view will affect
and any changes made to the original array will affect the view.

In [7]:
#Make a copy, change the original array, and display both arrays:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42

print(arr)
print(x)

#The copy SHOULD NOT be affected by the changes made to the original array

[42 2 3 4 5]
[1 2 3 4 5]

In [8]:
#Make a view, change the original array, and display both arrays:

import numpy as np

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


x = arr.view()
arr[0] = 42

print(arr)
print(x)
#The view SHOULD be affected by the changes made to the original array.

[42 2 3 4 5]
[42 2 3 4 5]

In [9]:
#Make a view, change the view, and display both arrays:

import numpy as np

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


x = arr.view()
x[0] = 31

print(arr)
print(x)

#The original array SHOULD be affected by the changes made to the view.

[31 2 3 4 5]
[31 2 3 4 5]

In [ ]:
# Shape of an Array
The shape of an array is the number of elements in each dimension.

Get the Shape of an Array


NumPy arrays have an attribute called shape that returns a tuple with each

In [1]:
#Print the shape of a 2-D array:

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

print(arr.shape)

(2, 4)
In [2]: #Create an array with 5 dimensions using ndmin using a vector with values 1

import numpy as np

arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr)
print('shape of array :', arr.shape)

[[[[[1 2 3 4]]]]]
shape of array : (1, 1, 1, 1, 4)

In [ ]:
# Reshaping arrays
Reshaping means changing the shape of an array.

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

By reshaping we can add or remove dimensions or change number of elements i

In [1]:
#Reshape From 1-D to 2-D
#Convert the following 1-D array with 12 elements into a 2-D array.

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)

[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

In [7]:
#Reshape From 1-D to 3-D
#Convert the following 1-D array with 12 elements into a 3-D array.

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = arr.reshape(2, 3, 2)

print(newarr)
#print(arr)

[[[ 1 2]
[ 3 4]
[ 5 6]]

[[ 7 8]
[ 9 10]
[11 12]]]

In [9]:
#Flattening the arrays
#Flattening array means converting a multidimensional array into a 1D array

#We can use reshape(-1) to do this.

#Convert the array into a 1D array:


import numpy as np

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

newarr = arr.reshape(-1)

print(newarr)
#print(arr)

[1 2 3 4 5 6]

In [ ]:
# Iterating Arrays
Iterating means going through elements one by one.

If we iterate on a 1-D array it will go through each element one by one.

In [10]:
#Iterate on the elements of the following 1-D array:

import numpy as np

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

for x in arr:
print(x)

1
2
3

In [11]:
#Iterate on the elements of the following 2-D array:

import numpy as np

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

for x in arr:
print(x)

[1 2 3]
[4 5 6]

In [13]:
#Iterate on each scalar element of the 2-D array:

import numpy as np

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

for x in arr:
for y in x:
print(y)

1
2
3
4
5
6
In [12]: #Iterate down to the scalars:

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

for x in arr:
for y in x:
for z in y:
print(z)

1
2
3
4
5
6
7
8
9
10
11
12

In [ ]:
# Joining NumPy Arrays
Joining means putting contents of two or more arrays in a single array.

In SQL we join tables based on a key, whereas in NumPy we join arrays by ax

We pass a sequence of arrays that we want to join to the concatenate() func


along with the axis. If axis is not explicitly passed, it is taken as 0.

In [17]:
#Join two arrays

import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([4, 5, 6])

arr = np.concatenate((arr1, arr2))

print(arr)

[1 2 3 4 5 6]

In [2]:
#Join two 2-D arrays along rows (axis=1):

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

arr = np.concatenate((arr1, arr2),axis=1)

print(arr)

[[1 2 5 6]
[3 4 7 8]]
In [13]:
#Join two 2-D arrays (axis=0):

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

arr = np.concatenate((arr1, arr2),axis=0)

print(arr)

[[1 2]
[3 4]
[5 6]
[7 8]]

In [14]:
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

arr = np.stack((arr1, arr2), axis=1)

print(arr)

[[[1 2]
[5 6]]

[[3 4]
[7 8]]]

In [18]:
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

arr = np.stack((arr1, arr2), axis=0)

print(arr)

[[[1 2]
[3 4]]

[[5 6]
[7 8]]]

In [15]:
#Stacking Along Rows
#NumPy provides a helper function: hstack() to stack along rows.

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

arr = np.hstack((arr1, arr2))


print(arr)

[[1 2 5 6]
[3 4 7 8]]

In [19]:
#Stacking Along Columns
#NumPy provides a helper function: vstack() to stack along columns.

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])


arr = np.vstack((arr1, arr2))

print(arr)

[[1 2]
[3 4]
[5 6]
[7 8]]

In [20]:
#Stacking Along Height (depth)
#NumPy provides a helper function: dstack() to stack along height, which is

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

arr = np.dstack((arr1, arr2))

print(arr)

[[[1 5]
[2 6]]

[[3 7]
[4 8]]]

In [ ]:
Splitting NumPy Arrays
Splitting is reverse operation of Joining.

Joining merges multiple arrays into one and Splitting breaks one array into

We use array_split() for splitting arrays, we pass it the array we want to

In [23]:
#Split the array in 3 parts:

import numpy as np

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

newarr = np.array_split(arr, 3)
print(newarr)
#The return value is an array containing three arrays.
#If the array has less elements than required, it will adjust from the end

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

In [24]:
#Split the array in 4 parts:

import numpy as np

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

newarr = np.array_split(arr, 4)

print(newarr)

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

In [ ]:
Splitting 2-D Arrays
Use the same syntax when splitting 2-D arrays.

Use the array_split() method, pass in the array you want to split and the n

Example

In [25]:
#Split the 2-D array into three 2-D arrays.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15

newarr = np.array_split(arr, 3)

print(newarr)

[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]]), array([[13, 14, 15],
[16, 17, 18]])]

Searching Arrays
You can search an array for a certain value, and return the indexes that get a match.

To search an array, use the where() method.

In [27]:
#Find the indexes where the value is 4:

import numpy as np

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

x = np.where(arr == 4)

print(x)

(array([3, 5, 6], dtype=int64),)


In [1]:
#Find the indexes where the values are even:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

x = np.where(arr%2 == 0)

print(x)

(array([1, 3, 5, 7], dtype=int64),)

In [2]:
#Find the indexes where the values are odd:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

x = np.where(arr%2 == 1)

print(x)

(array([0, 2, 4, 6], dtype=int64),)

Sorting Arrays
Sorting means putting elements in an ordered sequence.

Ordered sequence is any sequence that has an order corresponding to elements, like
numeric or alphabetical, ascending or descending.

The NumPy ndarray object has a function called sort(), that will sort a specified array.

In [23]:
#Sort the array:

import numpy as np

arr = np.array([3, 2, 0, 1])

print(np.sort(arr))
#This method returns a copy of the array, leaving the original array unchan

[0 1 2 3]

In [24]:
#Sort the array alphabetically:

import numpy as np

arr = np.array(['banana', 'cherry', 'apple'])

print(np.sort(arr))

['apple' 'banana' 'cherry']

In [25]:
#Sort a boolean array:
import numpy as np

arr = np.array([True, False, True])

print(np.sort(arr))

[False True True]

In [26]:
#If you use the sort() method on a 2-D array, both arrays will be sorted:

import numpy as np

arr = np.array([[3, 2, 4], [5, 0, 1]])

print(np.sort(arr))

[[2 3 4]
[0 1 5]]

In [ ]:
Some of the useful aggregate functions

In [3]:
# import NumPy library

import numpy as np

# Create a one-dimensional array

np_array = np.array([6, 4, 9, 3, 1])

# Print array and mean values

print("The values of the one-dimensional NumPy array are:\n ", np_array)

print("The mean value of the one-dimensional array is:\n", np.mean(np_array

# Create a two-dimensional array

np_array = np.array([[5, 3, 5], [5, 4, 3]])

# Print array and mean values

print("\nThe values of the two-dimensional NumPy array are:\n ", np_array

print("The mean values of the two-dimensional array are:\n", np.mean(np_arr

The values of the one-dimensional NumPy array are:


[6 4 9 3 1]
The mean value of the one-dimensional array is:
4.6

The values of the two-dimensional NumPy array are:


[[5 3 5]
[5 4 3]]
The mean values of the two-dimensional array are:
[4.33333333 4. ]

In [4]:
# import NumPy library
import numpy as np

# Create NumPy array of integers

np_array = np.array([21, 5, 34, 12, 30, 6])

# Find the maximum value from the array

max_value = np.max(np_array)

# Print the maximum value

print('The maximum value of the array is: ', max_value)

# Create a two-dimensional array

np_array = np.array([[5, 3, 5], [5, 4, 3]])

# Print array and max value

print("\nThe values of the two-dimensional NumPy array are:\n ", np_array

print("The max values of the two-dimensional array are:\n", np.max(np_array

The maximum value of the array is: 34

The values of the two-dimensional NumPy array are:


[[5 3 5]
[5 4 3]]
The max values of the two-dimensional array are:
[5 5]

In [5]:
# import NumPy library

import numpy as np

# Create NumPy array of integers

np_array = np.array([21, 5, 34, 12, 30, 6])

# Find the min value from the array

min_value = np.min(np_array)

# Print the min value

print('The minimum value of the array is: ', min_value)

# Create a two-dimensional array

np_array = np.array([[5, 3, 5], [5, 4, 3]])

# Print array and min value

print("\nThe values of the two-dimensional NumPy array are:\n ", np_array

print("The min values of the two-dimensional array are:\n", np.min(np_array

The minimum value of the array is: 5

The values of the two-dimensional NumPy array are:


[[5 3 5]
[5 4 3]]
The min values of the two-dimensional array are:
[3 3]

In [24]:
# import NumPy library

import numpy as np

# Create NumPy array of integers

np_array = np.array([21, 5, 34, 12, 30, 6])

# Find the sum of the array elements

sum_value = np.sum(np_array)

# Print the maximum value

print('The sum of the array elements is: ', sum_value)

# Create a two-dimensional array

np_array = np.array([[5, 3, 5], [5, 4, 3]])

# Print array and max value

print("\nThe values of the two-dimensional NumPy array are:\n ", np_array

print("The sum of the two-dimensional array are:\n", np.sum(np_array, axis=

The minimum value of the array is: 108

The values of the two-dimensional NumPy array are:


[[5 3 5]
[5 4 3]]
The min values of the two-dimensional array are:
[10 7 8]

In [ ]:

You might also like