Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Created February 12, 2016 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aravindhebbali/0f04a768630f4c8dd4dc to your computer and use it in GitHub Desktop.
Save aravindhebbali/0f04a768630f4c8dd4dc to your computer and use it in GitHub Desktop.
Creating NumPy Arrays
# import numpy
import numpy as np
# create numpy array from lists
np1 = np.array([1, 3, 5, 7, 9])
np1
# use the range function to create array
np2 = np.array(range(6))
np2
# array of sequential values
np3 = np.arange(0, 6)
np3
# step value
# increment by 2
np4 = np.arange(2, 20, 2)
np4
# decrement by 2
np5 = np.arange(20, 2, -2)
np5
# linspace() function
np6 = np.linspace(5, 25)
np6
# 10 values between 10 & 50
np7 = np.linspace(10, 50, 10)
np7
# array of zeros
np_zero = np.zeros(5)
np_zero
# two dimensional arrays
# list of lists
np8 = np.array([[2, 4], [6, 8]])
np8
# reshape method
# create a one dimensional array
np9 = np.arange(0, 12)
np9.reshape(3, 4)
np9
# array description
np10 = np.arange(0, 10)
np10
# array type
type(np10)
# array size
np.size(np10)
# data type
np10.dtype
# two dimensional array
np11 = np10.reshape(2, 5)
np11
# array type
type(np11)
# array size
np.size(np11)
# size of rows
np.size(np11, 0)
# size of columns
np.size(np11, 1)
# data type
np11.dtype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment