为什么我不能手动复制 nd 数组? [英] Why can I not reproduce a nd array manually?

查看:50
本文介绍了为什么我不能手动复制 nd 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这些数据结构感到困惑.

从 GIS 系统中,我使用一个函数来提取元数据(8 个不同的字段)

myList = FeatureClassToNumPyArray(...)myList = [('a', 'b', 'c'...) ('aa', 'bb', 'cc'...) ..] # 8 个字段打印(类型(myList))打印(myList.shape)打印(myList.size)

这会产生:

(1, 9893)9893# 我期望得到(9893 行 x 8 列),如 (8,9893)# 或 (9893, 8),但无论如何,我们现在不要担心.

所以我试试这个:

<预><代码>>>>source = [('a', 'b', 'c') ('aa', 'bb', 'cc')]回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:元组"对象不可调用

但是加入逗号分隔符,这很好...但现在它是一个列表.

<预><代码>>>>source = [('a', 'b', 'c'), ('aa', 'bb', 'cc')]>>>类型(来源)<类'列表'>

所以,这个神奇的 GIS 函数可以生成一个被接受为 numpy 数据数组的数据结构,但是如果我尝试手动创建它,这是不可能的.

我错过了什么?

解决方案

不知道该怎么做,但是 juanpa.arrivillaga 的评论应该标记为答案.

再说一次,你为什么期望 print(something) 生成一个字符串,该字符串是生成该对象的有效 python 源代码?那是你的基本假设是错误的.这就是你所缺少的.print(repr(something)) 通常会让你更接近一些东西,但永远不能保证它是有效的源代码.同样,它可能会返回一个带有一些结构化数据类型的 numpy.ndarray.myList.dtype 是什么?这是一个非常基本的例子,something = object();print(something) 现在尝试从字符串表示中重现它......没有理由期望能够做到.– juanpa.arrivillaga 5 月 13 日 21:18

我的问题源于对数据帧是什么及其工作原理的根本误解.我花了一些时间才弄明白.

用我的话来说,它是一个需要通过各种工具/功能进行操作的对象……它不仅仅是一个字符串"矩阵.

I'm confused about these data structures.

From a GIS system, I use a function to extract the meta data (8 different fields)

myList = FeatureClassToNumPyArray(...)
myList = [('a', 'b', 'c'...) ('aa', 'bb', 'cc'...) ..]    # 8 fields
print (type(myList ))
print (myList.shape)
print (myList.size)

This produces:

<class 'numpy.ndarray'>
(1, 9893)
9893

# I was expecting to get (9893 rows x 8 cols), as in (8,9893)   
# or (9893, 8), but anyway, let's not worry about that right now. 

So I try this:

>>> source = [('a', 'b', 'c') ('aa', 'bb', 'cc')]

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable

But throw in a comma separator, and it's fine... but now it's a list.

>>> source = [('a', 'b', 'c'), ('aa', 'bb', 'cc')]
>>> type(source)
<class 'list'>

So, this magical GIS function can produce a data structure that is accepted as a numpy data array, but if I try create it manually, it's not possible.

What am I missing?

解决方案

Not sure how to do this, but the comment by juanpa.arrivillaga should be marked as the answer.

Again, why do you expect print(something) to produce a string that is valid python source code to produce that object? That is your fundamental assumption that is wrong. That is what you are missing. print(repr(something)) will often get you something much closer, but it is never guaranteed to be valid source code. Again, likely it returns a numpy.ndarray with some structured dtype. What is myList.dtype? EDIT: so a very basic example, something = object(); print(something) now try to reproduce that from the string representation... there's no reason to expect to be able to. – juanpa.arrivillaga May 13 at 21:18

My question originates from a fundamental misunderstanding of what a dataframe is and how it works. Took me while to work it out.

In my words, it's an object that needs to be manipulated through various tools/functions... it's not just a matrix of "strings".

这篇关于为什么我不能手动复制 nd 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