Data Science, Optimization, Programming

How can GA help cut down problem space and converge towards a better solution?

Double Helix — Photo by on

If you have heard of systematic trading or algorithmic trading, then you must know that optimization of strategy is among one of the most important factors that dictate whether the strategy would even break even. And the worst part is: optimization is very computationally heavy. Imagine a simple MACD crossover strategy, there will be at least 3 parameters: fast, slow and signal moving average period, and hundreds of possible values for each, making it more than a million possible combinations.

Incomes genetic algorithm (GA): a probabilistic & heuristic searching algorithm inspired by Darwin’s theory on natural selection that the fittest…


Data Science, Programming, Pandas, Efficiency

Time to stop being too dependent on .iterrows() and .apply()

Photo by on

Python, arguably the coolest programming language these days (thanks for Machine Learning and Data Science), is not very well known for its efficiency when compared with one of the best programming language — C. An example of this would be conditional logics. When developing machine learning models, it is quite common that we would need to programmatically update labels based on hard-coded rules derived from statistical analysis or findings of the previous iteration. No shame in admitting it: I have been coding it with Pandas apply until one day I grew so fed up with the nested block that I…


Photo by on

Programming

You don’t always need indices

Welcome to a series of short posts each with handy Python tricks that can help you become a better Python programmer. In this blog, we will look into how to unpack iterables.

You don't need indices.

Say you have a tuple ("a", "b", "c").

If you want to assign the first element to a, the second element to b, and the third element to c, you actually don't need to use indices:

# Don't do this
t = ("a", "b", "c")
a = t[0]
b = t[1]
c = t[2]
# Do this instead
t = ("a", "b", "c")
a, b, c = t


Photo by on

How do generators work in Python?

Welcome to a series of short posts each with handy Python tricks that can help you become a better Python programmer. In this blog, we will look into generators.

Introduction

A generator is a subclass of a iterable.

To understand how generators work, we will need to first revise how iterables function.

Iterables

An iterable is an object that can be iterated.

When we write for loops, it starts with a for x in y: statement. The for statement is iterating the expression y. Hence, y is an iterable.

Some very common iterables are: lists, tuples, strings etc

for i in [0…


Photo by on

Are you still iterating through for loops?

Welcome to a series of short posts each with handy Python tricks that can help you become a better Python programmer. In this blog, we will look into how to flatten lists.

Situation

We have all dealt with lists of lists or even worse: lists of nested lists.

Theoretically, we can peel the list layer by layer like an onion:

l = [0, 1, 2, [3, 4, 5, [6, 7, 8]]]from collections import Iterabledef unwrap(l):
flat_list = []
for item in l:
if isinstance(item, Iterable):
flat_list.extend(item)
else:
flat_list.append(item)
return flat_list
l = unwrap(l)
# [0, 1, 2, 3, 4, 5…


Photo by on

One keyword argument that saves you some frustrations

Welcome to a series of short posts each with handy Python tricks that can help you become a better Python programmer. In this blog, we will look into checking joins in Pandas.

Situation

When we work with a collection of datasets in Python, knowing how to join the tables together is crucial. We all know about INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN in SQL and how to do them in Pandas, but do you know that there is a lesser-known keyword argument called indicator?

For illustrative purposes, we will be using the following tables where ID will…


Photo by on

How to compare multiple variables with a single value at once?

Welcome to a series of short posts each with handy Python tricks that can help you become a better Python programmer. In this blog, we will look into variable comparisons.

Situation

You have variables x, y, z, and you have also got a constant c that you would like to check whether it exists in any of the three variables. We can do it with a brute-force approach with the following logic, but are there better solutions?

if (x == c) or (y == c) or (z == c):
print("It exists!")
else:
print("It does not exist!")

Could-be Solution: Iterator

if any(i == c for…


Photo by on

How does boolean evaluation help shorten if statements?

Welcome to a series of short posts each with handy Python tricks that can help you become a better Python programmer. In this blog, we will look into boolean evaluation.

Situation

Let’s say we have a variable x and we would like to do something if a certain condition is met. A basic approach would be as follows:

if x == condition:
print("Conditions met!")
else:
print("Conditions not met!")

With the operator ==, we are doing a boolean evaluation of whether x equals condition. …


Photo by on

What are the impacts? And how can we avoid them?

A common machine learning modelling process goes by (1) weight initialization, (2) forward propagation, (3) loss (or cost) computation, (4) backpropagation, and (5) weight update using optimization algorithms. While weight initialization is usually as simple as one line, it is often easily overlooked how delicate weight initialization is and how impactful it is to the final model performance.

In this blog, we will look into some of the common problems in neural network initialization, their corresponding impacts, and more importantly how can we detect and avoid them.

# one-liner weight initialisation for tensorflowtf.keras.initializers.RandomUniform(minval=-0.05, maxval=0.05, seed=None)

Zero Initialisation — Symmetry Problem

  • Why does it happen…


Image by from

THOUGHTS AND THEORY

What is Attention Mechanism & Why is RFA better than Softmax?

Google has recently released a new approach — Random Feature Attention — to replace softmax attention mechanisms in transformers for achieving similar or better performance with significant improvement in time and space complexity.

In this blog, we will look into the background of transformers, what is an attention mechanism, and why is RFA a better alternative to softmax attention mechanism. We will finish the blog with a couple of takeaways from RFA.

Note: This blog is based on Google Deepmind’s latest paper ‘Random Feature Attention’ by Peng et al. Although we will cover the majority of the core concepts, please…

Louis Chan

Learn from your own mistakes today makes you a better person tomorrow.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store