简体   繁体   English

反向展平numpy数组?

[英]reverse flatten numpy array?

I have an array 我有一个数组

[[0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0] ..., [0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0]] of Shape (38485,) i want to reshape to (38485,4) like [[0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0] ..., [0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0]]我想要重塑为(38485,4)的形状(38485,) (38485,4) [[0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0] ..., [0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0]]

[[0, 1, 0, 0] 
[0, 1, 0, 0] 
[1, 0, 0, 0]
.
.
.
[0, 1, 0, 0]
[0, 1, 0, 0]
[1, 0, 0, 0]]

but when i try array.reshape(-1,4) it throws me the error ValueError: cannot reshape array of size 38485 into shape (4) 但是当我尝试array.reshape(-1,4)会抛出错误ValueError: cannot reshape array of size 38485 into shape (4)

My code to get array: 我的代码获取数组:

dataset = pd.read_csv('train.csv')

y = dataset.iloc[:, 6]

fr=np.array([1,0,0,0])
re=np.array([0,1,0,0])
le=np.array([0,0,1,0])
ri=np.array([0,0,0,1])
for i in range(y.shape[0]):
    if y[i]=="Front":
        y[i]=fr
    elif y[i]=="Rear":
        y[i]=re
    elif y[i]=="Left":
        y[i]=le
    elif y[i]=="Right":
        y[i]=ri

array=y.values

Is there any way I can accomplish this? 我有什么办法可以做到这一点?

I Fixed this by 我通过修复

array = np.array([[n for n in row] for row in array])

Thanks to wim 感谢WIM

Updated answer: 更新的答案:

The variable y is a numpy array which contained strings and numpy.array s. 变量y是一个numpy数组,其中包含字符串和numpy.array Its dtype is object , so numpy doesn't understand it's a table, even though it's full of 4-element numpy.array s at the end of the preprocessing. 它的dtypeobject ,因此numpy不知道它是一个表,即使在预处理结束时它充满了4元素numpy.array

You could either avoid mixing object types by using another variable than y or convert y.values with : 您可以通过使用除y之外的其他变量来避免混合对象类型,或将y.values转换为:

array = np.array([x.astype('int32') for x in y.values])

As an example: 举个例子:

import numpy as np
y = np.array(["left", "right"], dtype = "object")
y[0] = np.array([1,0])
y[1] = np.array([0,1])
print(y)
# [[1 0] [0 1]]
print(y.dtype)
# object
print(y.shape)
# (2,)
y = np.array([x.astype('int32') for x in y])
print(y)
# [[1 0]
#  [0 1]]
print(y.dtype)
# int32
print(y.shape)
# (2, 2)

Original answer: 原始答案:

Your array is somehow incomplete. 您的array某种程度上是不完整的。 It has 38485 elements, many of which look like 4-elements arrays. 它具有38485个元素,其中许多元素看起来像4个元素的数组。 But somewhere in the middle, there must be at least one inner-array which doesn't have 4 elements. 但是在中间的某个地方,必须至少有一个内部数组,其中没有4个元素。 Or you might have a mix of collections ( list , array , ). 或者您可能会混合使用集合( listarray ,)。

That could be why the second value isn't defined in the shape. 这可能就是为什么未在形状中定义第二个值的原因。

Here's an example with one (8, 4) array and a copy of it, with just one element missing: 这是一个包含一个(8, 4)数组和一个副本的示例,仅缺少一个元素:

import numpy as np

data = np.array([[0, 1, 0, 0],[0, 1, 0, 0],[1, 0, 0, 0] , [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0],[1, 0, 0, 0]])
print(data.shape)
# (8, 4)
print(data.dtype)
# int64
print(set(len(sub_array) for sub_array in data))
# set([4])
print(data.reshape(-1, 4))
# [[0 1 0 0]
#  [0 1 0 0]
#  [1 0 0 0]
#  [0 1 0 0]
#  [0 1 0 0]
#  [0 1 0 0]
#  [0 1 0 0]
#  [1 0 0 0]]

broken_data = np.array([[0, 1, 0, 0],[0, 1, 0, 0],[1, 0, 0, 0] , [1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0],[1, 0, 0, 0]])
print(broken_data.shape)
# (8, )
print(broken_data.dtype)
# object
print(set(len(sub_array) for sub_array in broken_data))
# set([3, 4])
print(broken_data.reshape(-1, 4))
# [[[0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0] [1, 0, 0]]
#  [[0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0]]]
print([sub_array for sub_array in broken_data if len(sub_array) != 4])
# [[1, 0, 0]]

Find the sub-arrays that don't have exactly 4 elements and either filter them out or modify them. 查找没有正好4个元素的子数组,然后过滤掉它们或对其进行修改。

You'll then have a (38485,4) array, and you won't have to call reshape . 这样,您将获得一个(38485,4)数组,而不必调用reshape

数组长度必须是4的倍数。38485不能是4的倍数。否则,按照您编写的方式进行重整应该可以正常工作:

array.reshape(-1,4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM