# Difference Between Scalar, Vector, Matrix and Tensor

## Scalar

A scalar is just a single number

## Vector

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]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

### Dimension of vector1

In [2]:
vector1.ndim

Out[2]:
1

### Shape of vector1

In [3]:
vector1.shape

Out[3]:
(10,)

The value is 10.

## Matrix

A matrix is a 2-D array of numbers, so each element is identiﬁed by two indices instead of just one.

### 2x2 matrix

In [4]:
matrix1 = np.array([
[1, 2],
[3, 4]
])
matrix1

Out[4]:
array([[1, 2],
[3, 4]])
In [5]:
matrix1.shape

Out[5]:
(2, 2)
In [6]:
matrix1.ndim

Out[6]:
2

### 4x3 matrix

In [7]:
matrix2 = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])
matrix2

Out[7]:
array([[ 1,  2,  3],
[ 4,  5,  6],
[ 7,  8,  9],
[10, 11, 12]])
In [8]:
matrix2.shape

Out[8]:
(4, 3)
In [9]:
matrix2.ndim

Out[9]:
2

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]:
matrix([[1, 2],
[3, 4]])
In [11]:
matrix3.shape

Out[11]:
(2, 2)
In [12]:
matrix3.ndim

Out[12]:
2

## Tensor

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]:
array([[[ 1,  2,  3],
[ 4,  5,  6]],

[[ 7,  8,  9],
[10, 11, 12]]])
In [14]:
tensor1.shape

Out[14]:
(2, 2, 3)
In [15]:
tensor1.ndim

Out[15]:
3
In [ ]:


In [ ]: