def l1_fista(t, Y, bound, Nz, alpha, iterations=5000): h = np.log(bound[1] / bound[0]) / (Nz - 1) # equally spaced on logscale z = bound[0] * np.exp(np.arange(Nz) * h) # z (Nz by 1) # construct coefficients matrix C by integral discretization (trapzoidal) # ||F - K*f||^2 = || F - \int_lb^ub [f(z)*z]*exp(-z*t) d(lnz) ||^2 z_mesh, t_mesh = np.meshgrid(z, t) K = np.exp(-t_mesh * z_mesh) # specify integral kernel K[:, 0] /= 2. K[:, -1] /= 2. K *= h fista = Fista(loss='least-square', penalty='l11', lambda_=alpha * 1e-4, n_iter=5000) fista.fit(K, Y) return z, fista.coefs_, fista.predict(K)
from fista import Fista import numpy as np fista = Fista(lambda_=0.5, loss='squared-hinge', penalty='l11', n_iter=10000) X = np.random.normal(size=(10, 40)) y = np.sign(np.random.normal(size=10)) fista.fit(X, y, verbose=1) #print "taux de bonne prediction with l11: %f " % fista.score(X, y) #fista.penalty='l12' #fista.fit(X, y) #print "taux de bonne prediction with l12: %f " % fista.score(X, y) #fista.penalty='l21' #fista.fit(X, y) #print "taux de bonne prediction with l21: %f " % fista.score(X, y) #fista.penalty='l22' #fista.fit(X, y, verbose=1) #print "taux de bonne prediction with l22: %f " % fista.score(X, y) #
y = data.target X = data.data X = X[y < 2] y = y[y < 2] y[y == 0] = -1 K1 = X[:, 0] K2 = X[:, 1] K3 = X[:, 2] K4 = X[:, 3] K1 = np.dot(K1[:, np.newaxis], K1[:, np.newaxis].transpose()) K2 = np.dot(K2[:, np.newaxis], K2[:, np.newaxis].transpose()) K3 = np.dot(K3[:, np.newaxis], K3[:, np.newaxis].transpose()) K4 = np.dot(K4[:, np.newaxis], K4[:, np.newaxis].transpose()) K = np.concatenate((K1, K2, K3, K4), axis=1) fista = Fista(loss='squared-hinge', penalty='l11', lambda_=0.1, n_iter=50) fista.fit(K, y) print("pourcentage de bonne prediction avec l11: %d " % fista.score(K, y)) fista.penalty = 'l12' fista.fit(K, y) print("pourcentage de bonne prediction avec l12: %d " % fista.score(K, y)) fista.penalty = 'l21' fista.fit(K, y) print("pourcentage de bonne prediction avec l21: %d " % fista.score(K, y)) fista.penalty = 'l22' fista.fit(K, y) print("pourcentage de bonne prediction avec l22: %d " % fista.score(K, y))
X = data.data X = X[y<2] y = y[y<2] y[y==0] = -1 K1 = X[:, 0] K2 = X[:, 1] K3 = X[:, 2] K4 = X[:, 3] K1 = np.dot(K1[:, np.newaxis], K1[:, np.newaxis].transpose()) K2 = np.dot(K2[:, np.newaxis], K2[:, np.newaxis].transpose()) K3 = np.dot(K3[:, np.newaxis], K3[:, np.newaxis].transpose()) K4 = np.dot(K4[:, np.newaxis], K4[:, np.newaxis].transpose()) K = np.concatenate((K1, K2, K3, K4), axis=1) fista = Fista(loss='squared-hinge', penalty='l11', lambda_=0.1, n_iter=50) fista.fit(K, y) print "pourcentage de bonne prediction avec l11: %d " % fista.score(K, y) fista.penalty='l12' fista.fit(K, y) print "pourcentage de bonne prediction avec l12: %d " % fista.score(K, y) fista.penalty='l21' fista.fit(K, y) print "pourcentage de bonne prediction avec l21: %d " % fista.score(K, y) fista.penalty='l22' fista.fit(K, y) print "pourcentage de bonne prediction avec l22: %d " % fista.score(K, y)