NumPy > All categories


Name Code Output
Create array with random values
array = numpy.random.random((2,3))
print (array)
print ("shape",array.shape)
[[ 0.64677843  0.64422554  0.37583197]
 [ 0.6459913   0.86611144  0.30007033]]
shape (2, 3)
Array maximum value in each row
array = numpy.random.randint(10, size=(2, 3))
print ("array \n",array)
result = array.max(axis=1)
print ("max row result \n",result)
array 
 [[2 7 4]
 [1 4 5]]
max row result 
 [7 5]
Select elements (slice) in 1D array
array = numpy.random.randint(10, size=(4))
print ("array \n",array)
print ("slice elements <1,3)", array[1:3])
array 
 [6 9 1 3]
slice elements <1,3) [9 1]
Array calculate variance
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.var(array)
print ("variance result: \n",result)
array 
 [[3 4 1]
 [0 2 4]]
variance result: 2.22222222222
View array datatype
array = numpy.random.random((2,3))
print ("data type: ",array.dtype.name)
data type:  float64
Stack arrays vertically
array1 = numpy.random.randint(5, size=(1, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(5, size=(1, 3))
print ("array2 \n",array2)
result = numpy.vstack((array1,array2))
print ("stack result \n",result)
array1 
 [[0 0 4]]
array2 
 [[0 3 2]]
stack result 
 [[0 0 4]
 [0 3 2]]
Array insert row
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.insert(array1,1,7,axis=0)
print ("insert row result \n",result)
array 
 [[1 3 4]
 [2 0 4]]
insert row result 
 [[2 4 3]
 [7 7 7]
 [0 0 1]]
Create evenly spaced array
array = numpy.linspace(0,2,6)
print (array)
print ("shape",array.shape)
[ 0.   0.4  0.8  1.2  1.6  2. ]
shape (6,)
Concatenate arrays
array1 = numpy.random.randint(5, size=(1, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(5, size=(1, 3))
print ("array2 \n",array2)
result = numpy.concatenate((array1,array2),axis=0)
print ("concatenate result \n",result)
array1 
 [[2 4 1]]
array2 
 [[2 2 1]]
concatenate result 
 [[2 4 1]
 [2 2 1]]
Create array of zeros
array = numpy.zeros((3,5))
print (array)
print ("shape",array.shape)
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]
shape (3, 5)
Arrays broadcasting
array1 = numpy.random.randint(5, size=(2, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(5, size=(1, 3))
print ("array2 \n",array2)
result = array1 + array2
print ("broadcasted sum result \n",result)
array1 
 [[1 2 1]
 [1 2 2]]
array2 
 [[4 0 3]]
broadcasted sum result 
 [[5 2 4]
 [5 2 5]]
Delete one item from array
array = numpy.random.randint(5, size=(1, 4))
print ("array \n",array)
result = numpy.delete(array,[1])
print ("delete item result \n",result)
array 
 [[1 2 2 0]]
delete item result 
 [1 2 0]
Array maximum value in each column
array = numpy.random.randint(10, size=(2, 3))
print ("array \n",array)
result = array.max(axis=0)
print ("max result",result)
array 
 [[4 1 6]
 [5 7 0]]
max result [5 7 6]
Array sinus
array = numpy.random.randint(360, size=(2, 3))
print ("array \n",array)
result = numpy.sin(array)
print ("sinus result \n",result)
array 
 [[222 120 130]
 [179 178 315]]
sinus result 
 [[ 0.86895084  0.58061118 -0.93010595]
 [ 0.07072217  0.87757534  0.74513326]]
Split arrays vertically
array = numpy.random.randint(5, size=(3, 4))
print ("array \n",array)
result = numpy.vsplit(array,3)
print ("split result1 \n",result[0])
print ("split result2 \n",result[1])
array 
 [[1 4 4 4]
 [2 2 1 4]
 [3 2 3 3]]
split result1 
 [[1 4 4 4]]
split result2 
 [[2 2 1 4]]
View number of array elements
array = numpy.random.random((2,3))
print ("size: ",array.size)
size:  6
Save & load array from uncompressed  file
# create array
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)

# save array to uncompressed .npy file
numpy.save('array_file', array)

# list directory
for entry in os.scandir('.'):
    print(entry.name)

# load array from file
array_loaded = numpy.load('array_file.npy')
print ("array from file \n",array_loaded)
array 
 [[2 4 2]
 [4 0 0]]
array_file.npy
array from file 
 [[2 4 2]
 [4 0 0]]
Create copy of the array
array = numpy.random.randint(10, size=(2, 3))
print ("original array \n",array)
copy = numpy.copy(array)
# alternatively you can use also array.copy()
print("array copy \n",copy)
array[0,0] = 100
print ("modified array \n",array)
print("unmodified copy \n",copy)
original array 
 [[5 9 3]
 [9 6 0]]
array copy 
 [[5 9 3]
 [9 6 0]]
modified array 
 [[100   9   3]
 [  9   6   0]]
unmodified copy 
 [[5 9 3]
 [9 6 0]]
Print numpy version
print ("Numpy version",numpy.__version__)
Numpy version 1.13.1
Arrays element-wise equality comparison
array1 = numpy.random.randint(5, size=(2, 3))
print ("array1 \n",array1,)
array2 = numpy.random.randint(5, size=(2, 3))
print ("array2 \n",array2)
result = (array1 == array2)
print ("comparison result \n",result)
array1 
 [[0 4 1]
 [4 4 0]]
array2 
 [[3 4 1]
 [4 3 4]]
comparison result 
 [[False  True  True]
 [ True False False]]
Array cosinus
array = numpy.random.randint(360, size=(2, 3))
print ("array \n",array)
result = numpy.cos(array)
print ("cosinus result \n",result)
array 
 [[ 37 350 192]
 [303 125 285]]
cosinus result 
 [[ 0.76541405 -0.28363328 -0.93488971]
 [ 0.16296104  0.78771451 -0.63334253]]
Array calculate average
array = numpy.random.randint(5, size=(3, 4))
print ("array \n",array)
result = numpy.average(array)
print ("average result:",result)
array 
 [[0 1 0 1]
 [0 4 2 1]
 [4 1 2 1]]
average result: 1.41666666667
Create 1D array
array = numpy.array([1,2,3,4,5])
print (array)
print ("shape",array.shape)
[1 2 3 4 5]
shape (5,)
Create array from range
array = numpy.arange(10,15)
print (array)
print ("shape",array.shape)
[10 11 12 13 14]
shape (5,)
Select Nth row in 2D array
array = numpy.random.randint(10, size=(3, 3))
print ("array \n",array)
print ("third row \n", array[2])
array 
 [[2 7 3]
 [0 3 9]
 [1 7 8]]
third row 
 [1 7 8]
View number of array dimensions
array = numpy.random.random((2,3))
print ("number of dimensions:",array.ndim)
number of dimensions: 2
Array element-wise comparison to scalar
array = numpy.random.randint(10, size=(2, 3))
print ("array \n",array)
result = (array < 5)
print ("comparison result \n",result)
array 
 [[3 6 5]
 [0 7 1]]
comparison result 
 [[ True False False]
 [ True False  True]]
Sort array along axis
array = numpy.random.randint(20, size=(2, 3))
print ("original array \n",array)
array.sort(axis=0)
print("sorted array axis 0 \n",array)
array.sort(axis=1)
print("sorted array axis 1 \n",array)
original array 
 [[16 11  7]
 [ 1 12 16]]
sorted array axis 0 
 [[ 1 11  7]
 [16 12 16]]
sorted array axis 1 
 [[ 1  7 11]
 [12 16 16]]
Array dot product
array1 = numpy.random.randint(5, size=(2, 3))
print ("array1 \n",array1,)
array2 = numpy.random.randint(5, size=(3, 2))
print ("array2 \n",array2)
result = array1.dot(array2)
print ("dot product result \n",result)
array1 
 [[0 1 2]
 [0 4 3]]
array2 
 [[1 4]
 [1 0]
 [3 4]]
dot product result 
 [[ 7  8]
 [13 12]]
Addition of arrays
array1 = numpy.random.randint(10, size=(2, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(10, size=(2, 3))
print ("array2 \n",array2)
result = array1 + array2
print ("addition result \n",result)
array1 
 [[1 0 4]
 [7 0 1]]
array2 
 [[4 5 6]
 [5 7 9]]
addition result 
 [[ 5  5 10]
 [12  7 10]]
Array insert column
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.insert(array,1,7,axis=1)
print ("insert column result \n",result)
array 
 [[1 1 3]
 [2 0 4]]
insert column result 
 [[1 7 1 3]
 [2 7 0 4]]
Create array of constant values
array = numpy.full((3,5),4)
print (array)
print ("shape",array.shape)
[[4 4 4 4 4]
 [4 4 4 4 4]
 [4 4 4 4 4]]
shape (3, 5)
Select part of 2D matrix
array = numpy.random.randint(10, size=(4, 4))
print ("array \n",array)
print ("submatrix \n", array[0:2,0:2])
array 
 [[8 0 3 1]
 [4 8 9 7]
 [0 7 2 6]
 [2 7 4 3]]
submatrix 
 [[8 0]
 [4 8]]
Multiplication of arrays
array1 = numpy.random.randint(10, size=(2, 3))
print ("array1 \n",array1,)
array2 = numpy.random.randint(10, size=(2, 3))
print ("array2 \n",array2)
result1 = array1 * array2
print ("multiplication result method 1 \n",result1)
result2 = numpy.multiply(array1,array2)
print ("multiplication result method 2 \n",result2)
array1 
 [[7 6 8]
 [9 1 6]]
array2 
 [[2 7 8]
 [6 9 4]]
multiplication result method 1 
 [[14 42 64]
 [54  9 24]]
multiplication result method 2 
 [[14 42 64]
 [54  9 24]]
Array median
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.median(array)
print ("median result:",result)
array 
 [[4 3 0]
 [4 0 0]]
median result: 1.5
Reverse 1D array
array = numpy.array([1,2,3])
print ("array \n",array)
print ("reversed array \n", array[: : -1])
array 
 [1 2 3]
reversed array 
 [3 2 1]
Select Nth element in 1D array
array = numpy.random.randint(10, size=(3))
print ("array \n",array)
print ("second element \n", array[1])
array 
 [8 6 4]
second element 
 6
Setect single element in 2D array
array = numpy.random.randint(10, size=(2,3))
print ("array \n",array)
print ("element at index [0,1]:", array[0,1])
array 
 [[4 9 3]
 [1 3 2]]
element at index [0,1]: 9
Column stack arrays
array1 = numpy.random.randint(5, size=(2, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(5, size=(2, 3))
print ("array2 \n",array2)
result = numpy.column_stack((array1,array2))
print ("stack result \n",result)
array1 
 [[3 2 4]
 [2 4 3]]
array2 
 [[3 1 4]
 [3 1 4]]
stack result 
 [[3 2 4 3 1 4]
 [2 4 3 3 1 4]]
Division of arrays
array1 = numpy.random.randint(10, size=(2, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(10, size=(2, 3))
print ("array2 \n",array2)
result = array1 / array2
print ("division result \n",result)
array1 
 [[6 8 2]
 [4 5 6]]
array2 
 [[1 4 3]
 [5 8 8]]
division result 
 [[ 6.          2.          0.66666667]
 [ 0.8         0.625       0.75      ]]
Flatten array
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.ravel()
print ("flatened array \n",result)
array 
 [[4 4 1]
 [0 0 3]]
flatened array 
 [4 4 1 0 0 3]
Fancy array indexing
array = numpy.random.randint(10, size=(3, 3))
print ("array \n",array)
print ("fancy diagonal indexing \n", array[[0,1,2],[0,1,2]])
array 
 [[9 7 0]
 [9 7 3]
 [0 7 2]]
fancy diagonal indexing 
 [9 7 2]
Array exponential
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.exp(array)
print ("exponential result \n",result)
array 
 [[4 4 2]
 [1 1 0]]
exponential result 
 [[ 54.59815003  54.59815003   7.3890561 ]
 [  2.71828183   2.71828183   1.        ]]
Create empty array
array = numpy.empty((2,3))
print (array)
print ("shape",array.shape)
[[ 0.64677843  0.64422554  0.37583197]
 [ 0.6459913   0.86611144  0.30007033]]
shape (2, 3)
Array square root
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.sqrt(array)
print ("square root result \n",result)
array 
 [[2 3 1]
 [4 3 3]]
square root result 
 [[ 1.41421356  1.73205081  1.        ]
 [ 2.          1.73205081  1.73205081]]
Array sum
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.sum()
print ("sum result:",result)
array 
 [[2 4 4]
 [2 1 4]]
sum result: 17
Array cumulative sum of elements
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.cumsum()
print ("cumsum result \n",result)
array 
 [[4 2 0]
 [1 2 2]]
cumsum result 
[ 4  6  6  7  9 11]
Array correlation coefficient
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.corrcoef(array)
print ("correlation coeff result \n",result)
array 
 [[0 4 3]
 [0 0 4]]
correlation coeff result 
 [[ 1.         0.2773501]
 [ 0.2773501  1.       ]]
View numpy help
numpy.info(numpy.ndarray.ndim)
Number of array dimensions.

Examples
--------
>>> x = np.array([1, 2, 3])
>>> x.ndim
1
>>> y = np.zeros((2, 3, 4))
>>> y.ndim
3
Create array of ones
array = numpy.ones((3,5))
print (array)
print ("shape",array.shape)
[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]
shape (3, 5)
Save & load array to compressed file
# create aray
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)

# save array to compressed .npz file
numpy.savez('array_file_compr', array_data = array)

#list directory
for entry in os.scandir('.'):
    print(entry.name)

# load array from file
array_loaded = numpy.load('array_file_compr.npz')['array_data']
print ("array from file \n",array_loaded)
array 
 [[1 4 4]
 [0 3 0]]
array_file_compr.npz
array from file 
 [[1 4 4]
 [0 3 0]]
Append to array
array1 = numpy.random.randint(5, size=(2, 3))
print ("array \n",array1)
array2 = numpy.random.randint(5, size=(1, 3))
print ("array \n",array2)
result = numpy.append(array1,array2,axis=0)
print ("append result \n",result)
array 
 [[4 3 1]
 [4 0 3]]
array 
 [[4 2 3]]
append result 
 [[4 3 1]
 [4 0 3]
 [4 2 3]]
Create 3D array
array = numpy.array([[(1,1,1), (2,2,2)],\
                     [(3,3,3), (4,4,4)]])
print (array)
print ("shape",array.shape)
[[[1 1 1]
  [2 2 2]]

 [[3 3 3]
  [4 4 4]]]
shape (2, 2, 3)
Select Nth column in 2D array
array = numpy.random.randint(5, size=(2, 4))
print ("array \n",array)
print ("second column:", array[:,1])
array 
 [[2 0 0 4]
 [1 0 2 4]]
second column: [0 0]
Create 2D array
array = numpy.array([(1,2,3), (4,5,6)])
print (array)
print ("shape",array.shape)
[[1 2 3]
 [4 5 6]]
shape (2, 3)
Split array horizontally
array = numpy.random.randint(5, size=(3, 4))
print ("array \n",array)
result = numpy.hsplit(array,2)
print ("split result1 \n",result[0])
print ("split result2 \n",result[1])
array 
 [[1 4 2 3]
 [3 4 3 3]
 [4 1 0 4]]
split result1 
 [[1 4]
 [3 4]
 [4 1]]
split result2 
 [[2 3]
 [3 3]
 [0 4]]
Create array view
array = numpy.random.randint(10, size=(2, 3))
print ("original array \n",array)
view = array[1,]
print("array view \n",view)
array[:,:] = array + 1
print("view after array update \n",view)
original array 
 [[7 0 0]
 [6 2 3]]
array view 
 [6 2 3]
view after array update 
 [7 3 4]
Arrays subtraction
array1 = numpy.random.randint(20, size=(2, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(20, size=(2, 3))
print ("array2 \n",array2)
result = array1 - array2
print ("subtraction result \n",result)
array1 
 [[15 18  7]
 [19 16  7]]
array2 
 [[ 0 16  9]
 [17  1  2]]
subtraction result 
 [[15  2 -2]
 [ 2 15  5]]
View array legth
array = numpy.random.random((3,5))
print ("array length:",len(array))
array length: 3
Change array datatype
array = numpy.random.random((2,3))
print ("original data type: ",array.dtype.name)
array = array.astype(numpy.int64)
print ("new data type: ",array.dtype.name)

# NumPy Datatypes 
# BOOLEAN TYPES: 
# bool_
# INTEGER TYPES: 
# int_, intc, intp, int8, int16, int32, int64
# UNSIGNED INTEGER TYPES: 
# uint8, uint16, uint32, uint64
# FLOATING POINTS TYPES: 
# float_, float16, float32, float64
# COMPLEX NUMBER TYPES: 
# complex_, complex64, complex128
original data type:  float64
new data type:  int64
Array-wise comparison
array1 = numpy.random.randint(5, size=(2, 3))
print ("array1 \n",array1)
array2 = array1.copy()
print ("array2 \n",array2)
array3 = array2 *2
print ("array3 \n",array3)
result1 = numpy.array_equal(array1, array2)
print ("array1 vs array2 \n",result1)
result2 = numpy.array_equal(array1, array3)
print ("array1 vs array3 \n",result2)
array1 
 [[3 1 1]
 [2 1 4]]
array2 
 [[3 1 1]
 [2 1 4]]
array3 
 [[6 2 2]
 [4 2 8]]
array1 vs array2 
 True
array1 vs array3 
 False
Array remove column
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.delete(array,1,axis=1)
print ("remove column result \n",result)
array 
 [[0 1 0]
 [3 1 2]]
remove column result 
 [[0 0]
 [3 2]]
Array standard deviation
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.std(array)
print ("standard deviation result:",result)
array 
 [[3 4 2]
 [2 0 3]]
standard deviation result: 1.24721912892
Array mean
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.mean()
print ("mean result:",result)
array 
 [[4 3 4]
 [2 3 0]]
mean result: 2.66666666667
Array natural logarithm
array = numpy.random.randint(10, size=(2, 3))
print ("array \n",array)
result = numpy.log(array)
print ("logarithm result \n",result)
array 
 [[4 1 9]
 [7 1 7]]
logarithm result 
 [[ 1.38629436  0.          2.19722458]
 [ 1.94591015  0.          1.94591015]]
Create array from range with step value
array = numpy.arange(10,25,2)
print (array)
print ("shape",array.shape)
[10 12 14 16 18 20 22 24]
shape (8,)
Create identity matrix
array = numpy.eye(3)
print (array)
print ("shape",array.shape)
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
shape (3, 3)
Resize array
array = numpy.random.randint(5, size=(3, 4))
print ("array \n",array)
array.resize((2,3))
print ("resized array \n",array)
array 
 [[1 1 4 0]
 [1 4 1 3]
 [3 4 3 2]]
resized array 
 [[1 1 4]
 [0 1 4]]
Reshape array
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.reshape(3,2)
print ("reshaped array \n",result)
array 
 [[3 4 3]
 [3 3 2]]
reshaped array 
 [[3 4]
 [3 3]
 [3 2]]
Import numpy
import numpy
Print array shape
array = numpy.array([(1,2,3), (4,5,6)])
print ("shape",array.shape)
print ("shape axis0: ",array.shape[0])
print ("shape axis1: ",array.shape[1])
shape (2, 3)
shape axis0:  2
shape axis1:  3
Transpose array
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result1 = numpy.transpose(array)
print ("transpose method 1 \n",result1)
result2 = array.T
print ("transpose method 2 \n",result2)
array 
 [[1 4 1]
 [4 1 3]]
transpose method 1 
 [[1 4]
 [4 1]
 [1 3]]
transpose method 2 
 [[1 4]
 [4 1]
 [1 3]]
Array remove row
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = numpy.delete(array,1,axis=0)
print ("remove row result \n",result)
array 
 [[4 0 0]
 [3 1 4]]
remove row result 
 [[4 0 0]]
Stack arrays horizontally
array1 = numpy.random.randint(5, size=(1, 3))
print ("array1 \n",array1)
array2 = numpy.random.randint(5, size=(1, 3))
print ("array2 \n",array2)
result = numpy.hstack((array1,array2))
print ("stack result \n",result)
array1 
 [[3 1 1]]
array2 
 [[1 2 0]]
stack result 
 [[3 1 1 1 2 0]]
Conditionally select array elements (boolean indexing)
array = numpy.random.randint(10, size=(3, 3))
print ("array \n",array)
print ("elements less than 2 \n", array[array<4])
array 
 [[9 3 1]
 [6 3 2]
 [6 4 4]]
elements less than 2 
 [3 1 3 2]
Sort array
array = numpy.random.randint(10, size=(2, 3))
print ("original array \n",array)
array.sort()
print("sorted array \n",array)
original array 
 [[3 9 1]
 [5 3 8]]
sorted array 
 [[1 3 9]
 [3 5 8]]
Array minimum value
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.min()
print ("min result:",result)
array 
 [[2 0 4]
 [4 4 0]]
min result: 0