A scalar is just a single number
A vector is an array of numbers. One-dimensional array of numbers.
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
Create an array.
In [1]:
import numpy as np
vector1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
vector1
Out[1]:
In [2]:
vector1.ndim
Out[2]:
In [3]:
vector1.shape
Out[3]:
The value is 10.
A matrix is a 2-D array of numbers, so each element is identified by two indices instead of just one.
In [4]:
matrix1 = np.array([
[1, 2],
[3, 4]
])
matrix1
Out[4]:
In [5]:
matrix1.shape
Out[5]:
In [6]:
matrix1.ndim
Out[6]:
In [7]:
matrix2 = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])
matrix2
Out[7]:
In [8]:
matrix2.shape
Out[8]:
In [9]:
matrix2.ndim
Out[9]:
Another way we can create a matrix is by using the matrix
function.
class numpy.matrix(data, dtype=None, copy=True)
Returns a matrix from an array-like object, or from a string of data. A matrix is a specialized 2-D array that retains its 2-D nature through operations.
Parameters
dataarray_like or string
If data is a string, it is interpreted as a matrix with commas or spaces separating columns, and semicolons separating rows.
dtypedata-type
Data-type of the output matrix.
copybool
If data is already an ndarray, then this flag determines whether the data is copied (the default), or whether a view is constructed.
In [10]:
matrix3 = np.matrix([
[1, 2],
[3, 4]
])
matrix3
Out[10]:
In [11]:
matrix3.shape
Out[11]:
In [12]:
matrix3.ndim
Out[12]:
An array with more than two axes is called tensor. We can create 3-dimensional array, or tensor, by numpy array
function.
In [13]:
tensor1 = np.array([
[
[1, 2, 3],
[4, 5, 6]
],
[
[7, 8, 9],
[10, 11, 12]
]
])
tensor1
Out[13]:
In [14]:
tensor1.shape
Out[14]:
In [15]:
tensor1.ndim
Out[15]:
In [ ]:
In [ ]: