Skip to content

Instantly share code, notes, and snippets.

View skojaku's full-sized avatar

SADAMORI KOJAKU skojaku

View GitHub Profile
@skojaku
skojaku / one_pass_sampling_without_replacement.py
Created October 29, 2023 10:25
One-pass sampling without replacement by Efraimidis and Spirakis algorithm
# Efraimidis and Spirakis algorithm
# https://www.sciencedirect.com/science/article/abs/pii/S002001900500298
import numpy as np
from numba import njit
@njit(nogil=True)
def one_pass_sampling_without_replacement(n, k, weights):
# Draw a uniform random variable for each item
u = np.random.rand(n)
@skojaku
skojaku / element-centric-similarity.py
Last active September 7, 2023 15:57
Element-centric similarity
import numpy as np
from scipy import sparse
#
# Evaluation
#
def calc_esim(y, ypred, normalize=False):
"""
Element centric similarity.
@skojaku
skojaku / NonBacktrackingMatrixEmbedding.py
Last active June 2, 2023 19:58
NonBacktrackingMatrixEmbedding.py
"""Non-backtracking spectral embedding
Reference
---------
Krzakala, Florent, et al. "Spectral redemption in clustering sparse networks." Proceedings of the National Academy of Sciences 110.52 (2013): 20935-20940.
Example
-------
```python
@skojaku
skojaku / OAGBERT.py
Last active May 19, 2023 17:04
OAGBERT
# -*- coding: utf-8 -*-
# @Author: Sadamori Kojaku
# @Date: 2022-10-05 06:24:53
# @Last Modified by: Sadamori Kojaku
# @Last Modified time: 2023-05-19 13:04:26
import sys
from tqdm.auto import tqdm
import numpy as np
import pandas as pd
import torch
@skojaku
skojaku / truncated_RBO.py
Last active May 7, 2023 19:17
rank_biased_overlap
import numpy as np
def truncated_RBO(S, T, p=0.9):
"""
Calculate the (truncated) Rank Biased Overlap (RBO) similarity measure between two ranked lists S and T.
The original RBO takes the sum over entities beyond the ranking. Since the entities outside of the ranking is unknown,
the original paper introduces an exterpolation method. But this gives many complexity with additional assumptions on the input data.
So I take a simpler approach:
1. Instead of considering a ranking of infinite length, I consider finite ranking and take a sum over the entities in the ranking.
@skojaku
skojaku / degreeAssortativity.py
Last active May 5, 2023 18:11
degreeAssortativity
import numpy as np
def degreeAssortativity(src, trg, weight=None) -> float:
"""
Computes the degree assortativity of a graph based on the source and target node degrees and an optional edge weight.
Parameters
----------
src : array-like
The source nodes of the edges.
@skojaku
skojaku / Adam.py
Last active April 26, 2023 16:05
Adam
# -*- coding: utf-8 -*-
# @Author: Sadamori Kojaku
# @Date: 2023-04-26 12:02:58
# @Last Modified by: Sadamori Kojaku
# @Last Modified time: 2023-04-26 12:05:15
import numpy as np
class ADAM:
def __init__(self):
@skojaku
skojaku / generate_airport_network.py
Created April 18, 2023 09:55
generate_airport_network.py
import sys
import country_converter as coco
import networkx as nx
import numpy as np
import pandas as pd
from scipy import sparse
from scipy.sparse.csgraph import connected_components
@skojaku
skojaku / SoftMaxSampler.py
Last active April 17, 2023 10:12
Softmax sampler for embedding vectors
# -*- coding: utf-8 -*-
# @Author: Sadamori Kojaku
# @Date: 2023-04-14 17:09:54
# @Last Modified by: Sadamori Kojaku
# @Last Modified time: 2023-04-17 06:11:17
import faiss
import numpy as np
from numba import jit
from scipy import sparse
@skojaku
skojaku / FastRP.py
Last active April 20, 2023 01:41
fastRP
import pandas as pd
from scipy import sparse
import numpy as np
def fastRP(net, dim, window_size, beta=-1, s=3.0, edge_direction=False):
"""
.. function:: fastRP(net: scipy.sparse matrix, dim: int, window_size: int, beta: float = -1, s: float = 3.0, return_context: bool = False) -> numpy.ndarray
Fast Random Projection embedding