What are tensors how are they different from numpy arrays

This recipe explains what are tensors and how are they different from numpy arrays

Recipe Objective

What are tensors?

How are they different from numpy arrays? Tensors, this is nothing but the container which can be house data in n-dimensions along with its linear operations, tensors are the conclusion or generalization of the matrices to the n-dimensional space. Tensors are multidimensional array which is uniform in data type as type, we cannot update a tensor but we can create a new on. These are more than a data container as apart from holding numeric data these also include the descriptions of the valid linear transformation within the tensors. Mathematically the vector, scalar, matrix all are tensors. Numpy Array these are nothing but the matrix, the most important part in this Numpy package is the n-dimensional array or ndarray. These are not similar to others, they are different from the python arrays/list in which the NumPy arrays are homogeneous in nature. These are needed to perform mathematical operations on all the elements.

Step 1 - Import library

import tensorflow as tf import numpy as np

Step 2 - Create a tensor

tensor = tf.constant([[1,2,3],[2,3,4],[4.4,5,6]]) print("This is a simple tensor:","\n",tensor)

This is a simple tensor: 
 tf.Tensor(
[[1.  2.  3. ]
 [2.  3.  4. ]
 [4.4 5.  6. ]], shape=(3, 3), dtype=float32)

So here we can see the simple tensor with shape (3,3) which is 3 columns and 3 rows also the data type as "dtype" is being specified.

Step 3 - Create a Numpy array

array = np.array([[1,2,2],[3,33,3],[4,44,4],[5,55,5]]) print("This is a simple numpy array:","\n",array,"\n") print(array.shape, type(array))

This is a simple numpy array: 
 [[ 1  2  2]
 [ 3 33  3]
 [ 4 44  4]
 [ 5 55  5]] 

(4, 3) <class 'numpy.ndarray'>

Here we can see the simple numpy array, with there shape which is (4,3) means 4 columns and 3 rows also the data type is given as "ndarray".

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.