473,748 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 4966
Glenton
391 Recognized Expert Contributor
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
8269
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++ i read with fin.read((char *) &pl sizeof pl) which brings out the text in the file . But the matrix of numbers just appears as nothing.
6
2244
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: int(2), married: boolean, etc) and then use that custom data in a matrix? Actually, this is a two question question :P Im doing a simple hex based game and i need a way to store every hex property (color, owner,x, y, etc) in a matrix's "cell",...
3
427
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 noticed an apparent error of inverion of a 19x19 matrix. Denote this matrix as KK, U=KK^ -1, I found...
14
7125
by: James Stroud | last post by:
Hello All, I'm using numpy to calculate determinants of matrices that look like this (13x13):
2
3421
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
23572
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
5964
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 = file('test.dat','wb') a.tofile(fd) b.tofile(fd) fd.close()
0
1361
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
207
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 basic matrix library useful. Your short list covers most of the important stuff.
0
8983
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6072
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.