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

семестр 1 / lab16

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

def hypothesis(x, theta):
h = theta[0] + theta[1]*x
return h

def J(y, h):
qdc_err = (h - y)**2
J = 1/2 * qdc_err
return J

def theta_0(theta_0, alpha, y, h, m):
err = 0
for i in range(0, len(h)):
err += h[i] - y[i]
theta = theta_0 - alpha/m * err
return theta

def theta_1(theta_0, alpha, x, y, h, m):
err = 0
for i in range(0, len(h)):
err += (h[i] - y[i]) * x[i]
theta = theta_0 - alpha/m * err
return theta

#Input from file
all_data = [[],[]]
headings = []
count = 0
i = 0

f = open('student-mat-2.csv', mode='r', encoding='cp1251')
for line in f:
if count == 0:
headings.extend(line.split(','))
elif count > 0:
tmp = line.split(',')
all_data[0].append(float(tmp[2]))
all_data[1].append(float(tmp[12]))
count += 1
f.close()
count -= 1

#Parameter and result matrices
parameter = np.array(all_data[0])
parameter.shape = (count,1)

result = np.array(all_data[1])
result.shape = (count,1)

plt.figure(1)
plt.scatter(parameter,result)
plt.axis([np.min(parameter)-1, np.max(parameter)+1, np.min(result)-1, np.max(result)+1])
plt.xlabel("Степень образованности отца")
plt.ylabel("Результат экзамена ребёнка")
plt.grid(which='major')
plt.minorticks_on()
plt.grid(which='minor', linestyle=':')
plt.tight_layout()

#First hypothesis
theta = [random.random(), random.random()]
cost_func = 0
h = []

while i != count:
h.append(hypothesis(parameter[i], theta))
cost_func += J(result[i], h[i])
i += 1
i = 0
cost_func = cost_func / count

plt.figure(1)
plt.plot(parameter,h)

plt.figure(2)
plt.scatter(theta[0], cost_func, c="b", alpha=1)
plt.axis([-1, 10, 0, cost_func+5])
plt.xlabel("Тета нулевая")
plt.ylabel("Функция стоимости")
plt.grid(which='major')
plt.minorticks_on()
plt.grid(which='minor', linestyle=':')
plt.tight_layout()

plt.figure(3)
plt.scatter(theta[1], cost_func, c="b", alpha=1)
plt.axis([-1, 8, 0, cost_func+5])
plt.xlabel("Тета первая")
plt.ylabel("Функция стоимости")
plt.grid(which='major')
plt.minorticks_on()
plt.grid(which='minor', linestyle=':')
plt.tight_layout()

#Finding the minimum of cost function
alpha = 0.1
iter_number = 500
cost_func_arr = [cost_func]
iter_number_arr = [0]

i = 1
j = 0
while i != iter_number:
theta[0] = theta_0(theta[0], alpha, result, h, count)
theta[1] = theta_1(theta[1], alpha, parameter, result, h, count)
h = []
cost_func = 0

while j != count:
h.append(hypothesis(parameter[j], theta))
cost_func += J(result[j], h[j])
j += 1
j = 0
cost_func = cost_func / count

plt.figure(2)
plt.scatter(theta[0], cost_func, c="b", alpha=0.5)

plt.figure(3)
plt.scatter(theta[1], cost_func, c="b", alpha=0.5)

cost_func_arr.append(cost_func)
iter_number_arr.append(i)

if i >= iter_number-10:
plt.figure(1)
plt.plot(parameter, h, alpha=0.5)
i += 1
i = 0

#Plotting
plt.figure(1)
plt.plot(parameter,h, c="k")

plt.figure(4)
plt.plot(iter_number_arr, cost_func_arr)
plt.xlabel("Количество итераций")
plt.ylabel("Функция стоимости")
plt.grid(which='major')
plt.minorticks_on()
plt.grid(which='minor', linestyle=':')
plt.tight_layout()

plt.figure(1)
plt.figure(2)
plt.figure(3)
plt.figure(4)
plt.show()

#Линейный коэффициент корреляции по Пирсону
#x_av_s = 0
#y_av_s = 0
#
#for i in parameter:
# x_av_s += i
#for i in result:
# y_av_s += i
#
#x_av = x_av_s / count
#y_av = y_av_s / count
#
#cov_1 = 0
#for i in range(0,count):
# cov_1 += (parameter[i]-x_av)*(result[i]-y_av)
#
#x_1 = 0
#for i in range(0,count):
# x_1 += (parameter[i]-x_av)**2
#
#y_1 = 0
#for i in range(0,count):
# y_1 += (result[i]-y_av)**2
#
#cov_2 = 0
#for i in range(0,count):
# cov_2 = (x_1*y_1)**0.5
#
#print("Коэффициент линейной корреляции Пирсона:", cov_1/cov_2)
Соседние файлы в папке семестр 1