Skip to main content

Create NumPy array from Python list and tuples

1. Intro

In this tutorial, we will learn various ways to create a NumPy array from the Python structure like the list, tuple, and others. It will be helpful in use cases where we want to leverage the power of NumPy operations on existing data structures.

Python 3.6.5 and NumPy 1.15 are used. Visual Studio Code 1.30.2 used to run iPython interactive codes.

2. One dimensional NumPy array from Python list

We will use numpy.array(object) method to create a 1-dimensional NumPy array from the Python list. List contains integer values.

#%%
# Do some import
import numpy as np
print("Numpy Version is ", np.__version__)
#%%
# Creating 1 dimensional numpy array with Python list (int type)
one_d_list = [1, 2, 3, 4, 5]
array_one_dim_list = np.array(one_d_list)
print("NumPy array: ", array_one_dim_list)
print("Shape: ", array_one_dim_list.shape)
print("Data Type: ", array_one_dim_list.dtype.name)

OUTPUT

Numpy Version is  1.15.4
NumPy array:  [1 2 3 4 5]
Shape:  (5,)
Data Type:  int64

3. Two dimensional NumPy array from Python list

We will use numpy.array(object) method to create a 2-dimensional NumPy array from the Python list. The list contains float values.

#%%
# Creating 2 dimensional numpy array with Python list (float type)
two_d_list = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
array_two_d_py_list = np.array(two_d_list)
print("NumPy array: \\n", array_two_d_py_list)
print("Shape: ", array_two_d_py_list.shape)
print("Data Type: ", array_two_d_py_list.dtype.name)

OUTPUT:

NumPy array: 
 [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]
Shape:  (3, 3)
Data Type:  float64

4. Three Dimensional NumPy array using Python list

We will use numpy.array(object) method to create a 3-dimensional NumPy array from the Python list. The list contains String values.

Non-number values in the NumPy array define its purpose of it. However, it is possible to create a String data type NumPy array.

#%%
# Creating 3 dimensional numpy array with Python 3d list (String)
three_d_list = [
    [["aa", "bb", "cc"], ["dd", "ee", "ff"], ["gg", "hh", "kk"]],
    [["ll", "mm", "nn"], ["oo", "pp", "qq"], ["rr", "ss", "tt"]],
]
three_d_array = np.array(three_d_list)
print("NumPy array: \\n", three_d_array)
print("Shape: ", three_d_array.shape)
print("Data Type: ", three_d_array.dtype.name)

OUTPUT:

NumPy array: 
 [[['aa' 'bb' 'cc']
  ['dd' 'ee' 'ff']
  ['gg' 'hh' 'kk']]
 [['ll' 'mm' 'nn']
  ['oo' 'pp' 'qq']
  ['rr' 'ss' 'tt']]]
Shape:  (2, 3, 3)
Data Type:  str64

5. NumPy array using Python list with mixed data type elements

We will use numpy.array(object) method to create a 1-dimensional NumPy array from the Python list. The list contains integer and float values.

#%%
# Creating NumPy array with Python list with mix data type elements
mix_data_type_list = [1.0, 2, 3.5, 4, 5.0]
mix_data_type_array = np.array(mix_data_type_list)
print("NumPy array: \\n", mix_data_type_array)
print("Shape: ", mix_data_type_array.shape)
print("Data Type: ", mix_data_type_array.dtype.name)

OUTPUT:

NumPy array: 
 [1. 2. 3.5 4. 5. ]
Shape:  (5,)
Data Type:  float64

NumPy supports homogeneous elements in the array. While creating when NumPy found heterogeneous elements (float and int) Python list, it automatically converts the int elements to float.

NumPy cast an element to a larger byte data type element while creating an array, It is called upcasting.

6. Specify data type while creating NumPy array

We will use the same Python array used in the previous code block to create a new NumPy array. However, we will force a data type of NumPy array.

#%% 
# Creating NumPy array with Pythong list and specifying data type
fix_data_type_array = np.array(mix_data_type_list, np.int)
print("NumPy array: \\n", fix_data_type_array)
print("Shape: ", fix_data_type_array.shape)
print("Data Type: ", fix_data_type_array.dtype.name)

OUTPUT:

NumPy array: 
 [1 2 3 4 5]
Shape:  (5,)
Data Type:  int64

We forced NumPy to make array elements of int data type by passing an argument to np.array(object, datatype) and it forced conversion of float into an int.

In absence of this parameter, NumPy should upcast int elements as float.

7. Create a NumPy array with Python list and tuple

#%%
# Creating NumPy array with mix of List and Tuples upgraded
a_list = [1, 2.5, 3]
a_tuple = (1.5 , 2.3, 3)
two_d_list_tuple_array = np.array([a_list, a_tuple])
print("NumPy array: \n", two_d_list_tuple_array)
print("Shape: ", two_d_list_tuple_array.shape)
print("Data Type: ", two_d_list_tuple_array.dtype.name)

OUTPUT:

NumPy array: 
 [[1. 2.5 3. ]
 [1.5 2.3 3. ]]
Shape:  (2, 3)
Data Type:  float64

8. NumPy array with Jagged Python List

We will create a NumPy array with jagged 2 Dimensional Python list and observe the outcome data type.

