Skip to main content

Posts

Showing posts with the label Python

Featured Post

The Making of a Dumb Datum

Imagine 10,000 years in the future. Two archaeologists are digging a site, and they find the remains of human bones. They take those remains to the lab, perform tests on them, and try to reconstruct the habits and lifestyle of that human. Say those bones belong to me. They are trying to find out my story, how I lived, what I ate, where I traveled, etc. Once they find all the info they need, they toss it into a museum where they store all other such ancient human remains they find. I was essentially one data point to them in trying to understand how humanity evolved over time. Or I can also call myself a “datum”, the singular form of data, literally one piece of information to solve a bigger puzzle. Honestly, I don’t need 10,000 years to become a datum. I am already a part of several datasets people use these days to study many things. So for that objective learner, I am already a datum. Now let me get to how I got the dumb part of it. I have had an interesting and eventful life so far...

A random problem of decision tree

There's something you need to know about if you are building decision tree models using Python's famous scikit-learn package. The algorithm it uses for building the models is deterministic (it produces consistent results across multiple executions if the inputs don't change). Despite this nature, scikit-learn provides a 'Random state' hyperparameter to the decision tree's class. This hyperparameter is only needed when an algorithm is not deterministic, as fixing the random state to a constant integer value arrests the randomness. So, the random state must be a redundant parameter when building decision trees, right? Not really. The decision tree algorithm could use the value of the random state passed to it for making a 'decision' in the below three cases: i) If you set the max_features hyperparameter to an integer value lesser than the total number of features. It means the algorithm needs to decide which random subset of features to use at each node to...

My love for pd.read_csv() knows no bounds, but I had to say it!

  Who doesn’t love the read_csv() function of Pandas? It checks the boxes of being both simple and elegant! And it is the first thing I learned when I started my journey in the data world (which, to be honest, is still at the beginning phase and didn’t move ahead. But that’s a story for some other time  ) But how does this function fare against the datasets that contain millions or even billions of rows? Pandas API is primarily designed for in-memory analytics, and hence, it tries to load the entire data into the memory. So reading large datasets through this function is slower and can sometimes even lead to out-of-memory errors! One way around this problem is to use the ‘chunksize’ parameter along with the read_csv() function. It enables chunking of the dataset into several small portions, and we could load each chunk separately for our usage. One of the many other alternatives is to use Dask API’s read_csv function. Dask is almost similar to Pandas but uses the parallel comp...

The quirk of NumPy!

 

Unravelling the secret behind numpy.std!

  I know you could recite the formula for sample standard deviation even in your sleep. That (n-1) in the denominator inside the square root must be no stranger to you. There’s a good chance you already know that a sample has n-1 degrees of freedom, which is why we use it in the denominator instead of a straightforward “n.” It is a no-brainer that using the .std() function to calculate the standard deviation for a NumPy array is a piece of cake for you. But if you haven’t heard about the “ddof” parameter in this function yet, let me caution you against using the .std() directly to calculate sample standard deviations. This parameter accounts for the degree of freedom while calculating standard deviation, and its default value is zero. If you aren’t setting it to “1”, you are calculating population standard deviation instead of the sample statistic. Hence, always cross-check that you are setting the appropriate value to the ddof parameter while using the .std function.