Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

семестр 2 / лабы / lab2_alt

.py
Скачиваний:
0
Добавлен:
13.05.2026
Размер:
5.07 Кб
Скачать
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.optimize as spop

data_2 = pd.read_csv('/content/opuxol.csv', header=None, encoding='latin-1', names=['id', 'diagnosis', 'radius_mean', 'texture_mean', 'perimeter_mean', 'area_mean', 'smoothness_mean', 'compactness_mean', 'concavity_mean', 'concave points_mean', 'symmetry_mean', 'fractal_dimension_mean', 'radius_se', 'texture_se', 'perimeter_se', 'area_se', 'smoothness_se', 'compactness_se', 'concavity_se', 'concave points_se', 'symmetry_se', 'fractal_dimension_se', 'radius_worst', 'texture_worst', 'perimeter_worst', 'area_worst', 'smoothness_worst', 'compactness_worst', 'concavity_worst', 'concave points_worst', 'symmetry_worst', 'fractal_dimension_worst', ''], delimiter=',')
data_1 = pd.DataFrame(data_2)
data_1.drop(index=data_1.index[0], axis=0, inplace=True)
data = data_1[['area_mean', 'symmetry_mean', 'diagnosis']]
X = np.ones((data['area_mean'].size, 2))
X[:, 0], X[:, 1] = data['area_mean'], data['symmetry_mean']
# maxi = np.ones((2, 1))
# mins = np.ones((2, 1))
# X1 = np.ones((data['area_mean'].size, 2))
# for i in range(0, 2):
# maxi[i] = np.max(X.T[i])
# mins[i] = np.min(X.T[i])
# for p in range(len(data['area_mean'])):
# for o in range(0, 2):
# X1[p][o] = (X[p][o] - mins[o]) / (maxi[o] - mins[o])
# X = X1
X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)
X = np.hstack((np.ones((X.shape[0], 1)), X))
diagnosis_to_float = []
for diag in data['diagnosis']:
if 'M' in diag:
diagnosis_to_float.append(1)
elif 'B' in diag:
diagnosis_to_float.append(0)

def hypothesis(theta, X):
return 1 / (1 + np.exp(-np.dot(X, theta)))

n = len(data['area_mean'])

def cost_function(theta, X, y):
h = hypothesis(theta, X)
return -(1 / n) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))

def gradient(theta, X, y):
h = hypothesis(theta, X)
return (1 / len(y)) * np.dot(X.T, (h - y))

def gradient_descent(X, y, theta, alpha, num_iters):
J_history = []
theta_history = []
for i in range(num_iters):
cost = cost_function(theta, X, y)
grad = gradient(theta, X, y)
theta = theta - alpha * grad
J_history.append(cost)
theta_history.append(theta)
return theta, J_history, theta_history

y = np.array(diagnosis_to_float)
theta = np.zeros(X.shape[1])
alpha = 0.2
num_iters = 2500
theta, J_history, theta_history = gradient_descent(X, y, theta, alpha, num_iters)

print('Коэффициенты: ', theta)

plt.plot(J_history)
plt.xlabel('Номер итерации')
plt.ylabel('Значение стоимости')
plt.title('График значений стоимости')
plt.show()

theta_history = np.array(theta_history)
plt.plot(theta_history[:, 0], label='theta 0')
plt.plot(theta_history[:, 1], label='theta 1')
plt.plot(theta_history[:, 2], label='theta 2')
plt.xlabel('Номер итерации')
plt.ylabel('Значение коэффициента')
plt.title('График значений коэффициентов theta')
plt.legend()
plt.show()

grid_n = 100
x1_p = np.linspace(X[:, 1].min(), X[:, 1].max(), grid_n)
x2_p = np.linspace(X[:, 2].min(), X[:, 2].max(), grid_n)
x1_grid, x2_grid = np.meshgrid(x1_p, x2_p)
X_grid = np.c_[np.ones(x1_grid.size), x1_grid.flatten(), x2_grid.flatten()]

h_grid = hypothesis(theta, X_grid)

h_grid = h_grid.reshape((grid_n, grid_n))

plt.scatter(X[:, 1][y == 0], X[:, 2][y == 0], color='blue', label='Доброкачественная')
plt.scatter(X[:, 1][y == 1], X[:, 2][y == 1], color='red', label='Злокачественная')
plt.contour(x1_grid, x2_grid, h_grid, levels=[0.5], colors='black')
plt.xlabel('Средняя площадь')
plt.ylabel('Средняя симметрия')
plt.title('График исходных данных и границы решения')
plt.legend()
plt.show()
# Коэффициенты: [-0.26750713 4.61544797 1.28497749] a = 0.1, num_iters = 5000 топ эффективность масштабирования
# Коэффициенты: [-0.26751148 4.615412 1.28497019] a = 0.05, num_iters = 10000 топ эффективность масштабирования
# Коэффициенты: [-0.26483518 4.63755928 1.28946725] a = 0.1, num_iters = 10000 топ эффективность масштабирования
# Коэффициенты: [-0.26749842 4.61551992 1.2849921 ] a = 0.2, num_iters = 2500 топ эффективность масштабирования
# Коэффициенты: [-9.9673819 29.1284285 8.83930387] a = 0.2, num_iters = 50000 неэффективным нормированием
# Коэффициенты: [-10.45544309 30.78024015 9.24440712] a = 0.2, num_iters = 100000 неэффективным нормированием
# Коэффициенты: [-10.54623035 31.0876217 9.31960247] a = 0.2, num_iters = 200000 неэффективным нормированием
Соседние файлы в папке лабы