0

我是python的初学者,希望您能帮我解决我的问题。

我有两个文件 library.csv(9 列)和 case.csv(8 列),我用 np.loadtxt 读取它们。我从库中选择列将它们放入数组 base[] 中,除了最后一列,我将 case.csv 放入数组问题 [] 中。我会计算问题数组中每一行与基本 [] 数组的所有行之间的马氏距离,并将最小距离存储在表中。

这是我的代码:

# Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sn

from keras.models import load_model
from scipy.spatial import distance

# [1] Get the  library.csv and  cases.scv

library = np.loadtxt("library.csv", delimiter=",")
cases = np.loadtxt("cases.csv", delimiter=",")
problems = np.loadtxt("cases.csv", delimiter=",") #cases copie

# Select columns from library to use as base cases, except solutions
base = library[:, range(library.shape[1] - 1)] # Exclude last column (solution)

 # Move through all problem cases
for i in range(problems.shape[0]):
        
        # [3.1] Get inverse covariance matrix for the base cases

  covariance_matrix = np.cov(base)                                    # Covariance
  inverse_covariance_matrix = np.linalg.pinv(covariance_matrix)       # Inverse

        # [3.2] Get case row to evaluate
  case_row = problems[i, :]
  

        # Empty distances array to store mahalanobis distances obtained comparing each library cases
  distances = np.zeros(base.shape[0])

        # [3.3] For each base cases rows
  for j in range(base.shape[0]):
    # Get base case row
       base_row = base[j, :]

    # [3.4] Calculate mahalanobis distance between case row and base cases, and store it
       distances[j] = distance.mahalanobis(case_row, base_row, inverse_covariance_matrix)

# [3.5] Returns the index (row) of the minimum value in distances calculated
  min_distance_row = np.argmin(distances)

但我得到这个错误:

使用 TensorFlow 后端。

回溯(最后一次调用):
文件“C:\Users\HP\Desktop\MyAlgo\mainAlgo.py”,第 45 行,
距离 [j] = distance.mahalanobis(case_row, base_row, inverse_covariance_matrix)
文件“C:\ Users\HP\AppData\Local\Programs\Python\Python38\lib\site-packages\scipy\spatial\distance.py",第 1083 行,马哈拉诺比斯
m = np.dot(np.dot(delta, VI), delta )
文件“< array_function internals>”,第 5 行,点
ValueError:形状 (8,) 和 (384,384) 未对齐:8 (dim 0) != 384 (dim 0)

4

1 回答 1

0

您的问题似乎是长度为 8,base_rowcase_rowcovariance_matrix 包含 384 个变量,这些数字应该是相同的。因此,无法进行矩阵乘法。

我对您的数据和这里的统计属性缺乏了解,但我的猜测是您需要base在计算协方差矩阵之前进行转置。在调用np.cov(base)中的一行base应包含单个变量的所有观察值。 https://numpy.org/devdocs/reference/generated/numpy.cov.html

于 2020-06-21T19:27:03.843 回答