Skip to content

Instantly share code, notes, and snippets.

View liudong's full-sized avatar

Dong LIU liudong

  • Manchester, United Kingdom
View GitHub Profile
import numpy as np
from scipy import stats
differences = [B[i] - A[i] for i in range(len(A))]
# one sample t-test
stats.ttest_1samp(differences, popmean=0)
# assess the equality of variances
stats.levene(A, B)
@liudong
liudong / gist:22138f9af9261a0c72136ae28249a3c6
Created April 20, 2018 02:13
Python: sort dict by values
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))
from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
big_yellow = Animal(name="big_yellow", age=3, type="dog")
@liudong
liudong / gist:dcb168fd7544143713bb67c7b45ee11f
Created April 16, 2018 06:43
Python: create a date range
import datetime
def date_range(start_date, end_date):
start = datetime.datetime.strptime(start_date, "%Y-%m-%d")
end = datetime.datetime.strptime(end_date, "%Y-%m-%d")
return [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
@liudong
liudong / tf_summary.py
Created May 11, 2017 11:56
Tensorflow: SummaryWriter
import tensorflow as tf
sess = tf.Session()
writer = tf.summary.FileWriter('./my_graph', sess.graph)
@liudong
liudong / test_file
Last active March 4, 2016 14:55
Python: nose test
from nose.tools import *
def test_
@liudong
liudong / Python: import scipy
Last active June 22, 2016 09:38
Python: import scipy
%matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
@liudong
liudong / gist:5452884
Created April 24, 2013 15:12
Python: elapsed time
import time
start = time.time()
end = time.time()
elapsed = end - start
print "Time taken: ", elapsed, "seconds."
@liudong
liudong / gist:5353662
Created April 10, 2013 10:57
Python: Save something to a Excel spreadsheet
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
from openpyxl.cell import get_column_letter
class XlsLogger(object):
"""
Save something to a Excel spreadsheet
"""
def __init__(self):
@liudong
liudong / gist:5278212
Last active December 15, 2015 14:59
Python: Empty unit test
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import unittest
class simpleTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):