Numpy将N维数组转换为二维数组

时间:2015-09-18 16:20:04

标签: python numpy

给定形状为(N,d_1,...,d_{n-1})的n维数组,我想将该数组转换为形状为(N,D)的二维数组,其中D = \prod_i d_i。 numpy有没有任何巧妙的技巧来做到这一点?

1 个答案:

答案 0 :(得分:3)

您可以使用numpy.reshape()方法和array.shape来获取原始数组的形状,使用np.prod()获取onActivityCreated()的乘积。示例 -

var style = {
  'Point': [new ol.style.Style({
    image: new ol.style.Circle({
      fill: new ol.style.Fill({
        color: 'rgb(255,0,0)'
      }),
      radius: 5,
      stroke: new ol.style.Stroke({
        color: '#000000',
        width: 1
      }),
    })
  })],
  'LineString': [new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: '#ff0000',
      width: 3
    })
  })],
  'MultiLineString': [new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: '#0000ff',
      width: 3
    })
  })]
};


var map = new ol.Map({
  target: 'map-ol-canvas',
  interactions: ol.interaction.defaults({mouseWheelZoom: false}),
  layers: [new ol.layer.Tile({ source: new ol.source.OSM() })],
  view: new ol.View({
    zoom: 8,
    maxZoom: 16
  })
});
map.getView().fit(extent, map.getSize());

var trackSource = new ol.source.Vector({
  url: '/test.geojson',
  format: new ol.format.GeoJSON()
});

var track = new ol.layer.Vector({
  source: trackSource,
  style: function(feature, resolution) {
    return style[feature.getGeometry().getType()];
  }
});
map.addLayer(track);

var select = new ol.interaction.Select({
  filter: function (feature, layer) {
    return feature.getGeometry().getType() === 'Point';
  }
});
map.addInteraction(select);

// When user clicks on a waypoint, show a tooltip.
function onMouseClick(browserEvent) {
  var coordinate = browserEvent.coordinate;
  var pixel = map.getPixelFromCoordinate(coordinate);
  map.forEachFeatureAtPixel(pixel, function(feature) {
    if (feature.getGeometry().getType() === 'Point') {
      console.log(feature.get('date'));
    }
  });
}
map.on('click', onMouseClick);

或@hpaulj在评论中建议的更简单的版本 -

libclang-3.4-dev