Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mrdbourke
GitHub Repository: mrdbourke/zero-to-mastery-ml
Path: blob/master/section-2-data-science-and-ml-tools/introduction-to-numpy-video.ipynb
874 views
Kernel: Python 3
import numpy as np

1. DataTypes & Attributes

# NumPy's main datatype is ndarray a1 = np.array([1, 2, 3]) a1
array([1, 2, 3])
type(a1)
numpy.ndarray
a2 = np.array([[1, 2.0, 3.3], [4, 5, 6.5]]) a3 = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a1.shape
(3,)
a2.shape
(2, 3)
a3.shape
(2, 3, 3)
a1
array([1, 2, 3])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a1.ndim, a2.ndim, a3.ndim
(1, 2, 3)
a1.dtype, a2.dtype, a3.dtype
(dtype('int64'), dtype('float64'), dtype('int64'))
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a1.size, a2.size, a3.size
(3, 6, 18)
type(a1), type(a2), type(a3)
(numpy.ndarray, numpy.ndarray, numpy.ndarray)
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
# Create a DataFrame from a NumPy array import pandas as pd df = pd.DataFrame(a2) df

2. Creating arrays

sample_array = np.array([1, 2, 3]) sample_array
array([1, 2, 3])
sample_array.dtype
dtype('int64')
ones = np.ones((2, 3))
ones
array([[1., 1., 1.], [1., 1., 1.]])
ones.dtype
dtype('float64')
type(ones)
numpy.ndarray
zeros = np.zeros((2, 3))
zeros
array([[0., 0., 0.], [0., 0., 0.]])
range_array = np.arange(0, 10, 2) range_array
array([0, 2, 4, 6, 8])
random_array = np.random.randint(0, 10, size=(3, 5)) random_array
array([[5, 9, 7, 6, 4], [1, 9, 6, 7, 1], [7, 5, 5, 9, 0]])
random_array.size
15
random_array.shape
(3, 5)
random_array_2 = np.random.random((5, 3)) random_array_2
array([[0.22167345, 0.34437229, 0.73864985], [0.66670981, 0.82113975, 0.92073724], [0.03024207, 0.36297608, 0.39167642], [0.22192665, 0.85257304, 0.62190959], [0.15216592, 0.0638989 , 0.71405768]])
random_array_2.shape
(5, 3)
random_array_3 = np.random.rand(5, 3) random_array_3
array([[0.72816128, 0.82122761, 0.76051512], [0.00714328, 0.42025683, 0.46313622], [0.0554995 , 0.54144213, 0.60777075], [0.82845319, 0.94180927, 0.12814785], [0.23043067, 0.6591584 , 0.13247399]])
# Pseudo-random numbers np.random.seed(seed=99999) random_array_4 = np.random.randint(10, size=(5, 3)) random_array_4
array([[0, 3, 1], [8, 1, 3], [8, 5, 6], [0, 6, 0], [4, 0, 9]])
np.random.seed(7) random_array_5 = np.random.random((5, 3)) random_array_5
array([[0.07630829, 0.77991879, 0.43840923], [0.72346518, 0.97798951, 0.53849587], [0.50112046, 0.07205113, 0.26843898], [0.4998825 , 0.67923 , 0.80373904], [0.38094113, 0.06593635, 0.2881456 ]])
random_array_5 = np.random.random((5, 3)) random_array_5
array([[0.20484909, 0.49076589, 0.37238469], [0.47740115, 0.36589039, 0.83791799], [0.76864751, 0.31399468, 0.57262533], [0.27604905, 0.45284293, 0.35297837], [0.65739946, 0.37035108, 0.45909298]])
random_array_4
array([[0, 3, 1], [8, 1, 3], [8, 5, 6], [0, 6, 0], [4, 0, 9]])

3. Viewing arrays and matrices

