How to Pad an Array with Zeros or Ones in Python using Numpy 


               


How to Pad an Array with Zeros or Ones in Python using Numpy



Python


In this article, we show how to pad an array with zeros or ones in Python using numpy.

Say, you want to fill an array with all zeros or all ones.

Numpy has built-in functions that allows us to do this in Python.

We can create one-dimensional, two-dimensional, three-dimensional arrays, etc. padded with zeros or ones.

So let's go right into it now.


One-dimensional Array

We'll first go over how to create a one-dimensional array padded with zeros or ones.

To create a one-dimensional array padded with zeros, the following code can be used, shown below.



So, first, we must import numpy as np.

We then create a variable named array1 and set it equal to np.zeros(4)

What this line of code does is it creates a one-dimensional array with 4 zeros.

We then output the contents of array1, which you can see is an array with 4 zeros.

We then create an array called array2 padded with 4 ones.

So the np.zeros() function creates an array padded with zeros. And the np.ones() function creates an array padded with ones.

How to Create a 2-dimensional Array

So above we showed how to create a one-dimensional array padded with zeros or ones.

Now we will show how to create a 2-dimensional array padded with zeros or ones.

The code below creates a 2-dimensional array of 3 rows and 4 columns padded with zeros and then another array of the same dimensions padded with 1s.



So now we've created a 2-dimensional array. You know it's a 2-dimensional array by the double brackets.

All you have to do to create a 2-dimensional array is put the number of rows as the first argument and the number of columns as the second argument. This has to be done within double parentheses, not single parentheses.

We first do this with zeros and then ones.


How to Create a 3-dimensional Array

Next, we show how to create a 3-dimensional array in Python with numpy.

A 3-dimensional array is different than a 2-dimensional array in the order of arguments.

Once you get to 3-dimensional arrays, the array accepts 3 arguments.

The first argument it accepts is how many arrays you want created.

The second argument it accepts is the number of rows.

The third argument it accepts is the number of columns.

So, just like with 2-dimensional arrays, it accepts the number of rows and columns.

The first argument is how many duplicates of this array with n rows and n columns you want created.

Let's see how this works in the code below.



So in the code above, we create an array that has 3 duplicates, 5 rows, and 6 columns.

The first argument specified is how many of the arrays you want.

And the second and third columns is the number of rows and columns.

We assign this information to the variable, array3.

We then print out the contents of array3 and get the array, shown above.

And this is how we can create arrays padded with zeros or ones in Python with numpy.

We simply go through each item in the dictionary and print out each value from each item.

As you see, we get 24, 170cmb, 170lbs, and male.

And this is all that is required to iterate through all values of a dictionary in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...