我想要反转从prcomp计算出的PCA以恢复原始数据.
我认为以下内容可行:
pca$x %*% t(pca$rotation)
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
以下链接显示了如何从PC恢复原始数据,但仅针对在协方差矩阵上使用eigen的PCA进行了解释 http://www.di.fc.ul.pt/~jpn/r/pca/pca.html
prcomp不会以这种方式对PC进行钙化.
"计算是通过(居中和可能缩放的)数据矩阵的奇异值分解完成的,而不是通过在协方差矩阵上使用特征来完成的." -prcomp
有没有人有关于使用scalaandra与scala的建议?没有本地scala-cassandra客户端支持cassandra版本8.0+,所以我必须使用hector,它似乎工作正常但不简洁.你有任何尝试,建议或任何包装代码,...等hector?
我有几十个条件(例如foo > bar),我需要在~1MM的a行上进行评估DataFrame,最简洁的写法是将这些条件存储为字符串列表并创建一个DataFrame布尔结果(每行一行)每个条件记录x一列).(未评估用户输入.)
在寻求过早优化的过程中,我试图确定是否应该将这些条件写入评估范围内DataFrame(例如,df.eval("foo > bar")或者只是将其保留为python,如同eval("df.foo > df.bar")
您不应将eval()用于简单表达式或涉及小型DataFrame的表达式.事实上,对于较小的表达式/对象,eval()比普通的'Python'要快许多个数量级.一个好的经验法则是当你有一个超过10,000行的DataFrame时才使用eval().
能够使用df.eval("foo > bar")语法会很好,因为我的列表会更具可读性,但我总是找不到评估速度不慢的情况.文档显示了哪里pandas.eval()比python更快的例子eval()(符合我的经验)但没有DataFrame.eval()(因为它被列为'实验').
例如,DataFrame.eval()在一个大型的非简单表达式中仍然是一个明显的输家DataFrame:
import pandas as pd
import numpy as np
import numexpr
import timeit
someDf = pd.DataFrame({'a':np.random.uniform(size=int(1e6)), 'b':np.random.uniform(size=int(1e6))})
%timeit -n100 someDf.eval("a**b - a*b > b**a - b/a") # DataFrame.eval() on notional expression
%timeit -n100 eval("someDf['a']**someDf['b'] - someDf['a']*someDf['b'] > someDf['b']**someDf['a'] …Run Code Online (Sandbox Code Playgroud) 我是新手reshape2,data.table并试图学习语法.
我有一个data.table我想从每个分组变量的多行转换为每个分组变量一行.为简单起见,我们让它成为一个客户表,其中一些客户共享地址.
library(data.table)
# Input table:
cust <- data.table(name=c("Betty","Joe","Frank","Wendy","Sally"),
address=c(rep("123 Sunny Rd",2),
rep("456 Cloudy Ln",2),
"789 Windy Dr"))
Run Code Online (Sandbox Code Playgroud)
我希望输出具有以下格式:
# Desired output looks like this:
(out <- data.table(address=c("123 Sunny Rd","456 Cloudy Ln","789 Windy Dr"),
cust_1=c("Betty","Frank","Sally"),
cust_2=c("Joe","Wendy",NA)) )
# address cust_1 cust_2
# 1: 123 Sunny Rd Betty Joe
# 2: 456 Cloudy Ln Frank Wendy
# 3: 789 Windy Dr Sally NA
Run Code Online (Sandbox Code Playgroud)
我想要cust_1 ... cust_n的列,其中n是每个地址的最大客户数.我并不关心订单 - Joe是否是cust_1而Betty是cust_2,反之亦然.
我正在尝试为Android应用创建一个离散的搜索栏.
我知道我可以为a设置最大值和最小值SeekBar(我的理想解决方案是下面的Click示例)但是我希望条形图以特定间隔在滑块上上下移动 - 在我的情况下每10个.因此,唯一可用的选择SeekBar是;
是否可以为标准Android定义特定值,SeekBar或者至少更改项目之间的间隔.如果没有,那么使用自定义搜索栏是否可行?我没有在网上发现任何解决此问题的内容(至少不适用于Android - 令人沮丧,因为它是Google Material设计指南中的公认组件).
我正在尝试编写一个R代码来返回矩阵中最大的连续数字对.连续对可以是水平的,垂直的和两个对角线.
例如,如果我有矩阵:
ma = array(c(8,4,3,1,7,5,9,15,6,10,16,11,2,14,12,13), dim = c(4,4))
Run Code Online (Sandbox Code Playgroud)
(1)水平的最高连续对:16和12; (2)垂直:16和11(3)对角线():16和13; (4)对角线(/):16和15.
我怎么能在R?
我想做一个直方图,其中填充颜色根据bin的低端而变化.我不想手动填充.这个答案似乎很有希望,但我无法将其成功转换为直方图和双值(非渐变)颜色方案.我相信解决方案可能是一些ifelse逻辑,geom_histogram(fill= )但我不知道如何访问bin起始值.
例如,在下面的直方图中,我想将收入超过10万美元的红色显示为高收入客户.
library(ggplot2)
library(scales)
n <- 10000
cust <- data.frame(cust_id=1:n,cust_rev <- rexp(n,.00001))
# I want to use a log scale for my tick marks and bin breaks
powers <- function(base,exp) sapply(1:exp, function(exp) base^exp )
ggplot(cust, aes(cust_rev)) +
geom_histogram(color="black",fill="light blue", binwidth=1/3) +
scale_x_log10(labels=comma, breaks=powers(10,8)) +
scale_y_continuous(labels=comma) +
xlab("Customer Revenue") + ylab("Number of Customers") +
ggtitle("Distribution of Customer Value")
Run Code Online (Sandbox Code Playgroud)

此外,我尝试使用第二个geom_histogram()进行解决方法,但未成功.
ggplot(cust, aes(x=cust_rev)) +
geom_histogram(color="black",fill="light blue", binwidth=1/3) +
geom_histogram(data=subset(cust,cust_rev>100000),
color="black",fill="red", binwidth=1/3) + …Run Code Online (Sandbox Code Playgroud) 有没有办法来直接指定的宽度和高度绘图区有ggplot2,例如3"和4"个下面的数字?

示例代码:
library(ggplot2)
x <- seq(-5,5,by=.1)
d <- data.frame(x=x,y=cos(x))
p <- ggplot(d) + aes(x=x,y=y) + geom_line()
ggsave(p,file='~/example.pdf')
Run Code Online (Sandbox Code Playgroud) 我正在努力构建一个基于生成的元组的值计数的DataFrame的基本任务np.unique(arr, return_counts=True),例如:
import numpy as np
import pandas as pd
np.random.seed(123)
birds=np.random.choice(['African Swallow','Dead Parrot','Exploding Penguin'], size=int(5e4))
someTuple=np.unique(birds, return_counts = True)
someTuple
#(array(['African Swallow', 'Dead Parrot', 'Exploding Penguin'],
# dtype='<U17'), array([16510, 16570, 16920], dtype=int64))
Run Code Online (Sandbox Code Playgroud)
首先我试过了
pd.DataFrame(list(someTuple))
# Returns this:
# 0 1 2
# 0 African Swallow Dead Parrot Exploding Penguin
# 1 16510 16570 16920
Run Code Online (Sandbox Code Playgroud)
我也试过pd.DataFrame.from_records(someTuple),它返回同样的东西.
但我正在寻找的是:
# birdType birdCount
# 0 African Swallow 16510
# 1 Dead Parrot 16570
# 2 Exploding Penguin 16920
Run Code Online (Sandbox Code Playgroud)
什么是正确的语法?
r ×5
python ×3
ggplot2 ×2
pandas ×2
android ×1
cassandra ×1
colors ×1
data.table ×1
dataframe ×1
eval ×1
histogram ×1
if-statement ×1
matrix ×1
numpy ×1
performance ×1
phantom-dsl ×1
pivot-table ×1
pmml ×1
reshape2 ×1
scala ×1
scikit-learn ×1