np.unique(random_array_4)
array([0, 1, 3, 4, 5, 6, 8, 9])
a1
array([1, 2, 3])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a1[0]
1
a2.shape
(2, 3)
a2[0]
array([1. , 2. , 3.3])
a3.shape
(2, 3, 3)
a3[0]
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a2[1]
array([4. , 5. , 6.5])
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a3.shape
(2, 3, 3)
a3[:2, :2, :2]
array([[[ 1, 2], [ 4, 5]], [[10, 11], [13, 14]]])
a4 = np.random.randint(10, size=(2, 3, 4, 5)) a4
array([[[[8, 4, 9, 0, 2], [0, 7, 6, 2, 9], [9, 5, 1, 0, 0], [9, 1, 1, 5, 3]], [[2, 0, 4, 8, 7], [1, 4, 9, 3, 6], [7, 1, 0, 2, 7], [0, 0, 9, 8, 2]], [[0, 5, 5, 0, 9], [0, 8, 4, 5, 1], [4, 0, 7, 2, 0], [3, 9, 8, 3, 7]]], [[[1, 0, 1, 4, 6], [1, 0, 1, 0, 1], [8, 4, 1, 6, 8], [5, 2, 4, 7, 4]], [[1, 2, 8, 5, 2], [9, 6, 2, 5, 7], [7, 5, 5, 6, 0], [6, 4, 6, 2, 6]], [[8, 7, 0, 5, 8], [9, 7, 7, 6, 2], [5, 6, 1, 9, 3], [4, 0, 3, 5, 6]]]])
a4.shape, a4.ndim
((2, 3, 4, 5), 4)
# Get the first 4 numbers of the inner most arrays a4[:, :, :, :1]
array([[[[8], [0], [9], [9]], [[2], [1], [7], [0]], [[0], [0], [4], [3]]], [[[1], [1], [8], [5]], [[1], [9], [7], [6]], [[8], [9], [5], [4]]]])

4. Manipulating & comparing arrays

Arithmetic

a1
array([1, 2, 3])
ones = np.ones(3) ones
array([1., 1., 1.])
a1 + ones
array([2., 3., 4.])
a1 - ones
array([0., 1., 2.])
a1 * ones
array([1., 2., 3.])
a1
array([1, 2, 3])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a1 * a2
array([[ 1. , 4. , 9.9], [ 4. , 10. , 19.5]])
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
# How can you reshape a2 to be compatible with a3? # Search: "How to reshape numpy array" a2 * a3
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-177-37d8620f1b2c> in <module> ----> 1 a2 * a3 ValueError: operands could not be broadcast together with shapes (2,3) (2,3,3)
a1 / ones
array([1., 2., 3.])
a2 / a1
array([[1. , 1. , 1.1 ], [4. , 2.5 , 2.16666667]])
# Floor division removes the decimals (rounds down) a2 // a1
array([[1., 1., 1.], [4., 2., 2.]])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a2 ** 2
array([[ 1. , 4. , 10.89], [16. , 25. , 42.25]])
np.square(a2)
array([[ 1. , 4. , 10.89], [16. , 25. , 42.25]])
a1 + ones
array([2., 3., 4.])
np.add(a1, ones)
array([2., 3., 4.])
a1 % 2
array([1, 0, 1])
a1 / 2
array([0.5, 1. , 1.5])
a2 % 2
array([[1. , 0. , 1.3], [0. , 1. , 0.5]])
np.exp(a1)
array([ 2.71828183, 7.3890561 , 20.08553692])
np.log(a1)
array([0. , 0.69314718, 1.09861229])

Aggregation

Aggregation = performing the same operation on a number of things

listy_list = [1, 2, 3] type(listy_list)
list
sum(listy_list)
6
sum(a1)
6
np.sum(a1)
6

Use Python's methods (sum()) on Python datatypes and use NumPy's methods on NumPy arrays (np.sum()).

# Creative a massive NumPy array massive_array = np.random.random(100000) massive_array.size
100000
massive_array[:10]
array([0.67959677, 0.44959741, 0.3896631 , 0.66184575, 0.01048353, 0.26151906, 0.75223629, 0.93748825, 0.67568463, 0.49847177])
%timeit sum(massive_array) # Python's sum() %timeit np.sum(massive_array) # NumPy's np.sum()
17.9 ms ± 2.94 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 34 µs ± 391 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
17900 / 34
526.4705882352941
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
np.mean(a2)
3.6333333333333333
np.max(a2)
6.5
np.min(a2)
1.0

Standard deviation and variance are measures of 'spread' of data.

The higher standard deviation and the higher variance of data, the more spread out the values are.

The lower standard deviation and lower variance, the less spread out the values are.

# Standard deviation = a measure of how spread out a group of numbers is from the mean np.std(a2)
1.8226964152656422
# Variance = measure of the average degree to which each number is different to the mean # Higher variance = wider range of numbers # Lower variance = lower range of numbers np.var(a2)
3.3222222222222224
# Standard deviation = squareroot of variance np.sqrt(np.var(a2))
1.8226964152656422
# Demo of std and var high_var_array = np.array([1, 100, 200, 300, 4000, 5000]) low_var_array = np.array([2, 4, 6, 8, 10])
np.var(high_var_array), np.var(low_var_array)
(4296133.472222221, 8.0)
np.std(high_var_array), np.std(low_var_array)
(2072.711623024829, 2.8284271247461903)
np.mean(high_var_array), np.mean(low_var_array)
(1600.1666666666667, 6.0)
%matplotlib inline import matplotlib.pyplot as plt plt.hist(high_var_array) plt.show()
Image in a Jupyter notebook
plt.hist(low_var_array) plt.show()
Image in a Jupyter notebook

