我正在尝试对高斯混合(高斯混合模型)的均值和协方差的估计进行动画处理,我需要在每次迭代时更新均值和协方差的图。
重绘手段非常简单,因为我使用的行具有set_data可以在每次更新时调用的方法。不幸的是,更新协方差是另一回事,因为contour元素被表示为QuadContourSet对象并且没有set_data方法。
这是一个玩具示例:
import numpy as np
from matplotlib import mlab
# Toy data points (these are constant)
plt.plot(np.arange(-3,3,0.1), np.arange(-3,3,0.1))
x = np.arange(-5.0, 5.0, 0.1)
y = np.arange(-5.0, 5.0, 0.1)
X, Y = np.meshgrid(x, y)
# First toy iteration
Z1 = mlab.bivariate_normal(X, Y,
1, 1,
0, 0)
covariance1 = plt.contour(X, Y, Z1)
# Second toy iteration
Z2 = mlab.bivariate_normal(X, Y,
1, 1,
0, 3)
covariance2 = plt.contour(X, Y, Z2)
Run Code Online (Sandbox Code Playgroud)

在我绘制均值、方差和数据点的实际问题中,我不想清除整个轴。
问题是如何在covariance1 …
我想绘制这样的东西(来自本文),其中图标(在本例中为小图)用作刻度标签.

