HomeLinuxConvert 1d Array to 2nd Array Python

Convert 1d Array to 2nd Array Python


Like different programming languages, Python additionally incorporates a number of built-in knowledge constructions, for example, arrays. Arrays might be one-dimensional, two-dimensional, or relying on the person’s necessities, present a technique to set up and save knowledge. Moreover, you should use the built-in Python strategies to retrieve or modify that knowledge.

This put up will illustrate the completely different strategies for changing a 1d array right into a 2nd array.

The best way to Convert 1d Array to 2nd Array in Python?

To carry out the changing operation on a one-dimensional array to a two-dimensional array in Python, the “reshape()” perform might be utilized that permits customers to reshape an array. Extra particularly, reshaping is called altering the form of a given array which is decided by the variety of components in each dimension with out altering its knowledge.

Syntax

Make the most of the below-stated syntax for the reshape() methodology:

input_string.reshape(row, col)

Right here, the “input_string” is the variable title that incorporates the worth of the array, the “row” and “col” are two parameters which are handed to the “reshape” perform.

Instance 1: Convert 1d Array to 2nd Array in Python Utilizing “reshape()” Methodology With “np.arange()” Methodology

To transform a one-dimension array to a two-dimensional array, first import the Numpy library as np:

Now, create an array variable and initialize it with the time period “original_array”. Then, assign a price by using an “np.arange()” perform:

original_array = np.arange(8)

Right here, “arange()” is the perform of the “np” library, and we’ve got handed a price “8” because the perform parameter.

Print the beforehand created array utilizing the “print()” perform:

print(“Unique array: n, original_array)

Subsequent, create a variable that may hold the transformed array. Then, name the “reshape()” methodology by passing a desired variety of rows and columns as an argument to affiliate this methodology with the required variable:

resultant_array = original_array.reshape(2, 4)

Lastly, print the transformed array:

print(“Transformed array: n, resultant_array)

It may be seen that the required one-dimensional array has been efficiently transformed to a two-dimensional array:

Instance 2: Convert 1d Array to 2nd Array in Python Utilizing “np.reshape()” Methodology

One other technique to convert the 1d array into the 2nd array is to make the most of the “np.reshape()” perform. To take action, first, create a brand new 1d enter array:

array_list = np.array([0, 1, 2, 3, 4, 5, 6, 7])

Then, print the 1d array:

print(“Unique array: n, array_list)

Now, name the “np.reshape()” methodology with a 1d enter array by passing the parameters. Right here, the “-1” refers back to the dimension of the required enter array:

resultant_string = np.reshape(array_list, (1, 2))

Print the resultant 2nd array from the np.reshape() methodology:

print(“Transformed array: n, resultant_string)

Output

That’s all about changing a 1d array right into a 2nd array in Python by means of completely different strategies.

Conclusion

To transform a 1d array right into a 2nd array in Python, the “np.reshape()” methodology and “reshape()” methodology with the “np.arange()” methodology can be utilized. Each strategies allow customers to reshape the enter array with out modifying its knowledge. This put up illustrated the completely different strategies for changing a 1d array right into a 2nd array.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments