Skip to content

Instantly share code, notes, and snippets.

View rohithteja's full-sized avatar

Rohith Teja M rohithteja

  • CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
  • Paris
View GitHub Profile
@rohithteja
rohithteja / optuna-xgboost.py
Last active May 26, 2024 20:35
Optuna Hyperparameter Tuning with XGBoost
import optuna
from xgboost import XGBClassifier
from optuna.trial import TrialState
from sklearn.metrics import accuracy_score
# optuna's objective function
def objective(trial):
learning_rate = trial.suggest_float("learning_rate", 1e-5, 1e-1, log=True)
max_depth = trial.suggest_int("max_depth", 2, 10,step=2, log=False)
n_estimators = trial.suggest_int("n_estimators", 100, 300,step=100, log=False)
@rohithteja
rohithteja / deepwalk-karate.py
Last active February 19, 2024 16:24
DeepWalk embeddings on Karate Club dataset
from ge import DeepWalk
# load graph from networkx library
G = nx.karate_club_graph()
labels = np.asarray([G.nodes[i]['club'] != 'Mr. Hi' for i in G.nodes]).astype(np.int64)
# convert nodes from int to str format
keys = np.arange(0,34)
values = [str(i) for i in keys]
@rohithteja
rohithteja / choropleth.py
Last active November 1, 2022 10:43
choropleth map
import plotly.express as px
import imageio
# removing small countries and NA values
geo = shapefile.dropna()
geo = geo.reset_index(drop=True)
geo = geo[['iso3','name','geometry']]
# appending temperature values to geo DF
for i in range(df_temp.shape[0]):
@rohithteja
rohithteja / paris-choropleth.py
Created October 15, 2022 00:01
Choropleth city
import contextily as cx
# read data
df = gpd.read_file('data/arrondissements/arrondissements.shp',encoding='utf-8')
df = df.to_crs(epsg=3857)
fig, ax = plt.subplots(1, figsize=(10, 6),dpi=300,facecolor='w',edgecolor='w')
# colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.3)
@rohithteja
rohithteja / geopandas-county.py
Created October 14, 2022 22:34
Choropleth Geopandas County
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str})
df.rename(columns={'fips':'GEOID'}, inplace=True)
# read US county shapefile
county_map = gpd.read_file('data/cb_2018_us_county_500k/cb_2018_us_county_500k.shp')
county_map = county_map[~county_map.STATEFP.isin(['02','15','72','60','66','69','78','11'])]
county_map = county_map.to_crs("EPSG:2163")
# read US state shapefile
state_map = gpd.read_file('data/cb_2018_us_state_500k/cb_2018_us_state_500k.shp')
@rohithteja
rohithteja / geopandas-choropleth.py
Last active October 14, 2022 21:54
Choropleth geopandas
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# read data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
df.rename(columns={'code':'STUSPS'}, inplace=True)
# read US state shapefile
@rohithteja
rohithteja / plotly-choropleth.py
Created October 14, 2022 12:52
Choropleth plotly express
import pandas as pd
import geopandas as gpd
import plotly.express as px
# read data
df = pd.read_csv('data/population.csv',header=2)
df.rename(columns={'Country Code':'iso3'}, inplace=True)
# read shapefile
map = gpd.read_file('data/world-administrative-boundaries/world-administrative-boundaries.shp')
# merge df and shapefile
@rohithteja
rohithteja / pyg-karateclub.py
Last active October 9, 2022 22:00
Pytorch Geometric custom dataset
import torch
import pandas as pd
from torch_geometric.data import InMemoryDataset, Data
from sklearn.model_selection import train_test_split
import torch_geometric.transforms as T
# custom dataset
class KarateDataset(InMemoryDataset):
def __init__(self, transform=None):
super(KarateDataset, self).__init__('.', transform, None, None)
@rohithteja
rohithteja / karateclub.py
Last active September 24, 2022 02:56
Graph statistics
import networkx as nx
import numpy as np
import torch
from sklearn.preprocessing import StandardScaler
# load graph from networkx library
G = nx.karate_club_graph()
# retrieve the labels for each node
labels = np.asarray([G.nodes[i]['club'] != 'Mr. Hi' for i in G.nodes]).astype(np.int64)
@rohithteja
rohithteja / lstm-optuna.py
Last active June 21, 2022 11:37
Optuna LSTM
import optuna
from optuna.trial import TrialState
from sklearn.metrics import accuracy_score
def objective(trial):
optimizer_name = trial.suggest_categorical("optimizer", ["adam", "SGD", "RMSprop", "Adadelta"])
epochs = trial.suggest_int("epochs", 5, 15,step=5, log=False)
batchsize = trial.suggest_int("batchsize", 8, 40,step=16, log=False)
history, model = lstm(optimizer_name,epochs,batchsize)