Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG] MAINT: Continue moving from CBLAS to scipy cython blas #13084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions sklearn/cluster/_k_means.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ cimport cython
from cython cimport floating

from sklearn.utils.sparsefuncs_fast import assign_rows_csr
from ..utils._cython_blas cimport _dot

ctypedef np.float64_t DOUBLE
ctypedef np.int32_t INT

cdef extern from "cblas.h":
double ddot "cblas_ddot"(int N, double *X, int incX, double *Y, int incY)
float sdot "cblas_sdot"(int N, float *X, int incX, float *Y, int incY)

np.import_array()

Expand Down Expand Up @@ -60,18 +58,16 @@ cpdef DOUBLE _assign_labels_array(np.ndarray[floating, ndim=2] X,
center_squared_norms = np.zeros(n_clusters, dtype=np.float32)
x_stride = X.strides[1] / sizeof(float)
center_stride = centers.strides[1] / sizeof(float)
dot = sdot
else:
center_squared_norms = np.zeros(n_clusters, dtype=np.float64)
x_stride = X.strides[1] / sizeof(DOUBLE)
center_stride = centers.strides[1] / sizeof(DOUBLE)
dot = ddot

if n_samples == distances.shape[0]:
store_distances = 1

for center_idx in range(n_clusters):
center_squared_norms[center_idx] = dot(
center_squared_norms[center_idx] = _dot(
n_features, &centers[center_idx, 0], center_stride,
&centers[center_idx, 0], center_stride)

Expand All @@ -81,7 +77,7 @@ cpdef DOUBLE _assign_labels_array(np.ndarray[floating, ndim=2] X,
dist = 0.0
# hardcoded: minimize euclidean distance to cluster center:
# ||a - b||^2 = ||a||^2 + ||b||^2 -2 <a, b>
dist += dot(n_features, &X[sample_idx, 0], x_stride,
dist += _dot(n_features, &X[sample_idx, 0], x_stride,
&centers[center_idx, 0], center_stride)
dist *= -2
dist += center_squared_norms[center_idx]
Expand Down Expand Up @@ -129,16 +125,14 @@ cpdef DOUBLE _assign_labels_csr(X, np.ndarray[floating, ndim=1] sample_weight,

if floating is float:
center_squared_norms = np.zeros(n_clusters, dtype=np.float32)
dot = sdot
else:
center_squared_norms = np.zeros(n_clusters, dtype=np.float64)
dot = ddot

if n_samples == distances.shape[0]:
store_distances = 1

for center_idx in range(n_clusters):
center_squared_norms[center_idx] = dot(
center_squared_norms[center_idx] = _dot(
n_features, &centers[center_idx, 0], 1,
&centers[center_idx, 0], 1)

Expand Down
18 changes: 4 additions & 14 deletions sklearn/cluster/setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
# Author: Alexandre Gramfort <[email protected]>
# License: BSD 3 clause
import os
from os.path import join

import numpy

from sklearn._build_utils import get_blas_info


def configuration(parent_package='', top_path=None):
from numpy.distutils.misc_util import Configuration

cblas_libs, blas_info = get_blas_info()

libraries = []
if os.name == 'posix':
cblas_libs.append('m')
libraries.append('m')

config = Configuration('cluster', parent_package, top_path)
Expand All @@ -29,26 +23,22 @@ def configuration(parent_package='', top_path=None):
language="c++",
include_dirs=[numpy.get_include()],
libraries=libraries)

config.add_extension('_k_means_elkan',
sources=['_k_means_elkan.pyx'],
include_dirs=[numpy.get_include()],
libraries=libraries)

config.add_extension('_k_means',
libraries=cblas_libs,
sources=['_k_means.pyx'],
include_dirs=[join('..', 'src', 'cblas'),
numpy.get_include(),
blas_info.pop('include_dirs', [])],
extra_compile_args=blas_info.pop(
'extra_compile_args', []),
**blas_info
)
include_dirs=numpy.get_include(),
libraries=libraries)

config.add_subpackage('tests')

return config


if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
Loading