In [2]:
import numpy as np
In [2]:
np.info(np.ndarray.dtype)
In [6]:
a = np.array([1,2,3])
print("a = \n{}".format(a))
b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
print("b = \n{}".format(b))
c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype = float)
print("c = \n{}".format(c))
In [7]:
zeros_arr = np.zeros((3,4)) # Create an array of zeros
print("zeros_arr = \n{}".format(zeros_arr))
ones_arr = np.ones((2,3,4),dtype=np.int16) # Create an array of ones
print("ones_arr = \n{}".format(ones_arr))
d = np.arange(10,25,5) # Create an array of evenly spaced values (step value)
print("d = {}".format(d))
linspace_arr = np.linspace(0,2,9) # Create an array of evenly spaced values (number of samples)
print("linspace_arr = \n{}".format(linspace_arr))
e = np.full((2,2),7) # Create a constant array
print("e = \n{}".format(e))
f = np.eye(2) # Create a 2X2 identity matrix
print("f = \n{}".format(f))
random_arr = np.random.random((2,2)) # Create an array with random values
print("random_arr = \n{}".format(random_arr))
empty_arr = np.empty((3,2)) # Create an empty array
print("empty_arr = \n{}".format(empty_arr))
In [10]:
np.save('../sample_files/data/numpy_array', a) # Saves a binary file
np.savez('../sample_files/data/numpy_array.npz', a, b) # Saves uncompressed npz
np.load('../sample_files/data/numpy_array.npy') # Load file
Out[10]:
In [13]:
np.loadtxt("../sample_files/data/mnist.txt")
np.genfromtxt("../sample_files/data/winequality-red.csv", delimiter=',')
np.savetxt("../sample_files/data/numpy.txt", b, delimiter=" ")
In [14]:
np.int64 # Signed 64-bit integer types
np.float32 # Standard double-precision floating point
np.complex # Complex numbers represented by 128 floats
np.bool # Boolean type storing TRUE and FALSE values
np.object # Python object type
np.string_ # Fixed-length string type
np.unicode_ # Fixed-length unicode type
Out[14]:
In [21]:
ans = a.shape # Array dimensions
print("a.shape = {}".format(ans))
ans = len(a) # Length of array
print("len(a) = {} elements".format(ans))
ans = b.ndim # Number of array dimensions
print("b.ndim = {} dimensions".format(ans))
ans = e.size # Number of array elements
print("e.size = {} elements".format(ans))
ans = b.dtype # Data type of array elements
print("b.dtype = {}".format(ans))
ans = b.dtype.name # Name of data type
print("b.dtype.name = {}".format(ans))
ans = b.astype(int) # Convert an array to a different type
print("b.astype(int) = \n{}".format(ans))
In [23]:
g = a - b # Subtraction
np.subtract(a,b) # Subtraction
b + a # Addition
np.add(b,a) # Addition
a / b # Division
np.divide(a,b) # Division
a * b # Multiplication
np.multiply(a,b) # Multiplication
np.exp(b) # Exponentiation
np.sqrt(b) # Square root
np.sin(a) # Print sines of an array
np.cos(b) # Element-wise cosine
np.log(a) # Element-wise natural logarithm
e.dot(f) # Dot product
Out[23]:
In [29]:
ans = a == b # Element-wise comparison
print("a == b is \n{}".format(ans))
ans = a < 2 # Element-wise comparison
print("a < 2 is {}".format(ans))
ans = np.array_equal(a, b) # Array-wise comparison
print("np.array_equal(a, b) is {}".format(ans))
In [36]:
ans = a.sum() # Array-wise sum
print("a.sum() = {}".format(ans))
ans = a.min() # Array-wise minimum value
print("a.min() = {}".format(ans))
ans = b.max(axis=0) # Maximum value of an array row
print("b.max(axis=0) = {}".format(ans))
ans = b.cumsum(axis=1) # Cumulative sum of the elements
print("b.cumsum(axis=1) = \n{}".format(ans))
ans = a.mean() # Mean
print("a.mean() = {}".format(ans))
ans = np.median(b) # Median
print("np.median(b) = {}".format(ans))
ans = np.corrcoef(a) # Correlation coefficient
print("np.corrcoef(a) = {}".format(ans))
ans = np.std(b) # Standard deviation
print("np.std(b) = {}".format(ans))
In [37]:
h = a.view() # Create a view of the array with the same data
np.copy(a) # Create a copy of the array
h = a.copy() # Create a deep copy of the array
In [48]:
a.sort() # Sort an array
print("a.sort() = {}".format(a))
c.sort(axis=0) # Sort the elements of an array's axis
print("c.sort(axis=0) = \n{}".format(c))
In [64]:
# Subsetting
ans = a[2] # Select the element at the 2nd index
print("a={} => a[2]={}".format(a, ans))
ans = b[1,2] # Select the element at row 1 column 2
print("b=\n{} => b[1,2]={}".format(b, ans))
# Slicing
ans = a[0:2] # Select items at index 0 and 1
print("a={} => a[0:2]={}".format(a, ans))
ans = b[0:2,1] # Select items at rows 0 and 1 in column 1
print("b={} => b[0:2,1]={}".format(b, ans))
ans = b[:1] # Select all items at row 0
print("b={} => b[:1]={}".format(b, ans))
ans = c[1,:,:] # Same as [1,...]
print("c=\n{} =>\nc[1,:,:]=\n{}".format(c, ans))
ans = a[ : :-1] # Reversed array a
print("a={} => a[ : :-1]={}".format(a, ans))
# Boolean Indexing
ans = a[a<2] # Select elements from a less than 2
print("a={} => a[a<2]={}".format(a, ans))
# Fancy Indexing
ans = b[[1, 0, 1, 0],[0, 1, 2, 0]] # Select elements (1,0),(0,1),(1,2) and (0,0)
print("b=\n{} =>\nb[1, 0, 1, 0],[0, 1, 2, 0]]=\n{}".format(b, ans))
ans = b[[1, 0, 1, 0]][:,[0,1,2,0]] # Select a subset of the matrix’s rows
print("b=\n{} =>\nb[[1, 0, 1, 0]][:,[0,1,2,0]]=\n{}".format(b, ans))
In [82]:
# Transposing Array
ans = np.transpose(b) # Permute array dimensions
print("np.transpose(b) =\n{}".format(ans))
ans = ans.T # Permute array dimensions
print("ans.T =\n{}".format(ans))
# Changing Array Shape
ans = b.ravel() # Flatten the array
print("b.ravel() =\n{}".format(ans))
ans = g.reshape(3,-2) # Reshape, but don’t change data
print("g.reshape(3,-2) =\n{}".format(ans))
# Adding/Removing Elements
h.resize((2,6)) # Return a new array with shape (2,6)
print("h.resize((2,6)) =\n{}".format(h))
ans = np.append(h,g) # Append items to an array
print("np.append(h,g) =\n{}".format(ans))
ans = np.insert(a, 1, 5) # Insert items in an array
print("np.insert(a, 1, 5) = {}".format(ans))
ans = np.delete(a,[1]) # Delete items from an array
print("np.delete(a,[1]) = {}".format(ans))
# Combining Arrays
ans = np.concatenate((a,d),axis=0) # Concatenate arrays
print("np.concatenate((a,d),axis=0) =\n{}".format(ans))
ans = np.vstack((a,b)) # Stack arrays vertically (row-wise)
print("np.vstack((a,b)) =\n{}".format(ans))
ans = np.r_[e,f] # Stack arrays vertically (row-wise)
print("np.r_[e,f] =\n{}".format(ans))
ans = np.hstack((e,f)) # Stack arrays horizontally (column-wise)
print("np.hstack((e,f)) =\n{}".format(ans))
ans = np.column_stack((a,d)) # Create stacked column-wise arrays
print("np.column_stack((a,d)) =\n{}".format(ans))
ans = np.c_[a,d] # Create stacked column-wise arrays
print("np.c_[a,d] =\n{}".format(ans))
# Splitting Arrays
ans = np.hsplit(a,3) # Split the array horizontally at the 3rd
print("np.hsplit(a,3) =\n{}".format(ans))
ans = np.vsplit(c,2) # Split the array vertically at the 2nd index
print("np.vsplit(c,2) =\n{}".format(ans))
In [ ]: