fisher_score = np.sum(eig_val[:mfcc_order]/np.sum(eig_val))中的mfcc_order取值
时间: 2023-03-14 13:00:30 浏览: 106
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%。
# 对图像进行标准化处理 means = np.mean(img, axis=0).reshape(1, -1) std = np.std(img, axis=0).reshape(1, -1) img_std = (img - means) / std # 进行主成分分析 eigValue, eigVector = np.linalg.eig(np.cov(img_std)) w_args = np.flip(np.argsort(eigValue)) eigVector = eigVector[:, w_args] eigValue = eigValue[w_args] # 按照指定的维度进行降维处理 k = int((img[0, :]).size / 4) C = np.matmul(img_std, eigVector) img_new = np.matmul(C[:, :k], eigVector.T[:k, :]) img_new = img_new * std + means im = Image.fromarray(img_new) im = im.convert('L') im.show() im.save(r'E:\资料\课程\\biu\大数据分析\实验\实验5\test-new0.25.jpg')
这段代码是对图像进行标准化处理、主成分分析和降维操作,并保存为新的图像。首先,通过计算图像的均值和标准差,对图像进行标准化处理。然后,使用主成分分析(PCA)方法计算图像的协方差矩阵,并得到特征值和特征向量。接下来,将特征值按降序排序,并将特征向量按相应顺序重新排列。根据指定的维度k,将标准化后的图像矩阵与特征向量相乘,得到降维后的图像矩阵。最后,将降维后的图像矩阵乘以标准差并加上均值,得到恢复后的图像。该图像会以灰度图像的形式进行展示,并保存到指定路径中。
阅读全文
相关推荐














