用python代码将提取的MFCC特征生成可视化png图像
时间: 2024-03-03 16:52:51 浏览: 83
好的,以下是用Python代码将提取的MFCC特征生成可视化png图像的步骤:
1. 使用Librosa库提取音频的MFCC特征。示例代码如下:
```python
import librosa
# load audio file
y, sr = librosa.load('audio.wav')
# extract MFCC features
mfcc = librosa.feature.mfcc(y=y, sr=sr)
```
2. 使用Matplotlib库将MFCC特征可视化为png图像。示例代码如下:
```python
import matplotlib.pyplot as plt
# plot MFCC features
plt.figure(figsize=(10, 4))
librosa.display.specshow(mfcc, x_axis='time')
plt.colorbar()
plt.title('MFCC')
plt.tight_layout()
plt.savefig('mfcc.png')
```
3. 保存生成的png图像。示例代码中的`savefig()`函数会将图像保存为`mfcc.png`文件。
注意:在运行代码之前,需要先安装Librosa和Matplotlib库。可以通过以下命令进行安装:
```python
pip install librosa matplotlib
```
希望这个回答对你有帮助!
相关问题
在下面代码中修改添加一个可视化图,用来画出r经过t_sne之后前15行和15到30行数据的可视化图。import pandas as pd from sklearn import cluster from sklearn import metrics import matplotlib.pyplot as plt from sklearn.manifold import TSNE from sklearn.decomposition import PCA def k_means(data_set, output_file, png_file, png_file1, t_labels, score_file, set_name): model = cluster.KMeans(n_clusters=7, max_iter=1000, init="k-means++") model.fit(data_set) # print(list(model.labels_)) p_labels = list(model.labels_) r = pd.concat([data_set, pd.Series(model.labels_, index=data_set.index)], axis=1) r.columns = list(data_set.columns) + [u'聚类类别'] print(r) # r.to_excel(output_file) with open(score_file, "a") as sf: sf.write("By k-means, the f-m_score of " + set_name + " is: " + str(metrics.fowlkes_mallows_score(t_labels, p_labels))+"\n") sf.write("By k-means, the rand_score of " + set_name + " is: " + str(metrics.adjusted_rand_score(t_labels, p_labels))+"\n") '''pca = PCA(n_components=2) pca.fit(data_set) pca_result = pca.transform(data_set) t_sne = pd.DataFrame(pca_result, index=data_set.index)''' t_sne = TSNE() t_sne.fit(data_set) t_sne = pd.DataFrame(t_sne.embedding_, index=data_set.index) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False dd = t_sne[r[u'聚类类别'] == 0] plt.plot(dd[0], dd[1], 'r.') dd = t_sne[r[u'聚类类别'] == 1] plt.plot(dd[0], dd[1], 'go') dd = t_sne[r[u'聚类类别'] == 2] plt.plot(dd[0], dd[1], 'b*') dd = t_sne[r[u'聚类类别'] == 3] plt.plot(dd[0], dd[1], 'o') dd = t_sne[r[u'聚类类别'] == 4] plt.plot(dd[0], dd[1], 'm.') dd = t_sne[r[u'聚类类别'] == 5] plt.plot(dd[0], dd[1], 'co') dd = t_sne[r[u'聚类类别'] == 6] plt.plot(dd[0], dd[1], 'y*') plt.savefig(png_file) plt.clf() '''plt.scatter(data_set.iloc[:, 0], data_set.iloc[:, 1], c=model.labels_) plt.savefig(png_file) plt.clf()''' frog_data = pd.read_csv("D:/PyCharmPython/pythonProject/mfcc3.csv") tLabel = [] for family in frog_data['name']: if family == "A": tLabel.append(0) elif family == "B": tLabel.append(1) elif family == "C": tLabel.append(2) elif family == "D": tLabel.append(3) elif family == "E": tLabel.append(4) elif family == "F": tLabel.append(5) elif family == "G": tLabel.append(6) scoreFile = "D:/PyCharmPython/pythonProject/scoreOfClustering.txt" first_set = frog_data.iloc[:, 1:1327] k_means(first_set, "D:/PyCharmPython/pythonProject/kMeansSet_1.xlsx", "D:/PyCharmPython/pythonProject/kMeansSet_2.png", "D:/PyCharmPython/pythonProject/kMeansSet_2_1.png", tLabel, scoreFile, "Set_1")
您可以使用以下代码来添加可视化图来画出经过t-sne处理后的前15行和15到30行数据的可视化图:
```python
import pandas as pd
from sklearn import cluster
from sklearn import metrics
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
def k_means(data_set, output_file, png_file, png_file1, t_labels, score_file, set_name):
model = cluster.KMeans(n_clusters=7, max_iter=1000, init="k-means++")
model.fit(data_set)
p_labels = list(model.labels_)
r = pd.concat([data_set, pd.Series(model.labels_, index=data_set.index)], axis=1)
r.columns = list(data_set.columns) + [u'聚类类别']
print(r)
with open(score_file, "a") as sf:
sf.write("By k-means, the f-m_score of " + set_name + " is: " + str(metrics.fowlkes_mallows_score(t_labels, p_labels))+"\n")
sf.write("By k-means, the rand_score of " + set_name + " is: " + str(metrics.adjusted_rand_score(t_labels, p_labels))+"\n")
t_sne = TSNE()
t_sne.fit(data_set)
t_sne = pd.DataFrame(t_sne.embedding_, index=data_set.index)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
dd = t_sne[r[u'聚类类别'] == 0]
plt.plot(dd[0], dd[1], 'r.')
dd = t_sne[r[u'聚类类别'] == 1]
plt.plot(dd[0], dd[1], 'go')
dd = t_sne[r[u'聚类类别'] == 2]
plt.plot(dd[0], dd[1], 'b*')
dd = t_sne[r[u'聚类类别'] == 3]
plt.plot(dd[0], dd[1], 'o')
dd = t_sne[r[u'聚类类别'] == 4]
plt.plot(dd[0], dd[1], 'm.')
dd = t_sne[r[u'聚类类别'] == 5]
plt.plot(dd[0], dd[1], 'co')
dd = t_sne[r[u'聚类类别'] == 6]
plt.plot(dd[0], dd[1], 'y*')
# 画出前15行数据的可视化图
plt.savefig(png_file)
plt.clf()
# 画出15到30行数据的可视化图
dd = t_sne.iloc[15:30, :]
plt.plot(dd[0], dd[1], 'r.')
plt.savefig(png_file1)
plt.clf()
frog_data = pd.read_csv("D:/PyCharmPython/pythonProject/mfcc3.csv")
tLabel = []
for family in frog_data['name']:
if family == "A":
tLabel.append(0)
elif family == "B":
tLabel.append(1)
elif family == "C":
tLabel.append(2)
elif family == "D":
tLabel.append(3)
elif family == "E":
tLabel.append(4)
elif family == "F":
tLabel.append(5)
elif family == "G":
tLabel.append(6)
scoreFile = "D:/PyCharmPython/pythonProject/scoreOfClustering.txt"
first_set = frog_data.iloc[:, 1:1327]
k_means(first_set, "D:/PyCharmPython/pythonProject/kMeansSet_1.xlsx", "D:/PyCharmPython/pythonProject/kMeansSet_2.png", "D:/PyCharmPython/pythonProject/kMeansSet_2_1.png", tLabel, scoreFile, "Set_1")
```
请将代码中的路径和文件名更改为您实际的路径和文件名。这段代码会生成两个可视化图,一个是前15行数据的图(保存为`png_file`),另一个是15到30行数据的图(保存为`png_file1`)。
在下面代码中添加一个可视化图,用来画出r经过t_sne之后前15行数据的图 import pandas as pd from sklearn import cluster from sklearn import metrics import matplotlib.pyplot as plt from sklearn.manifold import TSNE from sklearn.decomposition import PCA def k_means(data_set, output_file, png_file, png_file1, t_labels, score_file, set_name): model = cluster.KMeans(n_clusters=7, max_iter=1000, init="k-means++") model.fit(data_set) # print(list(model.labels_)) p_labels = list(model.labels_) r = pd.concat([data_set, pd.Series(model.labels_, index=data_set.index)], axis=1) r.columns = list(data_set.columns) + [u'聚类类别'] print(r) # r.to_excel(output_file) with open(score_file, "a") as sf: sf.write("By k-means, the f-m_score of " + set_name + " is: " + str(metrics.fowlkes_mallows_score(t_labels, p_labels))+"\n") sf.write("By k-means, the rand_score of " + set_name + " is: " + str(metrics.adjusted_rand_score(t_labels, p_labels))+"\n") '''pca = PCA(n_components=2) pca.fit(data_set) pca_result = pca.transform(data_set) t_sne = pd.DataFrame(pca_result, index=data_set.index)''' t_sne = TSNE() t_sne.fit(data_set) t_sne = pd.DataFrame(t_sne.embedding_, index=data_set.index) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False dd = t_sne[r[u'聚类类别'] == 0] plt.plot(dd[0], dd[1], 'r.') dd = t_sne[r[u'聚类类别'] == 1] plt.plot(dd[0], dd[1], 'go') dd = t_sne[r[u'聚类类别'] == 2] plt.plot(dd[0], dd[1], 'b*') dd = t_sne[r[u'聚类类别'] == 3] plt.plot(dd[0], dd[1], 'o') dd = t_sne[r[u'聚类类别'] == 4] plt.plot(dd[0], dd[1], 'm.') dd = t_sne[r[u'聚类类别'] == 5] plt.plot(dd[0], dd[1], 'co') dd = t_sne[r[u'聚类类别'] == 6] plt.plot(dd[0], dd[1], 'y*') plt.savefig(png_file) '''plt.scatter(data_set.iloc[:, 0], data_set.iloc[:, 1], c=model.labels_) plt.savefig(png_file) plt.clf()''' frog_data = pd.read_csv("D:/PyCharmPython/pythonProject/mfcc3.csv") tLabel = [] for family in frog_data['name']: if family == "A": tLabel.append(0) elif family == "B": tLabel.append(1) elif family == "C": tLabel.append(2) elif family == "D": tLabel.append(3) elif family == "E": tLabel.append(4) elif family == "F": tLabel.append(5) elif family == "G": tLabel.append(6) scoreFile = "D:/PyCharmPython/pythonProject/scoreOfClustering.txt" first_set = frog_data.iloc[:, 1:1327] k_means(first_set, "D:/PyCharmPython/pythonProject/kMeansSet_1.xlsx", "D:/PyCharmPython/pythonProject/kMeansSet_2.png", "D:/PyCharmPython/pythonProject/kMeansSet_2_1.png", tLabel, scoreFile, "Set_1")
你可以在以下代码中添加一个可视化图来画出r经过t_sne之后前15行数据的图:
```python
import pandas as pd
from sklearn import cluster
from sklearn import metrics
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
def k_means(data_set, output_file, png_file, png_file1, t_labels, score_file, set_name):
model = cluster.KMeans(n_clusters=7, max_iter=1000, init="k-means++")
model.fit(data_set)
p_labels = list(model.labels_)
r = pd.concat([data_set, pd.Series(model.labels_, index=data_set.index)], axis=1)
r.columns = list(data_set.columns) + [u'聚类类别']
print(r)
with open(score_file, "a") as sf:
sf.write("By k-means, the f-m_score of " + set_name + " is: " + str(metrics.fowlkes_mallows_score(t_labels, p_labels))+"\n")
sf.write("By k-means, the rand_score of " + set_name + " is: " + str(metrics.adjusted_rand_score(t_labels, p_labels))+"\n")
t_sne = TSNE()
t_sne.fit(data_set)
t_sne = pd.DataFrame(t_sne.embedding_, index=data_set.index)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
dd = t_sne[r[u'聚类类别'] == 0]
plt.plot(dd[0], dd[1], 'r.')
dd = t_sne[r[u'聚类类别'] == 1]
plt.plot(dd[0], dd[1], 'go')
dd = t_sne[r[u'聚类类别'] == 2]
plt.plot(dd[0], dd[1], 'b*')
dd = t_sne[r[u'聚类类别'] == 3]
plt.plot(dd[0], dd[1], 'o')
dd = t_sne[r[u'聚类类别'] == 4]
plt.plot(dd[0], dd[1], 'm.')
dd = t_sne[r[u'聚类类别'] == 5]
plt.plot(dd[0], dd[1], 'co')
dd = t_sne[r[u'聚类类别'] == 6]
plt.plot(dd[0], dd[1], 'y*')
plt.savefig(png_file)
frog_data = pd.read_csv("D:/PyCharmPython/pythonProject/mfcc3.csv")
tLabel = []
for family in frog_data['name']:
if family == "A":
tLabel.append(0)
elif family == "B":
tLabel.append(1)
elif family == "C":
tLabel.append(2)
elif family == "D":
tLabel.append(3)
elif family == "E":
tLabel.append(4)
elif family == "F":
tLabel.append(5)
elif family == "G":
tLabel.append(6)
scoreFile = "D:/PyCharmPython/pythonProject/scoreOfClustering.txt"
first_set = frog_data.iloc[:, 1:1327]
k_means(first_set, "D:/PyCharmPython/pythonProject/kMeansSet_1.xlsx", "D:/PyCharmPython/pythonProject/kMeansSet_2.png", "D:/PyCharmPython/pythonProject/kMeansSet_2_1.png", tLabel, scoreFile, "Set_1")
```
请确保你已经安装了所需的库,并且将文件路径修改为你实际的文件路径。这段代码会将数据集进行聚类,并使用t-SNE进行降维,然后绘制出前15行数据的聚类可视化图。保存的图像将会保存在`png_file`路径下。
阅读全文