Data Science

Python One Line Dictionary

Python’s dictionary data structure is one of the most powerful, most underutilized data structures in Python. Why? Because checking membership is more efficient for dictionaries than for lists, while accessing elements is easier for dictionaries than for sets. In this tutorial, you’ll learn how to perform four common dictionary operations in one line of Python …

Python One Line Dictionary Read More »

[Dash + Flask] How to Deploy a Python Dash App on Pythonanywhere.com

Here’s the step-by-step approach of how to deploy your Dash app on Pythonanywhere.com using Flask and pip: Create an account on Pythonanywhere.com. Create a Flask application. Create a Dash application. Copy the Dash app into the Flask app. Connect the Flask server with the Dash app. Modify the WSGI configuration file. Install Dash with pip …

[Dash + Flask] How to Deploy a Python Dash App on Pythonanywhere.com Read More »

Calculating Entropy with SciPy

Problem: How to calculate the entropy with the SciPy library? Solution: Import the entropy() function from the scipy.stats module and pass the probability and the base of the logarithm into it. Try It Yourself: Run this code in the interactive code shell! Exercise: Change the probabilities. How does the entropy change? Let’s start slowly! You’ll …

Calculating Entropy with SciPy Read More »

How to Fix “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”

If you run the following code, you’ll experience a special ValueError: The output will be this error message: Try It Yourself in our interactive browser shell: Solution: Using the Numpy functions called logical_and() and logical_or() instead of Python’s logical operators (“and” and “or”) is the solution. Why does the ValueError occur at all? Many programmers …

How to Fix “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()” Read More »

How To Extract Numbers From A String In Python?

Summary: To extract numbers from a given string in Python you can use one of the following methods: Use the regex module. Use split() and append() functions on a list. Use a List Comprehension with isdigit() and split() functions. Use the num_from_string module. Extracting digits or numbers from a given string might come up in …

How To Extract Numbers From A String In Python? Read More »

How To Import A Module Given The Full Path

Summary: To import a module given the full path, the following methods can be used : Using the importlib module Using SourceFileLoader class Using sys.path.append() function Adding __init__ file to the folder containing the module and then importing it Problem: How to import a module if its full path has been given? Example: Consider we …

How To Import A Module Given The Full Path Read More »

How to Access an Object Attribute Given the Attribute Name as a String?

You may know the following problem: You have an object’s attribute name as a string—say, you’ve read it from a file—and you want to access the attribute with the given name. But you cannot use the syntax object.”attribute” because the dot notation only allows for name-access like this: object.attribute. How do you resolve this problem? …

How to Access an Object Attribute Given the Attribute Name as a String? Read More »

Matplotlib Histogram — A Simple Illustrated Guide

The histogram is one of the most important plots for you to know. You’ll use it every time you explore a dataset. It is the go-to plot for plotting one variable.  In this article, you’ll learn the basics and some intermediate ideas. You’ll plot histograms like a pro in no time using Python and matplotlib. …

Matplotlib Histogram — A Simple Illustrated Guide Read More »

Python String Formatting: How to Become a String Wizard with the Format Specification Mini-Language

Python provides fantastic string formatting options, but what if you need greater control over how values are presented? That’s where format specifiers come in.  This article starts with a brief overview of the different string formatting approaches. We’ll then dive straight into some examples to whet your appetite for using Python’s Format Specification Mini-Language in …

Python String Formatting: How to Become a String Wizard with the Format Specification Mini-Language Read More »

How to merge two Python dictionaries in a single expression in Python?

Summary: To merge two dictionaries dict1 and dict2 in a single expression, use the dictionary unpacking feature z = {**dict1, **dict2}. This creates a new dictionary and unpacks all (key-value) pairs into the new dictionary. Duplicate keys are automatically resolved by this method. Mastering dictionaries is one of the things that differentiates the expert coders …

How to merge two Python dictionaries in a single expression in Python? Read More »