0% found this document useful (0 votes)
7 views3 pages

TP1_ (AAr)

The document provides an overview of Numpy arrays and their application in Machine Learning, detailing the structure and properties of tensors, including scalars, vectors, matrices, and 3D tensors. It explains key attributes such as ndim, shape, size, and dtype, along with methods for accessing and manipulating array elements. Additionally, it covers reshaping arrays and the differences between various reshaping methods.

Uploaded by

marwa BEN
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)
7 views3 pages

TP1_ (AAr)

The document provides an overview of Numpy arrays and their application in Machine Learning, detailing the structure and properties of tensors, including scalars, vectors, matrices, and 3D tensors. It explains key attributes such as ndim, shape, size, and dtype, along with methods for accessing and manipulating array elements. Additionally, it covers reshaping arrays and the differences between various reshaping methods.

Uploaded by

marwa BEN
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/ 3

TP 1: The basics of Numpy arrays in Machine Learning

In Machine Learning, we always organize our data in vectors and matrices. Then, we use the
package in python “Numpy” which provides us with a lot of utilities for large multidimensional
arrays and matrices.

Tensors : Mathematically, a scalar, vector, matrix, all are a tensor. A Numpy tensor is a table
of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. Its
class is called numpy.array. The more important attributes of a Numpy array are:

 ndim - The number of axes of the array.


 shape - A tuple of integers indicating the size of the array in each axis.
 size - The total number of elements of the array (equal to the product of the elements
of shape ).
 dtype - The type of the elements in the array (e.g., numpy.int32 , numpy.float64 , etc).

Scalars (0D tensors):


- A tensor that contains only one number is called a scalar (float32 or float64)
x = np.array(12)
- A scalar has no axis.
x = np.dim
x.shape
Vectors (1D tensors) :
- An array of numbers is called a vector (or 1D tensor):
a = np.array([0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
- A vector has one axis. This particular vector has ten entries, so its size is 10 and its shape
is (10,).
Vectors can be indexed, sliced and iterated over.
# Access to the element in position 2 : a[2]
# Access to elements from position 2 to position 4 : a[2:5]
# Modify elements from start to position 5, every 2nd element Sintax: [start:stop:ste
p]) :a[:6:2] = -100
# Reverse a : a[::-1]
# Iterate over the elements of a :for i in a: print(i)

Matrices (2D tensors):


- An array of vectors is called a matrix (or 2D tensor):
b = np.array([[5, 78, 2, 34, 0],
[6, 79, 3, 35, 1],
[7, 80, 4, 36, 2],
[8, 81, 5, 37, 3]])

- A matrix has two axes


- This specific matrix has four-by-five entries, its size is 20 and its shape is (4,5).
- Note that : the entries from the first axis (axis=0) are called rows and the entries from the
second axis (axis=1) are called columns.
1
# Acces to the element in position (2,3) : b[2,3]
# Access to the 2nd column : b[:,1]
# Access to 2nd and 3rd rows : b[1:3,:]
- When fewer indices are provided than the number of axes, the missing indices are
considered complete slices: e.g., b[i] is treated as b[i,:] .
# Access the last row : b[-1]
- The None object can be used in all slicing operations to create an axis of length one.
c = b[-1] # : a vector
c = b[-1,None] # : a matrix with one row
c = b[:,[-1]] # : a matrix with one column
c = b[:,1]

- Iterating over matrices is done with respect to the rows:


for row in b:
print(row)

3D tensors:
If you pack matrices in a new array, you obtain a 3D tensor :
c = np.array([[[5, 78, 2, 34, 0],
[6, 79, 3, 35, 1],
[7, 80, 4, 36, 2],
[8, 81, 5, 37, 3]],

[[78, 2, 34, 0, 5],


[79, 3, 35, 1, 6],
[80, 4, 36, 2, 7],
[81, 5, 37, 3, 8]],

[[0, 5, 78, 2, 34],


[1, 6, 79, 3, 35],
[2, 7, 80, 4, 36],
[3, 8, 81, 5, 37]]])
c.ndim
- This tensor has three-by-four-by-five entries. Hence, its size is 60 and its shape is (3,4,5).
- Tensor elements can be accessed by giving one index per axis.
c[1,3,2]
- Iterating over multidimensional arrays is done with respect to the first axis:
for matrix in c:
print(matrix)

Reshaping:
- An array has a shape given by the number of elements along each axis:
a = np.floor(10*np.random.random((3,4)))
print("Shape: " + str(a.shape))
print(a)
- The shape of an array can be changed with various commands, without changing the
elements in the original array:

2
# array flattened as a vector ]: b = a.ravel()
# array with a modified shape :
b = a.reshape(6,2)
print("Shape: " + str(b.shape))
print()
print(b)
# array, transposed
b = a.T
print("Shape: " + str(b.shape))
print()
print(b)

- If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically


calculated :
b = a.reshape(4,-1)
print("Shape: " + str(b.shape))
print()
print(b)
- The np.reshape function returns its argument with a modified shape, whereas the
np.array.resize method modifies the array itself:
print("Shape: " + str(a.shape))
print()
print(a)
a.resize((2,6))
print("Shape: " + str(a.shape))
print()
print(a)

You might also like