#%%
# Creating NumPy array with jagged 2 d Python list
jagged_two_d_list = [[1, 2, 3], [4, 5, 6], [7, 8]]
jagged_two_d_array = np.array(jagged_two_d_list)
print("NumPy array: \n", jagged_two_d_array)
print("Shape: ", jagged_two_d_array.shape)
print("Data Type: ", jagged_two_d_array.dtype.name)

OUTPUT:

NumPy array: 
 [list([1, 2, 3]) list([4, 5, 6]) list([7, 8])]
Shape:  (3,)
Data Type:  object

When we pass a jagged array to numpy.array() method, NumPy create array with Object elements.

9. NumPy array with a minimum dimension

We can enforce minimum dimension for the NumPy array created using numpy.array(object = list, ndim = 3), even passed Python list is not of specified dimension.

#%%
# Create NumPy array with Python List and enforce minimum dimension
# We are using an already create one dimensional array one_d_list
array_enforced_three_dim_list = np.array(object=one_d_list, ndmin=3)
print("NumPy array: ", array_enforced_three_dim_list)
print("Shape: ", array_enforced_three_dim_list.shape)
print("Data Type: ", array_enforced_three_dim_list.dtype.name)

OUTPUT:

NumPy array:  [[[1 2 3 4 5]]]
Shape:  (1, 1, 5)
Data Type:  int64

NumPy pre-pend Ones to the shape as needed to meet the minimum dimensional requirement.

10. asarray method

NumPy asarray method doesn't copy an object if not required, while array method copy object as their default option. While we can change the default behavior by passing false to copy argument array(object = list, copy = false)

#%%
# Use asarray to create NumPy array
one_dim_list = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
one_dim_array_use_asarray = np.asarray(one_dim_list)
print("NumPy array: \\n", one_dim_array_use_asarray)
print("Shape: ", one_dim_array_use_asarray.shape)
print("Data Type: ", one_dim_array_use_asarray.dtype.name)

OUTPUT:

NumPy array: 
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
Shape:  (2, 5)
Data Type:  int64

When the Python list was used, NumPy asarray had copied object, because they need to convert it into NumPy array.

But when we use one NumPy array to create a new array, asarray doesn't copy.

#%%
# array method by default make copy and hence change applied to copy
np.array(one_dim_array_use_asarray)[1]=1
print("NumPy array: \\n", one_dim_array_use_asarray)
# asarray method don't make copy and hence change applied to one_dim_array_use_asarray
np.asarray(one_dim_array_use_asarray)[1]=1
print("NumPy array: \\n", one_dim_array_use_asarray)

OUTPUT:

NumPy array: 
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
NumPy array: 
 [[1 2 3 4 5]
 [1 1 1 1 1]]

We saw how asarray doesn't copy if not needed in contrast with array method behavior.

11. Conclusion

We learned various ways to create a NumPy array using Python List and Tuples.

Download source code related to this tutorial here

  1. Python - create-array-with-python-structures.py
  2. Jupyter - create-array-with-python-structures.ipynb

Run the Jupyter notebook for this tutorial.

12. References

  1. Various NumPy data types
  2. NumPy array(object) method
  3. NumPy array vs asarray

Comments

Popular posts from this blog

Extend and reuse an existing AirByte destination connector

AirByte is an open-source ELT (Extract, Load, and Transformation) application. It heavily uses containerization for the deployment of its various components. On the local machine, we need docker to run it. AirByte has an impressive list of source and destination connectors available. One of my use case data destinations is the  ClickHouse data warehouse and its destination connector is not yet (2021-12-08) available. As per the documentation, It seems that creating a destination connector is a non-trivial job. It's a great idea to build an open-source ClickHouse destination connector. However, I tried avoiding the temptation to create one because of the required effort. AirByte has a  MySql destination connector available. ClickHouse provides a MySQL connector for access from any MySQL client. We need to configure Clickhouse to give support for the MySQL connector. Accessing ClickHouse from AirByte using its MySQL destination connector looks promising. However, when ...

Understanding Type Checking

A few examples of types in the context of programming language can be integer, float, character, string, array, etc.  When a program executes then data flow between instructions and values of specific types are assigned to a variable after some operation. It's important for the system to verify if the correct types are used as operands in operations. For e.g. In a sum operation, the expectation for operands to be of numeric type. The program's execution should fail in the case there is inconsistency. We can classify programming languages into two categories based as per their ability to cater to type safety: Dynamically Typed Language Statically Typed Language

Setting Clickhouse column data warehouse at Google Cloud Compute Engine VM

I didn't have a Google Cloud account associated with my email, so I signed up for one. It needs a valid Credit Card and mobile number to check if you are human. On successful sign up I get 300$ to spend within 3 months. Creating a free forever Google Cloud Compute Engine VM As per Google Cloud documentation you can have 1 non-preemptible e2-micro VM instance (1GB 2vCPU, 30GB Disk, etc.) per month free forever in some regions with some restrictions. I wanted the following stuff in my VM before I can install Clickhouse on to that: Ubuntu 20.x LTS SSH access from my machine Enabling SSH-based access to Google Compute Engine VM Step 1 Created an ssh private and public key on my mac using the following command ssh-keygen -t rsa -f ~/.ssh/gcloud-ssh-key -C mrityunjay -b 2048 Step 2 Copied the public key from the console using the following command: cat ~/.ssh/gcloud-ssh-key.pub output ssh-rsa <Gibrish :)> mrityunjay Step 3 I went to Google Cloud Console > Co...