image_output_1.png
from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

import numpy as np

fig, ax = plt.subplots(1, 2, figsize=(15,5))

X = np.array([[1],[2],[3],[4],[8],[9],[10],[11]])
Y = np.array([0, 0, 0, 0, 1, 1, 1, 1])
model = LinearRegression()
model.fit(X, Y)
x_values = np.linspace(0, 12, 100)
y_values = [model.intercept_ + x * model.coef_[0] for x in x_values]
ax[0].plot(x_values, y_values)
ax[0].scatter(X, Y, c='b');
ax[0].title.set_text('Рис. 1')

X = np.array([[1],[2],[3],[4],[8],[9],[10],[11], [18], [19], [20], [21], [22], [23], [24]])
Y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
model = LinearRegression()
model.fit(X, Y)
x_values = np.linspace(0, 25, 100)
y_values = [model.intercept_ + x * model.coef_[0] for x in x_values]
ax[1].plot(x_values, y_values)
ax[1].scatter(X, Y, c='b');
ax[1].title.set_text('Рис. 2')

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

fig, ax = plt.subplots(1, 2, figsize=(15,5))

x_values = np.linspace(-5, 5, 100)
y_values =  [sigmoid(x) for x in x_values]

ax[0].plot(x_values, y_values);

X = np.array([[-1],[-2],[-3],[-4],[-8],[-9],[5],[8], [12], [13], [14], [15]])
Y = np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

x_values = np.linspace(-10, 25, 100)
y_values =  [sigmoid(x) for x in x_values]

ax[1].plot(x_values, y_values);
ax[1].scatter(X, [sigmoid(x) for x in X], c='r');

fig, ax = plt.subplots(1, 2, figsize=(15, 5))
def if_y_1(hx):
    return -np.log(hx)
x_values = np.linspace(0.001, 1, 100)
y_values = [if_y_1(hx) for hx in x_values]
ax[0].plot(x_values, y_values)
ax[0].title.set_text('If y = 1')
ax[0].set_xlabel('h(x) - sigmoid output')
ax[0].set_ylabel('Cost')
def if_y_0(hx):
    return -np.log(1 - hx)
x_values = np.linspace(0, 0.999, 100)
y_values = [if_y_0(hx) for hx in x_values]
ax[1].plot(x_values, y_values)
ax[1].title.set_text('If y = 0')
ax[1].set_xlabel('h(x) - sigmoid output')
ax[1].set_ylabel('Cost')