Skip to content

Instantly share code, notes, and snippets.

@Eyongkevin
Last active November 12, 2018 19:02
Show Gist options
  • Save Eyongkevin/cb93b99280079ec1ebfc484bdb18a97f to your computer and use it in GitHub Desktop.
Save Eyongkevin/cb93b99280079ec1ebfc484bdb18a97f to your computer and use it in GitHub Desktop.
Load data from txt file
# Load data
def loadDataSet(filename):
"""
load dataset into data-matrix and label-matrix
Parameters
----------
filename : String
File path containing the data
Returns
-------
dataLabel : collections
a simple class which represents the data-matrix and label-matrix
"""
#use collections.namedtuple to construct a simple class to represent each data matrix
data = collections.namedtuple('data',['dataMat','labelMat'])
with open(filename) as file:
numFeat = len(file.readline().split('\t')) - 1 # Assumes last value is the taget(-1)
dataMat = []; labelMat = []
with open(filename) as file:
for line in file.readlines():
lineArr = []
curLine = line.strip().split('\t')
[lineArr.append(float(curLine[i])) for i in range(numFeat)]
dataMat.append(lineArr)
labelMat.append(float(curLine[-1]))
dataLabel = data(dataMat, labelMat)
return dataLabel
filename = 'ex0.txt'
# Access data as data.datamat and label as data.labelMat
data = loadDataSet(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment