0

My code requires me to replace elements of an array of dimension 3x3 with a list or array of a certain dimension. How can I achieve that? When I write my code, it throws an error stating that:

ValueError: setting an array element with a sequence.

My code:

import numpy as np
Y=np.array([1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,4])
c_g=np.array([[[1,2],[2,3]],[[4,5],[1,6]]])
xx=[1,2,3]
var=2
theta_g=np.zeros((c_g.shape[0],c_g.shape[1]))
for i in range(c_g.shape[0]):
    for j in range(c_g.shape[1]):
         theta_g[i][j]=Y[var:var+len(c_g[i][j])**len(xx)]
         #here Y is some one dimensional array or list which I want to //
         #assign to each element of theta_g
         var=var+len(c_g[i][j])**len(xx)
print theta_g

In the code above I want to manipulate theta_g. In fact, I want to assign an array to each element of theta_g. How can I accomplish that? Desired Output: theta_g which is a matrix of dimension equal to that of c_g.

Amardeep Mishra
  • 129
  • 1
  • 4
  • 16
  • can you mention your desired o/p ? – Vikas Periyadath Feb 07 '18 at 07:07
  • what is Y in your code? and provide in detail traceback. – Sociopath Feb 07 '18 at 07:09
  • Desired output is a matrix that has dimension equal to that of c_g. That is it is a matrix with dimenion 2x2 and the elements of this matrix are nothing but list or array. – Amardeep Mishra Feb 07 '18 at 07:10
  • Akshay I have edited my code, Y is a single dimensional array. – Amardeep Mishra Feb 07 '18 at 07:11
  • what is var? please give a full testable example. Also I'm still a little confused as to what your desired output is. You want an array, similar to that of c_g, which has what in each entry? – Banana Feb 07 '18 at 07:13
  • Maybe transform your sequence into an array? To do that, this seems to be a good way: https://stackoverflow.com/a/41624067/3715676 – francisaugusto Feb 07 '18 at 07:16
  • @francisaugusto, Hi thanks for your inputs, I want the matrix theta_g to be of the same shape as c_g. Also each element of theta_g should be list. The main problem I face is in assigning a list to each element of theta_g. – Amardeep Mishra Feb 07 '18 at 07:22

2 Answers2

1

I think you should just specify the type of the elements of the array as np.ndarray or list, like so:

theta_g=np.zeros((c_g.shape[0],c_g.shape[1]), dtype=np.ndarray)

Because you didn't really explain the logic of assignment, let me demonstrate in my own example, where I assign some arrays to a 2x2 array:

from itertools import product
Y = np.array([0,0,1,2,10,20])
Z = np.zeros((2,2), dtype=np.ndarray)
for i,j in product(range(0,2), repeat = 2):
    Z[i,j] = Y[2*(i+j):2+2*(i+j)]
print(Z)

prints

[[array([0, 0]) array([1, 2])] [array([1, 2]) array([10, 20])]]

Banana
  • 1,149
  • 7
  • 24
  • Yeah thanks a lot man! the first trick using np.ndarray worked! thanks once again! – Amardeep Mishra Feb 07 '18 at 08:06
  • glad to be of help. – Banana Feb 07 '18 at 08:10
  • Hi @Banana , also do you know any method in python to control the execution rate of a python loop? Lets say I want to run my loop at 100 herts or 10 milli seconds. How can I achieve that? – Amardeep Mishra Feb 07 '18 at 08:28
  • @AmardeepMishra hey, yes you can use "import time" and then call "time.sleep(num_of_seconds)" to wait somewhere in the code. But if you have any further questions on that please search for it first and then open a new question, so others can also give their advice :) – Banana Feb 07 '18 at 08:36
0

You can use np.stack.

  >>> a = [np.array([1, 2]), np.array([3, 4])]
  >>> np.stack(a)
  array([[1, 2],
           [3, 4]])