diff --git a/multimodal/datasets/digit.py b/multimodal/datasets/digit.py new file mode 100644 index 0000000000000000000000000000000000000000..2411306adce3217e8e660db4e5f0a3053f646efa --- /dev/null +++ b/multimodal/datasets/digit.py @@ -0,0 +1,62 @@ +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) diff --git a/multimodal/kernels/lpMKL.py b/multimodal/kernels/lpMKL.py index 76436b6082041c36a3298a660e80f36120adf5c5..5bca05f8501b03b5e8e813ed9b4cb5688ad2701a 100644 --- a/multimodal/kernels/lpMKL.py +++ b/multimodal/kernels/lpMKL.py @@ -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): diff --git a/multimodal/kernels/mvml.py b/multimodal/kernels/mvml.py index 3a09996037633d171a2a23db622da894335a7e0d..2ce26b6fc3a807eab978675657eed6f09fad0c81 100644 --- a/multimodal/kernels/mvml.py +++ b/multimodal/kernels/mvml.py @@ -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 ------- diff --git a/multimodal/tests/data/digit_histogram.npy b/multimodal/tests/data/digit_histogram.npy new file mode 100644 index 0000000000000000000000000000000000000000..b08d2c6e85a3a92f30b5f88d95bdbd9edccb0f9d Binary files /dev/null and b/multimodal/tests/data/digit_histogram.npy differ