PyTorch view(): Reshape Tensors Without Copying

PyTorch view: Command Prompt output of tensor.view reshaping 12 values into a 4 by 3 tensor

PyTorch view() returns a tensor with a new shape that shares the original’s data, so it copies nothing. The new shape must hold the same number of elements. Pass -1 for one dimension and PyTorch works it out: I ran these examples with PyTorch 2.14.0+cpu on Python 3.12.5; a CPU build is enough for everything … Read more >>

How to Convert a String to a List in Python

Python string to list: Command Prompt output of split() and list() turning strings into lists

To convert a Python string to a list, use split(): “TX,CA,NY”.split(“,”) gives [‘TX’, ‘CA’, ‘NY’], and with no argument it splits on whitespace. To get one item per character, use list(text): These snippets use US cities, state codes and prices, run on Python 3.12.5. You’ll learn split() and list(), comma-separated values with spaces and numbers, … Read more >>

Environment Variables in Python: Read, Set and Check Them

Environment variables in Python: Command Prompt setting STORE_REGION and MAX_RETRIES and Python reading them with os.environ and os.getenv

Environment variables in Python live in os.environ, a dictionary-like object. Read a required one with os.environ[“NAME”], an optional one with os.getenv(“NAME”, default), and check for one with “NAME” in os.environ: Every screenshot here is from the Windows Command Prompt running Python 3.12.5 with python-dotenv 1.2.4, and every variable is one I created for the demo. … Read more >>

How to Create an Empty List in Python

Create an empty list in Python: Command Prompt output of [] and list() creating empty lists

You can create an empty list in Python with a pair of square brackets, items = [], or with items = list(). Both give you a new list with no elements. I use [], because it’s shorter and faster: Python 3.12.5 produced the outputs below, and mypy checked the type-hint example. You’ll see how the … Read more >>

Check the Type of a Variable in Python: type() vs isinstance()

Check the type of a variable in Python: Command Prompt output of type() on an int, float, string, bool, None, list and dict

Checking a variable’s type in Python takes one of two built-ins. type(x) tells you exactly what x is, and isinstance(x, SomeType) tells you whether it’s that type or a subclass of it: I tested everything below on Python 3.12.5 with NumPy 2.5.3; the screenshots are the real Command Prompt output. Use isinstance() for checks in … Read more >>

How to Create PyTorch Empty Tensor?

pytorch empty tensor

Have you ever been in the middle of coding a neural network and needed to create a placeholder tensor to store results? I know I have, countless times. After working with PyTorch for over a decade, creating empty tensors has become second nature to me. When I first started, this seemingly simple task caused me … Read more >>

Python SciPy Stats Mode with Examples

stats mode 2

Recently, I was working on a data analysis project where I needed to find the most frequently occurring values in my datasets. The mode is an important statistical measure that represents the most common value in a dataset, and SciPy’s stats module makes calculating it simple in Python. In this article, I’ll cover multiple ways … Read more >>

Python SciPy Pairwise Distance – Compute Distances Between Point Sets

pairwise distance matrix python

Recently, I was working on a machine learning project where I needed to calculate distances between multiple data points. The challenge was finding an efficient way to compute these distances without writing complex loops. That’s when I discovered SciPy’s pairwise distance functionality. In this article, I’ll share how to use SciPy’s spatial distance functions to … Read more >>

Print a Binary Search Tree in Python

print binary tree python

Recently, I was working on a data analytics project for a tech startup in San Francisco, where I needed to visualize hierarchical employee data. The challenge was displaying a binary search tree structure in a readable format. I’ve discovered there’s no single “best” way to print a BST. Different scenarios require different approaches. In this … Read more >>