Creating Arrays with NumPy



Resources


# Import NumPy
import numpy as np
# Create a list
list_1 = [1,2,3,4,5]
# Create another list # we will use this later
list_2 = [6,7,8,9,10]

Create an Array with a List

# Create a array by passing the list as an object
new_array = np.array(list_1)
new_array

Create a multi-dimensional array or matrix

# Create a list of lists
list_of_lists = [list_1, list_2]
list_of_lists
# Create a multi-dimensional array or matrix 
matrix_array = np.array(list_of_lists)
matrix_array

Check Array Data Type, Dimension or Shape

# Check Array Data Type # Can be Mixed
matrix_array.dtype
# Check Number of Dimensions
matrix_array.ndim
# Check Array Shape
matrix_array.shape

Creating Arrays with Ones or Zeros

These are built-in functions for creating arrays from scratch

## Create Zeros Array
np.zeros(3)
## Create Zeros Array
np.zeros((3, 2))
## Create Ones Array
np.ones(3)
## Create Ones Array with Shape
np.ones((3, 2))

Creating Arrays with Arrange Examples

#Creating Arrays with Arrange
#numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)
np.arange(3)
np.arange(3.0)
np.arange(2,9)
np.arange(3,33,3)
np.array()