TP1_ (AAr)
TP1_ (AAr)
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:
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]],
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)