-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode Improved performance.py
More file actions
128 lines (120 loc) · 4.64 KB
/
Copy pathCode Improved performance.py
File metadata and controls
128 lines (120 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def read_csv(filepath):
"""Reads CSV data from file and returns header and all data as rows of lists."""
with open(filepath, 'r', encoding='utf-8-sig') as f: # Handles UTF-8 BOM
header = f.readline().strip().split(',')
all_data = []
for line in f:
row = line.strip().split(',')
if len(row) == len(header): # Only include rows with correct length
all_data.append(row)
return header, all_data
def read_column(filepath, column_name):
"""Reads a single column from CSV file by column name."""
header, all_data = read_csv(filepath)
if column_name not in header:
raise ValueError(f"{column_name} not found in header {header}")
col_idx = header.index(column_name)
return [row[col_idx] for row in all_data]
def build_data_dict(header, all_data):
"""Creates a dictionary mapping column names to lists of cleaned data."""
data_dict = {col: [] for col in header}
for col_idx, col_name in enumerate(header):
for row in all_data:
val = row[col_idx]
try:
if '.' in val:
val = float(val)
else:
val = int(val)
except ValueError:
pass # Keep as string if conversion fails
data_dict[col_name].append(val)
return data_dict
def kendall_tau(x, y):
"""Calculates Kendall Tau coefficient for two equal-length lists."""
if len(x) != len(y):
raise ValueError("Lists must be the same length")
n = len(x)
concordant = discordant = 0
for i in range(n):
for j in range(i+1, n):
a, b = x[i] - x[j], y[i] - y[j]
prod = a * b
if prod > 0:
concordant += 1
elif prod < 0:
discordant += 1
if concordant + discordant == 0:
return 0
return (concordant - discordant) / (concordant + discordant)
def all_kendall_tau(data_dict):
"""Returns list of (col1, col2, tau) for all column pairs."""
cols = list(data_dict.keys())
coefs = []
for i in range(len(cols)):
for j in range(i+1, len(cols)):
col1, col2 = cols[i], cols[j]
try:
tau = kendall_tau(data_dict[col1], data_dict[col2])
except Exception as e:
tau = None
coefs.append((col1, col2, tau))
return coefs
def print_kendall_table(coefs, columns, border='*'):
"""Prints a square correlation matrix for selected columns."""
# Build lookup for fast access
coef_dict = {}
for c1, c2, tau in coefs:
coef_dict[(c1, c2)] = tau
coef_dict[(c2, c1)] = tau
# Determine cell width
cellw = max(max(len(col) for col in columns), 7) + 2
# Print header
hdr = border + ''.join([border + col.center(cellw) for col in columns]) + border
sep = border * len(hdr)
print(sep)
print(' ' * (cellw + 1) + hdr)
print(sep)
for row_col in columns:
row = [row_col.center(cellw)]
for col in columns:
if row_col == col:
cell = '-'.center(cellw)
else:
tau = coef_dict.get((row_col, col))
cell = f"{tau:.4f}".center(cellw) if tau is not None else "N/A".center(cellw)
row.append(cell)
print(border + ''.join([border + cell for cell in row]) + border)
print(sep)
def main():
csv_path = "C:/Users/aldom/Msc Data Science/Modules/Module 3 - Programming for Data Science/Assessment/Assessment resources/part_1.csv"
header, all_data = read_csv(csv_path)
print("Header:", header)
print("First row:", all_data[0])
# Build data dict
data_dict = build_data_dict(header, all_data)
# Example: print first 10 values of 'depression'
col = 'depression'
if col in data_dict:
print(f"First 10 values of '{col}': {data_dict[col][:10]}")
# Compute Kendall Tau for each pair
coefs = all_kendall_tau(data_dict)
# Print first 10 coefficients
print("\nFirst 10 Kendall Tau coefficients:")
for c1, c2, tau in coefs[:10]:
print(f"{c1:>12} vs {c2:>12}: {tau:.3f}")
# Print correlation matrix
print("\nKendall Tau Correlation Matrix:")
print_kendall_table(coefs, header)
# Optional: interactive column selection
# Uncomment below to allow user to select column
# print("\nAvailable columns:", header)
# colname = input("Enter column name to print first 10 values: ")
# if colname in data_dict:
# print(f"First 10 of {colname}: {data_dict[colname][:10]}")
# else:
# print(f"{colname} not found in columns.")
def test_sample():
assert 1 + 1 == 2
if __name__ == "__main__":
main()