Python numpy array strange return - python

I am relatively new to python. I have a numpy array that has 3 dimensions. I know we can display only few elements using :.
It seems to work just fine while I'm doing it starting from a small value, but at one point, it returns something different than a matrix.
I want to get the mean value for the array. So, for instance, given an array c, I do numpy.mean(c[0:200][0:200][0:200]). This works just fine. But increasing the starting point (i.e. c[200:][200:][200:]) doesn't work and returns nan. So, printing the result explains the nan value. But I don't get why c[200:][200:][200:] returns this kind of answer.
Here's two examples:
In [68]: c.shape
Out[68]: (448, 433, 446)
In [63]: c[100:][100:][100:]
Out[63]:
array([[[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]],
[[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]],
[[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]],
...,
[[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]],
[[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]],
[[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]]])
In [67]: c[200:][200:][200:]
Out[67]: array([], shape=(0, 433, 446), dtype=float64)

You're indexing into the arrays improperly. The way to index on multiple dimensions is array[x, y, z], not array[x][y][z]. So you want to do c[200:, 200:, 200:].
When you use a single index in brackets, it indexes into the first dimension. So when you do c[200:][200:][200:], you try to get the first 200 elements of the array along the first dimension every time. But that dimension is less than 600 elements long, so when you do it three times there's nothing left to get.

Related

How to store a numpy array with shape (32,32) as (1,32,32)

For various reasons, I need to transfer a 2d numpy array of shape (32,32) to 3d shape (1,32,32). Is there a way to do this?
Just use np.reshape:
>>> a = np.zeros((32, 32))
>>> a.reshape((1, 32, 32))
array([[[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]]])
>>>
I used a array of zeros for demonstrating.

CVXPY: Using two sets of constraints of different format returns None

I am new to CVXPY. I have been trying to solve an easy feasibility problem. Here is the code:
from cvxpy import *
import numpy as np
dim = np.shape(Bs[0])[1]
X = Variable(dim)
objective = Minimize(0)
constraintsA = [X.T * M * X + B * X + C == 0 for M, B, C in zip(Ms, Bs, Cs)]
constraintsB = [A * X - b == 0 for A, b in zip(As, bs)]
constraints = constraintsA + constraintsB
prob = Problem(objective, constraints)
result = prob.solve()
R = X.value
print R
Below, you can find the input matrices:
Ms = [array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]]),
array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]]),
array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]])]
Bs = [array([[-18., 0., 0., 18., 0., 0.]]),
array([[ 0., 0., 0., 0., -36., 0.]]),
array([[ 0., 0., -18., 0., 0., -18.]])]
Cs = [270.0, 540.0, 810.0]
As = [array([[ 0., -1., 1., 0., 0., 0.],
[ 0., 0., 0., 0., -1., 1.]]),
array([[-1., 1., 0., 0., 0., 0.],
[ 0., 0., 0., -1., 1., 0.]]),
array([[-1., 0., 1., 0., 0., 0.],
[ 0., 0., 0., -1., 0., 1.]])]
bs = [array([[ 0],
[ 1.00000000e+01]]),
array([[ 20.],
[ 10.]]),
array([[ -2.00000000e+01],
[ 0]])]
Running it only for constraintsA, the code is executed and returns back a result vector as expected. However, when considering the constraintsB set too, my result is None. Any ideas where I might have messed it up? Thanks a lot!

Appending matrix A with matrix B

Say I have two matrices A and B. For example,
A = numpy.zeros((5,5))
B = np.eye(5)
Is there a way to append A and B?
It sounds to me like you're looking for np.hstack:
>>> import numpy as np
>>> a = np.zeros((5, 5))
>>> b = np.eye(5)
>>> np.hstack((a, b))
array([[ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
np.vstack will work if you want to stack them downward:
>>> np.vstack((a, b))
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 1.]])

Python update a dictionary of array

I have an empty 'numpy.ndarray' to update.
import numpy as np
my_grid = np.zeros((5, 5))
# stat
parse = "max","min","avg"
# create a dictionary for each element of parse
grid_stat = {}
for arg in parse:
grid_stat[arg] = my_grid
grid_stat
{'avg': array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]),
'max': array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]),
'min': array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])}
I wish to update with new value each grid in the dictionary (it will be part of a loop)
ex: on dy = 0, dx = 0, max= 100, min= 50, avg = 75
grid_stat
{'avg': array([[ 75., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]),
'max': array([[ 100., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]),
'min': array([[ 50., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])}
i tried an easy solution
grid_stat['avg'][0,0] = 100 but also for max and min the value updated is 100
grid_stat
{'avg': array([[ 100., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]),
'max': array([[ 100., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]),
'min': array([[ 100., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])}
As I suggested in the comments to the last question, you probably want to use
for arg in parse:
grid_stat[arg] = my_grid.copy()
instead of
for arg in parse:
grid_stat[arg] = my_grid
sets each value of grid_stat to the very same array, the one called my_grid. It doesn't make three separate arrays of the same shape and contents. You can confirm this by using id or is:
>>> id(my_grid)
4325774752
>>> id(grid_stat['max'])
4325774752
>>> id(grid_stat['avg'])
4325774752
>>> id(grid_stat['min'])
4325774752
>>> my_grid is grid_stat['max']
True
>>> grid_stat['max'] is grid_stat['avg']
True
etc.

Using numpy to Obtain Submatrix

I'm trying to do the following with numpy (python newbie here)
Create a zeroed matrix of the rigth dimensions
num_rows = 80
num_cols = 23
A = numpy.zeros(shape=(num_rows, num_cols))
Operate on the matrix
k = 5
numpy.transpose(A)
U,s,V = linalg.svd(A)
Extract sub-matrix
sk = s[0:(k-1), 0:(k-1)]
Results on error
Traceback (most recent call last):
File "tdm2svd.py", line 40, in <module>
sk = s[0:(k-1), 0:(k-1)]
IndexError: too many indices
What am I doing wrong?
to answer your question s is only a 1d array ... (even if you did actually transpose it ... which you did not)
>>> u,s,v = linalg.svd(A)
>>> s
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>>
for selecting a submatrix
I think this does what you want ... there may be a better way
>>> rows = range(10,15)
>>> cols = range(5,8)
>>> A[rows][:,cols]
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
or probably better
>>> A[15:32, 2:7]
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])

Categories

Resources