Reshaping & transposing

a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a2.shape
(2, 3)
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a3.shape
(2, 3, 3)
a2 * a3
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-224-37d8620f1b2c> in <module> ----> 1 a2 * a3 ValueError: operands could not be broadcast together with shapes (2,3) (2,3,3)
a2.shape
(2, 3)
a2.reshape(2, 3, 1).shape
(2, 3, 1)
a3.shape
(2, 3, 3)
a2_reshape = a2.reshape(2, 3, 1) a2_reshape
array([[[1. ], [2. ], [3.3]], [[4. ], [5. ], [6.5]]])
a2_reshape * a3
array([[[ 1. , 2. , 3. ], [ 8. , 10. , 12. ], [ 23.1, 26.4, 29.7]], [[ 40. , 44. , 48. ], [ 65. , 70. , 75. ], [104. , 110.5, 117. ]]])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a2.shape
(2, 3)
# Transpose = switches the axis' a2.T
array([[1. , 4. ], [2. , 5. ], [3.3, 6.5]])
a2.T.shape
(3, 2)
a3
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
a3.shape
(2, 3, 3)
a3.T
array([[[ 1, 10], [ 4, 13], [ 7, 16]], [[ 2, 11], [ 5, 14], [ 8, 17]], [[ 3, 12], [ 6, 15], [ 9, 18]]])
a3.T.shape
(3, 3, 2)

Dot product

np.random.seed(0) mat1 = np.random.randint(10, size=(5, 3)) mat2 = np.random.randint(10, size=(5, 3)) mat1
array([[5, 0, 3], [3, 7, 9], [3, 5, 2], [4, 7, 6], [8, 8, 1]])
mat2
array([[6, 7, 7], [8, 1, 5], [9, 8, 9], [4, 3, 0], [3, 5, 0]])
mat1.shape, mat2.shape
((5, 3), (5, 3))
mat1
array([[5, 0, 3], [3, 7, 9], [3, 5, 2], [4, 7, 6], [8, 8, 1]])
mat2
array([[6, 7, 7], [8, 1, 5], [9, 8, 9], [4, 3, 0], [3, 5, 0]])
# Element-wise multiplication (Hadamard product) mat1 * mat2
array([[30, 0, 21], [24, 7, 45], [27, 40, 18], [16, 21, 0], [24, 40, 0]])
# Dot product np.dot(mat1, mat2)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-262-0da3c94cc631> in <module> 1 # Dot product ----> 2 np.dot(mat1, mat2) <__array_function__ internals> in dot(*args, **kwargs) ValueError: shapes (5,3) and (5,3) not aligned: 3 (dim 1) != 5 (dim 0)
# Transpose mat1 mat1.T
array([[5, 3, 3, 4, 8], [0, 7, 5, 7, 8], [3, 9, 2, 6, 1]])
mat1.shape, mat2.T.shape
((5, 3), (3, 5))
mat3 = np.dot(mat1, mat2.T) mat3
array([[ 51, 55, 72, 20, 15], [130, 76, 164, 33, 44], [ 67, 39, 85, 27, 34], [115, 69, 146, 37, 47], [111, 77, 145, 56, 64]])
mat3.shape
(5, 5)

Dot product exmaple (nut butter sales)

