ValueError: отрицательные измерения не допускаются при создании выборок из многомерного нормального распределения - PullRequest
0 голосов
/ 18 мая 2018

Я хочу генерировать выборки из многомерного нормального распределения в numpy.Вот мой код:

import numpy as np
import random
from random import uniform
import math
from sklearn.datasets import make_spd_matrix

k = 5   # how many classes
N = 20   # how many samples
x_min = -20
x_max = 20
y_min = -20
y_max = 20


def main():

w = np.random.random(k)    # generate k random numbers
w /= w.sum() # divide by their sum
# now these sum to 1
#print(w)  # actual weight vector

number_of_divisions_x = number_of_divisions_y = math.ceil(math.sqrt(k))
x_div = (x_max - x_min)/number_of_divisions_x
y_div = (y_max - y_min)/number_of_divisions_y

# divide a grid
# x = x_min to x_max
# y = y_min to y_max

# now form the grid and put the means

points = []  # the list of actual means

# now generate means
for i in range(number_of_divisions_x):
    for j in range(number_of_divisions_y):
        if len(points) == k:   # after generating k points, break the loop
            break
        point = [x_min + i * x_div, y_min + j * y_div]  # array (not tuple) is needed for the function
        points.append(point)

    if len(points) == k:  # after generating k points, break the loop
        break  

samples = []  # list containing all the samples
actual_sample = [None] * k   # how many samples in each class actually. Stored for later weight correction

covs = []    # actual covariances

for i in range(k):

    number_of_samples = math.ceil(N * w[i])  # number of samples from class i, generate at least one sample
    actual_sample[i] = number_of_samples  # for later weight correction

    if i == k-1:
        number_of_samples = N - sum(actual_sample[:k-1])   # for the last class, take all the remaining samples
        actual_sample[i] = number_of_samples

    mean = points[i]

    # generate covariance matrix

    cov = make_spd_matrix(2)   # Generate a random symmetric, positive-definite matrix, whose size is 2x2

    covs.append(cov)

    file = open("class " + str(i+1) + ".txt", "w")  # class 1, 2, 3 etc
    file.write(str(mean) + "\n\n")
    file.write(str(cov))
    file.close() # save the actual mean and covariance to file

    s = np.random.multivariate_normal(mean, cov, number_of_samples)  # list of lists
    for element in s:
        e = list(element)
        samples.append(e)   # make list and insert to the main list

Если я запускаю код около 10 раз, он выполняется без ошибок 9 раз, но показывает ошибку во время одного запуска.

s = np.random.multivariate_normal(mean, cov, number_of_samples)  # list of lists

File "mtrand.pyx", line 4508, in mtrand.RandomState.multivariate_normal 
File "mtrand.pyx", line 1550, in mtrand.RandomState.standard_normal
File "mtrand.pyx", line 167, in mtrand.cont0_array

ValueError: negative dimensions are not allowed

Итакошибка возникает из-за генерации случайных чисел из многомерного нормального распределения.Я сгенерировал ковариационную матрицу, чтобы она была положительно определенной, используя make_spd_matrix(), поэтому определитель ковариационной матрицы всегда должен быть положительным.Тогда почему я иногда получаю ошибку?Вот список средств:

[[-20.0, -20.0], [-20.0, -6.666666666666666], [-20.0, 6.666666666666668], [-6.666666666666666, -20.0], [-6.666666666666666, -6.666666666666666]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...