gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
This code is for predicting about covid-19 infection from a cough recording it's taking…
- User
This code is for predicting about covid-19 infection from a cough recording it's taking about 21 seconds with a dataset of 12 wav files can it be faster ?
import os import librosa import numpy as np from sklearn.model_selection import train_test_split, cross_val_score from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn import svm from sklearn.svm import SVC import time import multiprocessing def extract_features(file_path): audio_data, sample_rate = librosa.load(file_path, sr=None, mono=True) audio_features = librosa.feature.mfcc(y=audio_data, sr=sample_rate, n_mfcc=13) features = [] for feature in audio_features.T: features.append(feature) return features if __name__ == '__main__': multiprocessing.freeze_support() start_time = time.time() # Set path to directory containing positive and negative cough sound files data_dir = 'data' # Create empty arrays for storing features and labels features = [] labels = [] # Loop through positive and negative subdirectories and extract features for label, sub_dir in enumerate(['positive', 'negative']): sub_dir_path = os.path.join(data_dir, sub_dir) file_paths = [os.path.join(sub_dir_path, file_name) for file_name in os.listdir(sub_dir_path)] print(12) with multiprocessing.get_context('spawn').Pool() as pool: result = pool.map(extract_features, file_paths) for feature in result: features.extend(feature) labels.extend([label] * len(feature)) print(1234) # Convert feature and label arrays to numpy arrays X = np.vstack(features) y = np.array(labels) # Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Create a pipeline for feature scaling and SVM classification clf = make_pipeline(StandardScaler(), SVC(kernel='linear', probability=True)) # Use cross-validation to evaluate the model's performance scores = cross_val_score(clf, X, y, cv=5) print(f"Cross-validation accuracy: {scores.mean():.3f} (+/- {scores.std() * 2:.3f})") # Train the model on all available data clf.fit(X, y) # Load new cough sound file to classify new_audio_file = 'data/positive_bak/610_Positivo_53_m_12-6-20_1.wav' new_audio_data, sample_rate = librosa.load(new_audio_file, sr=None, mono=True) # Extract acoustic features from new cough sound new_features = librosa.feature.mfcc(y=new_audio_data, sr=sample_rate, n_mfcc=13) # Classify new cough sound using SVM classifier # prediction = clf.predict(new_features.T) prediction = clf.predict_proba(new_features.T) prediction_probability = clf.predict_proba(new_features.T)[0][1] average_prediction = np.mean(prediction[:, 1]) # Print prediction (1 for COVID-19 positive, 0 for COVID-19 negative) and prediction probability print("Prediction:", prediction) print("Prediction probability:", average_prediction) end_time = time.time() elapsed_time = end_time - start_time print("Elapsed time: ", elapsed_time, " seconds") print(125) - Assistant