In [1]:
import pandas as pd
import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt
fin_nm = input('Input Data File (*csv) = ')
fout_nm = input('Output file (*.txt) = ')
fout = open(fout_nm, 'w')
r_data = pd.read_csv(fin_nm, header=None)
fout.write(f'Input data file = {fin_nm}\n')
print(r_data)
fout.write(f'\nInput Data...\n{r_data}\n')
var_names = r_data.values[0][1:]
n_vars = len(var_names)
CaseID = r_data.values[:,0][1:]
n_data = len(CaseID)
print('n_data =', n_data)
X = r_data.values[1:,1:]
X = np.array(X, dtype=float)
Z = ss.zscore(X, axis=0) # Standardizaion
0 1 2 3 4 5 6 7 \ 0 ID Q1-0 Q1-1 Q2-0 Q2-1 Q3-0 Q3-1 Q4-0 1 39D 0.79057 1.27475 -1.48663 0.83062 1.22508 1.35371 1.69367 2 40D 0.79057 1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 3 41D 0.79057 1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 4 42D -1.26491 0.00000 -1.48663 0.83062 -0.87731 0.05524 -0.65002 5 43D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 6 44D 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 7 45D 0.79057 1.27475 -1.48663 0.83062 -0.87731 0.05524 -0.65002 8 46D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 1.69367 9 47D 0.79057 1.27475 -1.48663 0.83062 1.22508 1.35371 1.69367 10 48D 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 11 49D -1.26491 0.00000 0.77081 0.17566 1.03440 -1.67482 1.69367 12 50D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 13 51D -1.26491 0.00000 -0.72662 -2.50440 1.22508 1.35371 -0.65002 14 52D 0.79057 1.27475 -1.48663 0.83062 -0.87731 0.05524 -0.65002 15 53D 0.79057 1.27475 0.77081 0.17566 1.03440 -1.67482 -0.65002 16 54D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 17 55D 0.79057 1.27475 -1.48663 0.83062 1.03440 -1.67482 -0.65002 18 56D 0.79057 1.27475 -0.72662 -2.50440 1.03440 -1.67482 0.76920 19 57D 0.79057 -1.27475 -1.48663 0.83062 1.03440 -1.67482 -0.65002 20 58D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 21 59A -1.26491 0.00000 -1.48663 0.83062 -0.87731 0.05524 -0.65002 22 60A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 23 61A -1.26491 0.00000 0.77081 0.17566 1.03440 -1.67482 -0.65002 24 62A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 0.76920 25 63A 0.79057 -1.27475 -0.72662 -2.50440 1.22508 1.35371 1.69367 26 64A 0.79057 1.27475 -1.48663 0.83062 1.03440 -1.67482 0.76920 27 65A 0.79057 -1.27475 0.77081 0.17566 1.22508 1.35371 1.69367 28 66A -1.26491 0.00000 0.77081 0.17566 1.22508 1.35371 1.69367 29 67A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 30 68A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 31 69A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 32 70A -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 1.69367 33 71A -1.26491 0.00000 -1.48663 0.83062 1.03440 -1.67482 -0.65002 34 72A -1.26491 0.00000 -0.72662 -2.50440 1.22508 1.35371 1.69367 35 73A -1.26491 0.00000 0.77081 0.17566 1.22508 1.35371 -0.65002 36 74A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 37 75A 0.79057 1.27475 0.77081 0.17566 1.22508 1.35371 -0.65002 38 76A 0.79057 1.27475 -0.72662 -2.50440 -0.87731 0.05524 -0.65002 39 77A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 8 9 10 11 12 0 Q4-1 Q5-0 Q5-1 Q6 Q7 1 0.68177 -0.58248 -0.07445 10 0 2 0.14803 -0.58248 -0.07445 40 0 3 0.14803 -0.58248 -0.07445 0 90 4 0.14803 -0.58248 -0.07445 60 80 5 0.14803 -0.58248 -0.07445 80 0 6 0.14803 -0.58248 -0.07445 80 80 7 0.14803 -0.58248 -0.07445 20 10 8 0.68177 -0.58248 -0.07445 20 0 9 0.68177 -0.58248 -0.07445 20 50 10 0.14803 -0.58248 -0.07445 60 45 11 0.68177 -0.58248 -0.07445 50 50 12 0.14803 1.89362 -1.38354 80 30 13 0.14803 1.89362 -1.38354 80 80 14 0.14803 -0.58248 -0.07445 80 75 15 0.14803 -0.58248 -0.07445 90 60 16 0.14803 1.38256 2.61506 50 50 17 0.14803 -0.58248 -0.07445 50 50 18 -3.37762 -0.58248 -0.07445 -20 0 19 0.14803 1.89362 -1.38354 70 50 20 0.14803 -0.58248 -0.07445 0 0 21 0.14803 -0.58248 -0.07445 80 80 22 0.14803 1.38256 2.61506 80 40 23 0.14803 -0.58248 -0.07445 70 50 24 -3.37762 -0.58248 -0.07445 90 50 25 0.68177 -0.58248 -0.07445 0 0 26 -3.37762 -0.58248 -0.07445 100 90 27 0.68177 -0.58248 -0.07445 90 80 28 0.68177 1.89362 -1.38354 -100 -100 29 0.14803 -0.58248 -0.07445 80 50 30 0.14803 1.89362 -1.38354 100 80 31 0.14803 1.38256 2.61506 65 25 32 0.68177 -0.58248 -0.07445 60 60 33 0.14803 -0.58248 -0.07445 50 50 34 0.68177 1.89362 -1.38354 -90 0 35 0.14803 -0.58248 -0.07445 50 70 36 0.14803 -0.58248 -0.07445 60 70 37 0.14803 -0.58248 -0.07445 80 100 38 0.14803 1.38256 2.61506 0 -10 39 0.14803 -0.58248 -0.07445 50 0 n_data = 39
In [2]:
#
# Singular Value Decomposition
#
U, S, Vh = np.linalg.svd(Z / (n_data**0.5), full_matrices=False)
print('S =\n', S)
fout.write(f'\nSingular values...\n{S}\n')
plt.plot(np.arange(len(S)) + 1, S)
plt.title('Scree Plot of Singular Values')
plt.ylabel('Singular Value')
plt.tight_layout()
plt.savefig('FigScreePlot.png')
plt.show()
Pat = Vh.T @ np.diag(S) # Pattern Matrix
print('Pat =\n', Pat)
fout.write('\nPattern matrix...\n')
for i, v in enumerate(Pat):
fout.write(f'{var_names[i]:>8s}')
for vv in v:
fout.write(f' {vv:>8.3f}')
fout.write('\n')
S = [1.66847039 1.40764817 1.15939721 1.05814652 1.03339498 0.98129721 0.83306725 0.79109174 0.72968262 0.65983557 0.52202777 0.42408479]
Pat = [[-3.14421151e-01 -2.02685062e-01 4.40492435e-01 -1.78618653e-01 5.11051355e-01 -3.77760734e-01 3.95209801e-01 4.10165677e-02 -2.53165061e-01 2.02817367e-02 8.80399381e-02 -1.07314483e-02] [ 1.63249947e-01 -6.54322107e-01 1.41851078e-01 4.87455187e-01 -1.64410205e-01 -1.50059534e-01 -2.02456212e-01 -2.48073682e-01 -1.75269698e-01 2.85780491e-01 1.30385134e-01 7.75232818e-02] [-1.45051209e-01 6.79559781e-01 -3.61987895e-02 -7.58756091e-02 2.60332659e-01 4.23375004e-01 -2.00151084e-01 -8.15224710e-02 -4.00059375e-01 2.32484143e-01 3.28203680e-02 -3.45221231e-02] [-5.58208809e-01 -1.03113556e-01 -4.28643896e-01 4.33098098e-01 6.60763954e-02 1.13579454e-01 3.48034160e-01 2.24465784e-01 9.81990991e-02 2.97696532e-01 -1.11867364e-01 -8.30742237e-02] [ 4.66891415e-01 -5.58679513e-01 -2.58996654e-01 -3.15896064e-01 -2.11382813e-03 -1.02946788e-01 -1.71784665e-01 3.30701553e-01 -3.01032655e-01 5.56145012e-02 -2.44592029e-01 -2.26436980e-03] [ 4.55525955e-01 4.05002531e-01 -2.13235984e-01 1.58628545e-02 4.11398559e-01 -4.64151924e-01 -8.52498752e-02 -2.96601893e-01 1.88344547e-01 1.71030047e-01 -1.92780461e-01 3.05381798e-02] [ 6.45524890e-01 -2.23886816e-01 -1.50300352e-01 -1.03815448e-01 4.76608522e-01 2.19694013e-01 -7.95450893e-02 2.64039567e-01 2.51359461e-01 1.17787069e-01 2.63879439e-01 4.06359464e-02] [ 2.48106512e-01 4.56826874e-01 -4.91885053e-01 4.13387440e-01 -4.20394306e-02 -3.69951138e-01 1.55436823e-02 2.10005116e-01 -2.41016620e-01 -2.18486736e-01 1.62146859e-01 4.26650145e-02] [ 3.33881042e-01 4.44826743e-01 1.64594859e-01 -3.44822441e-01 -5.44959602e-01 -2.62502911e-01 1.58972332e-01 1.69915170e-01 4.23337389e-02 3.33879996e-01 1.05442105e-01 -1.55076603e-02] [-2.76636243e-01 2.60116031e-01 6.14674802e-01 3.41580015e-01 1.10715181e-01 -1.64485920e-01 -3.81191425e-01 3.94446356e-01 1.12916109e-01 2.26921374e-02 -8.35147141e-02 -2.93603980e-02] [-8.67133969e-01 2.48075053e-02 -2.13520125e-01 -2.49498443e-01 5.72884188e-04 -8.31504850e-02 -1.18508718e-01 7.69310858e-02 5.07990581e-02 6.49641742e-02 1.49987055e-02 3.25146216e-01] [-6.94239958e-01 -1.96744194e-01 -3.42641607e-01 -2.78007448e-01 1.21004505e-02 -3.02547084e-01 -3.28121245e-01 -6.35587064e-02 5.87067808e-02 2.15269416e-02 1.51456143e-01 -2.33233601e-01]]
In [3]:
dimx = 1
dimy = 2
while True:
plt.scatter(Pat.T[dimx-1], Pat.T[dimy-1])
plt.xlabel(f'Dim.{dimx}', fontsize=12)
plt.ylabel(f'Dim.{dimy}', fontsize=12)
plt.xticks([-1, 0, 1])
plt.yticks([-1, 0, 1])
plt.plot([-1, 1], [0, 0])
plt.plot([0,0], [-1, 1])
for i in range(n_vars):
plt.text(Pat.T[dimx-1][i], Pat.T[dimy-1][i], var_names[i])
plt.tight_layout()
sf_nm = f'Fig_{dimx}_{dimy}.png'
plt.savefig(sf_nm)
plt.show()
print(sf_nm, 'was saved.')
print('\nFor the next plot')
dimx = int(input(f'dimx(<={len(S)}) = '))
if dimx < 1:
break
if dimx > len(S):
dimx = len(S)
dimy = int(input(f'dimy(<={len(S)}) = '))
if dimy < 1:
break
if dimy > len(S):
dimy = len(S)
fout.close()
print(f'\n{fout_nm} was saved.')
Fig_1_2.png was saved. For the next plot
Fig_1_3.png was saved. For the next plot
temp.txt was saved.
In [ ]: