Posts

Double-hashing and The Birthday Problem

Image
The birthday problem (also called the birthday paradox) deals with the probability, that in a set of n randomly selected people, at least two people share the same birthday. Though it is not technically a paradox, it is often referred to as such because the probability is counter-intuitively high. The birthday problem is an answer to the following question: In a set of n randomly selected people, what is the probability, that at least two people share the same birthday? What is the smallest value of n where the probability is at least 50% or 99%? Let p(n) be the probability that at least two of a group of n randomly selected people share the same birthday. By the pigeonhole principle , since there are 366 possibilities for birthdays (including February 29), it follows that when n≥367, p(n)=100%. The counterintuitive part of the answer is that for smaller n, the relationship between n and p(n) is (very) non-linear. In fact, the thresholds to surpass 50% and 99% are quite sm...

K-nearest Neighbors (KNN) in Python

Image
Introduction Neighbors-based classification is a type of instance-based learning or non-generalizing learning: it does not attempt to construct a general internal model, but simply stores instances of the training data. Classification is computed from a simple majority vote of the nearest neighbors of each point: a query point is assigned the data class which has the most representatives within the nearest neighbors of the point. Implementation scikit-learn implements two different nearest neighbors classifiers: KNeighborsClassifier implements learning based on the k nearest neighbors of each query point, where k is an integer value specified by the user. RadiusNeighborsClassifier implements learning based on the number of neighbors within a fixed radius r of each training point, where r is a floating-point value specified by the user. import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn import neighbors, datase...

Keccak hashing algorithm (SHA-3)

Image
What is a Hash? The hash is one of the main building blocks underlying blockchain technology. That’s why you need to understand hashing and its related concepts—things like hash rate and function and later discuss what is new about the Keccak algorithm. Hash is the process of taking any input data and running it through an algorithm that then produces output data of a specific and consistent size. The output data is a hash. The algorithm used in the process is called a hash function. And finally, the rate at which you can push data through the hash function to generate a new hash is your hash rate. All of this is crucial to blockchain technology. In large part, it’s what gives blockchain many of its unique properties. How blockchain uses hashes Essentially, a blockchain is built by constantly adding new data to an ever-expanding list. More specifically, when you’re processing a new batch of transactions, you begin by building on top of the data from the previous batch of transa...

Classifier Boosting with Python

Image
Introduction Remember we've talked about random forest and how it was used to improve the performance of a single Decision Tree classifier . The idea of fitting a number of decision tree classifiers on various sub-samples of the dataset and using averaging to improve the predictive accuracy can be used to other algorithms as well and it's called boosting. There are several boosting techniques, which can be used to improve our algorithm, we'll cover the most used ones: AdaBoost and Bagging boost. AdaBoost An AdaBoost classifier begins by fitting a classifier on the original dataset and then fits additional copies of the classifier on the same dataset, but where the weights of incorrectly classified instances are adjusted such that subsequent classifiers focus more on difficult cases. Bagging boost A Bagging classifier fits base classifiers each on random subsets of the original dataset and then aggregate their individual predictions to form a final prediction. I...

K-means clustering with Python

Image
Introduction K-means is one of the simplest unsupervised learning algorithms that solve the well known clustering problem. The procedure follows a simple and easy way to classify a given data set through a certain K number of clusters. The main idea is to define K centroids, one for each cluster. The next step is to take each point belonging to a given data set and associate it to the nearest centroid. At this point we need to re-calculate K new centroids of the clusters resulting from the previous step. After we have these K new centroids, a new binding has to be done between the same data set points and the nearest new centroid. As a result of this loop we may notice that the K centroids change their location step by step until no more changes are done. Implementation Scikit-learn provides with full implementation of K-means algorithm though KMeans class. Let's have a look at several interesting situations, which might occur during data clustering: import numpy ...

What Happens After We’ve Mined all 21M Bitcoin?

Image
There can never be more than 21 million bitcoin. From an investment standpoint, this is a good thing: Bitcoin can have long-term value because it’s finite. Additionally, we won’t have mined all the bitcoin until 2140. But the ever-decreasing availability of new bitcoins is already affecting the market and will have serious consequences way before 2140. In fact, we’ll have mined close to 100% of bitcoin by 2040. Though bitcoin mining will still be theoretically possible for another 100 years, bitcoin’s price and the nature of its transactions will never be the same. Here’s what you should know about the impending cap on one of the world’s most valuable currencies. We’ll Have Mined all 21M Bitcoin by 2140 2140 is the theoretical year we will create the last bitcoin block. Thereafter, it will be impossible to make even a fraction of a new bitcoin—no matter the demand. Per the law of supply and demand, bitcoin’s value could increase significantly once its supply becomes fixed. What pe...

Naïve Bayes with Python

Image
Introduction The Naive Bayes algorithm is based on conditional probabilities. It uses Bayes' Theorem , a formula that calculates a probability by counting the frequency of values and combinations of values in the historical data. Bayes' Theorem finds the probability of an event occurring given the probability of another event that has already occurred. If B represents the dependent event and A represents the prior event, Bayes' theorem can be stated as follows. To calculate the probability of B given A , the algorithm counts the number of cases where A and B occur together and divides it by the number of cases where A occurs alone. Implementation Scikit-learn provides implementation of Naïve Bayes algorithm of 3 flavors: MultinomialNB implementing the naive Bayes algorithm for multinomially distributed data ; GaussianNB implementing the Gaussian Naive Bayes algorithm for classification; and BernoulliNB implements the naive Bayes training and classificat...