473,545 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3410
de****@gmail.co m 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,in d))
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.key s()
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(evectma t,x)) for x in range(len(evala rray))]
)
klst=mydict.key s()

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

newevectmat=mat rix(zeros((4,5) ))

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

print "newevectma t:"
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,in d))
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.key s()
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
1793
by: Andrew Felch | last post by:
So I wanted to matrixmultiply , ] * Of course this is impossible, because the number of columns in the
22
1986
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. Some are allegedly discontinued but still seem to get updated. Could we start a discussion about which package will or may or should survive ?
15
2201
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 them, using LMSE linear regression. Simple enough. I thought instead of looking up the formulas I'd just see if there isn't a NumPy function that does...
2
3957
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 had a problem, however we have a problem set where we are creating a large matrix and finding it's inverse to solve the problem. To invert the matrix...
1
2003
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 can't figure out the mojo to do it. Is that possible? MO
4
3367
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 recommend which one I should use? If one is considered the officially blessed one going forward, that would be my ideal. Thanks, Christian
6
6907
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 operations, mostly matrix inversion and trasposition. The dimentions of the matrices used are about 29x19,19x19 and 29x29. During the calculation, I...
7
1904
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 121, in b_maker b = map(function,b) TypeError: 'matrix' object is not callable This is on a windows32 system, using python 2.5 with the numpy...
1
4946
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 np # for array/matrix processing import matplotlib.pyplot as plt # for general plotting from mpl_toolkits.mplot3d import axes3d ...
0
7406
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7660
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7431
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7761
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5337
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3457
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1888
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.