请阐明以下Python NumPy数组初始化和拼接示例 [英] Please clarify the following Python NumPy array initialization and splicing examples

查看:57
本文介绍了请阐明以下Python NumPy数组初始化和拼接示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python版本2.6,正在学习 NumPy 版本1.3.

I am using Python version 2.6 and am learning NumPy version 1.3.

我已经在下面尝试了几个NumPy数组初始化和列拼接示例,并在最后添加了一些内联问题作为注释和发现列表.希望有人可以向我解释行为差异背后的原因.许多相互关联的问题和较长的帖子,但每个示例都很小,可以随意回答一个或几个.

I have tried out several NumPy array initialization and column splicing examples below, and added some inline questions as comments and a list of findings in the end. Hopefully someone can explain to me what is behind the differences in behaviors. Lots of inter-related questions and a rather long post, but each example is small, feel free to just answer one or a couple.

import numpy as np

print "Initializing a number of numpy arrays:\n"

a)从元组列表初始化

a) Initialize from a list of tuples

a = np.zeros((3,),dtype=('i4,i4,a1'))
a[:] = [(1, 2, 'A'), (3, 4, 'B'),(5, 6, 'A')]
print "a: "
print a         # print => [(1, 2, 'A') (3, 4, 'B') (5, 6, 'A')]
print repr(a)   # print => array([(1, 2, 'A'), (3, 4, 'B'), (5, 6, 'A')],
                #     dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '|S1')]
print '\n'

b)元组的常规列表

b = [];
b[:] = [(1, 2, 'A'), (3, 4, 'B'),(5, 6, 'A')]
print "b: "
print b         # print => [(1, 2, 'A'), (3, 4, 'B'), (5, 6, 'A')
print '\n'

问题1 :a)看起来像印刷版中的元组列表,除非没有 元组之间的逗号.如果我用repr(a)打印它,它甚至有 逗号.即使这样,也不应再将其视为与b)相同, 对吗?

Question 1: a) looks like a list of tuples from print, except without the comma between the tuples. If I print it with repr(a), it even has the commas. Even so, it should no longer be considered the same as b), correct?

c)失败:尝试将np.zeroes返回的数组初始化为列表列表

c) Fail: Try to initialize array returned from np.zeroes as a list of list

问题2 :由于dtype与dtype不匹配,以下内容是否失败? 我传来的名单?

Question 2: Is the below failing because the dtype does not match the list that I passed in?

c = np.zeros((3,),dtype=('i4,i4,a1'))
#c[:] = [[1, 2, 'A'], [3, 4, 'B'],[5, 6, 'A']]
# TypeError: expected a readable buffer object
print '\n'

d)失败:与c)相同,但尝试将dtype设置为列表

d) Fail: Same as c) but try to set the dtype as a list

问题3 :以下内容是否失败,因为不允许我指定列表的dtype?

Question 3: Is the below failing, because I am not allowed to specify a dtype that is a list?

#d = np.zeros((3,),dtype=['i4,i4,a1'])
# TypeError: data type not understood
#d[:] = [[1, 2, 'A'], [3, 4, 'B'],[5, 6, 'A']]
print '\n'

e)尝试从列表列表中使用np.array初始化数组

e) Try to initialize array using np.array from a list of a list

问题4 :为什么在下面的e)也是列表工作列表,但是d)失败了?

Question 4: Why would e) below which is also a list of list work, but d) fail?

e = np.array( [[1, 2, 'A'], [3, 4, 'B'],[5, 6, 'A']] )
print "e: "
print e     # print =>  [['1' '2' 'A']
            #   ['3' '4' 'B']
            #   ['5' '6' 'A']]
print '\n'

f)尝试使用元组列表中的np.array初始化数组

f) Try to initialize array using np.array from a list of a tuples

问题5 :与e)相同的示例,但是这次使用 他从f)中打印出的元组列表与e)相同,因此 用元组列表初始化和列表列表确实是 一样吗?

Question 5: Same example as e), but this time initializing with list of tuples he print out of f) is identical as e), so initializing with list of tuples and list of list are really identical then?

f = np.array( [(1, 2, 'A'), (3, 4, 'B'),(5, 6, 'A')] )
print "f: "
print f     # print =>  [['1' '2' 'A']
            #   ['3' '4' 'B']
            #   ['5' '6' 'A']]
print '\n'

g)尝试使用 CSV 文件中的np.array初始化数组

g) Try to initialize array using np.array from a CSV file

问题6 :与e和f相同的示例,但是这次是初始化 与文件的报价差异不大.那里 像这样生成的数组之间应该没有区别 和e)和f)对吗?

Question 6: Same example as e and f, but this time initializing from file Minor difference in quoting for the print out. There should be no difference # between the array generated like this and e) and f) right?

from StringIO import StringIO
data = StringIO( """
1, 2, A
3, 4, B
5, 6, A
""".strip())
g = np.genfromtxt(data, dtype=object, delimiter=',')
print "g: "
print g     # print =>  [[1 2 A]
            #   [3 4 B]
            #   [5 6 A]]
print '\n'

h)按列拼接NumPy数组

h) Splicing the NumPy arrays by column

#print "a: "
#print a[:,2]   # IndexError: invalid index
print "a: "
print a['f2']   # This is ok though

# Splicing a normal list of tuples if not expected to work
#print "b: "
#print b[:,2]   # IndexError: invalid index

