using numpy where results from one array to another - python

I have two numpy arrays - both generated from the same main array. one is generated from a slice of the main array and the other is from a where clause. the problem I am having is that when I get the indices from the where clause and apply it to the second array, I get an error: only integer scalar arrays can be converted to a scalar index. I've tried converting the array using transpose and using just the row portion of the where result, but still with the same issue.
y=np.empty([0,24])
#y array is filled with records
z[:,0]=y[:,1]
z[:,1]=y[:,2]
z[:,3]=y[:,3]
#...(total of 24 columns in y and I only use 6 in the z array)
#some data calcs from y
ar = np.array(y[:,7] - y[:,8]) #PVExport
ar = [maxamount if i > maxamount else i for i in ar]
ar = [0 if i < 0 else i for i in ar]
#where clause - get all false values in column 6. the
#here is where the error happens
naList=np.where(y[:,6]==0)
ar[naList[0]]=maxamount
z[:,4]=ar
when the above line executes - I get the error only integer scalar arrays can be converted to a scalar index.
when I try something similar by filling an array in the interactive window it seems to work.
looking to fill the 4th column of that z array with the results of some data calculations in the y array. any help is appreciated

Related

Printing a Python Array

I have the array below that represent a matrix of 20 cols x 10 rows.
What I am trying to do is to get the value located on the third position after I provide the Column and Row Values. For example if I type in the values 3 and 0, I expect to get 183 as answer. I used the print command as follows print(matrix[3][0][I don't know]) either I get out of range or the undesirable results.
I also organized the data as matrix[[[0],[0],[180]], [[1],[0],[181]], [[2],[0],[182]],... without too much success.
I have the matrix data on a csv file, so I can formatted accordingly if the problem is the way I am presenting the data.
Can soomeone, please, take a look to this code and direct me? Thanks
matrix =[]
matrix =[
['0','0','180'],
['1','0','181'],
['2','0','182'],
['3','0','183'],
['4','0','184'],
['5','0','185'],
['6','0','186'],
['7','0','187'],
['18','0','198']]
print(matrix[?][?][value])
your matrix here is 9 * 3
if you want the 185, it's in the 6th row 3rd column, so indexes are 5 and 2 respectively.
matrix[5][2] will print the result, idk why you have a 3rd bracket.
basically to access an element you will do [rowNumber][colNumber] , first brackets will give you whatever is in that position of the big array (a 2 d array is just an array of arrays) so you get an array (1D with 3 element) you then put the index of the element in that 1D array.

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 variables instead of the rows 0:6 and 3:9.
import numpy as np
a = np.zeros((9,9))
b = np.ones((6,6))
a[0:6,3:9] += b #Inserts the 6x6 ones matrix into the top right corner of the 9x9 zeros
Now using variables:
rows = np.array([0,1,2,3,4,5])
cols = np.array([3,4,5,6,7,8])
a[rows,3:9] += b #This works fine
a[0:6,cols] += b #This also works fine
a[rows,cols += b #But this gives me the following error: ValueError: shape mismatch: value array of shape (6,6) could not be broadcast to indexing result of shape (6,)
I have spent hours reading through forums and trying different solutions but nothing has ever worked. The reason I need to use variables is because these are input by the user and could be any combination of rows and columns. This notation worked perfectly in MatLab, where I could add b into a with any combination of rows and columns.
Explanation:
rows = np.array([0,1,2,3,4,5])
cols = np.array([3,4,5,6,7,8])
a[rows,cols] += b
You could translate the last line to the following code:
for x, y, z in zip(rows, cols, b):
a[x, y] = z
That means: rows contains the x-coordinate, cols the y-coordinate of the field you want to manipulate. Both arrays contain 6 values, so you effectively manipulate 6 values, and b must thus also contain exactly 6 values. But your b contains 6x6 values. Therefore this is a "shape mismatch". This site should contain all you need about indexing of np.arrays.

How to change the number of columns of a 2D array in python?

In the below snippet of the code (which is written just to show the issue), I have initially created a 2D array of size 3*4. Now during the execution of the code, at some time step, I need to change the number of columns in the third row from 4 to 2. I tried by the folowing way but it is showing the value error. How to do this ? Can somebody describe ?
import numpy as np
A=np.ones((3,4))
# Some other portion of the code
for i in range(0,3):
if(i==2): # In the third row
A[i,:]=np.ones(2) # Change the size of this third row.Now only need two elements (two 1's) in it
print(A)
ValueError: could not broadcast input array from shape (2) into shape (4)
Because Numpy only support the new row has the same dimension as the input of column (4 for your example).
You can change from Numpy to list.
import numpy as np
A=np.ones((3,4))
A = A.tolist()
for i in range(0,3):
if(i==2): # In the third row
A[i] = [1]*2
print(A)
You cannot change the dimension of a structured data table,
You could try:
A=np.ones((3,4))
row_no = 2
for i in range(0,row_no+1):
if(i==row_no):
A[i,:row_no]=np.ones(2)
A[i,row_no+1:]= np.nan #or 0 or somr other placeholder
print(A)

Isolating a column out of a numpy array using a variable?

I am trying to isolate the last column of a numpy array. However, the function needs to work for arrays of different sizes. When I put it like this:
array[:,array_length]
#array_length is a variable set to the length of one row of the array
which seems like it would work, it returns an error telling me that I can't slice with a variable, but only with an integer.
Is there a way to do this with numpy that I'm not seeing?
To access the last column of a numpy array, you can use -1
last_col = array[:, -1]
Or you can also do
array_length = len(array[0]) - 1
last_col = array[:, array_length]

Select specefic rows from a 2d Numpy array using a sparse binary 1-d array

I am having a issues figuring out to do this operation
So I have and the variable index 1xM sparse binary array and I have a 2-d array (NxM) samples. I want to use index to select specific rows of samples adnd get a 2-d array.
I have tried stuff like:
idx = index.todense() == 1
samples[idx.T,:]
but nothing.
So far I have made it work doing this:
idx = test_x.todense() == 1
selected_samples = samples[np.array(idx.flat)]
But there should be a cleaner way.
To give an idea using a fraction of the data:
print(idx.shape) # (1, 22360)
print(samples.shape) (22360, 200)
The short answer:
selected_samples = samples[index.nonzero()[1]]
The long answer:
The first problem is that your index matrix is 1xN while your sample ndarray is NxM. (See the mismatch?) This is why you needed to call .flat.
That's not a big deal, though, because we just need the nonzero entries in the sparse vector. Get those with index.nonzero(), which returns a tuple of (row indices, column indices). We only care about the column indices, so we use index.nonzero()[1] to get those by themselves.
Then, simply index with the array of nonzero column indices and you're done.

Categories

Resources