Image for post
Image for post
Photo by Johannes Plenio on Unsplash

Link to Kaggle Notebook for all these exercises together

Q: Product of two matrices

Given two matrices print the product of those two matrices

Solution with Explanations and Theory

Basics of Matrix Multiplication

Multiplication rule to remember — Here also the flow is Row and then Column

  1. Sum-Product of First Row * 1st Column => Becomes the 1-st_Row-1-st_Column of the resultant Matrix
  2. Sum-Product of 1st Row * 2-nd Column => Becomes the 1-st_Row-2-nd_Column of the resultant Matrix

There are four simple rules that will help us in multiplying matrices, listed here:

1. Firstly, we can only multiply two matrices when the number of columns in matrix A is equal to the number of rows in matrix B.

2. Secondly, the first row of matrix A multiplied by the first column of matrix B gives us the first element in the matrix AB, and so on.

3. Thirdly, when multiplying, order matters — specifically, AB ≠ BA.

4. Lastly, the element at row i, column j is the product of the ith row of matrix A and the jth column of matrix B.

Further read through this for a very nice visual flow of Matrix Multiplication.

def matrix_mul(A, B):
num_rows_A = len(A)
num_columns_A = len(A[0])
num_rows_B = len(B)
num_columns_B = len(B[0])

# To multiply an m×n matrix by an n×p matrix, the ns must be the same,
# and the result is an m×p matrix.
if num_columns_A != num_rows_B:
"Matrix multiplication of two arguments not possible as number of columns in first Matrix is NOT equal to the number of rows in second Matrix")

# Create an result matrix which will have
# dimensions of num_rows_A x num_columns_B
# And fill this matrix with zeros
result_matrix = [[0 for i in range(num_columns_B)] for j in range(num_rows_A)]

# Now implementing the key principle
# The element at row i, column j is the product of the ith row of matrix A and the jth column of matrix B.

for i in range(num_rows_A):
for j in range(num_columns_B):
for k in range(num_columns_A):
# k-th column of A should be the k-th row of B
result_matrix[i][j] += A[i][k] * B[k][j]
return result_matrix

A = [[1, 3, 4],
[2, 5, 7],
[5, 9, 6]]

B = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]

A1 = [[1, 2],
[3, 4]]

B1 = [[1, 2, 3, 4, 5],
[5, 6, 7, 8, 9]]

A2 = [[1, 3, 4], [5, 9, 6]]

B2 = [[1, 0, 0], [0, 0, 1]]

print(matrix_mul(A, B))
print(matrix_mul(A1, B1))

Image for post
Image for post
Ecospace, Marathahalli — Sarjapur Outer Ring Road, Bellandur, Bangalore, India Photo by Nishanth Avva on Unsplash

Link to Kaggle Notebook with UCI-Breast Cancer Data

What is Categorical Data

Categorical variables are those values in a dataset that are selected from a group of categories or labels. Typically, any data attribute which is categorical in nature represents discrete values that belong to a specific finite set of categories or classes. These are also often known as classes or labels in the context of attributes or variables which are to be predicted by a model (popularly known as response variables). These discrete values can be text or numeric in nature (or even unstructured data like images!).

There are two major classes of categorical data, nominal and ordinal.

In any nominal categorical data attribute, there is no concept of ordering amongst the values of that attribute. Consider a simple example of weather categories like — sunny, cloudy, rainy, etc. These are without any concept or notion of order (windy doesn’t always occur before sunny nor is it smaller or bigger than sunny). …

Achieved a score of 1.4714 with this Kernel in Kaggle

Image for post
Image for post
Photo by Prashant Gupta on Unsplash

Link To my Kaggle Notebook

(If you like the Kaggle Notebook, please consider upvoting it in Kaggle)

Getting the data and Kaggle Challenge Link

Gradient Boosted trees have become one of the most powerful algorithms for training on tabular data. Over the recent past, we’ve been fortunate to have may implementations of boosted trees — each with their own unique characteristics. In this notebook, I will implement LightGBM, XGBoost and CatBoost to tackle this Kaggle problem.

What is Boosting

To understand the absolute basics of the need for Boosting algorithm, let's ask a basic question — If a data point is incorrectly predicted by our first model, and then the next (probably all models), will combining the predictions provide better results? …

Image for post
Image for post

The full Kaggle Jupyter Notebook is here

And the Github link for the same Jupyter Notebook

