banner



How To Create Ndarray Python

How To Create NumPy Arrays From Python Data Structures

0

We can create numpy array using the following two ways.

  1. Numpy array from Python list
  2. Numpy array from Python tuple

NumPy, which stands for Numerical Python, is the library consisting of multidimensional array objects and the collection of routines for processing those arrays.

Using NumPy, mathematical and logical operations on arrays can be performed.

In this example, we will see how we can create a NumPy array using Python Data Structures like List or Tuple.

Create NumPy Arrays From Python Data Structures

We will perform all the practicals in Python Jupyter Notebook. Now, you can check your NumPy version using the following code. Write the following code inside the Jupyter Notebook cell.

import numpy as np print(np.__version__)

Now run the cell usingCtrl + Enterand see the output.

How To Create NumPy Arrays From Python Data Structures

Yours might be different as you will see this in the future.

Create Numpy Array From Python List

Let's define a list and then turn that list into the NumPy array.

app_list = [18, 0, 21, 30, 46] np_app_list = np.array(app_list) np_app_list

First, we have defined a List and then turn that list into the NumPy array usingthe np.arrayfunction.

See the output below.

Create Numpy Array From Python List

Let's add 5 to all the values inside the numpy array. See the following code.

np_app_list + 5

See the following output.

Create NumPy Arrays From Python Data Structures

Create Numpy Array From Python Tuple

Let's define a tuple and turn that tuple into an array.

app_tuple = (18, 19, 21, 30, 46) np_app_tuple = np.array(app_tuple) np_app_tuple

See the output below.

Create Numpy Array From Python Tuple

Let's take an example of a complex type in the tuple.

complex_tuple = (21.00 - 0.j, 19.00 + 9.j, 18.00 + 5.j, 30 - 1.j, 19, 18, 30) np_complex = np.array(complex_tuple) np_complex

In the above code, we have filled the tuple items with complex numbers and some regular integers.

When we convert the tuple to the numpy arrays, all the items should be converted to thecomplex numbersinNumPy array.

See the output below.

Complex Numbers in NumPy Array

You can see that the numpy array items are converted into complex numbers.

The significant difference between Numpy array and Python Tuple is that, if you perform the multiplication operation on the NumPy, all the items in the tuple will be multiplied by a provided integer.

However, that does not case with Python Tuple; it will not multiply with each item of the tuple with a provided eight value. It will create eight times the whole tuple items. See the example below.

See the example of the tuple below.

python tuple and numpy array difference

You can see that items are repeating up to 8 times, but not multiplying 8 with each item.

Now, see the case with NumPy arrays.

Difference between numpy and python data structures

See, eight is multiplied with each value of NumPy array.

Finally, How To Create NumPy Arrays From Python Data Structures is over.

How To Create Ndarray Python

Source: https://appdividend.com/2019/01/30/how-to-create-numpy-arrays-from-python-data-structures/

Posted by: upchurchsucken.blogspot.com

0 Response to "How To Create Ndarray Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel