Problem while concatenating two numpy arrays - python-3.x

My aim is to rotate a 1d numpy array by left.For example desired output for numpy array [1,2,3,4] should be [2,3,4,1].
Here is my approach:
import numpy as np
x = np.array([1,2,3,4])
x1 = x[1:]
x2 = x[:1]
print(np.concatenate(x1,x2))
I am facing an error while concatenating. Why is that?
I tried this approach also:
lst = x[1:] + x[:1]
print(np.array(lst))
Although I am getting the desired output I am also getting a error
DeprecationWarning: elementwise comparison failed; this will raise an error in the future.

I already saw this kind of problem to build AES in python. But I didn't used numpy, only list: the step is called ShiftRows
Anyway, you can use: np.roll(your_array, int_shift)
For example:
>>> x = np.array([1,2,3,4])
>>> shift_x = np.roll(x, 3)
>>> shift_x
>>> array([2, 3, 4, 1])

Related

dask array map_blocks, with differently shaped dask array as argument

I'm trying to use dask.array.map_blocks to process a dask array, using a second dask array with different shape as an argument. The use case is firstly running some peak finding on a 2-D stack of images (4-dimensions), which is returned as a 2-D dask array of np.objects. Ergo, the two first dimensions of the two dask arrays are the same. The peaks are then used to extract intensities from the 4-dimensional dataset. In the code below, I've omitted the peak finding part. Dask version 1.0.0.
import numpy as np
import dask.array as da
def test_processing(data_chunk, position_chunk):
output_array = np.empty(data_chunk.shape[:-2], dtype='object')
for index in np.ndindex(data_chunk.shape[:-2]):
islice = np.s_[index]
intensity_list = []
data = data_chunk[islice]
positions = position_chunk[islice]
for x, y in positions:
intensity_list.append(data[x, y])
output_array[islice] = np.array(intensity_list)
return output_array
data = da.random.random(size=(4, 4, 10, 10), chunks=(2, 2, 10, 10))
positions = np.empty(data.shape[:-2], dtype='object')
for index in np.ndindex(positions.shape):
positions[index] = np.arange(10).reshape(5, 2)
data_output = da.map_blocks(test_processing, data, positions, dtype=np.object,
chunks=(2, 2), drop_axis=(2, 3))
data_output.compute()
This gives the error ValueError: Can't drop an axis with more than 1 block. Please useatopinstead., which I'm guessing is due to positions having 3 dimensions, while data has 4 dimensions.
The same function, but without the positions dask array works fine.
import numpy as np
import dask.array as da
def test_processing(data_chunk):
output_array = np.empty(data_chunk.shape[:-2], dtype='object')
for index in np.ndindex(data_chunk.shape[:-2]):
islice = np.s_[index]
intensity_list = []
data = data_chunk[islice]
positions = [[5, 2], [1, 3]]
for x, y in positions:
intensity_list.append(data[x, y])
output_array[islice] = np.array(intensity_list)
return output_array
data = da.random.random(size=(4, 4, 10, 10), chunks=(2, 2, 10, 10))
data_output = da.map_blocks(test_processing, data, dtype=np.object,
chunks=(2, 2), drop_axis=(2, 3))
data_computed = data_output.compute()
This has been fixed in more recent versions of dask: running the same code on version 2.3.0 of dask works fine.

how to remove element pairs from numpy array?

I have an array:
coordinates = np.asarray(list(product(seq, seq))) - fieldSize_va/2.0
This coordinates is numpy.ndarray type with 1600 elements (pairs). And can be seen as:
>>> array([[-4.5, -4.5], [-4.5, -4.26923077], [-4.5 , -4.03846154], ..., [4.5, 4.03846154], [4.5, 4.26923077], [4.5, 4.5]])
I have another array:
centralLines = np.asarray([(xa, ya),(xa, yb),(xb, ya),(xb, yb)])
which has values as:
>>> array([[ 0.11538462, 0.11538462], [ 0.11538462, -0.11538462], [-0.11538462, 0.11538462], [-0.11538462, -0.11538462]])
The coordinates variable contains all the pairs that are in centralLines variable. I want to remove centralLines pair elements from coordinates. How to do this??
The coordinates variable is computed using the following code:
import math
import numpy as np
from itertools import product
from numpy import linspace,degrees,random
N = 40 * 40
fieldSize_va = 9
seq = linspace(0, fieldSize_va, math.sqrt(N))
coordinates = np.asarray(list(product(seq, seq))) - fieldSize_va/2.0
Solution
One easy way to solve this would be to sweep the original array and keep the different pairs:
result = np.array([position for position in coordinates if position not in centralLines])
However, I must warn you that this solution is not optimized. Perhaps somebody else comes with a faster vectorized solution.
Sidenote 1
I would recommend you to follow some of the common guidelines of python syntax, namely PEP8.
Sidenote 2
Importing numpy just once improves readability of your code!
Repetitive:
import numpy as np
from numpy import linspace
seq = linspace(0, fieldSize_va, math.sqrt(N))
Better:
import numpy as np
seq = np.linspace(0, fieldSize_va, math.sqrt(N))
Sidenote 3
The square root is already included in numpy, as np.sqrt. You can then prescind of importing the math module.

Difference in the array type using numpy and cupy