我到目前为止,图标或多或少正确放置:
这是代码:
library(igraph)
npoints <- 15
y <- rexp(npoints)
x <- seq(npoints)
par(fig=c(0.05,1,0.3,1), new=FALSE)
plot(y, xlab=NA, xaxt='n', pch=15, cex=2, col="red")
lines(y, col='red', lwd=2)
xspan <- 0.9
xoffset <- (0.07+0.5/npoints)*xspan
for(i in 1:npoints){
x1 <- (xoffset+(i-1)/npoints)*xspan
x2 <- min(xspan*(xoffset+(i)/npoints),1)
par(fig=c(x1,x2,0,0.5), new=TRUE)
plot(graph.ring(i), vertex.label=NA)
}
Run Code Online (Sandbox Code Playgroud)
但是,如果点数增加(例如npoints <- 15)它会抱怨,因为图标没有位置:
Error in plot.new() : figure margins too large
Run Code Online (Sandbox Code Playgroud)
我不知道有没有更自然的方法来做到这一点,以便它适用于任何(合理的)点数.
欢迎任何建议.
我在PCA转换后进行聚类,我想在PCA空间的前两个或三个维度中可视化聚类的结果,以及从原始轴到投影的PCA的贡献.
我使用的factoextra是使用ggplot 的库,它工作正常,但我想关掉传说:
我的代码:
# Load iris dataset
data(iris)
# PCA
pca <- prcomp(iris[,-5], scale=TRUE)
df.pca <- pca$x
# Cluster over the three first PCA dimensions
kc <- kmeans(df.pca[,1:3], 5)
# 2-D biplot (how to get rid of legend?)
# install.packages("devtools")
# library("devtools")
# install_github("kassambara/factoextra")
library(factoextra)
fviz_pca_biplot(pca, label="var", habillage=as.factor(kc$cluster)) +
labs(color=NULL) + ggtitle("") +
theme(text = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
legend.key = element_rect(fill …Run Code Online (Sandbox Code Playgroud) 鉴于两个ggplots,我们可以并排排列它们
library(ggplot2)
library(gridExtra)
# Data
data(iris)
df <- data.frame(y = rnorm(100))
# Plotting
p1 <- qplot(data=iris, Sepal.Width, Sepal.Length)
p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line()
grid.arrange(p1, p2, ncol=2)
Run Code Online (Sandbox Code Playgroud)
如果其中一个图是一个ggpair对象,我们怎么能这样做呢?
library(GGally)
p1 <- ggpairs(iris, colours='Species')
p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line()
grid.arrange(l, p2, ncol=6)
# Error in gList(list(list(data = list(Sepal.Length = c(5.1, 4.9, 4.7,
# 4.6, : only 'grobs' allowed in "gList"
Run Code Online (Sandbox Code Playgroud) 假设我收集 Stack Overflow 中的帖子,并将它们分类为 N 个类别。我的目标是每天绘制 N 个百分比和一条包含每天帖子总数的线。
为了玩,我将使用一个玩具数据框。我可以绘制每天每个类别的百分比:
data(beav1)
beav1$day <- as.factor(beav1$day)
beav1[beav1$day==346,]$time <- 1:sum(beav1$day==346)
beav1[beav1$day==347,]$time <- 1:sum(beav1$day==347)
beav1 <- filter(beav1, time<23)
ggplot(beav1, aes(x=time, y=temp, group=day, fill=day, color=day)) +
geom_line()
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能加上总温度的线呢?还是均值?
编辑:与另一个问题的不同之处在于,我希望所有组都使用一行,而不是每组一行。
数据集
dput(beav1)
structure(list(day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, …Run Code Online (Sandbox Code Playgroud) 我知道如何将两个 id.vars 与melt. 这很简单:
x = data.frame(subject = c("John", "Mary"),
time = c(1,1),
age = c(33,35),
weight = c(90, 67),
height = c(2,2))
melt(x, id.vars = c('subject', 'time'), measure.vars = c('age', 'weight', 'height'))
# subject time variable value
#1 John 1 age 33
#2 Mary 1 age 35
#3 John 1 weight 90
#4 Mary 1 weight 67
#5 John 1 height 2
#6 Mary 1 height 2
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能做同样的事情(使用两个 id.vars 或key)gather呢?
gather(data, key, value, …Run Code Online (Sandbox Code Playgroud) 我想在访问RcppArmadillo中的矩阵元素时禁用绑定检查.
犰狳的文件说
可以通过编辑文件include/armadillo_bits/config.hpp来配置Armadillo.通过取消注释或注释掉下面列出的特定#define,可以启用或禁用特定功能.
但是在R包的上下文中,我该如何激活该指令?
我试过用它来创建一个config.h 文件
#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif
Run Code Online (Sandbox Code Playgroud)
然后将它包含在我的文件夹的每个 .cpp文件中/src,但我不确定它是否正常工作,或者除了#include "config.h"在每个.cpp文件中添加一个以外还有其他方法.
目前我有一个.cpp(包含主算法的那个),它以:
#include "configs.h"
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
SEXP sample_gibbs_cpp(const arma::vec& v_n, const arma::mat& W,
arma::vec h_n, double alpha = 1, double beta = 1, int iter=100,
double burnin = 0.5){
... code ...
}
Run Code Online (Sandbox Code Playgroud)
然后是其他人
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
... code ...
Run Code Online (Sandbox Code Playgroud)
我的描述文件:
Package: mypackage
Title: What the …Run Code Online (Sandbox Code Playgroud) 例如,我有以下玩具图表示论坛主题:
import igraph as ig
g = ig.Graph(n = 12, directed=True)
g.add_edges([(1,0),(2,1), (3,2), (4,3),
(5,1),
(6,2), (7,6), (8,7),
(9,0),
(10,0), (11,10)])
g.vs["label"] = ["A", "B", "A", "B", "C", "F", "C", "B", "D", "C", "D", "F"]
ig.plot(g, layout="kk")
Run Code Online (Sandbox Code Playgroud)
但是,似乎没有将根顶点(id 0,标签 A)置于顶部并向下生长的布局。
我错过了什么吗?
我有以下正态分布点:
import numpy as np
from matplotlib import pylab as plt
from matplotlib import mlab
mean_test = np.array([0,0])
cov_test = array([[ 0.6744121 , -0.16938146],
[-0.16938146, 0.21243464]])
Run Code Online (Sandbox Code Playgroud)
协方差矩阵是确定的半正,因此它可以用作协方差
# Semi-positive definite if all eigenvalues are 0 or
# if there exists a Cholesky decomposition
print np.linalg.eigvals(cov_test)
print np.linalg.cholesky(cov_test)
Run Code Online (Sandbox Code Playgroud)
[0.72985988 0.15698686]
[[0.82122597 0.] [-0.20625439 0.41218172]]
如果我产生一些积分,我得到:
data_test = np.random.multivariate_normal(mean_test, cov_test, 1000)
plt.scatter(data_test[:,0],data_test[:,1])
Run Code Online (Sandbox Code Playgroud)

问题:
bivariate_normal当我尝试绘制协方差轮廓时,为什么方法失败(返回NaNs)?
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z …Run Code Online (Sandbox Code Playgroud) 矩阵中最大值的位置(行和列)可以通过以下方式找到:
ma <- matrix(1:50, nrow = 5)
which(ma == max(ma), arr.ind = TRUE)
Run Code Online (Sandbox Code Playgroud)
如果我们不只想要最大值的坐标,而是想要 N 个最大值的坐标怎么办?
就像是:
order(ma, arr.ind = TRUE, decreasing = TRUE)[1:N] # this does not exist :(
Run Code Online (Sandbox Code Playgroud)