In [1]:
import pandas as pd
import numpy as np
import scipy.stats as ss
fin_nm = input('Input Data File (*.csv) = ') # Input CSV file
fout_nm = input('Output Test File (*.txt) = ') # Output file of SVD results
outcsv_nm = input('\nOutput CSV File (*.csv) = ') # Output file of transformed data
fout = open(fout_nm, 'w')
fout.write(f'Input data file = {fin_nm}\n')
r_data = pd.read_csv(fin_nm, header=0)
fout.write(f'\nData =\n{r_data}\n')
vars = r_data.keys().values
$$G = an\ indicator\ matrix$$$$f=1_n^TG$$$$H=G-\frac{1}{n}1_nf$$$$H^TH\omega=\lambda\omega$$$$z=\left(\frac{\lambda}{n}\right)^{-1/2}H\omega$$
Okamoto,Y.(2016), pp.100-102.
https://jwu.repo.nii.ac.jp/records/2223
In [2]:
Xgen = {}
for v_nm in vars:
if int(r_data[v_nm].values[0]) == 0:
Xgen[v_nm] = r_data[v_nm].values[1:]
if int(r_data[v_nm].values[0]) == 1:
Xgen[v_nm+'_z'] = ss.zscore(r_data[v_nm].values[1:])
elif int(r_data[v_nm].values[0]) > 1:
#v_item = np.array(r_data[v_nm].values, dtype=int)
NCase = len(r_data[v_nm]) - 1
NCat = r_data[v_nm].values[0]
G = np.zeros((NCase, NCat), dtype=float)
for i in range(NCase):
G[i][r_data[v_nm].values[i+1] - 1] = 1.0
onesRow = np.full((1, NCase), 1)
onesCol = np.full((NCase, 1), 1)
f = onesRow @ G
H = G - (onesCol @ f) / NCase
HpH = H.T @ H
eigVal, eigVec = np.linalg.eigh(HpH)
eigOrder = np.argsort(-eigVal)
w = eigVal[eigOrder]
#print('w =\n', w)
v = eigVec[:, eigOrder]
#print('v =\n', v)
n_quant = 0;
while (w[n_quant] > 1.0e-7):
n_quant = n_quant + 1
eigen_val = w[:n_quant]
eigen_vec = v[:, :n_quant]
#print(f'H =\n{H}')
#print(f'eigen_vec =\n{eigen_vec}')
z = H @ eigen_vec / ((eigen_val/NCase)**0.5)
for j in range(n_quant):
Xgen[v_nm+f'-{j}'] = z.T[j]
print(f'\n\nVariable {v_nm}')
for j in range(n_quant):
print(f'\n Eigen value = {eigen_val[j]:.5f}')
print(' Eigen vector = ', end = '')
for t in eigen_vec.T[j]:
print(f' {t:.5f}', end='')
print()
fout.write(f'\n\nVariable {v_nm}\n')
for j in range(n_quant):
fout.write(f' {v_nm}-{j}:')
fout.write(f'\n Eigen value = {eigen_val[j]:.5f}\n')
fout.write(' Eigen vector = ')
for t in eigen_vec.T[j]:
fout.write(f' {t:.5f}')
fout.write('\n')
Variable Q1 Eigen value = 13.84615 Eigen vector = 0.40825 -0.81650 0.40825 Eigen value = 12.00000 Eigen vector = -0.70711 0.00000 0.70711 Variable Q2 Eigen value = 14.78034 Eigen vector = -0.61920 0.77052 -0.15132 Eigen value = 6.24530 Eigen vector = 0.53222 0.27013 -0.80235 Variable Q3 Eigen value = 14.42481 Eigen vector = 0.34889 -0.81375 0.46486 Eigen value = 8.44699 Eigen vector = -0.73820 0.06695 0.67125 Variable Q4 Eigen value = 13.99230 Eigen vector = 0.09878 -0.75130 0.65252 Eigen value = 4.00770 Eigen vector = -0.81050 0.31970 0.49080 Variable Q5 Eigen value = 11.41055 Eigen vector = -0.80075 0.53859 0.26216 Eigen value = 4.69201 Eigen vector = -0.15960 -0.61367 0.77327
In [3]:
pd.DataFrame(Xgen).to_csv(outcsv_nm, index=False)
print()
print(outcsv_nm, 'was saved.')
fout.close()
print(fout_nm, 'was saved')
CQResults.csv was saved. temp.txt was saved
In [ ]: