## NumPy Reshape 1D to 2D

Problem Formulation: Given a one-dimensional NumPy array. How to create a new two-dimensional array by reshaping the original array so that the new array has x rows and y columns? Here’s an example of what you’re trying to do: # Given: [0 1 2 3 4 5] x = 2 # rows y = 3 … Read more

## What’s the Standard Way to Check for Type in Python?

Problem: What is the best way to check whether a given object is of a given type? For example, if you have a variable object, how to check if it’s a string? Check with Subclasses Solution: To check if an object is an instance of str or any of its subclasses, use isinstance: Note that … Read more

## How to Multiply 2D Matrices in Numpy?

Given two 2D arrays a and b. You can perform standard matrix multiplication with the operation np.matmul(a, b) if the array a has shape (x, y) and array be has shape (y, z) for some integers x, y, and z. Problem Formulation: Given a two-dimensional NumPy array (=matrix) a with shape (x, y) and a … Read more

## How to Create a Sequence of Evenly Spaced Values [Vanilla Python & Numpy Linspace]

Problem: How to create a sequence of evenly-spaced values Using pure, vanilla Python, and Using NumPy’s linspace() method. Example: Given three arguments: start=10, stop=20, number_of_values=11. How do you create a sequence of 11 values x0, x1, …, x10 where two subsequent values xi and x(i-1) have the same distance for all i in {0, …, … Read more

## Python tuple() — A Simple Guide with Video

Python’s built-in tuple() function creates and returns a new tuple object. When used without an argument, it returns an empty tuple. When used with the optional iterable argument, it initializes the new tuple with the elements in the iterable. Read more about tuples in our full tutorial about Python Tuples. Usage Learn by example! Here … Read more

## How to Create a Sequence of Linearly Increasing Values with Numpy Arange?

Problem: How to create a sequence of linearly increasing values? Solution: Use NumPy’s arange() function. The np.arange([start,] stop[, step]) function creates a new NumPy array with evenly-spaced integers between start (inclusive) and stop (exclusive). The step size defines the difference between subsequent values. For example, np.arange(1, 6, 2) creates the NumPy array [1, 3, 5]. … Read more

## Python list() — A Simple Guide with Video

Python’s built-in list() function creates and returns a new list object. When used without an argument, it returns an empty list. When used with the optional iterable argument, it initializes the new list with the elements in the iterable. Read more about lists in our full tutorial about Python Lists. Usage Learn by example! Here … Read more

## How to Initialize a NumPy Array with Zeros and Ones

Numpy is a popular Python library for data science focusing on linear algebra. In this article, you’ll learn how to initialize your NumPy array. How to Initialize a NumPy Array with Zeros? To initialize your NumPy array with zeros, use the function np.zeros(shape) where shape is a tuple that defines the shape of your desired … Read more

## How to Increment a Filename in Python?

Challenge: Given a Python program that writes data into a file. If you run the program again, it’ll overwrite the file written by the first execution of the program. Each time you run this program, the original content in file.dat will be overwritten. How to avoid this overwriting by adding an integer suffix to the … Read more

## How to Get the Shape of a Numpy Array?

NumPy is a popular Python library for data science. The focus of the library is computations on arrays, vectors, and matrices. If you work with data, there is no way that you can avoid NumPy. In this tutorial, you’ll learn more about the shape of a NumPy array. In particular, you’re going to learn the … Read more

## How to Get map() to Return a List in Python 3.x

Recap: Python’s map() function takes a function and an iterable—such as a list—as arguments. It then applies the function to each element in the iterable. It returns a map object. ? But what if you don’t need a map object but a list? Old: In Python 2.x this was easy: the map() function simply returned … Read more

## The Dimension of a Numpy Array

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. If you work with data, you simply cannot avoid NumPy. Challenge: How to get the number of dimensions of a NumPy array? Solution: Use the attribute array.ndim to access the number of dimensions of a NumPy array. Note that this … Read more