问题7 为什么下面的e可以工作,但是上面的e却使用相同的语法失败,但出现Index错误?

Question 7 Why would splicing e below work, but a fail above with Index error with the same syntax?

print "e: "
print e[:,2]    # print => ['A' 'B' 'A']

print "f: "
print f[:,2]    # print => ['A' 'B' 'A']

print "g: "
print g[:,2]    # print => [A B A]

发现1:使用nd.array和元组列表,列表列表或CSV文件初始化numpy.ndarray是相同的.这可能与我看过的另一个答案说的np.array需要一个元组列表相反,堆栈溢出问题 定义dtypes在NumPy中使用列表? .

Finding 1: Initializing numpy.ndarray by using nd.array and a list of tuples, list of list, or CSV file are identical. This is maybe contrary to what this other answer that I viewed that says np.array expects a list of a tuples, Stack Overflow question Define dtypes in NumPy using a list?.

发现2:通过使用np.zeroes初始化numpy.ndarray,我无法从列表中的列表中初始化ndarray.

Finding 2: Initializing numpy.ndarray by using np.zeroes, I am unable to initialize the ndarray from a list of a list.

发现3:对于列拼接,通过使用nd.array初始化numpy.ndarray,我可以进行列拼接(即e [:,2],但是拼接的语法是使用np.zeroes初始化方法的方式与a ['f2']不同,无法拼接普通的元组列表.

Finding 3: For column splicing, initializing numpy.ndarray by using nd.array, I could do a column splice (that is, e[:,2], but the syntax of splicing, using the np.zeroes initialization method is different a['f2']. A normal list of tuples cannot be spliced.

推荐答案

问题1

a)看起来像是打印出来的元组列表,除了元组之间没有逗号.如果我用repr(a)打印它,甚至有逗号.即使这样,也不应再将其视为与b)正确吗?

a) looks like a list of tuples from print, except without the comma between the tuples. If I print it with repr(a), it even has the commas. Even so, it should no longer be considered the same as b) correct?

绝对. ab具有不同的类型:type(a)numpy.ndarraytype(b)list

Absolutely. a and b have different types: type(a) is numpy.ndarray, type(b) is list

由于dtype与我传入的列表不匹配,以下内容是否失败?

Is the below failing because the dtype does not match the list that I passed in?

否-问题是您试图用列表列表填充它,而不是像使用a那样用元组列表填充.请参见此处.我不能完全确定导致此行为的深层原因,但我怀疑这与元组是不可变的对象有关,而列表(和数组)是可变的.

No - the issue is that you're trying to fill it with a list of lists, rather than a list of tuples as you did with a. See here. I'm not totally sure what the deep reason is for this behaviour, but I suspect it has to do with tuples being immutable objects, whereas lists (and arrays) are mutable.

以下内容是否失败,因为不允许我指定列表的dtype?

Is the below failing because I am not allowed to specify a dtype that is a list?

是的,此外,您也将无法用列表列表填充d(请参阅上一个答案).

Yes, and furthermore you would also fail to fill d with a list of lists (see previous answer).

为什么e)下方的也是列表工作列表,而d)失败?

Why would e) below which is also a list of list work, but d) fail?

查看edtype-它是|S1,即数组中的每个元素都是长度为1的字符串.如果不为数组构造函数指定dtype,则类型为将被确定为保存序列中全部对象所需的最小类型.在这种情况下,由于您将序列交给了包含一些字符串的序列,因此它将整数转换为字符串.

Look at the dtype of e - it is |S1, i.e. every element in the array is a string of length 1. If you don't specify a dtype for the array constructor, the type will be determined as the minimum type required to hold all of the objects in the sequence. In this case, since you handed it a sequence containing some strings, it will upcast the integers to strings.

与e)相同的示例,但是这次用他从f)中打印出的元组列表进行初始化与e)相同,因此用元组列表和list列表进行初始化真的一样吗?

Same example as e), but this time initializing with list of tuples he print out of f) is identical as e), so initializing with list of tuples and list of list are really identical then?

同样,因为您没有给构造函数一个dtype,所以所有内容都将转换为|S1.

Again, since you don't give the constructor a dtype, everything will get upcast to |S1.

与e和f相同的示例,但是这次从文件初始化,但在引用打印输出方面存在细微差别.这样生成的数组与e)和f)之间应该没有区别#是吗?

Same example as e and f, but this time initializing from file Minor difference in quoting for the print out. There should be no difference # between the array generated like this and e) and f) right?

否,现在您要告诉构造函数使用dtype=object创建数组,而ef将具有dtype=|S1.

No, now you're telling the constructor to create an array with dtype=object, whereas e and f will have dtype=|S1.

为什么下面的e可以工作,但上面的语法相同但索引错误却失败呢?

Why would splicing e below work, but a fail above with Index error with the same syntax?

查看a.shape-您将看到它是(3,),即a是长度为3的一维向量.尽管它确实具有可以对其进行索引的字段,但它没有第二维索引.相比之下,e.shape(3,3),因此您可以按列对其进行索引.

Look at a.shape - you'll see that it's (3,), i.e. a is a 1d vector of length 3. Although it does have fields that you can index it by, it has no second dimension for you to index into. By contrast, e.shape is (3,3), so you can index it by column.

这篇关于请阐明以下Python NumPy数组初始化和拼接示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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