Python update a dictionary of array - python

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.

Related

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!

Python sklearn OneHotEncoding categorical and sometimes repeated values

This is my problem with sklearn's OneHotEncoder.
with an array a = [1,2,3,4,5,6,7,8,9,22] i.e ALL UNIQUE of a.shape=[10,1] (after reshape(-1,1), a [10,10] matrix of OneHotEncoded values is returned.
array([[ 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., 0., 0., 0., 1., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]])
But with an array like a = [1,2,2,4,4,6,7,8,9,22] i.e NON UNIQUE of a.shape=[10,1] (after reshape(-1,1), a [10,8] matrix of OneHotEncoded values is returned.
array([[ 1., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 1.]])
But I cannot use this as my input placeholder expects a [10,10] matrix as input. Can anyone help me handle non-unique values in sklearn's OneHotEncoder?
P.S Adding the parameter n_values= 10 gives an error saying ValueError: Feature out of bounds for n_values=10
Do you know all the values your categorical feature can take? If so, you can do something like this:
enc = OneHotEncoder()
enc.fit(np.asarray([1,2,3,4,5,6,7,8,9,22]).reshape(-1, 1)) #fit your encoder to the values
data_for_encoding = np.asarray([1,2,2,4,4,6,7,8,9,22]).reshape(-1, 1) #your data
sparse_matrix = enc.transform(data_for_encoding) #encoded data

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.]])

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.]])

Assigning subvectors to multidimensional arrays in Numpy

Say I have a matrix:
from numpy import *
a = zeros(shape=(nRows,nColumns));
and I would like to fill in the first column with ones, when I try to do:
a[:][0] = ones(shape=(nRows,1))
I get:
ValueError: output operand requires a reduction, but reduction is not enabled
Why is it not working?
It should be as easy as:
a[:,0] = 1
broadcasting will handle the details.
>>> a = np.zeros((10,10))
>>> a
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.]])
>>> a[:,0]=1
>>> a
array([[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])

Categories

Resources