TGViewer
Библиотека собеса по Data Science | вопросы с собеседований Библиотека собеса по Data Science | вопросы с собеседований @ds_interview_lib · 4.47K subscribers
Post #611 1.26K
Напишите логистическую регрессию

import numpy as np

class LogisticRegression:

def __init__(self, learning_rate=0.01, n_iters=1000):
self.learning_rate = learning_rate
self.n_iters = n_iters
self.weights = None
self.bias = None

def fit(self, X, y):
# initialize weights and bias to zeros
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0

# gradient descent optimization
for i in range(self.n_iters):
# calculate predicted probabilities and cost
z = np.dot(X, self.weights) + self.bias
y_pred = self._sigmoid(z)
cost = (-1 / n_samples) * np.sum(y * np.log(y_pred) + (1 - y) * np.log(1 - y_pred))

# calculate gradients
dw = (1 / n_samples) * np.dot(X.T, (y_pred - y))
db = (1 / n_samples) * np.sum(y_pred - y)

# update weights and bias
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db

def predict(self, X):
# calculate predicted probabilities
z = np.dot(X, self.weights) + self.bias
y_pred = self._sigmoid(z)
# convert probabilities to binary predictions
return np.round(y_pred).astype(int)

def _sigmoid(self, z):
return 1 / (1 + np.exp(-z))


#python
#машинное_обучение
  • 👍 11
  • ❤ 1
More from @ds_interview_lib
  1. Sep 15, 2026💬 Как инициализировать параметры в логистической регрессии, и важно ли это Частый вариант…
  2. Sep 15, 2026😭 Как не потратить недельный лимит AI-кодинга за три дня? Разберём на вебинаре, как трати…
  3. Aug 5, 2026❓ Могут ли в Adam возникать проблемы исчезающих или взрывающихся градиентов Да, полностью…
  4. Aug 5, 2026👅 Самое сложное — выбрать не курс, а направление Сегодня хочется разобраться в AI-агентах…
  5. Aug 5, 2026Один доступ вместо вечного выбора между «нужно для работы» и «давно хотелось изучить» 👇
  6. Jul 31, 2026Post #1596
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →