In [1]:
import numpy as np
In [2]:
# Create a Python list
my_list = [0, 1, 2, 3, 4]
In [3]:
# Transform Python list to numpy n-dimensional array
arr = np.array(my_list)
In [4]:
arr
Out[4]:
In [5]:
type(arr)
Out[5]:
In [6]:
# Create an array containing a sequence for given range
# By default the step is equal to 1.
np.arange(0, 10)
Out[6]:
In [7]:
# last number is NOT INCLUDED!
# Step = 2
np.arange(start = 0, stop = 10, step = 2)
Out[7]:
In [8]:
# Create a n-dimensional array of zeros
# In this scenario it will be array 5 by 5
np.zeros((5, 5))
Out[8]:
In [9]:
# Create an n-dimensional array of ones
# This array will have 2 rows and 4 columns
np.ones((2, 4))
Out[9]:
In [10]:
# Generate a random integer between
np.random.randint(0, 10)
Out[10]:
In [11]:
# Generate an n-dimensional array of random integers betwen 0 and (10-1)
np.random.randint(0, 10, size = (3, 3))
Out[11]:
In [12]:
# Create an n-dimensial array of size 'num'
# The number are evenly spaces (includin 'stop' values)
np.linspace(start = 0, stop = 10, num = 6, dtype = float)
Out[12]:
In [13]:
np.linspace(0, 10, 101)
Out[13]:
In [14]:
# Setting a specific seed for reproducability
np.random.seed(101)
arr = np.random.randint(0, 100, 10)
In [15]:
arr
Out[15]:
In [16]:
arr2 = np.random.randint(0, 100, 10)
In [17]:
arr2
Out[17]:
In [18]:
arr.max()
Out[18]:
In [19]:
arr.min()
Out[19]:
In [20]:
arr.mean()
Out[20]:
In [21]:
arr.argmin()
Out[21]:
In [22]:
arr.argmax()
Out[22]:
In [23]:
# Reshaping the array.
# The number of values before and after reshaping MUST BE EQUAL!
arr.reshape(2, 5)
Out[23]:
In [24]:
mat = np.arange(0, 100).reshape(10, 10)
In [25]:
mat
Out[25]:
In [26]:
row = 0
col = 1
In [27]:
# Obtaining first value (of index = 0) from the second column (index = 1)
mat[row, col]
Out[27]:
In [28]:
# Slicing
# Obtaining all values from the second column (index = 1)
mat[:, col]
Out[28]:
In [29]:
# Slicing
# Obtaining all values from the first row (index = 0)
mat[row, :]
Out[29]:
In [30]:
mat[0:3, 0:3]
Out[30]:
In [31]:
mat > 50
Out[31]:
In [32]:
mat[mat> 50]
Out[32]: