How to make an array immutable using numpy

This recipe helps you make an array immutable using numpy

Recipe Objective

While operating over arrays, some arrays might become keyvalues that must not be altered throughout the processing. So it's better if we can make the array fixed or immutable.

So this recipe is a short example on how to make an array immutable. Let's get started.

Step 1 - Import the library

import numpy as np

Let's pause and look at these imports. Numpy is generally helpful in data manipulation while working with arrays. It also helps in performing mathematical operation.

Step 2 - Generating a random array

a = np.random.random(10)

We have generated a random array of size 10.

Step 3 - Making the array immutable

a.flags.writeable = False a[0] = 1

Here, we are making the array unwritable.

Step 5 - Let's look at our dataset now

Once we run the above code snippet, we will see a ValueError being thrown stating assignment destination is read-only

Scroll down to ipython file to visualize the error.

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.