1,创建.py文件
def getSource(fun):
f = open('./' + fun.__name__ + '.py', 'w')
f.write(ist.getsource(fun))
f.close
2.getSource获取任意想查看的函数
from sklearn.cluster import KMeans
getSource(KMeans.fit)
3.运行
import inspect as ist
def getSource(fun):
f = open('./' + fun.__name__ + '.py', 'w')
f.write(ist.getsource(fun))
f.close
from sklearn.cluster import KMeans
getSource(KMeans.fit)
4.结果
def fit(self, X, y=None, sample_weight=None):
"""Compute k-means clustering.
Parameters
----------
X : array-like or sparse matrix, shape=(n_samples, n_features)
Training instances to cluster. It must be noted that the data
will be converted to C ordering, which will cause a memory
copy if the given data is not C-contiguous.
y : Ignored
not used, present here for API consistency by convention.
sample_weight : array-like, shape (n_samples,), optional
The weights for each observation in X. If None, all observations
are assigned equal weight (default: None)
"""
random_state = check_random_state(self.random_state)
self.cluster_centers_, self.labels_, self.inertia_, self.n_iter_ = \
k_means(
X, n_clusters=self.n_clusters, sample_weight=sample_weight,
init=self.init, n_init=self.n_init,
max_iter=self.max_iter, verbose=self.verbose,
precompute_distances=self.precompute_distances,
tol=self.tol, random_state=random_state, copy_x=self.copy_x,
n_jobs=self.n_jobs, algorithm=self.algorithm,
return_n_iter=True)
return self