473,399 Members | 4,177 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,399 software developers and data experts.

How can I read a data file into a 2xN matrix using NumPy arrays?

I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays.

Expand|Select|Wrap|Line Numbers
  1. import os                # for file handling functions
  2. import numpy as np            # for array/matrix processing
  3. import matplotlib.pyplot as plt        # for general plotting
  4. from mpl_toolkits.mplot3d import axes3d    # for 3d plotting
  5.  
  6. wordList=[]
  7. numFiles = 0
  8. numDataSets = numVectors = sizeVector = 0
  9.  
  10. print"\n\nBeginning data processing."
  11. # traverse filePath folder
  12. filePath = "Train"
  13. for file in os.listdir(filePath):
  14.     numDataSets +=1                # one data set per file
  15.     print "Loading the file: " + file + "."
  16.     tempFile = open(filePath+"/"+file, 'rU')
  17.     for line in tempFile:
  18.         numVectors +=1            # one vector per line
  19.         for word in line.split():
  20.             sizeVector +=1        # components in vector
  21.             wordList.append(word)
  22.  
  23. sizeVector = sizeVector / numVectors
  24. numVectors = numVectors / numDataSets
  25.  
  26. # print"\nHere's all the data:\n", wordList, "\n\n"
  27. print "numDataSets: ",numDataSets
  28. print "numVectors: ",numVectors
  29. print "sizeVector: ",sizeVector
  30. x = eval(wordList[5])
  31.  
  32. # Structure our arrays to hold our data.
  33. # We'll use the three sample class data sets from the text.
  34. # sizeVector of rows and numVectors of columns for column vector data.
  35. # In other words, each feature vector will be a column vector for set array.
  36. w1 = np.zeros((sizeVector, numVectors))
  37. w2 = np.zeros((sizeVector, numVectors))
  38. w3 = np.zeros((sizeVector, numVectors))
  39.  
  40. #   Load matrix array with class w1 numeric values,
  41. # replacing elements indexed as in matrices row, col starting from 0.
  42. #   Pull elements from wordList based on dataSet, sizeVector, and
  43. # which vector we're on during the column, row iteration.
  44.  
  45. skip = numVectors*sizeVector
  46.  
  47. dataSet = 0    
  48. for j in range(sizeVector):
  49.     for i in range(numVectors):
  50.         (do something - nothing I've tried is working)
  51. print "\nClass w1 is as follows: \n",w1
  52.  
  53. dataSet = 1    
  54. for j in range(sizeVector):
  55.     for i in range(numVectors):
  56.         (do something - nothing I've tried is working)
  57. print "\nClass w2 is as follows: \n",w2
  58.  
  59. dataSet = 2    
  60. for j in range(sizeVector):
  61.     for i in range(numVectors):
  62.         (do something - nothing I've tried is working)
  63. print "\nClass w3 is as follows: \n",w3
I need advice on how to get my dataSet(s) set up in this 2xN format.
Sep 15 '09 #1
1 4928
Glenton
391 Expert 256MB
Hi

This is a bit difficult to engage on, because of the level of detail. You might also get better responses if you say more generally what you're trying to achieve and why.

But, if I understand it, you have a list, called wordList which has (3 x numVectors x sizeVector) elements, and you want to change it into 3 separate numpy arrays each of which has 2 columns and (numVectors x sizeVector)/2 rows. Or, perhaps into arrays which have sizeVector columns and numVectors rows.

So the key lines might be something like
w1[i,j]=wordList[i+numVectors*j]
w2[i,j]=wordList[i+numVectors*j+size]
w2[i,j]=wordList[i+numVectors*j+2*size]

or maybe w1[i][j]

I haven't had a chance to check it yet. Is something like this what you've tried?
Sep 24 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: maxwell | last post by:
I used to program in matlab An read an image file with : fread(a,,0) which gave me a matrix of 256*256 full of numbers which were the pidxel values from 0 to 256. How do i do the same in c++ ...
6
by: Gaz | last post by:
Hi guys. I've been lookig for this in the numpy pdf manual, in this group and on google, but i could not get an answer... Is there a way to create a custom data type (eg: Name: string(30), Age:...
3
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...
14
by: James Stroud | last post by:
Hello All, I'm using numpy to calculate determinants of matrices that look like this (13x13):
2
by: devnew | last post by:
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= and a numpy matrix object= matrix((, , , ))
2
by: sapsi | last post by:
Hello, I have a numpy array (2 rows 3 colums) import numpy a=numpy.array( , ]) I wish to add a row, this is how i do it s=a.shape numpy.resize(a,s+1,s)
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
0
by: arsyed | last post by:
On Thu, Aug 7, 2008 at 1:36 PM, Simon Parker <simon_ecc@yahoo.co.ukwrote: You can use numpy: http://www.scipy.org/NumPy for example: In : arr = numpy.char.array()
0
by: Carl Banks | last post by:
On Sep 22, 4:02 am, Al Kabaila <akaba...@pcug.org.auwrote: I wouldn't since I do pretty advanced stuff and I'm happy to use numpy as-is, but I suspect a lot of people would find a convenient...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.