Thanks to theidioms.com

Learn NumPy for Data Science (Course II)

Learn NumPy for Data Science (Course II)

Creating NumPy Arrays

In the previous chapter, we got familiar with NumPy and NumPy arrays. In this chapter, we will be learning about the different ways by which we can create NumPy Arrays. Following are the most common ways for creating NumPy arrays:

  • From other Python Data Structures (e.g., Lists, Tuples)
  • Using NumPy’s Built-In Functions (e.g., arange, ones, zeros, etc.)

Create NumPy Array from other Python Data Structures

NumPy arrays can be created by using other Python Data Structures such as list and tuple by passing them into the numpy.array function.

 # Importing Numpy 
import numpy as np

# Defining a python list and tuple
lis = [1, 2, 3, 4]
tup = (5, 6, 7, 8)

# Creating numpy arrays
arr_lis = np.array(lis)
arr_tup = np.array(tup)

# Printing the arrays
print("NumPy array from List: ", arr_lis)
print("NumPy array from Tuple: ", arr_tup)
Array from List: [1 2 3 4]
Array from Tuple: [5 6 7 8]

Create NumPy Array using Built-In Functions

NumPy provides a wide variety of function for creating NumPy array from scratch. Some of those functions are illustrated below:

numpy.zeros()

This function will create a NumPy array of the given shape, with all elements of the array as zeros. The basic parameters that need to be passed into this function are:

  • shape: Shape of the desired array (integer or tuple of integers).
  • dtype: Datatype of the desired array (default is numpy.float64).
# Importing Numpy 
import numpy as np

# Creating numpy arrays
arr1 = np.zeros(shape=(1, 5), dtype=np.int16)
arr2 = np.zeros(shape=(5, 5)) # Default dtype is np.float64

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)
arr1: [[0 0 0 0 0]]
arr2: [[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
numpy.ones()

This function will create a NumPy array of the given shape, with all elements of the array as ones. The basic parameters that need to be passed into this function are:

  • shape: Shape of the desired array (integer or tuple of integers).
  • dtype: Datatype of the desired array (default is numpy.float64).
# Importing Numpy 
import numpy as np

# Creating numpy arrays
arr1 = np.ones(shape=(1, 5), dtype=np.int16)
arr2 = np.ones(shape=(5, 5)) # Default dtype is np.float64

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)
arr1: [[1 1 1 1 1]]
arr2: [[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
numpy.arange()

This function is used to create a one dimensional NumPy array having evenly spaced values within a given interval. The basic parameters that need to be passed into this function are:

  • start: The starting value of the interval. It should be an integer and the default value is 0. This value is included in the array created.
  • stop: The ending value of the interval. This value is not generally included in the created array.
  • step: The distance between two adjacent values in the array to be created. The default step size is 1.
  • dtype: Datatype of the desired array.
import numpy as np

# Creating numpy arrays
arr1 = np.arange(start=1, stop=5, step=1, dtype=np.int16)
arr2 = np.arange(start=10, stop=20, step=2, dtype=np.float32)

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)
arr1: [1 2 3 4]
arr2: [10. 12. 14. 16. 18.]
numpy.linspace()

This function returns an evenly spaced NumPy array over a specified interval. The basic parameters that need to be passed into this function are:

  • start: The starting value of the interval. This value is included in the array created.
  • stop: The ending value of the interval. This value is also generally included in the created array.
  • num: The number of evenly spaced samples to be generated within the given interval. The default value is 50.
import numpy as np

# Creating numpy arrays
arr1 = np.linspace(start=1, stop=3, num = 5)
arr2 = np.linspace(start=10, stop=20, num=2) 

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)
arr1: [1. 1.5 2. 2.5 3. ]
arr2: [10. 20.]

Note: Only the basic parameters of numpy.linspace() is discussed above. If you want to know more about other parameters that you can pass into the function, do check this official documentation of NumPy.

numpy.random.rand()

This function will generate a NumPy array of the desired shape whose elements are random samples from a uniform distribution over [0, 1). Only the shape of the desired array needs to be passed on to this function.

import numpy as np

# Creating numpy arrays
arr1 = np.random.rand(3,)
arr2 = np.random.rand(2, 2) 

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)
arr1: [0.52768115 0.73880502 0.12197086]
arr2: [[0.7268723 0.77066793]
[0.39853139 0.28570399]]

With this chapter, you have learned to create NumPy arrays from standard python data structures and from NumPy’s Built-In functions. In the next chapter, you will learn to perform operations on NumPy Arrays.

Leave your thought here

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami