Selecting a second array based on the first one - python

Let's say I have two arrays of len(1000) each
array_a = np.array([1,2,3,....,1000]) # length of 1000
array_b = np.array([32344,83242,94323,....,48984]) # length of 1000
Now I select a subset of array_a based on certain conditions:
subset_a = array_a[(array_a>10) * (array_a<500)]
Now how do I select those values of array_b that belong to the above subset_a ?
I tried
subset_b = array_b[subset_a]
but I get an error
IndexError: arrays used as indices must be of integer (or boolean) type

Are you looking for this?
import numpy as np
array_a = np.array([1,2,3,4,5]) # length of 5
array_b = np.array([6,7,8,9,10]) # length of 5
condition = array_a>3
print condition
subset_a = array_a[condition]
print subset_a
subset_b = array_b[condition]
print subset_b
http://ideone.com/dAFLYL

Related

How to combine two arrays for a specific index without concatenation?

I have two arrays that I want to merge to a new one but I need to insert the indices in specific places
array1 = np.arange(95,320,4)
array2 = np.arange(0,360,2)
For example.. array1[0] = 95, but I want this value to be in a new array between array2[47] which equals 94 and array2[48] that equals 96, and so on with the rest of the values inside array1.
Is this possible?
I think that you're looking for numpy.insert
array1 = np.arange(95,320,4)
array2 = np.arange(0,360,2)
for i, value in enumerate(array1):
index = i+48+i*2
array2 = np.insert(array2, index, array1[i])
It is not clear what you want exactly, but it might be the following:
So you have two arrays (arr1 and arr2):
import numpy as np
arr1 = np.arange(95, 320, 4)
arr2 = np.arange(0, 360, 2)
And you have two lists of indexes (idxs1 and idxs2) mapping between those arrays' elements and their index in the merged array:
n1, n2 = len(arr1), len(arr2)
n = n1 + n2
idxs = np.arange(n)
rng = np.random.default_rng()
rng.shuffle(idxs)
idxs1 = idxs[:n1]
idxs2 = idxs[n1:]
Then you can construct the merged array pretty easily as so:
merged = np.empty(n)
merged[idxs1] = arr1
merged[idxs2] = arr2
Let's check the properties I assumed you want to achieved:
for i in range(n1):
assert merged[idxs1[i]] == arr1[i]
for i in range(n2):
assert merged[idxs2[i]] == arr2[i]

Extracting elements from a 1D array. Getting error invalid index to scalar variable

I have to extract elements from a 1D array and write a file.
import numpy as np
k0 =78
tpts = 10
x_mirror = [1,2,3,4,5,6,7,8,9,10]
alpha = -3
x_start = 3
u0 = []
for p in range(0,tpts): #initial condition at t = 0
ts = ((np.exp(alpha*((x_mirror[p]-x_start)**2))*(np.cos(k0*(x_mirror[p]-x_start)))))
u0.append(ts)
u0_array = np.array(u0)[np.newaxis]
u0_array_transpose = np.transpose(u0_array)
matrix_A = np.zeros((tpts,tpts))
matrix_A[tpts-1][tpts-1] = 56
matrixC = matrix_A # u0_array_transpose
matrixC2 = matrix_A # u0
u_pre = np.array(np.zeros(tpts))
print(u0_array)
In this I want to extract suppose elements of u0_array separately. I get my u0_array as [[ 2.89793185e-06 -4.27075012e-02 1.00000000e+00 -4.27075012e-02 2.89793185e-06 9.14080657e-14 -7.91091805e-22 2.42062877e-33 -1.24204313e-47 1.15841796e-64]]. This is just for example. How can I get different elements of u0_array? By using u0[][], I am getting error. Any help is highly appreciated.
u0_array is an array that contains an array of floats. To index an individual element, use u0_array[0][(index to access)]. You can also use .flatten(), as the comment states.

Removing indexes to match array dimensions

