gpt4 book ai didi

python - 如何使这个估算器与 scikit-learn 兼容?

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:38 25 4
gpt4 key购买 nike

我正在尝试使这个估计器与 scikit-learn 兼容,以便我可以使用 GridSearchCV 搜索参数空间。

编辑:

我已经按照建议修改了脚本(见下文)。

  • 拟合签名修改为fit(self, X, y)
  • 所有参数都在__init__中传递

GripdSearchCV 仍然存在兼容性问题,可能是因为估计器是多标签分类器。

ValueError: Can't handle mix of multilabel-indicator and continuous-multioutput

但这超出了重点;属性错误现在消失了。因此,我们可以有把握地得出结论,建议的修改使估算器与 scikit-learn 兼容。

最终代码脚本:

import numpy as np
from sklearn.grid_search import GridSearchCV
from sklearn.datasets import make_classification
from sklearn.preprocessing import LabelBinarizer
from sklearn.cross_validation import train_test_split
from sklearn.base import BaseEstimator, ClassifierMixin

class LogisticClassifier(BaseEstimator, ClassifierMixin):
def __init__(self, basis=None, itrs=100, learn_rate=0.1, reg=0.1, momentum=0.5, proj_layer_size=10):
self.W = []
self.A = None
if basis == 'rectifier':
self.basisfunc = self.rectifier_basis
else:
self.basisfunc = self.identity

self.itrs = itrs
self.learn_rate = learn_rate
self.reg = reg
self.momentum = momentum
self.proj_layer_size = proj_layer_size

def identity(self, x):
return np.hstack((x, 1))

def rectifier_basis(self, x):
xn = np.dot(self.A, x)
return self.identity(np.maximum(xn, 0))

def basismap(self, X):
new_dimensions = self.basisfunc(X[0,:]).shape[0]
Xn = np.zeros((X.shape[0], new_dimensions))
for i, xi in enumerate(X):
Xn[i,:] = self.basisfunc(xi)
return Xn

def fit(self, X, Y):
self.A = np.random.uniform(-1, 1, (self.proj_layer_size, X.shape[1]))
Xn = self.basismap(X)
self.W = np.array(np.random.uniform(-0.1, 0.1, (Y.shape[1], Xn.shape[1])))
costs_train, costs_test = [], []
previous_grad = np.zeros(self.W.shape)
for i in range(self.itrs):
grad = self.grad(Xn, Y)
self.W = self.W - self.learn_rate*(grad+self.momentum*previous_grad)
previous_grad = grad
costs_train.append(self.loss(X, Y))
#costs_test.append(self.loss(Xtest, Ytest))
#return (costs_train, costs_test)
return costs_train

def softmax(self, Z):
Z = np.maximum(Z, -1e3)
Z = np.minimum(Z, 1e3)
numerator = np.exp(Z)
return numerator/np.sum(numerator, axis=1).reshape((-1,1))

def predict(self, X):
Xn = self.basismap(X)
return self.softmax(np.dot(Xn, self.W.T))

def grad(self, Xn, Y):
Yh = self.softmax(np.dot(Xn, self.W.T))
return -np.dot(Y.T-Yh.T,Xn)/Xn.shape[0] + self.reg*self.W

def loss(self, X, Y):
Yh = self.predict(X)
return -np.mean(np.mean(Y*np.log(Yh)))-self.reg*np.trace(np.dot(self.W,self.W.T))/self.W.shape[0]

def get_params(self, deep=True):
return {"itrs": self.itrs, "learn_rate": self.learn_rate, "reg": self.reg, "momentum": self.momentum,
"report_cost": self.report_cost, "proj_layer_size": self.proj_layer_size, "iseed": self.iseed}

def set_params(self, **parameters):
for parameter, value in parameters.items():
setattr(self, parameter, value)

#make data
X, Y = make_classification(n_features=2, n_informative=2, n_redundant=0, n_repeated=0, n_classes=3,
n_clusters_per_class=1, random_state=31)
lb = LabelBinarizer()
Y = lb.fit_transform(Y)
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.25, random_state=5)

#model optimization
param_grid = {'learn_rate': [0.1, 0.01, 0.001],
'reg': [0.001, 0.01]
}

clf = LogisticClassifier(basis='rectifier')
gs_cv = GridSearchCV(clf, param_grid, scoring='accuracy').fit(Xtrain, Ytrain)
print('Best hyperparameters: %r' % gs_cv.best_params_)

最佳答案

get_params方法中你调用了self.itrs,但是你的对象没有这样的属性。

此外,我建议您将 fit 签名更改为 fit(self, X, y)

  1. 传递__init__中的所有参数
  2. 拆分 Xy 以使用 sklearn.cross_validation.train_test_split 进行训练和测试。

这将使您的代码更像 sklearn,并且与库函数更兼容。

关于python - 如何使这个估算器与 scikit-learn 兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com