|
1 | 1 | import numpy as np |
2 | | -import pandas as pd |
3 | | -import snmf |
| 2 | +import snmf_class |
4 | 3 |
|
5 | | -# Define fixed feature matrix (X) with distinct, structured features |
6 | | -X = np.array( |
7 | | - [ |
8 | | - [10, 0, 0], # First component dominates first feature |
9 | | - [0, 8, 0], # Second component dominates second feature |
10 | | - [0, 0, 6], # Third component dominates third feature |
11 | | - [4, 4, 0], # Mixed contribution to the fourth feature |
12 | | - [3, 2, 5], # Mixed contribution to the fifth feature |
13 | | - ], |
14 | | - dtype=float, |
15 | | -) |
16 | | - |
17 | | -# Define fixed coefficient matrix (Y) representing weights |
18 | | -Y = np.array( |
19 | | - [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]], |
20 | | - dtype=float, |
21 | | -) |
22 | | - |
23 | | -# Compute the resulting data matrix M |
24 | | -MM = np.dot(X, Y) |
25 | | - |
26 | | -# Normalize matrices X, Y, and M to be between 0 and 1 |
27 | | -X_norm = (X - X.min()) / (X.max() - X.min()) |
28 | | -Y_norm = (Y - Y.min()) / (Y.max() - Y.min()) |
29 | | -MM_norm = (MM - MM.min()) / (MM.max() - MM.min()) |
30 | | - |
31 | | -# Generate an initial guess Y0 with slightly perturbed values |
32 | | -Y0 = np.array( |
33 | | - [ |
34 | | - [1.5, 1.8, 2.9, 3.6, 4.8, 5.7, 7.1, 8.2, 9.4, 10.3], |
35 | | - [2.2, 4.1, 5.9, 8.1, 9.8, 11.9, 14.2, 16.5, 18.1, 19.7], |
36 | | - [2.7, 5.5, 8.8, 11.5, 14.6, 17.8, 20.5, 23.9, 26.3, 29.2], |
37 | | - ], |
38 | | - dtype=float, |
39 | | -) |
40 | | - |
41 | | -# Normalize Y0 as well |
42 | | -Y0_norm = (Y0 - Y0.min()) / (Y0.max() - Y0.min()) |
| 4 | +X0 = np.loadtxt("inputs/my_X0_4.txt", dtype=float) |
| 5 | +MM = np.loadtxt("inputs/my_MM_4.txt", dtype=float) |
| 6 | +A0 = np.loadtxt("inputs/my_A0_4.txt", dtype=float) |
| 7 | +Y0 = np.loadtxt("inputs/my_W0_4.txt", dtype=float) |
| 8 | +N, M = MM.shape |
43 | 9 |
|
44 | 10 | # Convert to DataFrames for display |
45 | | -df_X = pd.DataFrame(X, columns=[f"Comp_{i+1}" for i in range(X.shape[1])]) |
46 | | -df_Y = pd.DataFrame(Y, columns=[f"Sample_{i+1}" for i in range(Y.shape[1])]) |
47 | | -df_MM = pd.DataFrame(MM, columns=[f"Sample_{i+1}" for i in range(MM.shape[1])]) |
48 | | -df_Y0 = pd.DataFrame(Y0, columns=[f"Sample_{i+1}" for i in range(Y0.shape[1])]) |
| 11 | +# df_X = pd.DataFrame(X, columns=[f"Comp_{i+1}" for i in range(X.shape[1])]) |
| 12 | +# df_Y = pd.DataFrame(Y, columns=[f"Sample_{i+1}" for i in range(Y.shape[1])]) |
| 13 | +# df_MM = pd.DataFrame(MM, columns=[f"Sample_{i+1}" for i in range(MM.shape[1])]) |
| 14 | +# df_Y0 = pd.DataFrame(Y0, columns=[f"Sample_{i+1}" for i in range(Y0.shape[1])]) |
49 | 15 |
|
50 | 16 | # Print the matrices |
51 | 17 | """ |
|
55 | 21 | print("Initial Guess (Y0):\n", df_Y0, "\n") |
56 | 22 | """ |
57 | 23 |
|
58 | | -my_model = snmf.SNMFOptimizer(MM_norm, Y0_norm) |
| 24 | +my_model = snmf_class.SNMFOptimizer(MM=MM, Y0=Y0, X0=X0, A=A0) |
59 | 25 | print(f"My final guess for X: {my_model.X}") |
60 | 26 | print(f"My final guess for Y: {my_model.Y}") |
61 | | -print(f"Compare to true X: {X_norm}") |
62 | | -print(f"Compare to true Y: {Y_norm}") |
| 27 | +# print(f"Compare to true X: {X_norm}") |
| 28 | +# print(f"Compare to true Y: {Y_norm}") |
| 29 | +np.savetxt("my_new_X.txt", my_model.X, fmt="%.6g", delimiter=" ") |
| 30 | +np.savetxt("my_new_Y.txt", my_model.Y, fmt="%.6g", delimiter=" ") |
| 31 | +np.savetxt("my_new_A.txt", my_model.A, fmt="%.6g", delimiter=" ") |
0 commit comments