np.random.seed(0) # Number of jars sold sales_amounts = np.random.randint(20, size=(5,3)) sales_amounts
array([[12, 15, 0], [ 3, 3, 7], [ 9, 19, 18], [ 4, 6, 12], [ 1, 6, 7]])
# Create weekly_sales DataFrame weekly_sales = pd.DataFrame(sales_amounts, index=["Mon", "Tues", "Wed", "Thurs", "Fri"], columns=["Almond butter", "Peanut butter", "Cashew butter"]) weekly_sales
# Create prices array prices = np.array([10, 8, 12]) prices
array([10, 8, 12])
prices.shape
(3,)
# Create butter_prices DataFrame butter_prices = pd.DataFrame(prices.reshape(1, 3), index=["Price"], columns=["Almond butter", "Peanut butter", "Cashew butter"]) butter_prices
prices.shape
(3,)
sales_amounts.shape
(5, 3)
total_sales = prices.dot(sales_amounts)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-276-e4058314ac09> in <module> ----> 1 total_sales = prices.dot(sales_amounts) ValueError: shapes (3,) and (5,3) not aligned: 3 (dim 0) != 5 (dim 0)
# Shape aren't aligned, let's transpose total_sales = prices.dot(sales_amounts.T) total_sales
array([240, 138, 458, 232, 142])
# Create daily_sales butter_prices.shape, weekly_sales.shape
((1, 3), (5, 3))
weekly_sales.T.shape
(3, 5)
daily_sales = butter_prices.dot(weekly_sales.T) daily_sales.shape
(1, 5)
weekly_sales.shape
(5, 3)
weekly_sales
# Doesn't work, not the right shape weekly_sales["Total ($)"] = daily_sales weekly_sales
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2896 try: -> 2897 return self._engine.get_loc(key) 2898 except KeyError: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Total ($)' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/internals/managers.py in set(self, item, value) 1068 try: -> 1069 loc = self.items.get_loc(item) 1070 except KeyError: ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2898 except KeyError: -> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance) pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Total ($)' During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) <ipython-input-291-d761693dcb57> in <module> ----> 1 weekly_sales["Total ($)"] = daily_sales 2 weekly_sales ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/frame.py in __setitem__(self, key, value) 3470 else: 3471 # set column -> 3472 self._set_item(key, value) 3473 3474 def _setitem_slice(self, key, value): ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/frame.py in _set_item(self, key, value) 3548 self._ensure_valid_index(value) 3549 value = self._sanitize_column(key, value) -> 3550 NDFrame._set_item(self, key, value) 3551 3552 # check if we are modifying a copy ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/generic.py in _set_item(self, key, value) 3379 3380 def _set_item(self, key, value): -> 3381 self._data.set(key, value) 3382 self._clear_item_cache() 3383 ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/internals/managers.py in set(self, item, value) 1070 except KeyError: 1071 # This item wasn't present, just insert at end -> 1072 self.insert(len(self.items), item, value) 1073 return 1074 ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/internals/managers.py in insert(self, loc, item, value, allow_duplicates) 1179 new_axis = self.items.insert(loc, item) 1180 -> 1181 block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1)) 1182 1183 for blkno, count in _fast_count_smallints(self._blknos[loc:]): ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/internals/blocks.py in make_block(values, placement, klass, ndim, dtype, fastpath) 3265 values = DatetimeArray._simple_new(values, dtype=dtype) 3266 -> 3267 return klass(values, ndim=ndim, placement=placement) 3268 3269 ~/Desktop/ml-course/sample-project/env/lib/python3.7/site-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim) 126 raise ValueError( 127 "Wrong number of items passed {val}, placement implies " --> 128 "{mgr}".format(val=len(self.values), mgr=len(self.mgr_locs)) 129 ) 130 ValueError: Wrong number of items passed 5, placement implies 1
weekly_sales["Total ($)"] = daily_sales.T weekly_sales

Comparison Operators

a1
array([1, 2, 3])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a1 > a2
array([[False, False, False], [False, False, False]])
bool_array = a1 >= a2 bool_array
array([[ True, True, False], [False, False, False]])
type(bool_array), bool_array.dtype
(numpy.ndarray, dtype('bool'))
a1 > 5
array([False, False, False])
a1 < 5
array([ True, True, True])
a1 == a1
array([ True, True, True])
a1
array([1, 2, 3])
a2
array([[1. , 2. , 3.3], [4. , 5. , 6.5]])
a1 == a2
array([[ True, True, False], [False, False, False]])

5. Sorting arrays

random_array = np.random.randint(10, size=(3, 5)) random_array
array([[7, 8, 1, 5, 9], [8, 9, 4, 3, 0], [3, 5, 0, 2, 3]])
random_array.shape
(3, 5)
np.sort(random_array)
array([[1, 5, 7, 8, 9], [0, 3, 4, 8, 9], [0, 2, 3, 3, 5]])
random_array
array([[7, 8, 1, 5, 9], [8, 9, 4, 3, 0], [3, 5, 0, 2, 3]])
np.argsort(random_array)
array([[2, 3, 0, 1, 4], [4, 3, 2, 0, 1], [2, 3, 0, 4, 1]])
a1
array([1, 2, 3])
np.argsort(a1)
array([0, 1, 2])
np.argmin(a1)
0
np.argmax(a1)
2
random_array
array([[7, 8, 1, 5, 9], [8, 9, 4, 3, 0], [3, 5, 0, 2, 3]])
np.argmax(random_array, axis=0)
array([1, 1, 1, 0, 0])
np.argmax(random_array, axis=1)
array([4, 1, 1])

