Numpy数组 - 将3D数组转换为2D数组

时间:2017-03-06 19:02:24

标签: python arrays numpy

拥有以下3D阵列(9,9,9):

np.arange(729).reshape((9,9,9))

max = 0 'set "inital" max 
For Each rcell in rrng.cells 'loop through values
   if rcell.value > max then 'if a value is larger than the old max, 
   max = rcell.value ' store it as the new max!
Next rcell 

如何重塑它看起来像这个2D数组(27,27):

[[[  0   1   2   3   4   5   6   7   8]
  [  9  10  11  12  13  14  15  16  17]
  [ 18  19  20  21  22  23  24  25  26]
  [ 27  28  29  30  31  32  33  34  35]
  [ 36  37  38  39  40  41  42  43  44]
  [ 45  46  47  48  49  50  51  52  53]
  [ 54  55  56  57  58  59  60  61  62]
  [ 63  64  65  66  67  68  69  70  71]
  [ 72  73  74  75  76  77  78  79  80]]
...
  [[648 649 650 651 652 653 654 655 656]
  [657 658 659 660 661 662 663 664 665]
  [666 667 668 669 670 671 672 673 674]
  [675 676 677 678 679 680 681 682 683]
  [684 685 686 687 688 689 690 691 692]
  [693 694 695 696 697 698 699 700 701]
  [702 703 704 705 706 707 708 709 710]
  [711 712 713 714 715 716 717 718 719]
  [720 721 722 723 724 725 726 727 728]]]

3 个答案:

答案 0 :(得分:2)

您可以先将阵列重塑为4d阵列,交换第二和第三轴,然后将其重新整形为27 X 27:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;
    public static ArrayList<Integer> selected = new ArrayList<>();
    private Realm realm = Realm.getDefaultInstance();

    public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                       HashMap<String, List<String>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
        }
        TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
        expandedListTextView.setText(expandedListText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

    @Override
    public int getGroupCount() {
        return this.expandableListTitle.size();
    }

    @Override
    public long getGroupId(int listPosition) {
        return listPosition;
    }

    @Override
    public View getGroupView(final int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        SharedPreferences pref = context.getSharedPreferences("MAIN_PREF", MODE_PRIVATE);
        final SharedPreferences.Editor editor = pref.edit();
        final String listTitle = (String) getGroup(listPosition);
        final boolean[] sel = {RealmController.getGroup(listTitle, realm).isSelect()};
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            convertView = layoutInflater.inflate(R.layout.list_group, null);

            convertView = layoutInflater.inflate(R.layout.holder_layout, null);
        }
//        TextView listTitleTextView = (TextView) convertView
//                .findViewById(R.id.listTitle);

        TextView listTitleTextView = (TextView) convertView.findViewById(R.id.showText);
        final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxHolder);
//        if (pref.getInt(listTitle, 0) == 1) {
//            checkBox.setSelected(true);
//        } else {
//            checkBox.setSelected(false);
//        }
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isSelected()) {
                    checkBox.setSelected(false);
                    RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
                    int position = -1;
                    for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
                        for (ObjectsInGroupRealm o : objectsInGroupRealms) {
                            if (o.getName().equals(objectDefExtends.name)) {
                                position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
                                Singleton.getInstance().getListaODE().get(position).visible = false;
                                editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 0);
                                editor.apply();
//                                RealmController.updateGroup(o.getName(),false,realm);

                            }
                        }
                    }
//                    editor.putInt(listTitle, 0);
//                    editor.apply();
                    selected.remove(Integer.valueOf(listPosition));
                } else {
                    checkBox.setSelected(true);
                    selected.add(listPosition);
                    RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
                    int position = -1;
                    for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
                        for (ObjectsInGroupRealm o : objectsInGroupRealms) {
                            if (o.getName().equals(objectDefExtends.name)) {
                                position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
                                Singleton.getInstance().getListaODE().get(position).visible = false;
                                editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 1);
                                editor.apply();
                                RealmController.updateGroup(o.getName(),true,realm);
                                sel[0] =true;
                                checkBox.setSelected(true);
//                                notifyDataSetChanged();
                            }
                        }
                    }
//                    editor.putInt(listTitle, 1);
//                    editor.apply();
                }
//                notifyDataSetChanged();

            }
        });
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        checkBox.setSelected(sel[0]);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

答案 1 :(得分:1)

import numpy as np

x = np.arange(729).reshape(9, 9, 9)

y = x.transpose(1, 0, 2).reshape(27, 27)
y[y[:,2].argsort()]

<强>说明

我使用numpy.transpose来置换每个轴的步幅和形状信息。

>>> x.strides
(324L, 36L, 4L)
>>> x.transpose(1, 0, 2).strides
(36L, 324L, 4L)

answer中的详细信息。

然后我按预期使用了numpy.reshape 2D中的(9L, 9L, 9L)

>>> x.reshape(27, 27)
(27L, 27L)

当然,transpose中的函数组合(如reshapenumpy)非常常见。它允许您在单行中进行矩阵变换:

x.transpose(1, 0, 2).reshape(27, 27)

修改

正如@PaulPanzer指出的那样,数组未被排序。

sort array column by column,可以使用:

y[y[:,2].argsort()]

但也许这不是最简单的答案。

答案 2 :(得分:0)

如果您可以在列表中移动数据,则可以使用:

np.hstack([x for x in np.arange(729).reshape((9,9,9))])