Skip to content

Instantly share code, notes, and snippets.

View razhangwei's full-sized avatar

Wei Zhang razhangwei

  • Facebook
  • Bay Area
View GitHub Profile
@razhangwei
razhangwei / cross_validation.py
Last active March 11, 2017 20:45
sklearn: tune parameter using cross validation
"""
This file uses cross validation to tune the parameters.
"""
import multiprocessing
import sys
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
@razhangwei
razhangwei / network.py
Last active November 30, 2017 19:42
plot network structure #Plotting
def plot_graph(weights, labels, ax=None):
import networkx as nx
G = nx.Graph()
for i in range(weights.shape[0]):
for j in range(weights.shape[1]):
if weights[i][j] != 0:
G.add_edge(labels[i], labels[j], weight=weights[i][j])
e_pos = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0]
@razhangwei
razhangwei / axis_style_date.py
Last active November 30, 2017 19:41
Customize the styles of x-axis with dates. #Plotting
import matplotlib.dates as mdates
plot.hist(mdates.date2num(dts), 100) # plot the histogram
ax = plt.gca()
ax.xaxis.set_major_locator(mdates.AutoDateLocator()) # set the major ticker, use AutoDateLocator
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d')) # show the label
ax.xaxis.set_minor_locator(mdates.DayLocator()) # set the minor ticker, use DayLocator
@razhangwei
razhangwei / tf_print.py
Last active December 12, 2018 12:47 — forked from vihari/tf_print.py
Tensorflow's tf.Print to stdout instead of default stderr #TensorFlow
"""
The default tf.Print op goes to STDERR
Use the function below to direct the output to stdout instead
Usage:
> x=tf.ones([1, 2])
> y=tf.zeros([1, 3])
> p = x*x
> p = tf_print(p, [x, y], "hello")
> p.eval()
hello [[ 0. 0.]]
@razhangwei
razhangwei / abc.py
Created March 23, 2019 01:40
Python abstract class #Python
from abc import ABC, abstractmethod
class Foo(ABC, object):
@property
@abstractmethod
def f(self):
pass
@abstractmethod
@razhangwei
razhangwei / argument.md
Created March 24, 2019 19:33
tips practice for defining class or function arguments #Python
  1. Make base class an abc.ABC and specify methods as abstractmethod or staticmethod as much as possible.
  2. Interface function, specify the known/shared arguments as much as possible. Keep the special argument as **kwargs.
@razhangwei
razhangwei / environment.yml
Last active October 24, 2019 15:08
conda environment.yml
channels:
- defaults
- conda-forge
- r
- pytorch
- derickl
dependencies:
- python=3.6.9
- dill
@razhangwei
razhangwei / spotify.md
Last active May 1, 2023 03:43
Spotify Tips
  • Disable Ads:
    • MacOS:
      • (Recommended) Web client + regular ad blocker.
      • Regular client + Spotifree: Won't block the ads, but it will automatically mute when ads are being played.
  • Download:
    • Downloader: spotify-downloader.com; too good to be true.
@razhangwei
razhangwei / extract.py
Last active April 1, 2019 00:57
extract and modify data from Figure #matplotlib
# the better practice would be always export your data along with the graph
# maybe in the future when mpld3.fig_to_json() has been greately improved,
# one can simply use this function.
fig = ...
ax = fig.axes[0]
# line
line = ax.lines[0]
line.set_ydata(line.get_ydata() + 1)
@razhangwei
razhangwei / efficient_pytorch.md
Created April 11, 2019 14:29
Efficient Pytorch for extremely large dataset #PyTorch

Some simple (yet not most efficient) solutions:

  • torchvision.datasets.ImageFolder/torchvision.datasets.DatasetFolder + data.DataLoader
  • lmdb (Lightning Memory-mapped database manager)

Key technologies:

  • lapack for process the data
  • Magma suuport: MAGMA is a collection of next generation linear algebra (LA) GPU accelerated libraries designed and implemented by the team that developed LAPACK and ScaLAPACK.