Skip to content

Instantly share code, notes, and snippets.

@yuyay
yuyay / l-bfgs.py
Created July 7, 2012 16:53
L-BFGS example in Scipy
import numpy as np
from scipy.optimize import fmin_bfgs
def rosen(x):
return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:1])**2.0)
def rosen_der(x):
xm = x[1:-1]
xm_m1 = x[:-2]
xm_p1 = x[2:]
@yuyay
yuyay / splitFile.py
Created July 17, 2012 11:09
split an image into 6 small images
# -*- coding:utf-8 -*-
import os
import cv2
row = 1882 #縦
col = 1913 #横
point = [(195, 133), (2073, 133), (3948, 133),
(200, 2035), (2072, 2035), (3938, 2035)] #
@yuyay
yuyay / remove.py
Created August 21, 2012 19:46
<>切り取る
# -*- coding:utf-8 -*-
import os
import cv2
folderName = ''
outputFolder = ''
for files in os.listdir(folderName):
@yuyay
yuyay / gist:3838642
Created October 5, 2012 07:54
RTの発信元を抽出する正規表現
import re
r = re.compile("RT @([0-9a-zA-Z_]{1,15}):(?!.*RT @[0-9a-zA-Z_]{1,15})\s?(.*?)$")
text = "RT @aaa: あああ RT @bbb: いいい"
s = r.search(text)
s.group(0) # マッチした全体(RT @bbb: いいい)
s.group(1) # 発信者screen_name(bbb)
s.group(2) # 発信情報(いいい)
@yuyay
yuyay / zikan.py
Created October 24, 2014 19:41
Measure elapsed time of execution
#! encoding=UTF-8
"""
zikan: a class of measuring elasped time
"""
from datetime import datetime, timedelta
class Zikan(object):
"""
Zikan class
@yuyay
yuyay / kcca.py
Last active October 21, 2021 04:54
kernel canonical correlation analysis in python
#! encoding=UTF-8
"""
kernel canonical correlation analysis
"""
import numpy as np
from scipy.linalg import svd
from sklearn.metrics.pairwise import pairwise_kernels, euclidean_distances
class KCCA(object):
@yuyay
yuyay / rtm.py
Created April 11, 2015 17:05
relational topic model (I added prediction function)
import numpy as np
# import utils
from scipy.special import gammaln, psi
# from formatted_logger import formatted_logger
eps = 1e-20
# log = formatted_logger('RTM', 'info')
class rtm:
""" implementation of relational topic model by Chang and Blei (2009)
@yuyay
yuyay / gist:0d037d3543405fb4c30120eb5b0f648f
Created May 6, 2017 11:53 — forked from jdiscar/gist:9144764
RGB Value to Nearest Color Name (Python)
"""
Original Author Ernesto P. Adorio, Ph.D
Original Source: http://my-other-life-as-programmer.blogspot.com/2012/02/python-finding-nearest-matching-color.html
Modifed By: JDiscar
This class maps an RGB value to the nearest color name it can find. Code is modified to include
ImageMagick names and WebColor names.
1. Modify the minimization criterion to use least sum of squares of the differences.
2. Provide error checking for input R, G, B values to be within the interval [0, 255].