python : create a variable with different dimension sizes - python

I want to create a variable D by combining two other variable x and y.
x has the shape [731] and y has the shape [146].
At the end D should be 2D so that D[0] contains all x-values and D[1] all y-values.
I hope I explained it in a way someone can understand what I want to do.
Can someone help me with this?

It is a simple as: D = [x, y]
Hope it helped :)

Nested lists will do that*:
D = [x, y]
print(D[0] == x) # True
print(D[1] == y) # True
print(D[1] == x) # False
Note that the result cannot be interpreted as a 2D array, if that is what you have in mind. A 2D array would require each row (and column) to have the same number of elements. Accessing D[0][700] will work, while D[1][700] will fail.
* The terminology 'nested lists' assumes that x and y are lists. Enclosing them in another list [ ] makes them nested. However, if x and y are not lists but other types the principle is the same.

I believe what you are trying to do is make a 2D-Array. Such that for each place (such as array[0]) in the array there is another array?
myArray=[[1,2],[3,4]]
Or maybe just a regular array..

Is not possible to make arrays with different sizes as I understood you want to, and this is because a 2D-array is basically a table with rows and columns, and each row has the same number of columns, no matter what.
But, you can join the values in each variable and save the resulting strings in the array, and to use them again just split it back and parse the values to the type you need them.

Related

Numpy/Pytorch generate mask based on varying index values

I've been trying to do the following as a batch operation in numpy or torch (no looping). Is this possible?
Suppose I have:
indices: [[3],[2]] (2x1)
output: [[0,0,0,0,1], [0,0,0,1,1]] (2xfixed_num) where fixed_num is 5 here
Essentially, I want to make indices up to that index value 0 and the rest 1 for each element.
Ok, so I actually assume this is some sort of HW assignment - but maybe it's not, either way it was fun to do, here's a solution for your specific example, maybe you can generalize it to any shape array:
def fill_ones(arr, idxs):
x = np.where(np.arange(arr.shape[1]) <= idxs[0], 0, 1) # This is the important logic.
y = np.where(np.arange(arr.shape[1]) <= idxs[1], 0, 1)
return np.array([x, y])
So where the comment is located - we use a condition to assign 0 to all indices before some index value, and 1 after such value. This actually creates a new array as opposed to a mask that we can use to the original array - so maybe it's "dirtier".
Also, I suspect it's possible to generalize to arrays more than 2 dimensions, but the solution i'm imagining now uses a for-loop. Hope this helps!
Note: arr is just a numpy array of whatever shape you want the output to be and idxs is a tuple of what indices past you want to the array elements to turn into 1's - hope that is clear

How to opperate each cell of 2D array/matrix?

Hi I'm reading two rasters A & B as arrays.
What I'm looking for is to make an opperation to certain cells within two 2D arrays (two rasters). I need to subtract -3.0 to the cells in one array (A) that are greater than the other cells within the 2D array (B).
All the other cells don't need to change, so my answer will be the 2D array (B) with some changed cells that fit that condition and the other 2D array (A) untouched.
I tried this but doesn't seem to work (also takes TOO long):
A = Raster_A.GetRasterBand(1).ReadAsArray()
B = Raster_B.GetRasterBand(1).ReadAsArray()
A = array([ 917.985028, 916.284480, 918.525323, 920.709505,
921.835315, 922.328555, 920.283029, 922.229594,
922.928670, 925.315534, 922.280360, 922.715303,
925.933969, 925.897328, 923.880606, 923.864701])
B = array([ 913.75785758, 914.45941854, 915.17586919, 915.90724705,
916.6534542 , 917.4143068 , 918.18957846, 918.97902532,
919.78239295, 920.59941086, 921.42978108, 922.27316565,
923.12917544, 923.99736194, 924.87721232, 925.76814782])
for i in np.nditer(A, op_flags=['readwrite']):
for j in np.nditer(B, op_flags=['readwrite']):
if j[...] > i[...]:
B = j[...]-3.0
So the answer, the array B should be something like:
B = array([ 913.75785758, 914.45941854, 915.17586919, 915.90724705,
916.6534542 , 917.4143068 , 918.18957846, 918.97902532,
919.78239295, 920.59941086, 921.42978108, 922.27316565,
923.12917544, 923.99736194, 921.87721232, 922.76814782])
Please notice the two bottom right values :)
I'm a bit dizzy already from trying and doing other stuff at the same time so I apologize if I did any stupidity right there, any suggestion is greatly appreciated. Thanks!
Based on your example, I conclude that you want to subtract values from the array B. This can be done via
B[A<B] -= 3
The "mask" A<B is a boolean array that is true at all the values that you want to change. Now, B[A<B] returns a view to exactly these values. Finally, B[A<B] -= 3 changes all these values in place.
It is crucial that you use the inplace operator -=, because otherwise a new array will be created that contain only the values where A<B. Thereby, the array is flattened, i.e. looses its shape, and you do not want that.
Regarding speed, avoid for loops as much as you can when working with numpy. Fancy indexing and slicing offers you very neat (and super fast) options to work with your data. Maybe have a look here and here.

