I am reading in files containing 238 x 1 feature vectors. Attempting to grab the first 2 numbers in each file to create a 2d plot using numpy. I've got a decent start, just am stuck on how to fill the np array that I want to plot.

``````import os                # for file handling functions
import numpy as np            # for array/matrix processing
import matplotlib.pyplot as plt        # for general plotting

femaleDataSets = 0
females = []
lists = []
femaleVectors = 0
femaleSizes = 0

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
index = fname.find("female")
if index != -1:
females.append(index)
femaleDataSets +=1
tempFile = open(filePath+"/"+fname, 'rU') # open each female file
for line in tempFile:
femaleVectors +=1
for word in line.split():
femaleSizes +=1
lists.append(word)

print "Number of females: ", femaleDataSets

femaleData = np.zeros((femaleSizes, femaleVectors))

for j in range(femaleSizes):
for i in range(femaleVectors):
femaleData[j, i] = # fill array

plt.hold('on')
plt.plot(femaleData[0,:],femaleData[1,:],'r+')
plt.title('Female/Male Feature Vectors')
plt.show()``````

## All 3 Replies

Can you give us a short example what your data file looks like and what you want to extract?

Sample data file contains:
0.157957
-0.108352
-0.416099
-0.147049
0.194426
-0.0338146
-0.146482
0.0952099

..all the way down to 238 numbers.

I'd like to to go ahead and read them all into 238x1 np arrays. As for plotting, I'd just grab the first 2 numbers as a 2x1 vector for each file.

Can someone show me how to go about filling an array using the nested for loops at the bottom.. for just one of my 238 X1 files - just to give me a start?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.