Я новичок ie в области наук о данных или машинного обучения. Я пытаюсь реализовать код из здесь , но предсказание возвращает только 1 класс. Вот мой код:
classification_data = data.drop([10], axis=1).values
classification_label = data[10].values
class LogisticRegression:
def __init__(self, lr=0.01, num_iter=100000):
self.lr = lr
self.num_iter = num_iter
self.weights = None
self.bias = None
def fit(self, X, y):
'''Build a logistic regression classifier from the training set (X, y)'''
n_samples, n_features = X.shape
# init parameters
self.weights = np.zeros(n_features)
self.bias = 0
# gradient descent
for _ in range(self.num_iter):
# approximate y with linear combination of weights and x, plus bias
linear_model = np.dot(X, self.weights) + self.bias
# apply sigmoid function
y_predicted = self._sigmoid(linear_model)
# compute gradients
dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
db = (1 / n_samples) * np.sum(y_predicted - y)
# update parameters
self.weights -= self.lr * dw
self.bias -= self.lr * db
#raise NotImplementedError()
def predict_proba(self, X):
return self._sigmoid(X)
raise NotImplementedError()
def predict(self, X, threshold=0.5): # default threshold adalah 0.5
'''Predict class value for X'''
'''hint: you can use predict_proba function to classify based on given threshold'''
linear_model = np.dot(X, self.weights) + self.bias
#print (linear_model)
y_predicted = self._sigmoid(linear_model)
#print (self.predict_proba(linear_model))
y_predicted_cls = [2 if i > threshold else 1 for i in y_predicted]
return np.array(y_predicted_cls)
raise NotImplementedError()
def _sigmoid(self, x):
return 1 / (1 + np.exp(-x))
, когда я пытаюсь вызвать прогнозирование, он возвращает только один класс:
model.predict(classification_data, threshold=0.5)
результат:
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, etc])
это когда попытайтесь вызвать Forex_Proba:
model.predict_proba(classification_data)
Результат:
array([[0.58826319, 0.5 , 0.52721189, ..., 0.60211507, 0.64565631,
0.62245933],
[0.58586893, 0.73105858, 0.52944351, ..., 0.57793101, 0.62245933,
0.61387647],
[0.63513751, 0.73105858, 0.57590132, ..., 0.6357912 , 0.55971365,
0.52497919]. etc ]])
Действительно благодарны за любую помощь.