Difference Between Scalar, Vector, Matrix and Tensor

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 identified 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 [ ]:
 

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing