Created
December 27, 2021 17:44
-
-
Save kirilltobola/c516561eee1af8285068563cd09c13f4 to your computer and use it in GitHub Desktop.
ter inf 5, 6 lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import formulas as f | |
matrix: list = [ | |
[1/16, 6/16, 1/16], | |
[3/16, 2/16, 3/16], | |
] | |
if __name__ == '__main__': | |
h_a, h_b = f.H(matrix) | |
h_b_if_a = f.H_B_if_A(matrix) | |
h_ab = f.H_AB(h_a, h_b_if_a) | |
I_B = f.I_B(h_b, h_b_if_a) | |
I_A = f.I_A(h_a, h_b, h_ab) | |
print( | |
f'I_B = {I_B} == I_A = {I_A}' | |
) | |
mean_x, mean_y, mean_xy = f.mean(matrix) | |
print( | |
f'MX = {mean_x} MY = {mean_y} MXY = {mean_xy}' | |
) | |
cov = f.covariation(mean_x, mean_y, mean_xy) | |
dispersion_x, dispersion_y = f.dispersion(matrix, mean_x, mean_y) | |
correlation = f.correlation( | |
cov, | |
f.standart_deviation(dispersion_x), | |
f.standart_deviation(dispersion_y) | |
) | |
print( | |
f'COV = {cov} DX = {dispersion_x} DY = {dispersion_y} COR = {correlation}' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import formulas as f | |
matrix: list = [ | |
[1/36, 0, 0, 0, 0, 0], | |
[0, 2/36, 0, 0, 0, 0], | |
[0, 1/36, 2/36, 0, 0, 0], | |
[0, 0, 2/36, 2/36, 0, 0], | |
[0, 0, 1/36, 2/36, 2/36, 0], | |
[0, 0, 0, 2/36, 2/36, 2/36], | |
[0, 0, 0, 1/36, 2/36, 2/36], | |
[0, 0, 0, 0, 2/36, 2/36], | |
[0, 0, 0, 0, 1/36, 2/36], | |
[0, 0, 0, 0, 0, 2/36], | |
[0, 0, 0, 0, 0, 1/36], | |
] | |
if __name__ == '__main__': | |
mean_x, mean_y, mean_xy = f.mean(matrix) | |
print(f'MX = {mean_x} MY = {mean_y}') | |
dispersion_x, dispersion_y = f.dispersion(matrix, mean_x, mean_y) | |
print(f'DX = {dispersion_x} DY = {dispersion_y}') | |
correlation = f.correlation( | |
f.covariation(mean_x, mean_y, mean_xy), | |
f.standart_deviation(dispersion_x), | |
f.standart_deviation(dispersion_y) | |
) | |
print(f'cor = {correlation}') | |
print( | |
f'r(SM)(DS/DM)^1/2 = {correlation * ((dispersion_x/dispersion_y) ** 0.5)}' | |
) | |
h_s, h_m = f.H(matrix) | |
print(f'HS = {h_s} HM = {h_m}') | |
i_m = f.I_B(h_m, f.H_B_if_A(matrix)) | |
print(f'IM = {i_m}') | |
h_s_if_m = f.H_A_if_B(matrix, True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from pandas.core.frame import DataFrame | |
import formulas as f | |
def init(file_name: str) -> DataFrame: | |
return pd.read_csv(file_name) | |
def task1(data: DataFrame, n_columns: int) -> None: | |
for i in range(n_columns): | |
item = data.columns[i] | |
unique_elems = ' '.join(data[item].unique()) | |
print(f'{item} : {unique_elems}') | |
def task2(data: DataFrame) -> None: | |
for i in range(1, 4): | |
column = data.columns[-i] | |
data[column] = data[column].map(range_marks) | |
#print(data) | |
def range_marks(x) -> str: | |
if x < 50: | |
return 'очень плохо' | |
elif x < 60: | |
return 'плохо' | |
elif x < 66: | |
return 'посредственно' | |
elif x < 72: | |
return 'хорошо' | |
elif x < 78: | |
return 'очень хорошо' | |
elif x < 85: | |
return 'отлично' | |
elif x <= 100: | |
return 'превосходно' | |
else: | |
return 'unexpected value' | |
def task3(data: DataFrame, row: str, col: str) -> DataFrame: | |
unique_row = data[row].unique() | |
unique_col = data[col].unique() | |
res = pd.DataFrame(columns=unique_col, index=unique_row) | |
for r in unique_row: | |
for c in unique_col: | |
res.at[r, c] = len( | |
data[(data[row] == r) & (data[col] == c)] | |
) / len(data) | |
#print(res) | |
return res | |
def task4(data: DataFrame) -> float: | |
h_a, h_b = f.H(data.values.tolist()) | |
h_b_if_a = f.H_B_if_A(data.values.tolist()) | |
return f.H_AB(h_a, h_b_if_a) | |
def task5(data: DataFrame) -> float: | |
h_a, h_b = f.H(data.values.tolist()) | |
h_b_if_a = f.H_B_if_A(data.values.tolist()) | |
h_ab = f.H_AB(h_a, h_b_if_a) | |
return f.I_A(h_a, h_b, h_ab) | |
def test(data: DataFrame, to_test: list) -> None: | |
for testing in to_test: | |
for test in data.columns: | |
if testing != test: | |
I = task5(task3(data, testing, test)) | |
alias = '-'.join([testing, test]) | |
print(f'{alias}: {I:.5f}') | |
print() | |
if __name__ == '__main__': | |
data = init('StudentsPerformance.csv') | |
#task1(data, 5) | |
task2(data) | |
table = task3(data, 'gender', 'math score') | |
res = task4(table) | |
print(f'H(AB) = {res}') | |
test( | |
data, | |
['math score', 'reading score', 'writing score'] | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def __get_matrix(matrix: list) -> tuple: | |
return len(matrix), len(matrix[0]) | |
def mean(matrix: list) -> tuple: | |
n, m = __get_matrix(matrix) | |
mean_x, mean_y, mean_xy = 0, 0, 0 | |
for i in range(n): | |
for j in range(m): | |
mean_x += i * matrix[i][j] | |
mean_y += j * matrix[i][j] | |
mean_xy += i*j * matrix[i][j] | |
return mean_x, mean_y, mean_xy | |
def covariation(mean_x: float, mean_y: float, mean_xy: float) -> float: | |
return mean_xy - mean_x*mean_y | |
def dispersion(matrix: list, mean_x: float, mean_y: float) -> tuple: | |
n, m = __get_matrix(matrix) | |
d_x, d_y = 0, 0 | |
for i in range(n): | |
for j in range(m): | |
d_x += i*i * matrix[i][j] | |
d_y += j*j * matrix[i][j] | |
d_x -= mean_x ** 2 | |
d_y -= mean_y ** 2 | |
return d_x, d_y | |
def standart_deviation(dispersion: float) -> float: | |
return math.sqrt(dispersion) | |
def correlation(cov: float, deviation_x: float, deviation_y: float) -> float: | |
return cov / (deviation_x * deviation_y) | |
def H(matrix: list) -> tuple: | |
n, m = __get_matrix(matrix) | |
H_A = 0 | |
for row in matrix: | |
a_i = sum(row) | |
if a_i: | |
H_A += a_i * math.log2(a_i) | |
H_B = 0 | |
for i in range(m): | |
b_i = 0 | |
for j in range(n): | |
b_i += matrix[j][i] | |
if b_i: | |
H_B += b_i * math.log2(b_i) | |
return -H_A, -H_B | |
def H_B_if_A(matrix: list) -> float: | |
n, m = __get_matrix(matrix) | |
res = 0 | |
for i in range(n): | |
H = 0 | |
for j in range(m): | |
p = matrix[i][j] / sum(matrix[i]) | |
if p: | |
H += p * math.log2(p) | |
res += sum(matrix[i]) * H | |
return -res | |
def H_A_if_B(matrix: list, prnt: bool=False) -> float: | |
n, m = __get_matrix(matrix) | |
res = 0 | |
for i in range(m): | |
H = 0 | |
sum = 0 | |
for j in range(n): | |
sum += matrix[j][i] | |
for j in range(n): | |
p = matrix[j][i] / sum | |
if p: | |
H += p * math.log2(p) | |
res += H | |
if prnt: | |
print(f'M = {i} | {-H}') | |
return -res | |
def H_AB(H: float, H2: float) -> float: | |
"""H_A + H_Bifa or HB + H_AifB""" | |
return H + H2 | |
def I_A(h_a: float, h_b: float, h_ab: float) -> float: | |
return h_a + h_b - h_ab | |
def I_B(h_b: float, h_b_if_a: float) -> float: | |
return h_b - h_b_if_a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import formulas as f | |
matrix1: list = [ | |
] | |
matrix2: list = [ | |
[1/36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 3/36, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 3/36, 0, 0, 2/36, 0, 0, 0, 0, 0], | |
[0, 0, 0, 5/36, 0, 0, 0, 2/36, 0, 0, 0], | |
[0, 0, 0, 0, 3/36, 0, 2/36, 0, 2/36, 2/36, 0], | |
[0, 0, 0, 0, 0, 7/36, 0, 2/36, 0, 0, 2/36], | |
] | |
matrix3: list = [ | |
[0, 1/32, 0, 0, 0, 0], | |
[1/32, 0, 8/32, 4/32, 2/32, 1/32], | |
[0, 8/32, 0, 0, 0, 0], | |
[0, 4/32, 0, 0, 0, 0], | |
[0, 2/32, 0, 0, 0, 0], | |
[0, 1/32, 0, 0, 0, 0], | |
] | |
if __name__ == "__main__": | |
matrix = matrix2 | |
h_a, h_b = f.H(matrix) | |
print( | |
f'H(A) = {h_a} H(B) = {h_b}' | |
) | |
h_ab = f.H_AB(h_b, f.H_A_if_B(matrix)) | |
print(f'H(AB) = {h_ab}') | |
i_a = f.I_A(h_a, h_b, h_ab) | |
print(f'I_A(B) = {i_a}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment