fisher_score = np.sum(eig_val[:mfcc_order]/np.sum(eig_val))中的mfcc_order取值
时间: 2023-03-14 15:00:30 浏览: 54
mfcc_order指的是Mel-frequency cepstral coefficients(MFCC)的次数,它是一种用于提取声音信息的常用频谱分析方法。取值范围可以根据具体情况进行调整,一般取值范围是1~20。
相关问题
import numpy as np def pca(X, threshold): # 去均值 X_mean = np.mean(X, axis=0) X = X - X_mean # 计算协方差矩阵 cov = np.dot(X.T, X) / (X.shape[0] - 1) # 计算特征值和特征向量 eig_vals, eig_vecs = np.linalg.eig(cov) # 对特征值进行排序 eig_vals_sort = np.argsort(eig_vals)[::-1] # 计算累计贡献率 eig_vals_sum = np.sum(eig_vals) cumsum = np.cumsum(eig_vals[eig_vals_sort]) / eig_vals_sum # 寻找最佳的n_components best_n_components = np.argmax(cumsum >= threshold) + 1 # 提取前best_n_components个特征向量 eig_vecs_sort = eig_vecs[:, eig_vals_sort[:best_n_components]] # 将数据投影到新的特征空间上 X_pca = np.dot(X, eig_vecs_sort) return X_pca # 生成数据集 data = np.random.rand(643, 1024) # 进行PCA降维 X_pca = pca(data, threshold=0.9) # 输出结果print("最佳的n_components为:", X_pca.shape[1])中threshold=0.9是怎么算出来的
在这段代码中,threshold=0.9 是作为一个参数传入函数pca()中的,它代表着累计贡献率的阈值,用于确定保留多少个主成分。在该函数中,累计贡献率是通过计算特征值的和来计算的,然后通过计算每个特征值在特征值总和中的占比,来确定保留多少个主成分。因此,当阈值设为0.9时,函数会保留主成分的数量,使得它们对原始数据的解释方差和至少达到90%。
import os import numpy as np import matplotlib.pyplot as plt from PIL import Image from colorcet.plotting import arr from sklearn.cluster import SpectralClustering from sklearn.decomposition import PCA from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.resnet50 import ResNet50 from tensorflow.keras.applications.resnet50 import preprocess_input # 定义加载图片函数 def load_image(img_path): img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x # 加载ResNet50模型 model = ResNet50(weights='imagenet', include_top=False, pooling='avg') # 加载图片并提取特征向量 img_dir = 'D:/wjd' img_names = os.listdir(img_dir) X = [] for img_name in img_names: img_path = os.path.join(img_dir, img_name) img = load_image(img_path) features = model.predict(img)[0] X.append(features) # 将特征向量转化为矩阵 X = np.array(X) # 将复数类型的数据转换为实数类型 X = np.absolute(X) # 计算相似度矩阵 S = np.dot(X, X.T) # 归一化相似度矩阵 D = np.diag(np.sum(S, axis=1)) L = D - S L_norm = np.dot(np.dot(np.sqrt(np.linalg.inv(D)), L), np.sqrt(np.linalg.inv(D))) # 计算特征向量 eigvals, eigvecs = np.linalg.eig(L_norm) idx = eigvals.argsort()[::-1] eigvals = eigvals[idx] eigvecs = eigvecs[:, idx] Y = eigvecs[:, :2] # 使用谱聚类进行分类 n_clusters = 5 clustering = SpectralClustering(n_clusters=n_clusters, assign_labels="discretize", random_state=0).fit(Y) # 可视化聚类结果 pca = PCA(n_components=2) X_pca = pca.fit_transform(X) plt.scatter(X_pca[:, 0], X_pca[:, 1], c=clustering.labels_, cmap='rainbow') plt.show(),反复会出现numpy.ComplexWarning: Casting complex values to real discards the imaginary part The above exception was the direct cause of the following exception,这个问题
这个问题是因为在计算相似度矩阵时,可能存在复数类型的数据,导致计算时出现警告或异常。你可以尝试在计算相似度矩阵前,将特征向量转换为实数类型。你可以使用numpy的绝对值函数numpy.absolute()来将复数类型的数据转换为实数类型。你可以将以下代码:
```
# 将特征向量转化为矩阵
X = np.array(X)
# 计算相似度矩阵
S = np.dot(X, X.T)
# 归一化相似度矩阵
D = np.diag(np.sum(S, axis=1))
L = D - S
L_norm = np.dot(np.dot(np.sqrt(np.linalg.inv(D)), L), np.sqrt(np.linalg.inv(D)))
```
修改为:
```
# 将特征向量转化为矩阵并转换为实数类型
X = np.array(X)
X = np.absolute(X)
# 计算相似度矩阵
S = np.dot(X, X.T)
# 归一化相似度矩阵
D = np.diag(np.sum(S, axis=1))
L = D - S
L_norm = np.dot(np.dot(np.sqrt(np.linalg.inv(D)), L), np.sqrt(np.linalg.inv(D)))
```
这样修改后,应该就可以避免出现这个警告或异常了。