Python – 机器学习:从数组列表中创建训练和测试集

我想创建一个基于RAVDESS数据集(https://smartlaboratory.org/ravdess/)训练的神经网络:我的想法是使用这个数据集来检测说话人通过我应用的麦克风传达的情绪。

使用librosa和下面的for循环,我已经提取了我想要用于分析的标签和特征。

# 我先从一个文件夹开始以加快操作速度
oneActorPath = '/content/drive/My Drive/RAVDESS/Audio_Speech_Actors_01-24/Actor_01/'
lst = []
# 遍历每个文件夹查找wavs文件
for subdir, dirs, files in os.walk(oneActorPath):
  for file in files:
    if file == '.DS_Store':
      continue
    else:
      # 检查文件格式是否有效
      try:
        # 加载librosa数组
        data, rate = librosa.load(os.path.join(subdir,file))
        # 通过文件名可以了解包含的情绪
        file = file[6:8]
        arr = data, file
        lst.append(arr)
        #print(list)
      # 如果格式无效,则跳过
      except ValueError:
        continue
      

这个循环的输出是一个如下格式的数组列表:

[(array([-8.1530527e-10,  8.9952795e-10, -9.1185753e-10, ...,          0.0000000e+00,  0.0000000e+00,  0.0000000e+00], dtype=float32),  '08'), (array([0., 0., 0., ..., 0., 0., 0.], dtype=float32), '08'), (array([0., 0., 0., ..., 0., 0., 0.], dtype=float32), '06'), (array([-0.00050612, -0.00057967, -0.00035985, ...,  0.        ,          0.        ,  0.        ], dtype=float32), '05'), (array([ 6.8139506e-08, -2.3837963e-05, -2.4622474e-05, ...,          3.1678758e-06, -2.4535689e-06,  0.0000000e+00], dtype=float32),  '05'), (array([ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,          6.9306935e-07, -6.6020442e-07,  0.0000000e+00], dtype=float32),  '04'), (array([-7.30260945e-05, -1.18022966e-04, -1.08280736e-04, ...,          8.83421380e-05,  4.97258679e-06,  0.00000000e+00], dtype=float32),  '06'), (array([0., 0., 0., ..., 0., 0., 0.], dtype=float32), '07'), (array([ 2.3406714e-05,  3.1186773e-05,  4.9467826e-06, ...,          1.2180173e-07, -9.2944845e-08,  0.0000000e+00], dtype=float32),  '01'), (array([ 1.1845550e-06, -1.6399191e-06,  2.5565218e-06, ...,         -8.7445065e-09,  5.9859917e-09,  0.0000000e+00], dtype=float32),  '04'), (array([0., 0., 0., ..., 0., 0., 0.], dtype=float32), '03'), (array([-1.3284328e-05, -7.4090644e-07,  7.2679302e-07, ...,          0.0000000e+00,  0.0000000e+00,  0.0000000e+00], dtype=float32),  '07'), (array([ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,          5.0694009e-08, -3.4546797e-08,  0.0000000e+00], dtype=float32),  '03'), (array([ 1.5591205e-07, -1.5845627e-07,  1.5362870e-07, ...,          0.0000000e+00,  0.0000000e+00,  0.0000000e+00], dtype=float32),  '01'), (array([0., 0., 0., ..., 0., 0., 0.], dtype=float32), '03'), (array([0.0000000e+00, 0.0000000e+00, 0.0000000e+00, ..., 1.1608539e-05,         8.2463991e-09, 0.0000000e+00], dtype=float32), '03'), (array([-3.6192148e-07, -1.4590451e-05, -5.3999561e-06, ...,         -1.9935460e-05, -3.4417746e-05,  0.0000000e+00], dtype=float32),  '02'), (array([ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,         -2.5319534e-07,  2.6521766e-07,  0.0000000e+00], dtype=float32),  '02'), (array([ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,         -2.5055220e-08,  1.2936166e-08,  0.0000000e+00], dtype=float32)...

上述列表中每个元素的第二个元素(如第一行的’08’)代表根据下面的字典定义的数据集标签

emotions = {    "neutral": "01",    "calm": "02",    "happy": "03",    "sad": "04",    "angry": "05",     "fearful": "06",     "disgust": "07",     "surprised": "08"}

此时,我已经有了我的标签和数据:如何分割这个数据集以获得训练集和测试集呢?

EDIT1:我需要了解如何从这个结构中获取X和y,以便对数据使用train_test_split函数。


回答:

你可以使用scikit-learn的train_test_split函数(相关文档)。文档中的示例非常简单直观:

import numpy as npfrom sklearn.model_selection import train_test_splitX, y = np.arange(10).reshape((5, 2)), range(5)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

在你的情况下,你可能需要对你的输出列表进行一些数据操作来获取Xy向量:

X, y = zip(*lst)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注