Web Technologies - NumPy

Back to Course

Lesson Description

Lession - #294 NumPy Creating Arrays

Creating arrays in NumPy

To create an array, you can use array method of numpy.
# Creating 1D array
a = np.array([1, 2, 3]>
# Creating 2D array b = np.array([[1,2,3],[4,5,6]]>

Functions to create NumPy arrays:
a = np.zeros((2,2>
# Create an array of all zeros b = np.ones((1,2>
# Create an array of all ones pi = 3.14 c = np.full((2,2>
, pi>
# Create a constant array of pi d = np.eye(3>
# Creates a 3x3 identity matrix e = np.random.random((2,2>
# Create an array of random values

To make groupings of numbers, NumPy gives a function practically equivalent to go that returns arrays rather than lists.
  1. arange: returns equitably divided values inside a given stretch. step size is indicated.
  2. linspace: returns uniformly separated values inside a given stretch. num no. of components are returned.

A = np.arange(0, 30, 5>
# Creates [ 0, 5, 10, 15, 20, 25] B = np.linspace(1, 15, 3>
# Creates [ 1.0, 8.0, 15.0]

The Python List insert(>
method is an inbuilt function in Python that inserts a given element at a given index in a list.
The simplest way to convert a Python list to NumPy array is to use the np.array(>
function that takes an iterable and returns a NumPy array.
numpy average ... Compute the weighted average along the specified axis. ... Axis or axes along which to average.
A tutorial on using NumPy GitHub Gist: instantly share code, notes, and snippets.