[SciPy-user] Transforming 1-d array to 2-d array
Rich Shepard
rshepard at appl-ecosys.com
Mon Feb 26 16:44:42 EST 2007
On Mon, 26 Feb 2007, Dave wrote:
Dave,
I tried applying this to one of my data arrays, but it fails at the next
to last step. I'll put in my code and the results of print statements and
ask what's different between your working example and my non-working data.
> #Define array size
> N = 8
>
> #Define the data to put in the upper-diagonal part of the array
> myData = rand(N*(N-1)/2)
I have three arrays; the first one is named barEco and contains:
[[ 2.29075869 2.12453058 3.06339111 2.88526612 3.32199956 2.96319486
3.12649018 2.81580625 3.23207315 2.493608 2.49459335 2.86643834
2.77111816 2.26500627 2.4902972 2.81149761 2.46213192 2.80432329
2.86150888 3.1135404 1.96135592 3.34577184 3.27458386 2.90845738
2.88745987 2.51245188 2.91666234 2.97438117]]
I see that your myData array has only single brackets while my data have
two. That seems to have resulted from manipulations where the database
records were separated into individual lists and then averages computed
> #Create an index to the upper-diagonal part
> idx = triu(ones([8,8])-eye(8)).nonzero()
idx = triu(ones([8,8])-eye(8)).nonzero()
print idx
(array([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4,
4, 4, 5, 5, 6]), array([1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 3, 4,
5, 6, 7, 4, 5, 6, 7, 5,
6, 7, 6, 7, 7]))
So this is OK.
> #Instantiate a zero array and populate the upper-triangular part with myData
> A = zeros([N,N],dtype=float)
symEco = zeros([N,N],dtype=float)
print symEco
[[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 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[idx] = myData
symEco[idx] = barEco
print symEco
Traceback (most recent call last):
File "/data1/eikos/scopingPage.py", line 184, in OnCalcWeights
inpWts = functions.weightcalc()
File "/data1/eikos/functions.py", line 177, in weightcalc
symEco[idx] = barEco
ValueError: array is not broadcastable to correct shape
Rich
--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc. | Accelerator(TM)
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
More information about the SciPy-User
mailing list