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]
Related
I am trying to swap two indices in the 2D array of NumPy. Unfortunately, only one element is getting swapped. Here is the code:
n = len(A)
perMatrix = np.zeros((n,n))
np.fill_diagonal(perMatrix, 1)
perMatrix = A
# swapping the row
print(perMatrix)
temp = perMatrix[switchIndex1]
print(temp)
# perMatrix[switchIndex1][0] = 14
perMatrix[switchIndex1], perMatrix[switchIndex2] = perMatrix[switchIndex2], perMatrix[switchIndex1]
print(perMatrix)
Here's what the code is outputting:
You could just add (on the line after perMatrix is created):
sigma = [switchIndex1, switchIndex2]
tau = [switchIndex2, switchIndex1]
perMatrix[sigma,:] = perMatrix[tau,:]
So I want to run this code to find a random element of a list containing numpy arrays with a certain condition
The conditions are that is for every element of arr1 not equal to arr2 it will append the element of that index of er and append it to lst:
import numpy as np
import random
arr1 = np.array([1,2,3,4])
arr2 = np.array([1,2,6,3])
arr = (arr1 == arr2)
er = np.array([[1,2],[-6,7],[4,7],[6,2]])
lst = []
for i in arr:
if i == False:
lst.append(er[i])
print(random.choice(lst))
But I don't know why its returning a empty list.Please help
arr is a boolean array, so in for loop i is True/False values (not an index)
for i in arr:
Change
for i in arr:
if i == False:
lst.append(er[i])
To:
for i, v in enumerate(arr): # retrieving index and value
if v == False: # test value
lst.append(er[i]) # use i to index into er
try this:
import numpy as np
import random
arr1 = np.array([1,2,3,4])
arr2 = np.array([1,2,6,3])
arr = (arr1 == arr2)
er = np.array([[1,2],[-6,7],[4,7],[6,2]])
lst = []
for i in range(len(arr1-1)):
if arr1[i] != arr2[i]:
lst.append(er[i])
print(random.choice(lst))
I have a question similar to one that was already answered before, with slight modifications:
I have a 2-D numpy array with values -1,0,1. I want to find the cluster sizes of elements with values 1 or -1 (separately). Do you have any suggestions on how to do this?
Thanks!
This is my solution:
import numpy as np
import copy
arr = np.array([[1,1,-1,0,1],[1,1,0,1,1],[0,1,0,1,0],[-1,-1,1,0,1]])
print(arr)
col, row = arr.shape
mask_ = np.ma.make_mask(np.ones((col,row)))
cluster_size = {}
def find_neighbor(arr, mask, col_index, row_index):
index_holder = []
col, row = arr.shape
left = (col_index, row_index-1)
right = (col_index,row_index+1)
top = (col_index-1,row_index)
bottom = (col_index+1,row_index)
left_ = row_index-1>=0
right_ = (row_index+1)<row
top_ = (col_index-1)>=0
bottom_ = (col_index+1)<col
#print(list(zip([left,right,top,bottom],[left_,right_,top_,bottom_])))
for each in zip([left,right,top,bottom],[left_,right_,top_,bottom_]):
if each[-1]:
if arr[col_index,row_index]==arr[each[0][0],each[0][1]] and mask[each[0][0],each[0][1]]:
mask[each[0][0],each[0][1]] = False
index_holder.append(each[0])
return mask,index_holder
for i in range(col):
for j in range(row):
if mask_[i,j] == False:
pass
else:
value = arr[i,j]
mask_[i,j] = False
index_to_check = [(i,j)]
kk=1
while len(index_to_check)!=0:
index_to_check_deepcopy = copy.deepcopy(index_to_check)
for each in index_to_check:
mask_, temp_index = find_neighbor(arr,mask_,each[0],each[1])
index_to_check = index_to_check + temp_index
# print("check",each,temp_index,index_to_check)
kk+=len(temp_index)
for each in index_to_check_deepcopy:
del(index_to_check[index_to_check.index(each)])
if (value,kk) in cluster_size:
cluster_size[(value,kk)] = cluster_size[(value,kk)] + 1
else:
cluster_size[(value,kk)] = 1
print(cluster_size)
cluster_size is a dictionary, the key is a two member tuple (a,b), a give the value of the cluster (that's what you want to solve, right), b gives the counts of that value. The value for each key is the number of cluster.
Are you trying to count the number of 1 or -1 existing in the 2D array?
If so, then it is fairly easy. Say
ar = np.array([1, -1, 1, 0, -1])
If so,
n_1 = sum([1 for each in ar if each==1]);
n_m1 = sum([1 for each in ar if each==-1])
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
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