I have two arrays (x, y) with different values and I am trying to find the median for y for values in x < 100. My problem is that I have filtered out some values in array x so the arrays are not the same shape. Is there a way I can remove the those indexes that I removed in array y in array x?
For example that they both are 24, 36 but after the filtering array y is 22, 32 and x is still 24, 36. How can I remove the same indexes? lets say I removed index 4, 7 and 9, 14. How can I remove those exact same ones in array x?
My code if needed. data_mg is y and data_dg is x.
data_mg = image_data_mg[0].data[0:x, 0:y].astype('float')
data_err = image_data_err[0].data[0:x, 0:y].astype('float')
data_dg = image_data_dg[0].data[0:x, 0:y].astype('float')
data_mg[data_mg == 0] = np.nan
data_err[data_err == 0] = np.nan
data_dg[data_dg == 0] = np.nan
data_mg = data_mg[data_mg/data_err > 2]
data_dg = np.ndarray.flatten(data_dg)
data_dg = data_dg[data_mg]
data_mg = np.ndarray.flatten(data_mg)
data_mg = data_mg[np.logical_not(np.isnan(data_mg))]
data_dg = np.ndarray.flatten(data_dg)
data_dg = data_dg[np.logical_not(np.isnan(data_dg))]
b = np.where(np.array(data_dg > 100))
median = np.median(data_mg[b])
print('Flux median at dispersion > 100 km/s is ' + str(median))
a = np.where(data_dg <= 100)
median1 = np.median(data_mg[a])
print('Flux median at dispersion <= 100 km/s is ' + str(median1))
IndexError: arrays used as indices must be of integer (or boolean) type, line 10
It looks like data_mg and data_dg start with the same shape and you use boolean indexing to keep the values that are not na in each. The trouble is that different values are nan in each array. I would suggest making a combined index that you can use for both arrays.
data_mg = np.ndarray.flatten(data_mg)
data_dg = np.ndarray.flatten(data_dg)
ix_mg = np.logical_not(np.isnan(data_mg))
ix_dg = np.logical_not(np.isnan(data_dg))
ix_combined = np.logical_and(ix_mg, ix_dg)
data_mg = data_mg[ix_combined]
data_dg = data_dg[ix_combined]
First, you could just do the same indexing operation on each array so they'll be of the same shape. I believe that would look something like this:
idx = data_mg / data_err > 2
data_mg = data_mg[idx]
data_df = data_dg[idx)
But the error you're getting may not be due to this. It looks like your error is coming from the line:
data_dg = data_dg[data_mg]
Giving the error:
IndexError: arrays used as indices must be of integer (or boolean) type, line 10
I'm not sure what your intent is here, so I'm not sure what to recommend. If this is you trying to get them to be the same shape, the lines I included above should do that for you.

Slicing numpy array taking every nth element

I have an numpy array of shape 24576x25 and i want to extract 3 array out of it. Where the first array contains the every 1st,4th,7th,10th,... element
while second array contains 2nd,5,8,11th,... element and third array with 3rd,6,9,12th,...
The output array sizes would be 8192x25.
I was doing the following in MATLAB
c = reshape(a,1,[]);
x = c(:,1:3:end);
y = c(:,2:3:end);
z = c(:,3:3:end);
I have tried a[:,0::3] in python but this works only if i have array of shape divisible by 3. What can i do?
X,Y = np.mgrid[0:24576:1, 0:25:1]
a = X[:,::,3]
b = X[:,1::3]
c = X[:,2::3]
does not work either. I need a,b,c.shape = 8192x25
A simple tweak to your original attempt should yield the results you want:
X,Y = np.mgrid[0:24576:1, 0:25:1]
a = X[0::3,:]
b = X[1::3,:]
c = X[2::3,:]
import numpy as np
a = np.arange(24576*25).reshape((24576,25))
a[::3]
a[::3].shape gives you (8192, 25)

filling numpy array by index

I have a function which gives me the index for a given value. Eg,
def F(value):
index = do_something(value)
return index
I want to use this index to fill a huge numpy array by 1s. Lets call array features
l = [1,4,2,3,7,5,3,6,.....]
NOTE: features.shape[0] = len(l)
for i in range(features.shape[0]):
idx = F(l[i])
features[i, idx] = 1
Is there a pythonic way to perform this (as the loop takes a lot of time if the array is huge)?
If you can vectorize F(value) you could write something like
indices = np.arange(features.shape[0])
feature_indices = F(l)
features.flat[indices, feature_indices] = 1
try this:
i = np.arange(features.shape[0]) # rows
j = np.vectorize(F)(np.array(l)) # columns
features[i,j] = 1

Categories

Resources