How to combine two arrays

I want to combine 2 arrays to get an array composed of the common values in the arrays. For example:
x = np.array ([1,2,3,4,6,11])
y = np.array ([3,6,5,2,9,8])
The result should be z = [2, 3, 6] which are the values that are common to both.
You're looking for the function np.intersect1d(x,y).
Edit: also, keep this readily accessible: https://docs.scipy.org/doc/numpy-1.17.0/numpy-ref-1.17.0.pdf I can't tell you how much I just pop over to it regularly for those kinds of weird one-off functions.

numpy array with Pillow's getcolors' dimensions

Pretty self-explanatory. Pillow's getcolors() method returns list of tuples, each with a (1,3) shape (i.e. (count, (r, g, b)) ). Unless there is a better way to handle this, how can I create a numpy array with a [n, [1, 3]] shape?
You should rather use a n x 4 dimensional numpy array. The first axis allows you to choose between different results of the getcolors method. The second axis contains your data. You can store in the first entry the count value, and then the r, g and the b value. Then you can do something like this:
result = np.empty(number, 4)
#get one entry
count, r, g, b = result[n]
You should always keep in mind, what you are acutally trying to do: The data you want to store contains 4 different integers, so it is 4-dimensional. And you expect n different data points of this type. Therefore, your array has to have the shape n x 4.
PS: You use a strange definition of shapes' dimension; this causes you a lot of trouble. I suggest using the default definition of shapes, and thinking about them as the axes of a multi-dimensional array.

List of Lists to 2D Array in Python

I have a list of lists in Python that holds a mix of values, some are strings and some are tuples.
data = [[0,1,2],["a", "b", "c"]]
I am wondering if there is a way to easily convert any length list like that to a 2D Array without using Numpy. I am working with System.Array because that's the format required.
I understand that I can create a new instance of an Array and then use for loops to write all data from list to it. I was just curious if there is a nice Pythonic way of doing that.
x = len(data)
y = len(data[0])
arr = Array.CreateInstance(object, x, y)
Then I can loop through my data and set the arr values right?
arr = Array.CreateInstance(object, x, y)
for i in range(0, len(data),1):
for j in range(0,len(data[0]), 1):
arr.SetValue(data[i][j], i,j)
I want to avoid looping like that if possible. Thank you,
Ps. This is for Excel Interop where I can set a whole Range in Excel by setting it to be equal to an Array. That's why I want to convert a list to an Array. Thank you,
Thing that I am wondering about is that Array is a typed object, is it possible to set its constituents to either string or integer? I think i might be constrained to only one. Right? If so, is there any other type of data that I can use?
Is setting it to Arrayobject ensures that I can combine str/int inside of it?
Also I though I could use this:
arr= Array[Array[object]](map(object, data))
but it throws an error. Any ideas?
You can use Array.CreateInstance to create a single, or multidimensional, array. Since the Array.CreateInstance method takes in a "Type" you specify any type you want. For example:
// gives you an array of string
myArrayOfString = Array.CreateInstance(String, 3)
// gives you an array of integer
myArrayOfInteger = Array.CreateInstance(Int32, 3)
// gives you a multidimensional array of strings and integer
myArrayOfStringAndInteger = [myArrayOfString, myArrayOfInteger]
Hope this helps. Also see the msdn website for examples of how to use Array.CreateInstance.

Categories