NumPy
keyboard_arrow_down 319 guides
chevron_leftNumPy
check_circle
Mark as learned thumb_up
1
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
NumPy | Recipes reference
schedule Aug 10, 2023
Last updated local_offer
Tags Python●NumPy
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
- Accessing a value in a NumPy 2D arrayTo access a value in a Numpy 2D array, use the [] syntax.
- Accessing columns of a 2D NumPy arrayTo access particular columns of a 2D Numpy array, use the [] syntax.
- Accessing rows of a 2D NumPy arrayIn Numpy, we use the [] syntax to access particular rows of a 2D Numpy array.
- Calculating the determinant of a matrix in PythonTo calculate the determinant of a matrix in Python, use NumPy's linalg.det(~) method.
- Checking allowed values for a NumPy data typeTo check the allowed values for a NumPy integer type use the iinfo(~) method. For float types use the finfo(~) method.
- Checking if a NumPy array is a view or copyTo check if a NumPy array is a view or not use the base property. To check if a NumPy array is a copy, we can use np.shares_memory(~) to check whether the two objects share memory or not.
- Checking the version of NumPyTo check the version of your NumPy use print(np.__version__).
- Checking whether a NumPy array contains a given rowTo check whether a NumPy array contains an instance of a particular row we can use the tolist(~) method.
- Computing Euclidean distance using NumPyTo compute the Euclidean distance between two vectors in NumPy, use the np.linalg.norm(~) method.
- Concatenating 1D NumPy arraysTo concatenate horizontally, we can use either use the np.concatenate(~) or np.hstack(~) method. To concatenate 1D Numpy arrays vertically, we can use the np.vstack(~) method.
- Converting NumPy array to lowercaseTo convert a NumPy array of strings to lowercase, use the np.char.lower(~) method.
- Converting type of NumPy array to stringTo convert the type of a NumPy array to string, use astype(str).
- Creating a copy of a NumPy arrayTo create a copy of a Numpy array, use its copy() method.
- Difference between Python List and Numpy arrayThe major benefit of Numpy arrays over Python lists is when you need to perform calculations over all the items contained within the array / list.
- Difference between the methods array_equal and array_equiv in NumPyThe key difference is that array_equal returns True when the shape of the arrays exactly match, whereas array_equiv will also return True if the one array can be broadcasted to take on the same shape.
- Difference between the methods mod and fmod in NumPyWhat differentiates mod(~) from Numpy's fmod(~) is confusingly not whether or not one is for floating numbers; they are both capable of parsing floating numbers. The defining difference is how they handle negative numbers.
- Difference between the methods power and float_power in NumPyThe key difference is that Numpy's power(~) method uses the same data-type as the input array to perform the calculation; if your input array only contains integers, then the returned result will also be of type int. On the other hand, float_power(~) always uses float64 for maximum precision.
- Finding the Index of Largest Value in a NumPy ArrayWe can find the index of the largest value in a NumPy Array using the argmax(~) function of the Numpy array or using the argmax(~) static function of the Numpy module.
- Finding the Index of Smallest Value in a NumPy ArrayIt is possible to find the index of smallest value in a Numpy array using the argmin(~) function.
- Finding the closest value in a NumPy arrayTo find the closest value in the Numpy array to a certain value we can leverage the following custom function.
- Finding the most frequent value in a NumPy arrayTo find the most frequent value in a NumPy array we can use the bincount(~) method together with argmax(~).
- Flattening NumPy arraysWe can reduce a n-dimensional Numpy array to 1D using either the flatten() or ravel() method available to all Numpy arrays. We first demonstrate their usages, and subsequently their differences.
- Getting constant pi in NumPyTo get pi as a constant in NumPy use np.pi.
- Getting elements from a two dimensional array using two dimensional array of indices in NumPyTo extract elements from a two dimensional array using two dimensional array of indices in NumPy, use x[np.arange(x.shape[0])[:, None], idx].
- Getting indices of N maximum values in NumPyTo get the indices of N maximum values in NumPy in an optimal way, use the argpartition(~) method. The argpartition(~) method allows you to select an index by which to partition, and ensures that all values at indices smaller than the value at this index appear before it, and all values at indices larger than this index value appear after it.
- Getting indices of N minimum values in NumPyTo get the indices of N miniumum values in NumPy in an optimal way, use the argpartition(~) method. The argpartition(~) method allows you to select an index by which to partition, and ensures that all values at indices smaller than the value at this index appear before it, and all values at indices larger than this index value appear after it.
- Getting the number of columns of a 2D NumPy arrayTo get the number of columns of a 2D Numpy array, use its shape property.
- Getting the number of non-zero elements in a NumPy arrayTo get the number of non-zero elements in a NumPy array use the count_nonzero(~) method.
- Getting the number of rows of a 2D NumPy arrayTo get the number of rows of a 2D Numpy array, use its shape property.
- Initializing a NumPy array of onesTo initialize a Numpy array of zeros, use Numpy's ones(~) method.
- Initializing a NumPy array of zerosTo initialise a Numpy array of zeros, use Numpy's zeros(~) method.
- Initializing an identity matrix in NumPyTo initialize an identity matrix in Numpy, use the np.eye(~) method.
- Limiting array values to a certain range in NumPyTo limit array values to a certain range in NumPy use the clip(~) method. Using this method, values within the array that fall outside the specified range will be replaced with the provided minimum and maximum values.
- Performing linear regression in NumPyLinear regression, in essence, is about computing the line of best fit given some data points. We can use NumPy's polyfit(~) method to find this line of best fit easily.
- Printing full or truncated NumPy arrayWhether the full or truncated NumPy array is printed can be controlled using the threshold parameter of set_printoptions(). By default, threshold=1000.
- Printing large NumPy arrays without truncationWe can use Numpy's set_printoptions(~) method to print large numpy arrays without truncation.
- Removing rows containing NaN in a NumPy arrayTo remove rows containing NaN in a NumPy array, we can use a combination of the isnan(~) and any(~) methods.
- Reversing a NumPy arrayTo reverse a NumPy array use the flipud(~) method for one-dimensional NumPy arrays and fliplr(~) for multi-dimensional NumPy arrays.
- Saving NumPy array to a fileTo save a NumPy array to a file, we can use the savetext(~) method.
- Shape of NumPy ArraysThe shape property of a NumPy array is a tuple that tells us the size of the each nested array. This property is most commonly used to access the number of rows and columns of a 2D NumPy array.
- Sorting value of one array according to another in PythonTo sort the value of one array according to another in Python use the np.argsort(~) method.
- Suppressing scientific notation when printing NumPy arraysTo suppress scientific notation when printing NumPy arrays, use np.set_printoptions(suppress=True).
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!