Shared Flashcard Set

Details

Numpy
Python's Numpy extension
19
Computer Science
Professional
06/26/2016

Additional Computer Science Flashcards

 


 

Cards

Term

Can an array have values of different data-types?


How do you find the data-type of an array? 


What is the shape of an array?


How many dimensions does an array have?

Definition

No. Only homogeneous data.


my_array.dtype


my_array.shape


my_array.ndim

Term

List the sevaral ways of creating arrays.


numpy equivalent to range(my_int)

 

Create an identity matrix

Definition

my_array = np.array(my_sequence)


# my_nested_list has lists of equal length

my_ndarray = np.array(my_nested_list)


np.zeros(some_shape)

np.ones(some_shape)

np.empty(some_shape)


np.arange(my_int)


np.identity(my_int)

Term

numpy has its own data-types ...


How to convert array from one data-type to another data-type

Definition

np.int32, np.float32, np.float64 ...

 

my_array.astype(some_np_dtype)


my_array.astype() always creates a new array i.e, makes a copy.

Term

What is vectorization?

 

What is broadcasting?

Definition

Vectorization: any arithmetic operations between equal-size arrays applies the operation elementwise

arr = np.array([[1., 2., 3.], [4., 5., 6.]])

arr * arr
arr - arr
 

Broadcasting: operations between differently sized arrays

 

arr ** 0.5

1 / arr
 
Term

What are views?

 

What happens when a scalar value is assigned to a slice of array?

 

How to make a copy of a slice?

 

 

 

 

Definition

Slices of arrays are views and not copies. So modifying a slice will modify the original array


Scalar value gets broadcasted and the entire slice in array will have that scalar value.


array_2 = array_1[5: 8].copy()

Term

Axis in a 2d array

 

Axis in 3darray

Definition
[image]
Term
Indexing and Slicing an ndarray
Definition

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


 

arr2d[2]

arr2d[0][2]

arr2d[0, 2]

 

arr2d[:2]

 

arr2d[:2, 1:]

Term

Boolean Indexing

 

Does boolean indexing make a view or a copy?

 

Do python keywords 'and' 'or' work with boolean indexing?

Definition

names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])

 

data = np.random.randn(7, 4)


 

data[names == 'Bob']

 

data[names == 'Bob', 2:]


 

mask = (names == 'Bob') | (names == 'Will')

mask_2 = data < 0

 

data[mask]  # mask is 1darray of boolean values

data[mask_2]  # mask_2 is of the same shape as data


Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.


The Python keywords and and or do not work with boolean arrays

Term

Fancy Indexing (part - I)

 

Does fancy indexing make a view or a copy?

Definition

data = np.random.randn(7, 4)

my_list = [4, 1, 6]


data[my_list]  # gives out a 2darray with rows 4, 1 and 6


data[[-3, -5, -1]]  # negative indices - slect rows from end.


Fancy indexing creates a new copy

Term

Fancy Indexing (part - II)

 

How do we extract a set of particular elements from an ndarray?

Definition

arr = np.arange(32).reshape((8, 4))


In [107]: arr[[1, 5, 7, 2], [0, 3, 1, 2]]

 

Out[107]: array([ 4, 23, 29, 10])

# the elements (1, 0), (5, 3), (7,1), and (2, 2) were selected.
Term
How to get a rectangular subset of values from a 2darray?
Definition

arr = np.arange(32).reshape((8, 4))


 

arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]

 

arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]

#  np.ix_ function converts two 1D integer arrays to an indexer that selects the square region


 
Term

Transpose a 2d array

 

Does T and tranpose() return a view or a copy?

Definition

arr = np.arange(15).reshape(5,3)


arr.T

arr.transpose()

np.transpose(arr)


Note that np.transpose() has no effect on 1D arrays. 


T and transpose() always retuns a view.

Term

What are universal functions (ufuncs)?

 

What are unary and binary ufuncs?

Definition

A universal function, or ufunc, is a function that performs elementwise operations on data in ndarrays.


arr = np.arange(10)

np.sqrt(arr)  # unary ufunc


 

x = np.random.randn(8)

 

y = np.random.randn(8)

np.maximum(x, y)  # binary ufunc

# elementwise maximum is calculated.

Term

Expressing Conditional Logic as Array Operations

 
Definition

xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])

yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])

cond = np.array([True, False, True, True, False])

 

result = np.where(cond, xarr, yarr)

# using where is faster than using loops


arr = randn(4, 4)

np.where(arr > 0, 2, -2)

np.where(arr > 0, 2, arr)



Term
Examples of functions of ndarray using axis as argument
Definition

arr = np.arange(15).reshape(5, 3)


arr.max(axis=0)

array([12, 13, 14])


arr.max(axis=1)

 

array([ 2,  5,  8, 11, 14])

Term

How to sort an ndarray in-place?

 

How to make a copy of a sorted array but leave the array unsorted?

Definition

my_array = np.random.rand(5,3)

array([[ 0.59903017,  0.89926112,  0.00292778],

       [ 0.64912747,  0.8558548 ,  0.41125557],

       [ 0.04489032,  0.3878664 ,  0.36893038],

       [ 0.46070177,  0.85481329,  0.48876301],

 

       [ 0.39229095,  0.61793863,  0.51719637]])

 

np.sort(my_array, axis=1)  # copy of sorted ndarray

array([[ 0.12774192,  0.44717661,  0.79421199],

       [ 0.00287633,  0.43717701,  0.92762263],

       [ 0.04967195,  0.62331689,  0.70401695],

       [ 0.18366832,  0.57997993,  0.73132519],

 

       [ 0.12530473,  0.239042  ,  0.40987859]])


arr.sort(axis=0)  # inplace sorting

arr

array([[ 0.00287633,  0.239042  ,  0.40987859],

       [ 0.04967195,  0.43717701,  0.70401695],

       [ 0.12530473,  0.44717661,  0.73132519],

       [ 0.12774192,  0.57997993,  0.79421199],

 

       [ 0.18366832,  0.62331689,  0.92762263]])

Term

Find all the unique values in an ndarray and give a 1d array as output.

 

Find out if elements in a ndarray are present in a 1darray?

Definition

np.unique(my_array)

# my_array could have any shape and dimension. Output is 1darray


np.in1d(my_ndarray, my_1darray)

# output is 1darray irrespective of the shape and dimension of my_ndarray.

Term

How are arrays saved and loaded as binary files?

 

How are multiple files saved as a zipped binary file and then loaded?

Definition

np.save('file_name', my_array)

np.load('file_name')

# .npy is the default file extension


np.savez('file_name', key1=arr_1, key2=arr_2, key3=arr_3)

arr = np.load('zipped_file_name')

arr['key1']

arr['key2']

# .npz is the default file name

Term
np.random has sevaral functions to deal with random numbers
Definition
---
Supporting users have an ad free experience!