I am using chainer library for my model and facing the below issue:
Say I have a file of test data having 3 features and a label (last column) with them. It is imported in the form of a list.
e.g.
test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]
Then I take the labels by converting the data into a numpy array and taking the labels column,
which I later convert into a list for comparison with the predicted labels say y_pred = [1,1,1,0]. i.e.
import numpy as np
y_true_np = list(np.array(test_set)[:,3])
print(y_true_np)
[1, 1, 0, 0]
My concern is when I run my model in GPU, it uses Cuda.cupy instead of numpy as I am using chainer library, and when I fetch the true labels I receive them as:
There by in cupy:
import cupy as cp
y_true_cp = list(cp.array(test_set)[:,3]) Or
y_true_cp = list(cuda.cp.array(test_set)[:,3])
Both returns a list of array:
y_true_cp: [array(1), array(1), array(0), array(0)]
As a workaround, I am using numpy in that specific place. Am I doing something wrong while using cupy, due to which I am not getting the values correctly?
While NumPy converts 0-dimensional arrays to scalars, CuPy does not.
https://docs-cupy.chainer.org/en/stable/reference/difference.html#zero-dimensional-array
In the result [array(1), array(1), array(0), array(0)], each data of arrays is on GPU. I'd use cupy.asnumpy if an efficient CPU array is needed.
y_true_cp = list(cp.asnumpy(cp.array(test_set)[:,3]))
There is no necessity to go through numpy.
Input
import cupy as cp
test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]
test_set = cp.array(test_set)
x_true = test_set[:, :3]
y_true = test_set[:, 3]
print("x_true:\n".format(x_true))
print("y_true:\n".format(y_true))
Output
x_true:
[[1 0 9]
[7 0 8]
[7 0 2]
[8 0 1]]
y_true:
[1 1 0 0]
As you wrote, it seems the behavior when you wrap by list is different
import numpy as np
import cupy as cp
print(list(np.arange(3)) # --> [0, 1, 2]
print(list(cp.arange(3)) # --> [array(0), array(1), array(2)]
However in your case, I think you can just use numpy array or cupy array without converting list.
y_true = test_set[:, 3] # it should work for both numpy & cupy
y_true_np = cuda.to_cpu(y_true) # If you want to convert the array to numpy

Creating mathematical equations using numpy in python

I want to create equations using numpy array multiplication ie I want to keep all variables in an array and its coefficients in other array and multiply both with each other to produce an expression so that I can use m.Equation() method of GEKKO. I tried the mentioned code but failed, please let me know how I can achieve my goal.
By "it failed" I meant that it just gave an error and did not let me use x*y==1 as equation in m.Equation() method available in GEKKO. My target is that I want to keep variables in one array and their coefficients in the other array and I multiply them to get mathematical equations to be used as input in m.Equation() method.
import numpy as np
from gekko import GEKKO
X = np.array([x,y,z])
y = np.array([4,5,6])
m = GEKKO(remote=False)
m.Equation(x*y==1)
# I wanted to get a result like 4x+5y+6z=1
The error I get is below
Traceback (most recent call last):
File "C:\Users\kk\AppData\Local\Programs\Python\Python37\MY WORK FILES\numpy practise.py", line 5, in <module>
X = np.array([x,y,z])
NameError: name 'x' is not defined
You need to define variables and make the coefficients into a Gekko object. You can use an array to make the variables and a parameter for the coefficients:
from gekko import GEKKO
m = GEKKO(remote=False)
X = m.Array(m.Var, 3)
y = m.Param([4, 5, 6])
eq = m.Equation(X.dot(y) == 1)
print(eq.value)
Output:
((((v1)*(4))+((v2)*(5)))+((v3)*(6)))=1

Print L and U matrices calculated by SuperLU using scipy

How can I print sparse L and U matrices calculated by splu, which uses SuperLU?
My MWE:
>>> import scipy
>>> import scipy.sparse
>>> import scipy.sparse.linalg
>>> from numpy import array
>>> M = scipy.array([ [19,0,21,21,0],[12,21,0,0,0],[0,12,16,0,0],[0,0,0,5,21],[12,12,0,0,18] ])
>>> cscM = scipy.sparse.csc_matrix(M)
>>> lu_obj = scipy.sparse.linalg.splu(cscM)
>>> b = array([1, 2, 3, 4, 5])
>>> lu_obj.solve(b)
array([ 0.01245301, 0.08812209, 0.12140843, -0.08505639, 0.21072771])
You can use
lu_obj = scipy.sparse.linalg.splu(A)
L,R = lu_obj.L, lu_obj.R
in the current scipy version, which returns the matrices in csc format (scipy docs).
Glancing through the scipy docs and source, scipy.sparse.linalg.splu does indeed use SuperLU. It looks like SuperLU may not explicitly calculate L or U. L & U are apt to be more dense than your original sparse matrix, so it makes sense to avoid storing them if they are not needed. If it is any consolation, your lu_obj does contain the permutaion info for L & U: lu_obj.perm_c, lu_obj.perm_r.
To get L & U, the path of least work is to use scipy.linalg.lu to get the LU matrixes. You'll have to convert your sparse matrixes to dense ones, though. ie
P, L, U = scipy.linalg.lu(cscM.todense())

Resources