Skip to content
Snippets Groups Projects
Commit 8af1ddec authored by Dominique Benielli's avatar Dominique Benielli
Browse files

mkl

parent 1133a020
No related branches found
No related tags found
No related merge requests found
from sklearn import datasets
import numpy as np
import PIL
import matplotlib.pyplot as plt
import os
import matplotlib.pyplot as plt
from multimodal.datasets.base import load_dict, save_dict
from multimodal.tests.data.get_dataset_path import get_dataset_path
from multimodal.datasets.data_sample import MultiModalArray
from multimodal.kernels.mvml import MVML
#Load the digits dataset
digits = datasets.load_digits()
#Display the first digit
plt.figure(1, figsize=(3, 3))
plt.imshow(digits.images[-1], cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
colors = digits.data
gradiant = np.gradient(digits.images, axis=[1,2])
print(gradiant[0].shape)
gradiant0 = gradiant[0].reshape(colors.shape[0], colors.shape[1])
gradiant1 = gradiant[1].reshape(colors.shape[0], colors.shape[1])
for ind in range(digits.images.shape[0]):
ima0 = digits.images[ind, :,:]
ima1 = gradiant[0][ind, :,:]
ima2 = gradiant[1][ind, :,:]
ama_pil0 = PIL.Image.fromarray(ima0, mode=None)
ama_pil1 = PIL.Image.fromarray(ima1, mode=None)
ama_pil2 = PIL.Image.fromarray(ima2, mode=None)
histo_color = np.asarray(ama_pil0.histogram())
histo_gradiant0 = np.asarray(ama_pil1.histogram())
histo_gradiant1 = np.asarray(ama_pil2.histogram())
if ind==0:
list_histogram_color = histo_color
list_histogram_gradiant0 = histo_gradiant0
list_histogram_gradiant1 = histo_gradiant1
else:
list_histogram_color = np.vstack((list_histogram_color, histo_color))
list_histogram_gradiant0 = np.vstack((list_histogram_gradiant0, histo_gradiant0))
list_histogram_gradiant1 = np.vstack((list_histogram_gradiant1, histo_gradiant1))
dict_digit = {0: list_histogram_color, 1: list_histogram_gradiant0, 2: list_histogram_gradiant1}
print(list_histogram_color.shape)
print(list_histogram_gradiant0.shape)
print(list_histogram_gradiant1.shape)
file = get_dataset_path("digit_histogram.npy")
save_dict(dict_digit, file)
d2 = load_dict(file)
figure = plt.figure(figsize=(27, 9))
ax = plt.subplot(2,1,1)
ax.scatter(list_histogram_color[:,3], list_histogram_color[:,4], c=digits.target, edgecolors='k')
ax = plt.subplot(2,1,2)
ax.scatter(list_histogram_color[:,0], list_histogram_color[:,1], c=digits.target, edgecolors='k')
plt.show()
mvml = MVML(lmbda=0.1, eta=1, nystrom_param=0.2)
mvml.fit(d2)
......@@ -6,6 +6,8 @@ from sklearn.utils.validation import check_X_y
from sklearn.utils.validation import check_array
from sklearn.utils.validation import check_is_fitted
from multimodal.kernels.mkernel import MKernel
from sklearn.utils.multiclass import type_of_target
from sklearn.utils.multiclass import check_classification_targets
class MKL(BaseEstimator, ClassifierMixin, MKernel):
......@@ -122,9 +124,18 @@ class MKL(BaseEstimator, ClassifierMixin, MKernel):
self : object
Returns self.
"""
self.X_, self.K_ = self._global_kernel_transform(X, views_ind=views_ind)
self.classes_ = unique_labels(y)
self.X_, self.K_= self._global_kernel_transform(X, views_ind=views_ind)
check_X_y(self.X_, y)
check_classification_targets(y)
if type_of_target(y) in "binary":
self.classes_, y = np.unique(y, return_inverse=True)
y[y==0] = -1.0
elif type_of_target(y) in "continuous":
y = y.astype(float)
self.regression_ = True
else:
raise ValueError("MKL algorithms is a binary classifier"
" or performs regression with float target")
self.y_ = y
n = self.K_.shape[0]
self._calc_nystrom(self.K_, n)
......@@ -270,13 +281,18 @@ class MKL(BaseEstimator, ClassifierMixin, MKernel):
Predicted classes.
"""
check_is_fitted(self, ['X_', 'C', 'K_', 'y_', 'weights'])
X , test_kernels = self._global_kernel_transform(X,
X, K = self._global_kernel_transform(X,
views_ind=self.X_.views_ind,
Y=self.X_)
check_array(X)
C = self.C
weights = self.weights
return self.lpMKL_predict(test_kernels, C, weights)
pred = self.lpMKL_predict(K, C, weights)
pred = np.sign(pred)
pred = pred.astype(int)
pred = np.where(pred == -1, 0, pred)
return np.take(self.classes_, pred)
def lpMKL_predict(self, X, C, weights):
......
......@@ -4,6 +4,7 @@ from scipy.sparse.linalg import splu
from scipy.sparse import csc_matrix
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from sklearn.base import RegressorMixin
from sklearn.utils.multiclass import unique_labels
from sklearn.metrics.pairwise import pairwise_kernels
from sklearn.utils.validation import check_X_y
......@@ -25,7 +26,7 @@ in International Conference on Artificial Intelligence and Statistics (AISTATS)
"""
class MVML(MKernel, BaseEstimator, ClassifierMixin):
class MVML(MKernel, BaseEstimator, ClassifierMixin, RegressorMixin):
r"""
The MVML Classifier
......@@ -426,16 +427,7 @@ class MVML(MKernel, BaseEstimator, ClassifierMixin):
for each view.
- {array like} with (n_samples, nviews * n_features) with 'views_ind' diferent to 'None'
views_ind : array-like (default=[0, n_features//2, n_features])
Paramater specifying how to extract the data views from X:
- views_ind is a 1-D array of sorted integers, the entries
indicate the limits of the slices used to extract the views,
where view ``n`` is given by
``X[:, views_ind[n]:views_ind[n+1]]``.
With this convention each view is therefore a view (in the NumPy
sense) of X and no copy of the data is done.
Returns
-------
......
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment