Home projects readings blog about

Arrays

Published on Wednesday, 10 January, 2018 general

Implementation

import numpy as np

Basic Commands

Creating a range of numbers using a range within a list.

a = [x for x in range(10)]
a = np.array(a)

2 or + dimensional arrays are intialized using nested lists.

y = np.array([[0.0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
y
array([[0., 1., 2., 3., 4.],
   [5., 6., 7., 8., 9.]])

Check the shape.

np.shape(y)
(2, 5)

Array Data Types

x = [0, 1, 2, 3, 4] # Integers
y = np.array(x)
y.dtype
dtype('int32')

x = [0.0, 1, 2, 3, 4] # 0.0 is a float
y = np.array(x)
y.dtype

dtype('float64')
x = [0.0 + 1x, 1, 2, 3, 4] # (0.0 + 1x) is a complex
y = np.array(x)
y

array([ 0.+1.x, 1.+0.x, 2.+0.x, 3.+0.x, 4.+0.x])
y.dtype
dtype('complex128')

More Basic Commands

reshape

x = np.array([[1,0],[0,1]])
y = np.reshape(x,[4,1])
y

array([[1], [0], [0], [1]])

size

x =  np.random.randn(4,3)
np.size(x)
2

np.size

2

ndim

Returns the number of dimension.

x = np.random.randn(4,3)

x.ndim
2

tile

 w = tile(x,(2,3))
y - w

ravel

x = array([[1,2],[3,4]])
x

array([[ 1, 2],[ 3, 4]])

x.ravel()

array([1, 2, 3, 4])

x.T.ravel()

array([1, 3, 2, 4])

flatten

flat

x = array([[1,2],[3,4]])
x.flat

x.flat[2] 3 x.flat[1:4] = -1 x array([[ 1, -1],[-1, -1]])

broadcast, broadcast_arrays

vstack, hstack

concatenate

split, vsplit, hsplit

delete

squeeze

fliplr, flipud

diag

triu, tril