Getting the Data

I have uploaded to this Kaggle Dataset which is a zipped file with 26,320 .csv files containing the top cryptocurrencies on by market cap worldwide. After 20:45:05 on August 4, data was collected every five minutes for three months.

Also, I have uploaded the 9,432 .csv files based on which this below EDA analysis done into my Github repository.

This dataset is from CoinMarketCap Data From August 4 to November 4, 2017

Filenames represent the date and time at which the data was…

This notebook achieved a score of 0.12 and within the top 25% in this Kaggle House Price competition

Image for post
Image for post

My Kaggle Notebook Link is here

As I intended this Notebook to be published as a blog on Linear Regression, Gradient Descent function and some EDA, so in the first 50% to 60% of this notebook I have mainly discussed the theory around those topics from mainly a beginner perspective.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.ensemble import RandomForestRegressor
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import mean_squared_log_error
from sklearn.metrics import mean_squared_error
from xgboost import XGBRegressor
import math

The evaluation criteria for this Kaggle Competition is RMSLE — “Submissions are evaluated on Root-Mean-Squared-Error (RMSE) between the logarithm of the predicted value and the logarithm of the observed sales price. …

Image for post
Image for post
Photo by Shahadat Rahman on Unsplash

As a career Data-Scientist, all through your life you have to deal with Matrix form of data where data in Numpy or Pandas or TensorFlow where Axis and Dimensions are the fundamental structural concept.

Basic Attributes of the ndarray Class

Image for post
Image for post
Basic Attributes of the ndarray Class

What is the Shape of an Array

Let's consider the below array

Image for post
Image for post

The “shape” of this array is a tuple with the number of elements per axis (dimension). In our example, the shape is equal to (6, 3), i.e. we have 6 lines and 3 columns.

Numpy has a function called “shape” which returns the shape of an array. The shape is a tuple of integers. …

Image for post
Image for post
Photo by Bogdan Cadar on Unsplash

The target of this blog post is to discuss the concept around and the Mathematics behind the below formulation of Bias-Variance Tradeoff.

Image for post
Image for post
Bias-Variance Tradeoff — Main Equation

And in super simple term

Total Prediction Error = Bias + Variance

The goal of any supervised machine learning model is to best estimate the mapping function (f) for the output/dependent variable (Y) given the input/independent variable (X). The mapping function is often called the target function because it is the function that a given supervised machine learning algorithm aims to approximate.

The Expected Prediction Error for any machine learning algorithm can be broken down into three parts:

Bias Error
Variance Error

Image for post
Image for post
Photo attribution — ’'

In this article, I shall go over the topic of arriving at the Vectorized Gradient-Descent formulae for the Cost function of the for Matrix form of training-data Equations. And along with that the Fundamentals of Calculus (especially Partial Derivative) and Matrix Derivatives necessary to understand the process.

So our target of this article is to understand the full Mathematics and the flow behind arriving at the below formulae, which is the Vectorized Gradient of the training-data Matrix

Image for post
Image for post

First a Refresher on basic Matrix Algebra

A matrix A over a field K or, simply, a matrix A (when K is implicit) is a rectangular array of scalars usually presented in the following…

Image for post
Image for post
Photo Source —

Multivariate Calculus is used all around Machine Learning and DataScience ecosystem, so having a first-principle understanding of it, is incredibly useful when you are dealing with some complex Math equations in implementing some ML Algo.

To start with, as soon as you need to implement multi-variate Linear Regression, you hit multivariate-calculus which is what you will have to use to derive the Gradient of a set of multi-variate Linear Equations i.e. Derivative of a Matrix. …

Image for post
Image for post
Image by Gerd Altmann from Pixabay

What Derivative of a function really is

The most fundamental defination of Derivative can be stated as — derivative measures the steepness of the graph of a function at some particular point on the graph. Thus, the derivative of a function is a slope at a particular point. Strictly speaking, curves don’t have slope, so we use the slope of the tangent line at a particular point on the curve. That also means that it is a ratio of change in the value of the function (dependent variable) to the change in the independent variable.

Image for post
Image for post

Take a note in the above image, when Δx approaches 0, the secant line become Tangent at x0 and derivative of the function representing the above graph gives the slope of the tangent line at x0. …


Rohan Paul

MachineLearning | DataScience | DeepLearning. Previously Frontend Engineer. Ex International Banking Analyst.

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