0

我有一个数据库,每个案例都包含有关手写数字的信息,例如:

Digit1Seq :当在 12 位数字序列中绘制“1”时

Digit1Ht:数字“1”的高度

Digit1Width:它的宽度

Digit2Seq:数字“2”的相同信息

最多数字“12”

我发现我现在也需要以不同方式组织的信息。特别是我想要一个新变量,其中写入第一个数字的高度和宽度,然后是第二个数字的高度和宽度等,作为 SPSS vars

第一位数字

第一个数字宽度 ...

第十二位宽度

这是我在 SPSS 中编写的一个 Python 程序,它应该是一个非常简单的计算,但它遇到了一种命名空间问题:

BEGIN PROGRAM PYTHON.

import spss
indices = ["1", "2", "3","4","5", "6", "7", "8", "9", "10", "11", "12"]
seq=0
for i in indices:
  spss.Submit("COMPUTE seq = COMDigit" + i + "Seq.")
  spss.Submit("EXECUTE.")
  spss.Submit("COMPUTE COM" + indices[seq] + "thWidth =  COMDigit" + i + "Width.")
  spss.Submit("COMPUTE COM" + indices[seq] + "thHgt =  COMDigit" + i + "Hgt.")
  spss.Submit("EXECUTE.")

END PROGRAM.

很清楚这里出了什么问题:seq第一个COMPUTE命令中的值不会返回到 Python,因此在接下来的两个COMPUTE命令中可能会发生正确的事情。Python 的值seq不会改变,所以我最终得到的 SPSS 代码只给了我两个变量 (COM1thWidthCOM1Hgt),其中COMDigit1Width,COMDigit2Width等被写入。

有什么方法可以让 Pythonseq每次访问 SPSS 的值,以便字符串连接将创建正确的值COMPUTE?还是我只是想错了?

已经广泛搜索,但找不到办法做到这一点。

由于我是在 SPSS 中使用 Python 的新手(而不是 SPSS 中的所有 wiz),因此很可能有一种更简单的方法来做到这一点。

欢迎所有建议。

4

3 回答 3

1

将 SPSS 变量数据转换为 Python 变量进行操作的最简单方法可能是使用 spss.Dataset 类。

为此,您需要: 1.) SPSS Dataset 的数据集名称 2.) 要从中提取数据的变量的名称或其在数据集中的索引。

如果您要从中提取数据的变量的名称被命名为“seq”(我相信它在您的问题中),那么您可以使用类似的东西:

BEGIN PROGRAM PYTHON.
from __future__ import with_statement
import spss
with spss.DataStep()
    #the lines below create references to your dataset,
    #to its variable list, and to its case data
    lv_dataset = spss.Dataset(name = <name of your SPSS dataset>)
    lv_caseData = lv_dataset.cases
    lv_variables = lv_dataset.varlist

    #the line below extracts all the data from the SPSS variable named 'seq' in the dataset referenced above into a list
    #to make use of an SPSS cases object, you specify in square brackets which rows and which variables to extract from, such as:
    #Each row you request to be extracted will be returned as a list of values, one value for each variable you request data for
    #lv_theData = lv_caseData[rowStartIndex:rowEndIndex, columnStartIndex:columnEndIndex]

    #This means that if you want to get data for one variable across many rows of data, you will get a list for each row of data, but each row's list will have only one value in it, hence in the code below, we grab the first element of each list returned
    lv_variableData = [itm[0] for itm in lv_caseData[0:len(lv_caseData), lv_variables['seq'].index]]

END PROGRAM.
于 2015-03-15T06:22:42.470 回答
0

There are lots of ways to process the case data held by Statistics via Python, but the case data has to be read explicitly using the spss.Cursor, spssdata.Spssdata, or spss.Dataset class. It does not live in the Python namespace.

In this case the simplest thing to do would be to just substitute the formula for seq into the later references. There are many other ways to tackle this.

Also, get rid of those EXECUTE calls. They just force unnecessary data passes. Statistics will automatically pass the data when it needs to based on the command stream.

于 2013-07-26T16:56:06.227 回答
0

嗨,我只是偶然发现了这一点,您可能已经继续前进,但这可能对其他人有所帮助。我不认为您实际上需要访问让 Python 访问 SPSS 值。我认为这样的事情可能会奏效:

BEGIN PROGRAM PYTHON.

import spss

for i in range(1,13):
  k = "COMPUTE seq = COMDigit" + str(i) + "Seq."
  l = "Do if seq = " + str(i)+ "."
  m = "COMPUTE COM" + str(i) + "thWidth =  COMDigit" + str(i) + "Width."
  n = "COMPUTE COM" + str(i) + "thHgt =  COMDigit" + str(i) + "Hgt."
  o = "End if."
  print k
  print l
  print m
  print n
  print o
  spss.Submit(k)
  spss.Submit(l)
  spss.Submit(m)
  spss.Submit(n)
  spss.Submit(o)

spss.Submit("EXECUTE.")
END PROGRAM.

但我必须查看数据以确保我正确理解您的问题。此外,打印的东西使代码看起来很难看,但这是我可以掌握引擎盖下发生的事情的唯一方法。干杯!

于 2014-04-24T18:50:03.647 回答