Skip to content

Instantly share code, notes, and snippets.

View MechCoder's full-sized avatar
💭
away

manoj kumar MechCoder

💭
away
View GitHub Profile
# -*- coding: utf-8 -*-
"""
Strong rules for coordinate descent
Author: Fabian Pedregosa <fabian@fseoane.net>
"""
import numpy as np
from scipy import linalg
@MechCoder
MechCoder / sparse.py
Created March 21, 2014 05:32
Sparse matrix handling
# Accessing rows and columns.
for ptr in xrange(len(csc.indptr) - 1):
strptr = csc.indptr[ptr]
endptr = csc.indptr[ptr + 1]
temp = xrange(strptr, endptr)
if temp:
for row in temp:
print ptr, csc.data[row], csc.indices[row]
@MechCoder
MechCoder / setup.py
Last active August 29, 2015 14:00
Setup cblas
from numpy.distutils.system_info import get_info
import os
from os.path import join
import numpy
def configuration():
from numpy.distutils.misc_util import Configuration
from libc.math cimport fabs, sqrt
cimport cython
cdef extern from "cblas.h":
void daxpy "cblas_daxpy"(int N, double alpha, double *X, int incX,
double *Y, int incY)
def b(int x):
print x
@MechCoder
MechCoder / benchmark.py
Created May 10, 2014 05:57
Benchmarking with gil and with nogil
import pylab as pl
import numpy as np
from sklearn.linear_model import *
from sklearn.datasets import make_regression
import time
def plot():
pl.figure("Benchmark with nogil")
from sklearn.linear_model import ElasticNetCV
from sklearn.datasets import make_regression
import numpy as np
from scipy.sparse import csr_matrix
X, y = make_regression(n_samples=2000, n_features=2000)
clf = ElasticNetCV(n_jobs=4, cv=3, n_alphas=100, fit_intercept=False, l1_ratio=np.linspace(0.1, 0.9, 5))
clf.fit(X, y)
from sklearn.linear_model import LogisticRegressionCV
from sklearn.datasets import make_classification
from time import time
import numpy as np
rng = np.random.RandomState(0)
X, y = make_classification(n_samples=2000, n_features=2000, random_state=rng)
clf = LogisticRegressionCV(n_jobs=4, Cs=[1, 10, 100, 1000], cv=10)
t = time()
clf.fit(X, y)
@MechCoder
MechCoder / bench.py
Last active August 29, 2015 14:02
Bench 20newsgroup and the digits data
from sklearn.datasets import load_digits, fetch_20newsgroups_vectorized
from sklearn.linear_model import LogisticRegressionCV
from sklearn.cross_validation import cross_val_score
from scipy.sparse import csr_matrix
digits = load_digits(n_class=2)
X, y = digits.data, digits.target
clf = LogisticRegressionCV(solver='liblinear')
csr = csr_matrix(X)
print cross_val_score(clf, X, y)
@MechCoder
MechCoder / bench_demo.py
Created June 25, 2014 06:30
Beching to measure the effect of random descent
import numpy as np
from scipy import signal
rng = np.random.RandomState(42)
n_samples, n_features, k = 500, 1000, 10
h = signal.gaussian(50, 15)
X = signal.convolve2d(np.eye(n_features), h[:, np.newaxis], 'same') # convolutional design
X = X[::n_features // n_samples]
@MechCoder
MechCoder / bench_random.py
Last active August 29, 2015 14:03
Random vs non_random
from sklearn.linear_model import *
from sklearn.datasets import *
X, y = load_svmlight_file("/home/manoj/Downloads/duke")
# Get the best alpha, l1_ratio combination first
clf = ElasticNetCV(max_iter=10000, n_jobs=-1)
clf.fit(X, y)
clf_random = ElasticNet(alpha=clf.alpha_, l1_ratio=clf.l1_ratio_, max_iter=50000, tol=0, random_state=42)
clf_random.fit(X.toarray(), y)