473,288 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

how to do the mapping btw numpy arrayvalues and matrix columns

hi
i am looking for some info about mapping btw values in an array and
corresponding columns of a matrix

i have an numpy array=[11.0,33.0,22.0,55.0,44.0]
and a numpy matrix object=
matrix(([1.3,2.5,3.2,6.7,3.1],
[9.7,5.6,4.8,2.5,2.2],
[5.1,3.7,9.6,3.1,6.7],
[5.6,3.3,1.5,2.4,8.5]))
the first value of array(ie 11.0) is related to the first column of
matrix and so on..
i wish to create a mapping btw each val of array and corresponding col
of matrix..and then i want to sort the array and retrieve the matrix
columns for some values of sorted array..can anyone advise how to go
about it..

dn

Nov 3 '07 #1
2 3397
de****@gmail.com wrote:
hi
i am looking for some info about mapping btw values in an array and
corresponding columns of a matrix

i have an numpy array=[11.0,33.0,22.0,55.0,44.0]
and a numpy matrix object=
matrix(([1.3,2.5,3.2,6.7,3.1],
[9.7,5.6,4.8,2.5,2.2],
[5.1,3.7,9.6,3.1,6.7],
[5.6,3.3,1.5,2.4,8.5]))
the first value of array(ie 11.0) is related to the first column of
matrix and so on..
i wish to create a mapping btw each val of array and corresponding col
of matrix..and then i want to sort the array and retrieve the matrix
columns for some values of sorted array..can anyone advise how to go
about it..

dn
If this were a less helpful mailing list I'd say something like "there
is also a numpy mailing list and you've overwritten the object class."

Anyway, the following would work, but it's not going to be fast for
millions of elements:

from numpy import matrix, asarray
obj = matrix(([1.3,2.5,3.2,6.7,3.1],
[9.7,5.6,4.8,2.5,2.2],
[5.1,3.7,9.6,3.1,6.7],
[5.6,3.3,1.5,2.4,8.5]))
ar = asarray(obj)
val_to_col_list = []
for row in ar:
for ind,val in enumerate(row):
val_to_col_list.append((val,ind))
val_to_col_list.sort()

If instead you require a map, such that each value maps to a list of the
columns it appears in, you could try the following:

val_col_map = {}
for row in ar:
for col,val in enumerate(row):
tmplist=val_col_map.get(val,[])
tmplist.append(col)
val_col_map[val]=tmplist
val_keys = val_col_map.keys()
val_keys.sort()

val_keys is now a sorted list of unique values from your original
matrix. Use these values as keys for val_col_map.

Eww... but it works. You'll want to be really careful with floating
point numbers as keys, see http://docs.python.org/tut/node16.html for
more details.

Best of luck,

Cameron.
Nov 3 '07 #2
CW,
thanx for the reply..but i was looking for a mapping BTW each item of
a numpy.ndarray and the corresponding column of a numpy.matrix ,after
some struggle :-) i came up with this

#a function to return a column from a matrix
def getcol(data, colindex):
return data[:,colindex] #returns a matrix obj

#function to set a column of a matrix with a given matrix
def setcol(data, inmat,colindex):
data[:,colindex]=inmat #both data and inmat are matrix objs

#now i have an ndarray with 5 elements
evalarray=array(([11.0,33.0,22.0,55.0,44.0]))

#and a matrix with 5 columns
evectmat=matrix(([1.3,2.5,3.2,6.7,3.1],
[9.7,5.6,4.8,2.5,2.2],
[5.1,3.7,9.6,3.1,6.7],
[5.6,3.3,1.5,2.4,8.5]

))

the first column of evectmat corresponds to first element of evalarray
and so on..
then i did this

mydict=dict(
[(evalarray[x],getcol(evectmat,x)) for x in range(len(evalarray))]
)
klst=mydict.keys()

klst.sort()
klst.reverse() #because i want the largest value as first

newevectmat=matrix(zeros((4,5)))

for x in range(len(klst)):
newcol=mydict[klst[x]]
setcol(newevectmat,newcol,x)

print "newevectmat:"
print newevectmat

this gives me the desired result..now i have a new matrix with columns
arranged corresponding to the values of evalarray

i don't know if this is a good way(or even pythonish ) to do it..i am
a beginner afterall.!. any suggestions most welcome
TIA
dn



>
from numpy import matrix, asarray
obj = matrix(([1.3,2.5,3.2,6.7,3.1],
[9.7,5.6,4.8,2.5,2.2],
[5.1,3.7,9.6,3.1,6.7],
[5.6,3.3,1.5,2.4,8.5]))
ar = asarray(obj)
val_to_col_list = []
for row in ar:
for ind,val in enumerate(row):
val_to_col_list.append((val,ind))
val_to_col_list.sort()

If instead you require a map, such that each value maps to a list of the
columns it appears in, you could try the following:

val_col_map = {}
for row in ar:
for col,val in enumerate(row):
tmplist=val_col_map.get(val,[])
tmplist.append(col)
val_col_map[val]=tmplist
val_keys = val_col_map.keys()
val_keys.sort()

val_keys is now a sorted list of unique values from your original
matrix. Use these values as keys for val_col_map.

Eww... but it works. You'll want to be really careful with floating
point numbers as keys, seehttp://docs.python.org/tut/node16.htmlfor
more details.

Best of luck,

Cameron.

Nov 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew Felch | last post by:
So I wanted to matrixmultiply , ] * Of course this is impossible, because the number of columns in the
22
by: J | last post by:
Hi I hope the title of this message indicates my question. I am looking for basic array functionality in Python and it turns out that there are all these packages which are somehow related....
15
by: nikie | last post by:
I'm a little bit stuck with NumPy here, and neither the docs nor trial&error seems to lead me anywhere: I've got a set of data points (x/y-coordinates) and want to fit a straight line through...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
1
by: Michael O'Brien | last post by:
Hola~ I have a large array of points (over a million). I would like to multiply each point in the array by a 4x4 matrix. I keep thinking there should be an easy way to do this using numpy, but I...
4
by: Christian Convey | last post by:
I need to bang out an image processing library (it's schoolwork, so I can't just use an existing one). But I see three libraries competing for my love: numpy, numarray, and numeric. Can anyone...
6
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix...
7
by: kulpojke | last post by:
I am trying to map a function to the contents of an array using the map() function. However, whenever the following function is called an error message is returned complaining that: ... line...
1
by: csgirlie | last post by:
I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays. import os # for file handling functions import numpy as...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.