반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

Learner's Log님의 블로그

음성데이터 K-fold 본문

Python

음성데이터 K-fold

Learner's Log 2025. 3. 25. 09:39
import os
import librosa
import numpy as np
import glob
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import KFold
from sklearn.svm import SVC
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score, confusion_matrix
import pandas as pd
# 데이터 경로 설정
DATA_PATH = "voice_data"
# 감정 레이블 매핑
target_emotions = {'A': 'angry', 'H': 'happy', 'N': 'neutral', 'S': 'sad'}
def extract_mfcc_feature(file_name):
    audio_signal, sample_rate = librosa.load(file_name, sr=22050)
    mfccs = librosa.feature.mfcc(y=audio_signal, sr=sample_rate, n_mfcc=20)
    return np.mean(mfccs.T, axis=0)
# 데이터 로드 및 전처리
def load_data():
    features, labels = [], []
    for file_path in glob.glob(os.path.join(DATA_PATH, "*.wav")):
        file_name = os.path.basename(file_path)
        emotion = file_name.split('-')[1][0]  # 감정 코드 추출
        if emotion in target_emotions:
            mfcc_feature = extract_mfcc_feature(file_path)
            features.append(mfcc_feature)
            labels.append(target_emotions[emotion])
    return np.array(features), np.array(labels)
X, y = load_data()
c:\ProgramData\anaconda3\Lib\site-packages\paramiko\pkey.py:82: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
  "cipher": algorithms.TripleDES,
c:\ProgramData\anaconda3\Lib\site-packages\paramiko\transport.py:219: CryptographyDeprecationWarning: Blowfish has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.Blowfish and will be removed from this module in 45.0.0.
  "class": algorithms.Blowfish,
c:\ProgramData\anaconda3\Lib\site-packages\paramiko\transport.py:243: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
  "class": algorithms.TripleDES,
# K-Fold 교차 검증
kf = KFold(n_splits=10, shuffle=True, random_state=50) # 중간 크기 데이터셋, n_splits = 10
accuracy_history = []
conf_matrices = []
for train_index, test_index in kf.split(X): # X는 feature, y는 label
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

    scaler = MinMaxScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)

    clf = SVC(C=800, kernel='rbf', probability=True)
    clf.fit(X_train_scaled, y_train)

    y_pred = clf.predict(X_test_scaled)
    accuracy_history.append(accuracy_score(y_test, y_pred))
    conf_matrices.append(confusion_matrix(y_test, y_pred, labels=list(target_emotions.values())))

print("각 분할의 정확도 기록:", accuracy_history)
print("평균 정확도:", np.mean(accuracy_history))
각 분할의 정확도 기록: [0.9950738916256158, 0.9950738916256158, 0.9802955665024631, 0.995049504950495, 0.9801980198019802, 0.9900990099009901, 1.0, 0.995049504950495, 0.995049504950495, 0.9900990099009901, 0.9950738916256158, 0.9950738916256158, 0.9802955665024631, 0.995049504950495, 0.9801980198019802, 0.9900990099009901, 1.0, 0.995049504950495, 0.995049504950495, 0.9900990099009901, 0.9950738916256158, 0.9950738916256158, 0.9802955665024631, 0.995049504950495, 0.9801980198019802, 0.9900990099009901, 1.0, 0.995049504950495, 0.995049504950495, 0.9900990099009901]
평균 정확도: 0.9915987904209139
# 혼동 행렬 시각화
cm = sum(conf_matrices)
plt.figure(figsize=(6, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Reds', xticklabels=target_emotions.values(), yticklabels=target_emotions.values())
plt.title("Confusion Matrix")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
반응형

'Python' 카테고리의 다른 글

파이썬 모듈과 패키지  (0) 2025.02.09