Skip to content

Instantly share code, notes, and snippets.

@Highstaker
Last active June 11, 2016 10:12
Show Gist options
  • Save Highstaker/e0fbea5cc6779ef8a8be12bf3027d6ab to your computer and use it in GitHub Desktop.
Save Highstaker/e0fbea5cc6779ef8a8be12bf3027d6ab to your computer and use it in GitHub Desktop.
Studies of numpy library
import numpy as np
a = np.array([20, 30, 40, 50])
b = np.arange(4)
# mathematical operationscan be done with arrays. Arrays should be of same size and dimensions
print(a+b) # [20 31 42 53]
print(a-b) # [20 29 38 47]
print(a*b) # [0 30 80 150]
print(a/b) # [inf 30 20 16.6666667]
print(a**b) # [1 30 1600 125000]
# or you can take an aray and a number
print(a**4) # [ 160000 810000 2560000 6250000]
# and even filtration
print(a<35) # [True True False False]
# mathematical functions can be appied.
# see https://docs.scipy.org/doc/numpy/reference/routines.math.html for more
print(np.sin(a))
print(np.sum(a))
print(a.sum()) # Some functions exist also a method of ndarray. This is equivalent to the previous.
# a function can be applied to axes
c = np.array([[42,76,12], [8,214,5]])
print(c.min(axis=0)) # smallest number in each row. [8 76 5]
print(c.min(axis=1)) # smallest number in each column. [12 5]
print()
# iterations over multidimensional arrays
# [42 76 12]
# [ 8 214 5]
for r in c:
print(r)
# an array can be flattened. A generator object is returned
print(list(c.flat))
print()
# an element can be got either in a classical way or by providing a tuple
print(c[1][2]) # 5
print(c[1,2]) # 5
print(c[:,1]) # [76, 214] the second column
import numpy as np
# 1D array
arr = np.array([1,2,3])
print(arr)
print(type(arr)) # <class 'numpy.ndarray'>
print()
# 2D array
arr = np.array([[1,2.5,3, 5], [4,5,6,7]])
print(arr)
print(arr[0][1]) # accessing the elements as if it were a list is fine
print("ndarray.ndim", arr.ndim) # amount of dimension. 2 in this case
print("ndarray.shape", arr.shape) # dimensions of an array. A tuple. (2,4) in this case
print("ndarray.size", arr.size) # number of elements. 8 in this case
print("arr.dtype", arr.dtype) # describes the type of elements
print("ndarray.itemsize", arr.itemsize) # size of each element in bytes
print()
# apply a type to all
arr = np.array([[1.5, 2, 3], [4, 5, 6]], dtype=np.complex)
print(arr)
print(arr[0][1]) # accessing the elements as if it were a list is fine
print()
# create an array of zeroes. Pass a tuple containing dimensions as an argument
arr = np.zeros((3,4))
print(arr)
print()
# create an array of ones. Pass a tuple containing dimensions as an argument
arr = np.ones((3,4))
print(arr)
print()
# create an identity matrix (единичная матрица)
arr = np.eye(5)
print(arr)
print()
# create an array of emptyness. Rather, it is filled with gibberish. Not sure why it is needed.
arr = np.empty((3,5))
print(arr)
print()
# an array containing a range of numbers with a step
arr = np.arange(10,30,5) # array([10 15 20 25])
print(arr)
print()
# an array containing a range of numbers with a number of elements specified
arr = np.linspace(10,30,5) # array([10 15 20 25 30])
print(arr)
print()
# create an array from function
arr = np.fromfunction(lambda x,y: (5 * x + y)**2, (3,5))
print(arr)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment