设计logo图案用什么软件,seo网站推广价格,开网店详细步骤流程,水果配送网站建设机器学习如何用于音频分析#xff1f;
一、说明
近十年来#xff0c;机器学习越来越受欢迎。事实上#xff0c;它被用于医疗保健、农业和制造业等众多行业。随着技术和计算能力的进步#xff0c;机器学习有很多潜在的应用正在被创造出来。由于数据以多种格式大量可用
一、说明
近十年来机器学习越来越受欢迎。事实上它被用于医疗保健、农业和制造业等众多行业。随着技术和计算能力的进步机器学习有很多潜在的应用正在被创造出来。由于数据以多种格式大量可用因此现在是使用机器学习和数据科学从数据中提取各种见解并使用它们进行预测的合适时机。
机器学习最有趣的应用之一是音频分析和分别了解不同音频格式的质量。因此使用各种机器学习和深度学习算法可确保使用音频数据创建和理解预测。 照片由 Med Badr Chemmaoui on Unsplash
二、机器学习处理音频
在进行音频分析之前必须单独采集和分析信号样本。我们取样的速率也称为采样率或奈奎斯特率。将时域信号转换为频域信号以便对信号有很好的逻辑理解同时计算有用的分量如功率和能量这将非常方便。所有这些都可以作为我们的机器学习模型的特征这些模型将使用它们进行预测。
音频信号通常可以转换为频谱图图像以便将其提供给卷积神经网络 CNN 进行预测。频谱图可以捕获音频信号的重要特征并给出 2D 表示因此可以与基于图像的网络一起使用。
如果给它们一个图像有很多 ML 模型可以很好地预测输出标签。因此由振幅和不同频率单位组成的音频信号也可以转换为图像并用于稳健的 ML 预测。
在本文中我们将通过考虑一个随机示例并绘制它来理解其图形表示从而介绍如何读取音频文件。稍后我们将使用图像数据执行特征工程并在音频转换为图像时执行卷积运算。最后我们将获得未见过数据的样本预测。请注意此代码用于演示不考虑特定的数据集。
读取数据
import matplotlib.pyplot as plt
import numpy as np
from scipy.io.wavefile import read# Read in the audio file
sample_rate, audio_data read(audio_file.wav)# Convert the audio data to a numpy array
audio_data np.array(audio_data)# Get the length of the audio data
length audio_data.shape[0] / sample_rate# Create a time axis
time np.linspace(0., length, audio_data.shape[0])# Plot the audio data
plt.plot(time, audio_data)
plt.xlabel(Time (s))
plt.ylabel(Amplitude)
plt.show()我们将导入必要的库这些库用于以主要以 ‘.wav’ 格式的形式读取音频文件。读取文件后我们将得到一个数组表示形式如上面的代码单元所示。最后我们将绘制输出只是为了看看它在 matplotlib 中的外观。
特征工程
import librosa# Calculate the short-term Fourier transform (STFT) of the audio data
stft librosa.stft(audio_data)# Extract the magnitude and phase of the STFT
magnitude, phase librosa.magphase(stft)# Calculate the mel-scaled spectrogram of the audio data
mel_spec librosa.feature.melspectrogram(audio_data, srsample_rate)# Extract the Mel-frequency cepstral coefficients (MFCCs) from the mel-scaled spectrogram
mfccs librosa.feature.mfcc(Slibrosa.power_to_db(mel_spec), n_mfcc20)# Transpose the MFCCs so that each row represents a time frame and each column represents a coefficient
mfccs mfccs.T现在数据已经绘制和可视化以查看 ‘.wav’ 文件中的异常情况我们现在将使用一个名为 ‘librosa’ 的流行库该库可用于计算音频数据的短期傅里叶变换。这是为了确保信号被分解成其组成频率是一种在大量行业中广泛使用的技术。
训练模型
# Convert the audio data to a spectrogram
X compute_spectrogram(X)# Split data into training and test sets
X_train, X_test, y_train, y_test train_test_split(X, y, test_size 0.2)# Build machine learning model
# In this case, lets use a random forest classifier with 100 trees
model RandomForestClassifier(n_estimators 100)# Train model on training data
model.fit(X_train, y_train)# Evaluate model on test data
accuracy model.score(X_test, y_test)
print(Test accuracy:, accuracy)现在我们已经使用 ‘librosa’ 来获取频率分量我们将使用机器学习模型进行预测。需要注意的是这是一个分类问题因此我们继续使用随机森林分类器。但是请随意使用适合您的需求和业务的任何其他机器学习模型。
我们现在将使用相同的代码但用于输出是连续而不是离散的回归任务。下面是有关如何在随机森林回归器的帮助下进行训练和监控性能的编码单元。
# Convert the audio data to a spectrogram
X compute_spectrogram(X)# Split data into training and test sets
X_train, X_test, y_train, y_test train_test_split(X, y, test_size 0.2)# Build machine learning model
# In this case, lets use a random forest regressor with 100 trees
model RandomForestRegressor(n_estimators 100)# Train model on training data
model.fit(X_train, y_train)# Evaluate model on test data
accuracy model.score(X_test, y_test)
print(Test accuracy:, accuracy)超参数优化 在实时部署模型 Random Forest 之前确定模型 Random Forest 的正确超参数非常重要。当涉及到深度神经网络时有很多超参数需要搜索。由于我们使用随机森林作为基准模型因此我们应该能够在最小的搜索空间中获得正确的超参数。让我们看看如何在常规数据集上执行超参数优化。
# Define hyperparameters to tune
param_grid {max_depth: [2, 4, 6, 8], min_samples_split: [2, 4, 6, 8]}# Create a grid search object with 5-fold cross-validation
grid_search GridSearchCV(model, param_grid, cv5)# Fit the grid search object to the training data
grid_search.fit(X_train, y_train)# Print the best hyperparameters and score
print(Best hyperparameters:, grid_search.best_params_)
print(Best cross-validation score:, grid_search.best_score_)# Evaluate the model on the test data
best_model grid_search.best_estimator_
accuracy best_model.score(X_test, y_test)
print(Test accuracy:, accuracy)在代码单元中我们指定估计器的数量和我们搜索的树的最大深度以便在测试集上获得最佳结果。最后我们监控分数看看超参数的变化如何使模型获得更好的性能。
模型部署
import pickle# Save the model to a file
with open(model.pkl, wb) as f:pickle.dump(model, f)# Load the model from a file
with open(model.pkl, rb) as f:model pickle.load(f)# Use the model to make predictions on new data
predictions model.predict(new_data)现在我们已经执行了超参数优化以给出最准确的预测是时候保存提供最佳结果的机器学习模型了。因此我们将在 python 中使用 pickle 库这样我们就可以自由地保存机器学习模型以便以后用于服务。
保存模型后我们将在构建生产就绪代码时再次加载它并使用它来预测传入的批次或数据流。需要注意的是在训练数据期间用于执行特征化的步骤集也必须在测试集上执行以便数据中没有偏斜。
三、持续监控
我们知道该模型在接收来自用户的传入数据的地方提供数据方面表现良好这也是一个重要但被忽视的步骤即监控模型的预测质量。通常模型可能无法像训练期间那样执行。这可能是因为训练数据和服务数据之间存在差异。例如可能存在概念漂移或数据漂移等情况这些情况可能会对投入生产的推理模型的性能产生重大影响。
持续监控可确保采取措施检查预测模型并了解它们在变化数据中的行为。如果预测最不准确并且导致企业收入损失则应采取措施再次使用这些偏差数据训练模型以便模型的行为不会发生意外变化。
四、结论
阅读本文后您可能已经对如何对音频数据执行机器学习并了解整个工作流程有了很好的了解。我们已经看到了读取数据、特征工程、训练模型、超参数调整、模型部署以及持续监控等步骤。应用这些步骤中的每一个并确保在开发管道时没有错误这将导致一个强大的机器学习生产系统。
以下是您可以联系我或查看我的作品的方式。