6. Practical Example - NumPy in Action!!!!

# Turn an image into a NumPy array from matplotlib.image import imread panda = imread("images/panda.png") print(type(panda))
<class 'numpy.ndarray'>
panda.size, panda.shape, panda.ndim
(24465000, (2330, 3500, 3), 3)
panda[:5]
array([[[0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], ..., [0.16470589, 0.12941177, 0.09411765], [0.16470589, 0.12941177, 0.09411765], [0.16470589, 0.12941177, 0.09411765]], [[0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], ..., [0.16470589, 0.12941177, 0.09411765], [0.16470589, 0.12941177, 0.09411765], [0.16470589, 0.12941177, 0.09411765]], [[0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], ..., [0.16470589, 0.12941177, 0.09411765], [0.16470589, 0.12941177, 0.09411765], [0.16470589, 0.12941177, 0.09411765]], [[0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], ..., [0.16862746, 0.13333334, 0.09803922], [0.16862746, 0.13333334, 0.09803922], [0.16862746, 0.13333334, 0.09803922]], [[0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], [0.05490196, 0.10588235, 0.06666667], ..., [0.16862746, 0.13333334, 0.09803922], [0.16862746, 0.13333334, 0.09803922], [0.16862746, 0.13333334, 0.09803922]]], dtype=float32)

car = imread("images/car-photo.png") print(type(car))
<class 'numpy.ndarray'>
car[:1]
array([[[0.5019608 , 0.50980395, 0.4862745 , 1. ], [0.3372549 , 0.34509805, 0.30588236, 1. ], [0.20392157, 0.21568628, 0.14901961, 1. ], ..., [0.64705884, 0.7058824 , 0.54901963, 1. ], [0.59607846, 0.63529414, 0.45882353, 1. ], [0.44705883, 0.47058824, 0.3372549 , 1. ]]], dtype=float32)

dog = imread("images/dog-photo.png") print(type(dog))
<class 'numpy.ndarray'>
dog
array([[[0.70980394, 0.80784315, 0.88235295, 1. ], [0.72156864, 0.8117647 , 0.8862745 , 1. ], [0.7411765 , 0.8156863 , 0.8862745 , 1. ], ..., [0.49803922, 0.6862745 , 0.8392157 , 1. ], [0.49411765, 0.68235296, 0.8392157 , 1. ], [0.49411765, 0.68235296, 0.8352941 , 1. ]], [[0.69411767, 0.8039216 , 0.8862745 , 1. ], [0.7019608 , 0.8039216 , 0.88235295, 1. ], [0.7058824 , 0.80784315, 0.88235295, 1. ], ..., [0.5019608 , 0.6862745 , 0.84705883, 1. ], [0.49411765, 0.68235296, 0.84313726, 1. ], [0.49411765, 0.68235296, 0.8392157 , 1. ]], [[0.6901961 , 0.8 , 0.88235295, 1. ], [0.69803923, 0.8039216 , 0.88235295, 1. ], [0.7058824 , 0.80784315, 0.88235295, 1. ], ..., [0.5019608 , 0.6862745 , 0.84705883, 1. ], [0.49803922, 0.6862745 , 0.84313726, 1. ], [0.49803922, 0.6862745 , 0.84313726, 1. ]], ..., [[0.9098039 , 0.81960785, 0.654902 , 1. ], [0.8352941 , 0.7490196 , 0.6509804 , 1. ], [0.72156864, 0.6313726 , 0.5372549 , 1. ], ..., [0.01568628, 0.07058824, 0.02352941, 1. ], [0.03921569, 0.09411765, 0.03529412, 1. ], [0.03921569, 0.09019608, 0.05490196, 1. ]], [[0.9137255 , 0.83137256, 0.6784314 , 1. ], [0.8117647 , 0.7294118 , 0.627451 , 1. ], [0.65882355, 0.5686275 , 0.47843137, 1. ], ..., [0.00392157, 0.05490196, 0.03529412, 1. ], [0.03137255, 0.09019608, 0.05490196, 1. ], [0.04705882, 0.10588235, 0.06666667, 1. ]], [[0.9137255 , 0.83137256, 0.68235296, 1. ], [0.76862746, 0.68235296, 0.5882353 , 1. ], [0.59607846, 0.5058824 , 0.44313726, 1. ], ..., [0.03921569, 0.10196079, 0.07058824, 1. ], [0.02745098, 0.08235294, 0.05882353, 1. ], [0.05098039, 0.11372549, 0.07058824, 1. ]]], dtype=float32)