# 基本库import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import MinMaxScaler
加载深度学习框架
# 搭建分类模型所需要的库from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPool2D, Dropout
from tensorflow.keras.utils import to_categorical
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
加载音频处理库
!pip install librosa --user
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Processing ./.cache/pip/wheels/1d/2e/93/ba4d8c80d35b317ae0086af9c18066ff7887730ca494569511/librosa-0.8.0-cp36-none-any.whl
Requirement already satisfied: joblib>=0.14 in /usr/local/lib/python3.6/dist-packages (from librosa) (1.0.1)
Processing ./.cache/pip/wheels/9e/a4/87/57f1f67be4f4bf2bc01949f7f6ef292468be3779d5e6986970/audioread-2.1.9-cp36-none-any.whl
Requirement already satisfied: decorator>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from librosa) (4.4.2)
Requirement already satisfied: numpy>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from librosa) (1.19.5)
Processing ./.cache/pip/wheels/fd/3f/d3/1cda32e621e1fb146ac69f70a9c00a00efb0b5c3ffd168ec84/resampy-0.2.2-cp36-none-any.whl
Requirement already satisfied: scipy>=1.0.0 in /usr/local/lib/python3.6/dist-packages (from librosa) (1.5.4)
Requirement already satisfied: scikit-learn!=0.19.0,>=0.14.0 in /usr/local/lib/python3.6/dist-packages (from librosa) (0.24.1)
Collecting numba>=0.43.0
Using cached https://mirrors.aliyun.com/pypi/packages/4a/c1/e7fdbfc886a9d9c11767533903db0d816c0f656fd6029f4a061742893694/numba-0.53.1-cp36-cp36m-manylinux2014_x86_64.whl
Collecting soundfile>=0.9.0
Using cached https://mirrors.aliyun.com/pypi/packages/eb/f2/3cbbbf3b96fb9fa91582c438b574cff3f45b29c772f94c400e2c99ef5db9/SoundFile-0.10.3.post1-py2.py3-none-any.whl
Collecting pooch>=1.0
Using cached https://mirrors.aliyun.com/pypi/packages/40/b9/9876662636ba451d4406543047c0b45ca5b4e830f931308c8274dad1db43/pooch-1.3.0-py3-none-any.whl
Requirement already satisfied: six>=1.3 in /usr/local/lib/python3.6/dist-packages (from resampy>=0.2.2->librosa) (1.15.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from scikit-learn!=0.19.0,>=0.14.0->librosa) (2.1.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages/setuptools-46.1.3-py3.6.egg (from numba>=0.43.0->librosa) (46.1.3)
Collecting llvmlite<0.37,>=0.36.0rc1
Using cached https://mirrors.aliyun.com/pypi/packages/4d/5a/707cc7e072d71bc19869d093e5cf9b7be98cb42d2398489465474d007ce8/llvmlite-0.36.0-cp36-cp36m-manylinux2010_x86_64.whl
Requirement already satisfied: cffi>=1.0 in /usr/local/lib/python3.6/dist-packages (from soundfile>=0.9.0->librosa) (1.14.5)
Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from pooch>=1.0->librosa) (2.25.1)
Requirement already satisfied: packaging in /usr/local/lib/python3.6/dist-packages (from pooch>=1.0->librosa) (20.9)
Collecting appdirs
Using cached https://mirrors.aliyun.com/pypi/packages/3b/00/2344469e2084fb287c2e0b57b72910309874c3245463acd6cf5e3db69324/appdirs-1.4.4-py2.py3-none-any.whl
Requirement already satisfied: pycparser in /usr/local/lib/python3.6/dist-packages (from cffi>=1.0->soundfile>=0.9.0->librosa) (2.20)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->pooch>=1.0->librosa) (1.26.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->pooch>=1.0->librosa) (2.9)
Requirement already satisfied: chardet<5,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->pooch>=1.0->librosa) (4.0.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->pooch>=1.0->librosa) (2020.12.5)
Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.6/dist-packages (from packaging->pooch>=1.0->librosa) (2.4.7)
Installing collected packages: audioread, llvmlite, numba, resampy, soundfile, appdirs, pooch, librosa
Successfully installed appdirs-1.4.4 audioread-2.1.9 librosa-0.8.0 llvmlite-0.36.0 numba-0.53.1 pooch-1.3.0 resampy-0.2.2 soundfile-0.10.3.post1
# 其他库import os
import librosa
import librosa.display
import glob
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
"""Entry point for launching an IPython kernel.
# 获取特征
X = np.vstack(data[:,0])# 获取标签
Y = np.array(data[:,1])print('X的特征尺寸是:',X.shape)print('Y的特征尺寸是:',Y.shape)
X的特征尺寸是: (1000, 128)
Y的特征尺寸是: (1000,)
# 在Keras库中:to_categorical就是将类别向量转换为二进制(只有0和1)的矩阵类型表示
Y = to_categorical(Y)
'''最终数据'''print(X.shape)print(Y.shape)
(1000, 128)
(1000, 20)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, random_state =1, stratify=Y)print('训练集的大小',len(X_train))print('测试集的大小',len(X_test))