在 for 循环中创建新数组 (Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14327548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating new array in for loop (Python)
提问by ono
I'm preparing a data set to run in the program rpy (R, which runs in Python) for statistical analysis. It looks like this:
我正在准备要在程序 rpy(R,在 Python 中运行)中运行的数据集以进行统计分析。它看起来像这样:
data = [[0, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 1, , 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0]]
For me to use this data, I need to isolate the dependent variable (y) from the independent ones (x). I need to create a new list for each column for year as such:
为了使用这些数据,我需要将因变量 (y) 与独立变量 (x) 隔离开来。我需要为每年的每一列创建一个新列表,如下所示:
y = data[:,9]
x1 = data[:,0]
x2 = data[:,1]
x3 = data[:,2]
x4 = data[:,3]
x5 = data[:,4]
x6 = data[:,5]
x7 = data[:,6]
x8 = data[:,7]
x9 = data[:,8]
x10 = data[:,9]
Suppose my data has 67 columns. Is there a way to loop through all the columns and create each one automatically without having to type out all of them? I do not want to hard code all the arrays up to 67.
假设我的数据有 67 列。有没有办法循环遍历所有列并自动创建每一列而无需输入所有列?我不想将所有数组硬编码到 67。
Something along the lines of this, but it doesn't work:
类似的东西,但它不起作用:
i=0
for d in data:
"x%d"%i = data[:,i-1]
i+=1
This is the rest of the code:
这是其余的代码:
rpy.set_default_mode(rpy.NO_CONVERSION)
linear_model = rpy.r.lm(rpy.r("y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10"), data = rpy.r.data_frame(x1=x1,x2=x2,x3=x3,x4=x4,x5=x5,x6=x6,x7=x7,x8=x8,x9=x9,x10=x10,y=y))
rpy.set_default_mode(rpy.BASIC_CONVERSION)
print linear_model.as_py()['coefficients']
summary = rpy.r.summary(linear_model)
采纳答案by Master_Yoda
Why not try something like this to transpose the columns:
为什么不尝试这样的事情来转置列:
x = []
for d in xrange(0,66):
x.append(data[:,d])
Unless it's absolutely essential that there is a separate data structure for each item, although I don't know why you would need separate data strucures...
除非每个项目都有一个单独的数据结构是绝对必要的,尽管我不知道你为什么需要单独的数据结构......
EDIT: If not here's something that should work precisely the way you described:
编辑:如果不是这里的东西应该完全按照你描述的方式工作:
for d in xrange(1,68):
exec 'x%s = data[:,%s]' %(d,d-1)
回答by lgautier
As you show a little bit of the rpy code, I thought that I could show how it would look like with rpy2.
当您展示一些 rpy 代码时,我想我可以展示使用 rpy2 的样子。
# build a DataFrame
from rpy2.robjects.vectors import IntVector
d = dict(('x%i' % (i+1), IntVector(data[:, i]) for i in range(68) if i != 9)
d['y'] = data[:, 9]
from rpy2.robjects.vectors import DataFrame
dataf = DataFrame(d)
del(d) # dictionary no longer needed
# import R's stats package
from rpy2.robjects.packages import importr
stats = importr('stats')
# fit model
dep_var = 'y'
formula = '%s ~ %s ' % (dep_var, '+'.join(x for x in dataf.names if x != dep_var))
linear_model = stats.lm(formula, data = dataf)

