Questions tagged [numpy-slicing]

questions related to numpy indexing routines, in particular, slicing, indexing and concatenation

Read more:

518 questions
1
vote
2 answers

Script to convert Matlab slices and index to numpy array notation

I am porting some Matlab code to Python. In this code there are many instances of a large matrix being sliced in many different ways, eg M(2:45,13:18), M(:12,:), V(4), V(:27), etc. It is technically possible to convert this to numpy notation…
Mathieu
  • 241
  • 1
  • 8
1
vote
2 answers

How to retrieve elements from an array without slicing?

Say I have an array, A1. B1 is another array storing the last two rows of A1. I need to retrieve the first two rows of A1 without slicing as another array (C1). Logically, I'm thinking something like A1 (the whole array) - B1 (the last two rows) =…
wowzer
  • 25
  • 5
1
vote
1 answer

how to highlight pandas data frame on selected rows

I have the data like this: df: A-A A-B A-C A-D A-E Tg 0.37 10.24 5.02 0.63 20.30 USL 0.39 10.26 5.04 0.65 20.32 LSL 0.35 10.22 5.00 0.63 20.28 1 0.35 10.23 5.05 0.65 20.45 2 …
A.E
  • 997
  • 1
  • 16
  • 33
1
vote
0 answers

Can't convert FITS data (FITS_rec type) to multidimensional numpy array

I am attempting to read in a FITS file and convert the data into a multidimensional numpy array (So i can easily index the data). The FITS data is structured like: FITS_rec([(time, [rate, rate, rate, rate], [ERROR, ERROR, ERROR, ERROR], TOTCOUNTS,…
1
vote
2 answers

Take only part of matrix in Python

given a matrix A, e.g. A = np.array([[1,2,3,4],[5,6,7,8]]) and two lists one of row indices and one of column indices, e.g. row=(0,1), column=(0,2) I want to extract the corresponding rows and columns of matrix A in python, so in this example I want…
samabu
  • 181
  • 6
1
vote
3 answers

array copy and view in numpy python

I am new to numpy.Recently only I started learning.I am doing one practice problem and getting error. Question is to replace all even elements in the array by -1. import numpy as np np.random.seed(123) array6 =…
Aakash Patel
  • 111
  • 7
1
vote
1 answer

How to get the specific out put for Numpy array slicing?

x is an array of shape(n_dim,n_row,n_col) of 1st n natural numbers b is boolean array of shape(2,) having elements True,false def array_slice(n,n_dim,n_row,n_col): x = np.arange(0,n).reshape(n_dim,n_row,n_col) b =…
Chandler
  • 15
  • 3
1
vote
3 answers

How can I get a numpy array slides by choosing with specific rows and columns inplace?

As in the title, if I have a matrix a a = np.diag(np.arange(5)) array([[0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 2, 0, 0], [0, 0, 0, 3, 0], [0, 0, 0, 0, 4]]) How can I assign a new 4x4 matrix or even 3x4 matrix to a…
1
vote
2 answers

Make a numpy array selecting only certain rows and certain columns from another

I have created a very large NumPy array of size 500 x 10000, called first_array, to solve a partial differential equation using the Crank Nicolson method. This method needs to create and use all the data stored in the array, but I only need a small…
User1234321
  • 321
  • 1
  • 10
1
vote
2 answers

Value Assignment by specific indices error in Tensorflow

I'm trying to build a custom loss function for my model, but whenever I try to convert Tensors into .numpy() arrays with run_eagerly = True, it gives "WARNING: Gradients do not exist for variables ...". So I debugged other custom loss functions…
1
vote
0 answers

Delete rows and column from a numpy array, given information to delete is in another vector array

I have the following code: deleterowandcolumns = np.array([0,3]) original = np.array ([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ]) The rows and columns to…
django
  • 53
  • 1
  • 6
1
vote
2 answers

Slicing multi-dimensional array with another array

edited with a clearer example, and included solution I'd like to slice an arbitrary dimensional array, where I pin the first n dimensions and keep the remaining dimensions. In addition, I'd like to be able to store the n pinning dimensions in a…
Steve Heim
  • 799
  • 12
  • 25
1
vote
1 answer

update 3D Numpy array with np.array as indices

I have a an image of the shape (180, 240, 3) and it will be modified according to 3 different numpy arrays that contains the location of each dimension. So the three numpy arrays looks like this: height_array = [ 90 148 161 165 40 77 73 108 167…
vsun92
  • 11
  • 2
1
vote
1 answer

Adding a smaller array to a larger array at a specified location (using variables)

Suppose I have a 6x6 matrix I want to add into a 9v9 matrix, but I also want to add it at a specified location and not necessarily in a 6x6 block. The below code summarizes what I want to accomplish, the only difference is that I want to use…
Ned Taylor
  • 11
  • 3
1
vote
2 answers

python slicing [:-N] [:-N+1]

I been trying to code the following line but I get the message that operands could not be broadcast together with shapes (0,) (30,) x has a length of 32 x[:-N+1] I want to access all elements except the last two x[N:-N] I want to access all…
Brucee
  • 31
  • 6