Thursday, May 29, 2025
3B1B notes
Saturday, May 17, 2025
Random Forest Classifier Code
Sunday, April 27, 2025
XGBoost Analysis Code
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import cohen_kappa_score
from scipy.stats import mode
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split
import xgboost as xgb
from xgboost import XGBClassifier
from xgboost import plot_importance
from matplotlib import pyplot
import shap
import warnings warnings.filterwarnings('ignore')
#Import dataset
data = 'C:/datasets/Wholesale customers data.csv'
df = pd.read_csv(data)
df.shape
df.head()
df.info()
df.describe()
df.isnull().sum()
#Checking for types of values
#declaring dependent and independent variables
X = df.drop('Channel', axis=1) y = df['Channel']
#Var checks
X.head()
y.head()
X_features=X
#Null imputation
# import XGBoost
#import xgboost as xgb
# define data_dmatrix
data_dmatrix = xgb.DMatrix(data=X,label=y)
# split X and y into training and testing sets
#from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)
# import XGBClassifier
#from xgboost import XGBClassifier
# declare parameters
params = {
'objective':'binary:logistic',
'max_depth': 4,
'alpha': 10,
'learning_rate': 1.0,
'n_estimators':100
}
# instantiate the classifier
xgb_clf = XGBClassifier(**params)
# fit the classifier to the training data
xgb_clf.fit(X_train, y_train)
#output:
XGBClassifier(alpha=10, base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=1.0,
max_delta_step=0, max_depth=4, min_child_weight=1, missing=None,
n_estimators=100, n_jobs=1, nthread=None,
objective='binary:logistic', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,
subsample=1, verbosity=1)
# alternatively view the parameters of the xgb trained model
print(xgb_clf)
XGBClassifier(alpha=10, base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=1.0,
max_delta_step=0, max_depth=4, min_child_weight=1, missing=None,
n_estimators=100, n_jobs=1, nthread=None,
objective='binary:logistic', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,
subsample=1, verbosity=1)
# make predictions on test data
y_pred = xgb_clf.predict(X_test)
# check accuracy score
from sklearn.metrics import accuracy_score
print('XGBoost model accuracy score: {0:0.4f}'. format(accuracy_score(y_test, y_pred)))
Sunday, April 20, 2025
Interesting Reads
Conjoint Analysis: https://www.qualtrics.com/en-au/experience-management/research/types-of-conjoint/
Great article on Deep Tech (plus awesome visuals): https://www.bcg.com/publications/2021/deep-tech-innovation
Basics on Mathematical Modelling: https://ocw.tudelft.nl/courses/mathematical-modeling-basics/
Books on Operations: https://orc.mit.edu/impact/textbooks/
Solving Cool Math Problems: https://projecteuler.net/archives
XGBoost Resources
Hello,
This page is designed to collate popular resources on xgb for my personal reference. None of it is my work.
Survival Modelling code snippet:#pip install lifelines
!pip install lifelines
#conda install -c conda-forge lifelinesimport pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lifelines import KaplanMeierFitter
df2 = pd.read_csv(r'FILELINK.csv')
T2 = df2['time']
S2 = df2['status']
print(T2)
kmf2 = KaplanMeierFitter()
kmf2.fit(T2, S2)
print("Survival function:")
print(kmf2.survival_function_)
print("Survival function plot:")
kmf2.plot()
plt.title("Survival Curve: 6-MP")
plt.xlabel("Time")
plt.ylabel("Survival Probability")
plt.savefig("6-MP.pdf")
plt.show()
https://gist.github.com/pb111/cc341409081dffa5e9eaf60d79562a03
I have used the Wholesale customers data set for this project, downloaded from the UCI Machine learning repository. This dataset can be found at the following url:
https://archive.ics.uci.edu/ml/datasets/Wholesale+customers
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline
import warnings warnings.filterwarnings('ignore')
#Import dataset data = 'C:/datasets/Wholesale customers data.csv' df = pd.read_csv(data)
#Exploring the dataset
df.shape
df.head()
df.info()
df.describe()#missing value check
df.isnull().sum()
#declaring dependent and independent variables
X = df.drop('Channel', axis=1) y = df['Channel']
#var checks
X.head()
y.head()
#Label encoding
# import XGBoost
import xgboost as xgb # define data_dmatrix
data_dmatrix = xgb.DMatrix(data=X,label=y)
# split X and y into training and testing sets from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)
General parameters
These parameters relate to which booster we are doing boosting. The common ones are tree or linear model.
Booster parameters
It depends on which booster we have chosen for boosting.
Learning task parameters
These parameters decide on the learning scenario. For example, regression tasks may use different parameters than ranking tasks.
Command line parameters
In addition there are command line parameters which relate to behaviour of CLI version of XGBoost.
The most important parameters that we should know about are as follows:-
learning_rate - It gives us the step size shrinkage which is used to prevent overfitting. Its range is [0,1].
max_depth - It determines how deeply each tree is allowed to grow during any boosting round.
subsample - It determines the percentage of samples used per tree. Low value of subsample can lead to underfitting.
colsample_bytree - It determines the percentage of features used per tree. High value of it can lead to overfitting.
n_estimators - It is the number of trees we want to build.
objective - It determines the loss function to be used in the process. For example, reg:linear for regression problems, reg:logistic for classification problems with only decision, binary:logistic for classification problems with probability.
XGBoost also supports regularization parameters to penalize models as they become more complex and reduce them to simple models. These regularization parameters are as follows:-
gamma - It controls whether a given node will split based on the expected reduction in loss after the split. A higher value leads to fewer splits. It is supported only for tree-based learners.
alpha - It gives us the L1 regularization on leaf weights. A large value of it leads to more regularization.
lambda - It gives us the L2 regularization on leaf weights and is smoother than L1 regularization.
Though we are using trees as our base learners, we can also use XGBoost’s relatively less popular linear base learners and one other tree learner known as dart. We have to set the booster parameter to either gbtree (default), gblinear or dart.
# import XGBClassifier from xgboost import XGBClassifier # declare parameters params = { 'objective':'binary:logistic', 'max_depth': 4, 'alpha': 10, 'learning_rate': 1.0, 'n_estimators':100 } # instantiate the classifier xgb_clf = XGBClassifier(**params) # fit the classifier to the training data xgb_clf.fit(X_train, y_train)
#output:
XGBClassifier(alpha=10, base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=1.0,
max_delta_step=0, max_depth=4, min_child_weight=1, missing=None,
n_estimators=100, n_jobs=1, nthread=None,
objective='binary:logistic', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, seed=None, silent=None,
subsample=1, verbosity=1)# alternatively view the parameters of the xgb trained model print(xgb_clf)XGBClassifier(alpha=10, base_score=0.5, booster='gbtree', colsample_bylevel=1, colsample_bynode=1, colsample_bytree=1, gamma=0, learning_rate=1.0, max_delta_step=0, max_depth=4, min_child_weight=1, missing=None, n_estimators=100, n_jobs=1, nthread=None, objective='binary:logistic', random_state=0, reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None, silent=None, subsample=1, verbosity=1)# make predictions on test data y_pred = xgb_clf.predict(X_test)# check accuracy score from sklearn.metrics import accuracy_scoreprint('XGBoost model accuracy score: {0:0.4f}'. format(accuracy_score(y_test, y_pred)))k-fold Cross Validation using XGBoost
To build more robust models with XGBoost, we must do k-fold cross validation. In this way, we ensure that the original training dataset is used for both training and validation. Also, each entry is used for validation just once. XGBoost supports k-fold cross validation using the
cv()method. In this method, we will specify several parameters which are as follows:-nfolds - This parameter specifies the number of cross-validation sets we want to build.
num_boost_round - It denotes the number of trees we build.
metrics - It is the performance evaluation metrics to be considered during CV.
as_pandas - It is used to return the results in a pandas DataFrame.
early_stopping_rounds - This parameter stops training of the model early if the hold-out metric does not improve for a given number of rounds.
seed - This parameter is used for reproducibility of results.
We can use these parameters to build a k-fold cross-validation model by calling
XGBoost's CV()method.#K-fold Cross Valfrom xgboost import cv params = {"objective":"binary:logistic",'colsample_bytree': 0.3,'learning_rate': 0.1, 'max_depth': 5, 'alpha': 10} xgb_cv = cv(dtrain=data_dmatrix, params=params, nfold=3,num_boost_round=50, early_stopping_rounds=10, metrics="auc", as_pandas=True, seed=123)xgb_cv.head()#xgb_cvcontains train and testaucmetrics for each boosting round. Let's previewxgb_cv.
14. Feature importance with XGBoost
XGBoost provides a way to examine the importance of each feature in the original dataset within the model. It involves counting the number of times each feature is split on across all boosting trees in the model. Then we visualize the result as a bar graph, with the features ordered according to how many times they appear.
XGBoost has a plot_importance() function that helps us to achieve this task. Then we can visualize the features that has been given the highest important score among all the features. Thus XGBoost provides us a way to do feature selection.
I will proceed as follows:-
xgb.plot_importance(xgb_clf) plt.rcParams['figure.figsize'] = [6, 4] plt.show()
Sidenote:
#Remember if categorical variable is there, input for xgb needs to be in dmatrix format
(ValueError: DataFrame.dtypes for data must be int, float, bool or categorical. When categorical type is supplied, DMatrix parameter enable_categorical must be set to True.Var1, Var2, Var3, Var4
Link: https://stackoverflow.com/questions/67080149/xgboost-error-when-categorical-type-is-supplied-dmatrix-parameter-enable-cat)
Code block For OHE:
import pandas as pd from xgboost
X_test, y_test = df.iloc[3:, :-1], df.iloc[3:, -1]
#Null imputation strategy for different variables
#Strategy for handling categorical variables:
Strategy 1:
cat_attribs = ['var1','var2','var3','var4'] X_train[cat_attribs] = X_train[cat_attribs].astype('category') X_test[cat_attribs] = X_test[cat_attribs].astype('category')
model = XGBRegressor(n_estimators=10, max_depth=20, enable_categorical=True, verbosity=2) model.fit(X_train, y_train) y_pred = model.predict(X_test)
Strategy 2:
# Create a mapping of labels to encoded values from X_train training_encoded_mapping = X_train['var1'].astype('category').cat.codes training_encoded_mapping = dict(zip(X_train['var1'].cat.categories, training_encoded_mapping)) X_train['var1'] = X_train['var1'].astype('category').cat.codes # Apply the mapping to X_test X_test['var1'] = X_test['var1'].map(training_encoded_mapping) # Do the same for other vars as well
And now don't pass enable_categorical=True in model initialization
Naive Bayes, DTs, Logistic Regression (it is a classification technique and not regression), NN, SVM
Saturday, April 19, 2025
Data Viz Resources
Hello,
Ideas on better plotting of distributions (banding and how the population lies for any variable):
https://rafalab.dfci.harvard.edu/dsbook/dataviz-distributions.html
https://seaborn.pydata.org/tutorial/distributions.html - Using seaborn
How to connect big query with python: https://codelabs.developers.google.com/codelabs/cloud-bigquery-python#0
The idea here is to assimilate resources from across the internet that will help me level up my visualization game using python. I am pretty much a noob at this and want to get better.
Most helpful visualization libraries in python (https://www.kaggle.com/discussions/getting-started/1087922)
1- matplotlib
matplotlib is the O.G. of Python data visualization libraries. Despite being over a decade old, it’s still the most widely used library for plotting in the Python community. It was designed to closely resemble MATLAB, a proprietary programming language developed in the 1980s.
2- Seaborn
Seaborn harnesses the power of matplotlib to create beautiful charts in a few lines of code. The key difference is Seaborn’s default styles and color palettes, which are designed to be more aesthetically pleasing and modern. Since Seaborn is built on top of matplotlib, you’ll need to know matplotlib to tweak Seaborn’s defaults.
3- ggplot
ggplot is based on ggplot2, an R plotting system, and concepts from The Grammar of Graphics. ggplot operates differently than matplotlib: it lets you layer components to create a complete plot. For instance, you can start with axes, then add points, then a line, a trendline, etc. Although The Grammar of Graphics has been praised as an “intuitive” method for plotting, seasoned matplotlib users might need time to adjust to this new mindset.
4- Bokeh
Like ggplot, Bokeh is based on The Grammar of Graphics, but unlike ggplot, it’s native to Python, not ported over from R. Its strength lies in the ability to create interactive, web-ready plots, which can be easily outputted as JSON objects, HTML documents, or interactive web applications. Bokeh also supports streaming and real-time data.
5- pygal
Like Bokeh and Plotly, pygal offers interactive plots that can be embedded in the web browser. Its prime differentiator is the ability to output charts as SVGs. As long as you’re working with smaller datasets, SVGs will do you just fine. But if you’re making charts with hundreds of thousands of data points, they’ll have trouble rendering and become sluggish.
6- Plotly
You might know Plotly as an online platform for data visualization, but did you also know you can access its capabilities from a Python notebook? Like Bokeh, Plotly’s forte is making interactive plots, but it offers some charts you won’t find in most libraries, like contour plots, dendograms, and 3D charts.
7- geoplotlib
geoplotlib is a toolbox for creating maps and plotting geographical data. You can use it to create a variety of map-types, like choropleths, heatmaps, and dot density maps. You must have Pyglet (an object-oriented programming interface) installed to use geoplotlib. Nonetheless, since most Python data visualization libraries don’t offer maps, it’s nice to have a library dedicated solely to them.
8- Gleam
Gleam is inspired by R’s Shiny package. It allows you to turn analyses into interactive web apps using only Python scripts, so you don’t have to know any other languages like HTML, CSS, or JavaScript. Gleam works with any Python data visualization library. Once you’ve created a plot, you can build fields on top of it so users can filter and sort data.
9- missingno
Dealing with missing data is a pain. missingno allows you to quickly gauge the completeness of a dataset with a visual summary, instead of trudging through a table. You can filter and sort data based on completion or spot correlations with a heatmap or a dendrogram.
10- Leather
Leather’s creator, Christopher Groskopf, puts it best: “Leather is the Python charting library for those who need charts now and don’t care if they’re perfect.” It’s designed to work with all data types and produces charts as SVGs, so you can scale them without losing image quality.
https://github.com/mathisonian/awesome-visualization-research
https://mode.com/blog/python-data-visualization-libraries
1. Seaborn
Seaborn is built on top of the matplotlib library. it has many built-in functions using which you can create beautiful plots with just simple lines of codes. It provides a variety of advanced visualization plots with simple syntax like box plots, violin plots, dist plots, Joint plots, pair plots, heatmap, and many more.
Key Features:It can be used to determine the relationship between two variables.
Differentiate when analyzing uni-variate or bi-variate distributions.
Plot the linear regression model for the dependent variable.
Provides multi-grid plotting
Official website: https://seaborn.pydata.org/
2. Plotly
Plotly is an advanced Python analytics library that helps in building interactive dashboards. The graphs build using Plotly are interactive plots, which means you can easily find value at any particular point or session of the graphs. Plotly makes it super easy to generate dashboards and deploying them on the server. It supports Python, R, and the Julia programming language.
You can create a wide range of graphs using Plotly:Basic Charts
Statistical charts
Scientific charts
Financial Charts
Maps
Subplots
Transforms
Jupyter Widgets Interaction
Official website: https://plotly.com/
3. Geoplotlib
Geoplotlib is an open-source Python toolbox for visualizing geographical data. It supports the development of hardware-accelerated interactive visualizations in pure Python and provides implementations of dot maps, kernel density estimation, spatial graphs, Voronoi tesselation, shapefiles, and many more common spatial visualizations.
Geoplotlib can be used to make a variety of maps, such as equivalent area maps, heat maps, and point density maps. There are also several extended modules:geoplotlib
geoplotlib.layers
geoplotlib.utils
geoplotlib.core
geoplotlib.colors
Official website: https://andrea-cuttone.github.io/geoplotlib/
4. Gleam
Gleam is inspired by R’s Shiny package. It allows you to turn analyses into interactive web apps using only Python scripts, so you don’t have to know any other languages like HTML, CSS, or JavaScript. Gleam works with any Python data visualization library. Once you’ve created a plot, you can build fields on top of it so users can filter and sort data.
Official website: https://github.com/dgrtwo/gleam
5. ggplot/ggplot2
ggplot works differently from matplotlib. It lets you add multiple components as layers to create a complete graph or plot at the end. For example, at the start you can add an axis, then points, and other components like a trend line.
They always say that you should store your data in a data frame before using ggplot to get simpler and efficient results.
Official website: https://ggplot2.tidyverse.org/reference/ggplot.html
Key code snippets:
Saturday, August 31, 2024
Things that I have learnt about coding in the past few months
Lights out Alice!
The past few months have been hectic and filled with a few lows in terms of my ability to code. However, there are a few things that I have realized that I hope to improve over the coming months.
Good things that need to be polished further:
- Nobody is ever going to be able to understand your learning requirements completely or better than you. It is up to you to find the resources that help solve your problem or work on framing the question better. The answer is almost always a silly error or easier than you think.
- However, don't spend too long trying to troubleshoot on your own. Our brain functions in heuristics and hence the errors are almost always overlooked because the brain has assumed/ overlooked it in the first place. A fresh set of eyes always gets it done faster, provided they have better knowledge on the topic. A fresh set of eyes who has no context on the problem is almost always going to lead you astray.
- Learning never stops. Not everyday is going to have an Ah-ha! moment but that does not mean you should not put in those 30 minutes to learn something new or revise. I don't get how the rest of the corporate world can get by remembering everything they only hear once, but I don't function like that and hence should remember to work in a way that my job gets done, regardless of the hype or the general SOP.
- I procrastinate too much. Generally by overthinking and most definitely by over complicating everything, putting too much thought into it and then making something that was a two step process into 10 steps, get overwhelmed and ultimately abandoning it without finishing it. ADHD never helps but I need to ensure that I have some side tasks going alongside, with both tasks being on a deadline to ensure that the priority gets done.
- WRITE IT DOWN. DOCUMENT. Stop expecting yourself to remember everything. Links, summaries, processes; take more notes.
Wednesday, February 28, 2024
Questions to ask while understanding the patient funnel
A key element of Pharma strategy is understanding who is your core audience you are targeting what is the universe of this patient pool universe that your product appeals to.
Usefulness:
- Is the patient pool relevant enough?
- Are there enough patients seeking treatment?
- What is the leakage in each step of the diagnosis funnel?
- When does a symptom become bothersome enough to turn to a doctor?
- Does the patient turn to a doctor in the first instance or after a certain point?
- What is the adherence level of the patient?
- What causes a patient to fall off treatment?
- Is the molecule choice dependent on the HCP or the chemist?
- Is there a critical symptom or issue that affects the patients Quality of Life (QOL) that we can provide a solution for?
- Is there some part of the treatment management in the entire ecosystem that we can take over that can help us build better connection with the HCP or the patient and solve a real issue?
Asking the right questions: Brand Evaluation Edition
- How does the patient present the symptom?
- What is the broader level awareness that can be driven?
- What is the top OTX and Rx seller doing?
- What are the conditions that are featuring in the prescription alongside your diagnosis?
- What is the %age of doctors in a particular speciality that prescribe a particular brand and how much business is contributed from that speciality out of your total - this is to understand what is the potential ROI and where efforts should be directed
- Where is the market - OTC or Rx - how are players approaching the HCP or patient differently in each?
Tuesday, November 21, 2023
Another beginning, another avenue to fail
- I am a procrastinator
- I am ADHD-er
- I go off on too many tangents
- I rarely finish what I start
- I start too many things
- I am too enthusiastic about everything BUT the thing in front of me
Volunteering exploration
1. Vidyanjali - MHRD 2. Amex itself? 3. Bhumi 4. Smile foundation 5. Lotus foundation - ggn 6.
-
#Imports import pandas as pd import os import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing ...
-
Conjoint Analysis: https://www.qualtrics.com/en-au/experience-management/research/types-of-conjoint/ Great article on Deep Tech (plus aweso...