<jupyter_start><jupyter_text>라이브러리 import<jupyter_code>import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>데이터 불러오기 특징값 데이터 불러와서 정상/고장 분리<jupyter_code>FeatureData = pd.read_csv('./ProcessdData/FeatureData', sep=',', header=None)
FeatureData.shape<jupyter_output><empty_output><jupyter_text>P value 순위 데이터 불러오기<jupyter_code>P_value_Rank = pd.read_csv('./ProcessdData/P_value_Rank' , sep=',' , header=None)
P_value_Rank<jupyter_output><empty_output><jupyter_text>P value 기준 Feature 선정<jupyter_code># StartRank 부터 Number 만큼의 Feature
StartRank = 1
Number = 30

SelectedFeatues = np.zeros((Number,FeatureData.shape[1]))

s = 0

for i in range(StartRank, StartRank+Number):
    
    index                 = int(P_value_Rank.iloc[i-1,0])
    SelectedFeatues[s,:]   = FeatureData.iloc[index,:].values
    s += 1

# 정상, 고장 특징값 합치기    
FeatureSelected = pd.DataFrame(SelectedFeatues)
FeatureSelected.shape
FeatureSelected = np.transpose(FeatureSelected)
FeatureSelected.shape<jupyter_output><empty_output><jupyter_text>데이터 정규화<jupyter_code># 선정된 특징데이터에 대한 정규화
FeatureSelected_std = StandardScaler().fit_transform(FeatureSelected)
FeatureSelected_std.shape<jupyter_output><empty_output><jupyter_text>선정된 Feature PCA<jupyter_code># 10개 PC(Principal Component) 추출
pca = PCA(n_components = 10)
PC = pca.fit_transform(FeatureSelected_std)

# 설명된 분산의 비율
VarRatio = pca.explained_variance_ratio_
VarRatio = pd.DataFrame(np.round_(VarRatio,3))

# 설명된 분산의 비율 누적
CumVarRatio = np.cumsum(pca.explained_variance_ratio_)
CumVarRatio_df = pd.DataFrame(np.round_(CumVarRatio,3))

Result = pd.concat([VarRatio , CumVarRatio_df], axis=1)
Result

# 누적값 그래프로 시각화
plt.plot(range(1,11), CumVarRatio*100, color='green', linestyle='-', marker='o')
plt.ylim(0,100)
plt.xlim(0,11)
plt.grid()
plt.xlabel('PC Number')
plt.ylabel('Percentage(%)')
plt.show()<jupyter_output><empty_output><jupyter_text>Selected Feature PCA (2차원) 기반 분포 시각화<jupyter_code>NoOfData = int(FeatureData.shape[1]/2)

plt.plot(PC[:NoOfData,0], PC[:NoOfData,1], color='b', linestyle='', marker='o', label='Normal')
plt.plot(PC[NoOfData:,0], PC[NoOfData:,1], color='r', linestyle='', marker='o', label='Abnormal')
plt.legend()
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13836477987421383, 'fraction_numerical': 0.01551362683438155, 'mean_word_length': 7.521428571428571, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '582127', 'license': 'MIT', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1130, 'n_words': 154, 'path': 'Robust Design/PracticMaterial_2/Code5_Dimensionality Reduction (PCA).ipynb', 'repo_name': 'nosy0411/Data_analysis_project', 'size': '2385'}