[Tutorial] K-Means Clustering with SKLearn in One Line

If there is one clustering algorithm you need to know – whether you are a computer scientist, data scientist, or machine learning expert – it’s the K-Means algorithm. In this tutorial drawn from my book Python One-Liners, you’ll learn the general idea and when and how to use it in a single line of Python … Read more

Python Greater Than

The Python greater than (left>right) operator returns True when its left operand exceeds its right operand. When the left operand is smaller than or equal to the right operand, the > operator returns False. For example, 3>2 evaluates to True, but 2>3 and 3>3 both evaluate to False. Examples Let’s explore a couple of examples … Read more

[NumPy] How to Calculate The Average Along an Axis?

This article explains how to calculate basic statistics such as average, standard deviation, and variance along an axis. We use the NumPy library for linear algebra computations. These three ways are very similar — if you understand one of them, you’ll understand all of them. TLDR; To average a NumPy array x along an axis, … Read more

Python Operators Overview

What Are Python Operators? Python operators are special syntactical sugar to run basic operations without calling their respective methods. For example, you can use the + operator in a + b instead of the more clunky .add() method in a.add(b). Each operator has a unique symbol that is placed between the two arguments called operands. … Read more

How to Divide Two Integers in Python 2 and 3?

In this article, you’ll learn about the division operators // and / in Python 2 and 3. You can check out the version in your Python script as shown here. A short visual overview of the division operator in Python 2 and 3: Assuming two integer values stored in variables a and b, there are … Read more

How to Append Data to a JSON File in Python? [+Video]

Problem Formulation Given a JSON object stored in a file named “your_file.json” such as a list of dictionaries. πŸ’¬ How to append data such as a new dictionary to it? # File “your_file.json” (BEFORE) [{“alice”: 24, “bob”: 27}] # New entry: {“carl”: 33} # File “your_file.json” (AFTER) [{“alice”: 24, “bob”: 27}, {“carl”: 33}] Method 1: … Read more

How to Get the Current Value of a Variable in TensorFlow?

Problem Formulation Given a TensorFlow variable created with tf.Variable(). As this variable may have changed during the training process (e.g., using assign()), you want to get the current value of it. How to accomplish this in TensorFlow? Sessions Are Gone in TensorFlow 2 In TensorFlow 1, computations were performed within Sessions. That’s why many people … Read more

Method Overriding vs Overloading in Python [+Video]

Method overriding vs overloading in Python—what’s the difference? If you’re short on time—here it is: Method overloading: creating a method that can be called with different arguments such as m() and m(1, 2, 3). Method overriding: overwriting the functionality of a method defined in a parent class. In method overloading, methods in a given class … Read more

5 Brag-Worthy Python Projects

Hacking Alexa’s Voice Recordings After David’s mother passed away, he wanted to hear her voice again. As she used Amazon Alexa, he figured, he could download the voice recordings from the Alexa device. But Amazon didn’t provide an interface for it. Fortunately he found this project online: [Cool Project] Hacking Alexa’s Voice Recordings After following … Read more