def gen_easytest(plot=True):
# set name
name = "easytest"
n = 10
# set generative parameters
mu1 = np.array([0,0])
sig1 = np.eye(2)
n1 = n
mu2 = np.array([math.sqrt(75),5])
sig2 = np.eye(2)
n2 = n
mu3 = np.array([0,10])
sig3 = np.eye(2)
n3 = n
param = {'mu1': mu1, 'sig1': sig1, 'n1': n1,
'mu2': mu2, 'sig2': sig2, 'n2': n2,
'mu3': mu3, 'sig3': sig3, 'n3': n3}
# make labels
labels = np.array([0]*n1+[1]*n2+[2]*n3)
# make coordinates
coord = np.concatenate((np.random.multivariate_normal(mu1,sig1,n1),
np.random.multivariate_normal(mu2,sig2,n2),
np.random.multivariate_normal(mu3,sig3,n3)))
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
python类multivariate_normal()的实例源码
def gen_blob(plot=True):
# set name
name = "blob"
# set generative parameters
mu1 = np.array([0,0])
sig1 = np.eye(2)
n1 = 90
param = {'mu1': mu1, 'sig1': sig1, 'n1': n1}
# make labels
labels = np.array([0]*n1)
# make coordinates
coord = np.random.multivariate_normal(mu1,sig1,n1)
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
def gen_3sph_evensamp_evenspacing(plot=True):
# set name
name = "3sph_evensamp_evenspacing"
# set generative parameters
mu1 = np.array([0,0])
sig1 = np.eye(2)
n1 = 30
mu2 = np.array([math.sqrt(75),5])
sig2 = np.eye(2)
n2 = 30
mu3 = np.array([0,10])
sig3 = np.eye(2)
n3 = 30
param = {'mu1': mu1, 'sig1': sig1, 'n1': n1,
'mu2': mu2, 'sig2': sig2, 'n2': n2,
'mu3': mu3, 'sig3': sig3, 'n3': n3}
# make labels
labels = np.array([0]*n1+[1]*n2+[2]*n3)
# make coordinates
coord = np.concatenate((np.random.multivariate_normal(mu1,sig1,n1),
np.random.multivariate_normal(mu2,sig2,n2),
np.random.multivariate_normal(mu3,sig3,n3)))
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
def gen_3sph_unevensamp_evenspacing(plot=True):
# set name
name = "3sph_unevensamp_evenspacing"
# set generative parameters
mu1 = np.array([0,0])
sig1 = np.eye(2)
n1 = 10
mu2 = np.array([math.sqrt(75),5])
sig2 = np.eye(2)
n2 = 30
mu3 = np.array([0,10])
sig3 = np.eye(2)
n3 = 60
param = {'mu1': mu1, 'sig1': sig1, 'n1': n1,
'mu2': mu2, 'sig2': sig2, 'n2': n2,
'mu3': mu3, 'sig3': sig3, 'n3': n3}
# make labels
labels = np.array([0]*n1+[1]*n2+[2]*n3)
# make coordinates
coord = np.concatenate((np.random.multivariate_normal(mu1,sig1,n1),
np.random.multivariate_normal(mu2,sig2,n2),
np.random.multivariate_normal(mu3,sig3,n3)))
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
def gen_3sph_evensamp_unevenspacing(plot=True):
# set name
name = "3sph_evensamp_unevenspacing"
# set generative parameters
mu1 = np.array([0,2.5])
sig1 = np.eye(2)
n1 = 30
mu2 = np.array([0,-2.5])
sig2 = np.eye(2)
n2 = 30
mu3 = np.array([15,0])
sig3 = np.eye(2)
n3 = 30
param = {'mu1': mu1, 'sig1': sig1, 'n1': n1,
'mu2': mu2, 'sig2': sig2, 'n2': n2,
'mu3': mu3, 'sig3': sig3, 'n3': n3}
# make labels
labels = np.array([0]*n1+[1]*n2+[2]*n3)
# make coordinates
coord = np.concatenate((np.random.multivariate_normal(mu1,sig1,n1),
np.random.multivariate_normal(mu2,sig2,n2),
np.random.multivariate_normal(mu3,sig3,n3)))
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
def gen_2cigars(plot=True):
# set name
name = "2cigars"
# set generative parameters
mu1 = np.array([0,-4])
sig1 = np.array([[25,0],[0,1]])
n1 = 50
mu2 = np.array([0,4])
sig2 = np.array([[25,0],[0,1]])
n2 = 50
param = {'mu1': mu1, 'sig1': sig1, 'n1': n1,
'mu2': mu2, 'sig2': sig2, 'n2': n2}
# make labels
labels = np.array([0]*n1+[1]*n2)
# make coordinates
coord = np.concatenate((np.random.multivariate_normal(mu1,sig1,n1),
np.random.multivariate_normal(mu2,sig2,n2)))
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
def gen_halfconcentric(plot=True):
# set name
name = "halfconcentric"
# set generative parameters
nt = 80 # number of thetas
nd = 1 # number of samples per theta
no = nd*nt # number of samples for outer circle
ni = 20 # number of samples for inner circle
r = 5 # radius of outer loop
so = .25 # gaussian noise variance of outer circle
si = .25 # gaussian noise variance of inner circle
thetas = -np.linspace(0,math.pi,nt)
x = [r*math.cos(theta) for theta in thetas]
y = [r*math.sin(theta) for theta in thetas]
param = {'nt': nt, 'nd': nd, 'no': no, 'ni': ni, 'r': r, 'so': so, 'si': si}
# make labels
labels = np.array([0]*ni+[1]*no)
# make coordinates
coord = np.random.multivariate_normal(np.array([0,0]),si*np.eye(2),ni)
for i in range(len(x)):
coord = np.concatenate((coord,np.random.multivariate_normal(np.array([x[i],y[i]]),so*np.eye(2),nd)))
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
def gen_concentric(plot=True):
# set name
name = "concentric"
# set generative parameters
nt = 80 # number of thetas
nd = 1 # number of samples per theta
no = nd*nt # number of samples for outer circle
ni = 20 # number of samples for inner circle
r = 8 # radius of outer loop
so = .25 # gaussian noise variance of outer circle
si = .25 # gaussian noise variance of inner circle
thetas = -np.linspace(0,2*math.pi,nt)
x = [r*math.cos(theta) for theta in thetas]
y = [r*math.sin(theta) for theta in thetas]
param = {'nt': nt, 'nd': nd, 'no': no, 'ni': ni, 'r': r, 'so': so, 'si': si}
# make labels
labels = np.array([0]*ni+[1]*no)
# make coordinates
coord = np.random.multivariate_normal(np.array([0,0]),si*np.eye(2),ni)
for i in range(len(x)):
coord = np.concatenate((coord,np.random.multivariate_normal(np.array([x[i],y[i]]),so*np.eye(2),nd)))
# make dataset
ds = dataset(coord = coord, labels = labels, gen_param = param, name = name)
# plot coordinates
if plot: ds.plot_coord()
# normalize
ds.normalize_coord()
if plot: ds.plot_coord()
return ds
# CODE BELOW NOT YET ADAPTED TO USE NEW IB DATASET CLASS
# GENERATE ONLY P(X,Y)
compute_class_distr_GMM.py 文件源码
项目:Sohu-LuckData-Image-Text-Matching-Competition
作者: WeitaoVan
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def create_mnormal_distr(mu, cov):
'''
mu, var are parameters of GMM
'''
K = mu.shape[0]
mnormal_distrs = []
for k in range(K):
mnormal_distrs.append(mnormal(mean=mu[k, :], cov=cov[k, ...]))
return mnormal_distrs
def exp6(num_data=1000):
if num_data < 2:
raise ValueError('num_data should be larger than 2. (num_data = {})'.format(num_data))
center = -5
sigma_x = 7
sigma_y = 7
n1 = num_data
# init data
d1x = torch.FloatTensor(n1, 1)
d1y = torch.FloatTensor(n1, 1)
d1x.normal_(center, sigma_x)
d1y.normal_(center, sigma_y)
d1 = torch.cat((d1x, d1y), 1)
d = d1
# label
label = torch.IntTensor(num_data).zero_()
label[:] = 0
# shuffle
#shuffle = torch.randperm(d.size()[0])
#d = torch.index_select(d, 0, shuffle)
#label = torch.index_select(label, 0, shuffle)
# pdf
rv1 = multivariate_normal([ center, center], [[math.pow(sigma_x, 2), 0.0], [0.0, math.pow(sigma_y, 2)]])
def pdf(x):
prob = (float(n1) / float(num_data)) * rv1.pdf(x)
return prob
def sumloglikelihood(x):
return np.sum(np.log((pdf(x) + 1e-10)))
return d, label, sumloglikelihood
def simulate_a(self, N):
"""
Assuming that the u's are normal, this method draws a random path
for x^N
"""
V = self.construct_V(N + 1)
d = spst.multivariate_normal(np.zeros(N + 1), V)
return d.rvs()
def _init_params(self, X):
init = self.init
n_samples, n_features = X.shape
n_components = self.n_components
if (init == 'kmeans'):
km = Kmeans(n_components)
clusters, mean, cov = km.cluster(X)
coef = sp.array([c.shape[0] / n_samples for c in clusters])
comps = [multivariate_normal(mean[i], cov[i], allow_singular=True)
for i in range(n_components)]
elif (init == 'rand'):
coef = sp.absolute(sprand.randn(n_components))
coef = coef / coef.sum()
means = X[sprand.permutation(n_samples)[0: n_components]]
clusters = [[] for i in range(n_components)]
for x in X:
idx = sp.argmin([spla.norm(x - mean) for mean in means])
clusters[idx].append(x)
comps = []
for k in range(n_components):
mean = means[k]
cov = sp.cov(clusters[k], rowvar=0, ddof=0)
comps.append(multivariate_normal(mean, cov, allow_singular=True))
self.coef = coef
self.comps = comps
def from_gaussian(z, R, N):
"""Init new PF from gaussian prior."""
var = multivariate_normal(z, R)
s = var.rvs(N)
w = np.full((N,), 1/N)
return PF((w, s))
def sample_normal(Q, N):
"""Generate random samples and their weights."""
var = multivariate_normal(np.zeros(Q.shape[0]), Q)
s = var.rvs(N)
return s
def sample(self, n, seed=3):
with util.NumpySeedContext(seed=seed):
mvn = stats.multivariate_normal(self.mean, self.cov)
X = mvn.rvs(size=n)
if len(X.shape) ==1:
# This can happen if d=1
X = X[:, np.newaxis]
return Data(X)
def load_lip_data_t2(image_id, flip=False, is_test=False):
fine_size=64
image_id = image_id[:-1]
image_path = './datasets/human/masks/{}.png'.format(image_id)
img_A = scipy.misc.imread(image_path).astype(np.float)
rows = img_A.shape[0]
cols = img_A.shape[1]
img_A = scipy.misc.imresize(img_A, [fine_size, fine_size])
img_B = np.zeros((fine_size, fine_size), dtype=np.float64)
with open('./datasets/human/pose/{}.txt'.format(image_id), 'r') as f:
lines = f.readlines()
points = lines[0].split(',')
for idx, point in enumerate(points):
if idx % 2 == 0:
c_ = int(point)
c_ = min(c_, cols-1)
c_ = max(c_, 0)
c_ = int(fine_size * 1.0 * c_ / cols)
else:
r_ = int(point)
r_ = min(r_, rows-1)
r_ = max(r_, 0)
r_ = int(fine_size * 1.0 * r_ / rows)
if c_ + r_ == 0:
continue
var = multivariate_normal(mean=[r_, c_], cov=2)
for i in xrange(fine_size):
for j in xrange(fine_size):
img_B[i, j] += var.pdf([i, j]) * 1.0
img_A = img_A/127.5 - 1.
img_BA = np.concatenate((img_B[:,:,np.newaxis], img_A), axis=2)
# print img_BA.shape
# img_AB shape: (fine_size, fine_size, input_c_dim + output_c_dim)
return img_BA
#------------------------------------------------------------
def gaussian_heatmap(h, w, pos_x, pos_y, sigma_h=1, sigma_w=1, init=None):
"""
Compute the heat-map of size (w x h) with a gaussian distribution fit in
position (pos_x, pos_y) and a convariance matix defined by the related
sigma values.
The resulting heat-map can be summed to a given heat-map init.
"""
init = init if init is not None else []
cov_matrix = np.eye(2) * ([sigma_h**2, sigma_w**2])
x, y = np.mgrid[0:h, 0:w]
pos = np.dstack((x, y))
rv = multivariate_normal([pos_x, pos_y], cov_matrix)
tmp = rv.pdf(pos)
hmap = np.multiply(
tmp, np.sqrt(np.power(2 * np.pi, 2) * np.linalg.det(cov_matrix))
)
idx = np.where(hmap.flatten() <= np.exp(-4.6052))
hmap.flatten()[idx] = 0
if np.size(init) == 0:
return hmap
assert (np.shape(init) == hmap.shape)
hmap += init
idx = np.where(hmap.flatten() > 1)
hmap.flatten()[idx] = 1
return hmap
def fit(self, X: pd.DataFrame, w: np.ndarray):
if len(X) == 0:
raise NotEnoughParticles("Fitting not possible.")
self._X_arr = X.as_matrix()
sample_cov = smart_cov(self._X_arr, w)
dim = sample_cov.shape[0]
eff_sample_size = 1 / (w**2).sum()
bw_factor = self.bandwidth_selector(eff_sample_size, dim)
self.cov = sample_cov * bw_factor**2 * self.scaling
self.normal = st.multivariate_normal(cov=self.cov, allow_singular=True)
def rvs_single(self):
sample = self.X.sample(weights=self.w).iloc[0]
perturbed = (sample +
np.random.multivariate_normal(
np.zeros(self.cov.shape[0]), self.cov))
return perturbed
def likelihood_statistics(self, samples):
s0, s1, s2 = {}, {}, {}
samples = zip(range(0, len(samples)), samples)
gaussians = {}
g = [multivariate_normal(mean=self.means[k],cov=self.covs[k]) for k in xrange(0, len(self.weights))]
for i,x in samples:
gaussians[i] = {k : g[k].pdf(x) for k in range(0, len(self.weights) ) }
for k in xrange(0, len(self.weights)):
s0[k] = reduce(lambda a, (i, x): a + self.likelihood_moment(x, gaussians[i], self.weights, k, 0), samples, 0)
s1[k] = reduce(lambda a, (i, x): a + self.likelihood_moment(x, gaussians[i], self.weights, k, 1), samples, 0)
s2[k] = reduce(lambda a, (i, x): a + self.likelihood_moment(x, gaussians[i], self.weights, k, 2), samples, 0)
return s0, s1, s2