gpt4 book ai didi

python - 在 SPSS Python Essentials 中,我可以获取返回给 Python 的 SPSS 变量的值以供进一步使用吗?

转载 作者:行者123 更新时间:2023-12-04 17:04:24 28 4
gpt4 key购买 nike

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

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等得到写入。

有什么方法可以让 Python 访问 SPSS 的值 seq每次这样字符串连接都会创建正确的 COMPUTE ?还是我只是在错误地思考这个问题?

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

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

欢迎提出所有建议。

最佳答案

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

为此,您将需要:
1.) SPSS 数据集的数据集名称
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.

关于python - 在 SPSS Python Essentials 中,我可以获取返回给 Python 的 SPSS 变量的值以供进一步使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17866247/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com