diff --git a/scikits/learn/metrics/__init__.py b/scikits/learn/metrics/__init__.py index f67eee7dc0bea917596ede2770d752a64f484f86..d0a4fb764f7fd5d9c2ad8999e43d9bebc9d9d04b 100644 --- a/scikits/learn/metrics/__init__.py +++ b/scikits/learn/metrics/__init__.py @@ -1,19 +1,3 @@ -""" -Metrics module with score functions, performance metrics and -pairwise metrics or distances computation -""" - -from .metrics import confusion_matrix, roc_curve, auc, precision_score, \ - recall_score, fbeta_score, f1_score, zero_one_score, \ - precision_recall_fscore_support, classification_report, \ - precision_recall_curve, explained_variance_score, r2_score, \ - zero_one, mean_square_error, hinge_loss - -from .cluster import homogeneity_completeness_v_measure -from .cluster import homogeneity_score -from .cluster import completeness_score -from .cluster import v_measure_score -from .pairwise import euclidean_distances, pairwise_distances import warnings warnings.warn('scikits.learn is deprecated, please use sklearn') from sklearn.metrics import * diff --git a/sklearn/metrics/pairwise.py b/sklearn/metrics/pairwise.py index 32309b93f6a6fce5904e60bcf6c002f7cabcc4e7..fcac47650bdf7199c3e29df20b85d83777ec0735 100644 --- a/sklearn/metrics/pairwise.py +++ b/sklearn/metrics/pairwise.py @@ -13,7 +13,7 @@ from ..utils.extmath import safe_sparse_dot # Utility Functions def check_pairwise_arrays(X, Y): - """ Sets X and Y appropriately and checks inputs + """ Set X and Y appropriately and checks inputs If Y is None, it is set as a pointer to X (i.e. not a copy). If Y is given, this does not happen. @@ -131,7 +131,7 @@ def euclidian_distances(*args, **kwargs): def l1_distances(X, Y=None, sum_over_features=True): - """ Computes the L1 distances between the vectors in X and Y. + """ Compute the L1 distances between the vectors in X and Y. With sum_over_features equal to False it returns the componentwise distances. @@ -293,16 +293,17 @@ def rbf_kernel(X, Y=None, gamma=0): # Helper functions - distance -pairwise_distance_functions = {} -pairwise_distance_functions['euclidean'] = euclidean_distances -pairwise_distance_functions['l2'] = euclidean_distances -pairwise_distance_functions['l1'] = l1_distances -pairwise_distance_functions['manhattan'] = l1_distances -pairwise_distance_functions['cityblock'] = l1_distances +pairwise_distance_functions = { + 'euclidean':euclidean_distances, + 'l2':euclidean_distances, + 'l1':l1_distances, + 'manhattan':l1_distances, + 'cityblock':l1_distances + } def pairwise_distances(X, Y=None, metric="euclidean", **kwds): - """ Calculates the distance matrix from a vector array X and optional Y. + """ Compute the distance matrix from a vector array X and optional Y. This method takes either a vector array or a distance matrix, and returns a distance matrix. If the input is a vector array, the distances are @@ -368,15 +369,16 @@ def pairwise_distances(X, Y=None, metric="euclidean", **kwds): # Helper functions - distance -pairwise_kernel_functions = {} -pairwise_kernel_functions['rbf'] = rbf_kernel -pairwise_kernel_functions['sigmoid'] = sigmoid_kernel -pairwise_kernel_functions['polynomial'] = polynomial_kernel -pairwise_kernel_functions['linear'] = linear_kernel +pairwise_kernel_functions = { + 'rbf':rbf_kernel, + 'sigmoid':sigmoid_kernel, + 'polynomial':polynomial_kernel, + 'linear':linear_kernel + } def pairwise_kernels(X, Y=None, metric="euclidean", **kwds): - """ Calculates the kernel between arrays X and optional array Y. + """ Compute the kernel between arrays X and optional array Y. This method takes either a vector array or a kernel matrix, and returns a kernel matrix. If the input is a vector array, the kernels are diff --git a/sklearn/metrics/tests/test_pairwise.py b/sklearn/metrics/tests/test_pairwise.py index 438806e9f96a3b569d89d7ed02079e288d73f83c..9fdcc0db56174a2addf11269340e44a4ea01a492 100644 --- a/sklearn/metrics/tests/test_pairwise.py +++ b/sklearn/metrics/tests/test_pairwise.py @@ -59,6 +59,11 @@ def test_pairwise_kernels(): K1 = pairwise_kernels(X, Y=Y, metric=metric) K2 = function(X, Y=Y) assert_equal(K1, K2) + # Test with sparse X and Y + X_sparse = csr_matrix(X) + Y_sparse = csr_matrix(Y) + K1 = pairwise_kernels(X, Y=Y, metric=metric) + assert_equal(K1, K2) # Test with a callable function, with given keywords. metric = callable_rbf_kernel kwds = {} @@ -74,7 +79,7 @@ def callable_rbf_kernel(x, y, **kwds): def test_euclidean_distances(): - """Check the pairwise Euclidean distances computation""" + """ Check the pairwise Euclidean distances computation""" X = [[0]] Y = [[1], [2]] D = euclidean_distances(X, Y) @@ -87,7 +92,7 @@ def test_euclidean_distances(): def test_kernel_symmetry(): - """valid kernels should be symmetric""" + """ Valid kernels should be symmetric""" rng = np.random.RandomState(0) X = rng.random_sample((5, 4)) for kernel in (linear_kernel, polynomial_kernel, rbf_kernel,