# 需要验证的参数的数据 param_grid = [ { 'weights': ['uniform'], 'n_neighbors': [i for i in range(1, 11)] }, { 'weights': ['distance'], 'n_neighbors': [i for i in range(1, 11)], 'p': [i for i in range(1, 6)] } ]
from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn import datasets
data = datasets.load_iris() x_train, x_test, y_train, y_test = train_test_split(data.data, data.target)
deffit(self, X: np.ndarray) -> 'StandardScaler': """ 根据训练数据集X获得数据的均值和方差 """ assert X.ndim == 2, "The dimension of X must be 2"
self.mean_ = np.array([np.mean(X[:, i]) for i in range(X.shape[1])]) self.scale_ = np.array([np.std(X[:, i]) for i in range(X.shape[1])])
return self
deftransform(self, X: np.ndarray) -> np.ndarray: """ 将X根据这个StandardScaler进行方差均值归一化处理 """ assert X.ndim == 2, "The dimension of X must be 2" assert self.scale_ isnotNoneand self.mean_ isnotNone, \ "must fit before transform" assert X.shape[1] == len(self.mean_), \ "the feature number of X must be equal to mean_ and scale_"
resX = np.empty(shape=X.shape, dtype=float) for col in range(X.shape[1]): resX[:, col] = (X[:, col] - self.mean_[col]) / self.scale_[col]