无法执行文件,然后转到python目录中的下一个文件

rxztt3cl  于 2023-02-15  发布在  Python
关注(0)|答案(1)|浏览(105)

这是我的代码,你能告诉我如何执行一个文件,让我的人工智能猜测它的东西,然后移动到下一个文件。我已经添加了一些描述,以显示我的代码是做什么

import os
import time

import matplotlib.pyplot as plt
import librosa
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import librosa.display

directory = 'Music'

# Load audio file
for filename in os.listdir(directory):
    if filename.endswith('.mp3'):
        audio_file, sampling_rate = librosa.load(os.path.join(directory, filename))
# Extract features using librosa
mfccs = librosa.feature.mfcc(y=audio_file, sr=sampling_rate)
chroma = librosa.feature.chroma_stft(y=audio_file, sr=sampling_rate)

plt.figure(figsize=(12,4))
# Combine features into one array
features = np.vstack((mfccs, chroma))
librosa.display.specshow(features, x_axis="time", y_axis="chroma", vmin=0, vmax=1)
plt.title('Mfccs')
plt.tight_layout()
plt.show()
# Load labels
labels = pd.read_csv('csvloader/tableConvert.com_y1m7y5.csv')
print(labels.head())
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2,                                         random_state=42)

# Standardize the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Train the model using a random forest classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
# Test the model on the test data
accuracy = clf.score(X_test, y_test)
print(f"Accuracy: {accuracy * 100:.2f}%")
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred))

我试着查找,但找不到解决方案

hl0ma9xz

hl0ma9xz1#

我可以知道你想要做的是通过一个模型一个文件一个文件地做一些预测,或者你的模型将一个文件一个文件地重新训练吗?另外,你意识到你的audio filesampling rate总是在for循环中被覆盖吗?如果你想使用你所有的mp3文件,你应该声明一个dict并把你所有的mfccschroma附加到它里面。

features = {}
features['mfccs'] = []
features['chroma'] = []
for filename in os.listdir(directory):
if filename.endswith('.mp3'):
    audio_file, sampling_rate = librosa.load(os.path.join(directory, filename))
# Extract features using librosa
    mfccs = librosa.feature.mfcc(y=audio_file, sr=sampling_rate)
    chroma = librosa.feature.chroma_stft(y=audio_file, sr=sampling_rate)
    features['mfccs'].append(mfccs)
    features['chroma'].append(chroma)

如果我有误解,请尽管说。

相关问题