|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import messagebox, filedialog |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +class CorrelationApp: |
| 7 | + def __init__(self, master): |
| 8 | + self.master = master |
| 9 | + self.master.title("Correlation Coefficient App") |
| 10 | + |
| 11 | + self.x_values = [] |
| 12 | + self.y_values = [] |
| 13 | + |
| 14 | + self.create_widgets() |
| 15 | + |
| 16 | + def create_widgets(self): |
| 17 | + # Input fields |
| 18 | + tk.Label(self.master, text="X Coordinate:").grid(row=0, column=0) |
| 19 | + tk.Label(self.master, text="Y Coordinate:").grid(row=1, column=0) |
| 20 | + |
| 21 | + self.x_entry = tk.Entry(self.master) |
| 22 | + self.y_entry = tk.Entry(self.master) |
| 23 | + |
| 24 | + self.x_entry.grid(row=0, column=1) |
| 25 | + self.y_entry.grid(row=1, column=1) |
| 26 | + |
| 27 | + # Buttons |
| 28 | + tk.Button(self.master, text="Add", command=self.add_data).grid(row=2, column=0, columnspan=2, pady=10) |
| 29 | + tk.Button(self.master, text="Calculate", command=self.calculate).grid(row=3, column=0, columnspan=2, pady=10) |
| 30 | + tk.Button(self.master, text="Load Data", command=self.load_data).grid(row=4, column=0, columnspan=2, pady=10) |
| 31 | + |
| 32 | + # Result area |
| 33 | + self.result_text = tk.Text(self.master, height=10, width=40) |
| 34 | + self.result_text.grid(row=5, column=0, columnspan=2) |
| 35 | + |
| 36 | + def add_data(self): |
| 37 | + x_value = self.x_entry.get() |
| 38 | + y_value = self.y_entry.get() |
| 39 | + |
| 40 | + try: |
| 41 | + x_value = float(x_value) |
| 42 | + y_value = float(y_value) |
| 43 | + |
| 44 | + self.x_values.append(x_value) |
| 45 | + self.y_values.append(y_value) |
| 46 | + |
| 47 | + self.result_text.insert(tk.END, f"Added: ({x_value}, {y_value})\n") |
| 48 | + |
| 49 | + # Clear entries |
| 50 | + self.x_entry.delete(0, tk.END) |
| 51 | + self.y_entry.delete(0, tk.END) |
| 52 | + |
| 53 | + except ValueError: |
| 54 | + messagebox.showerror("Error", "Invalid input. Please enter valid numbers.") |
| 55 | + |
| 56 | + def calculate(self): |
| 57 | + if not self.x_values or not self.y_values: |
| 58 | + messagebox.showerror("Error", "Please add data points before calculating.") |
| 59 | + return |
| 60 | + |
| 61 | + x_mean = np.mean(self.x_values) |
| 62 | + y_mean = np.mean(self.y_values) |
| 63 | + x_std = np.std(self.x_values) |
| 64 | + y_std = np.std(self.y_values) |
| 65 | + |
| 66 | + covariance = np.cov(self.x_values, self.y_values)[0, 1] |
| 67 | + correlation_coefficient = covariance / (x_std * y_std) |
| 68 | + |
| 69 | + result = f"Arithmetic Mean (X): {x_mean:.2f}\n" \ |
| 70 | + f"Arithmetic Mean (Y): {y_mean:.2f}\n" \ |
| 71 | + f"Standard Deviation (X): {x_std:.2f}\n" \ |
| 72 | + f"Standard Deviation (Y): {y_std:.2f}\n" \ |
| 73 | + f"Pearson Correlation Coefficient: {correlation_coefficient:.2f}\n" |
| 74 | + |
| 75 | + self.result_text.delete(1.0, tk.END) # Clear previous results |
| 76 | + self.result_text.insert(tk.END, result) |
| 77 | + |
| 78 | + # Bonus: Scatter plot |
| 79 | + plt.scatter(self.x_values, self.y_values) |
| 80 | + plt.title("Scatter Plot") |
| 81 | + plt.xlabel("X Coordinate") |
| 82 | + plt.ylabel("Y Coordinate") |
| 83 | + plt.show() |
| 84 | + |
| 85 | + def load_data(self): |
| 86 | + file_path = filedialog.askopenfilename(title="Select Data File", filetypes=[("Text files", "*.txt")]) |
| 87 | + |
| 88 | + if not file_path: |
| 89 | + return |
| 90 | + |
| 91 | + try: |
| 92 | + data = np.loadtxt(file_path, delimiter=',') |
| 93 | + self.x_values.extend(data[:, 0]) |
| 94 | + self.y_values.extend(data[:, 1]) |
| 95 | + |
| 96 | + self.result_text.insert(tk.END, f"Loaded data from file: {file_path}\n") |
| 97 | + except Exception as e: |
| 98 | + messagebox.showerror("Error", f"Error loading data from file: {e}") |
| 99 | + |
| 100 | +# Main |
| 101 | +if __name__ == "__main__": |
| 102 | + root = tk.Tk() |
| 103 | + app = CorrelationApp(root) |
| 104 | + root.mainloop() |
0 commit comments