Converting text into a numpy array, doing machine learning or any other task, is essential for text processing skills. The python implementation enables very thin and powerful text processing capabilities:
Suppose the file traindata.csv has data 1000 rows, 3 column features, and the fourth column (the last column) is the class label
1. Basic methods:
def File2matrix (): = [] = [] = open ('./traindata.csv','RB ' )
Fr.readline () for in fr.readlines (): #读取每一行
CurLine = Line.strip (). Split (' \ t ')
Linearr = []
For I in range (3):
Linearr.append (float (curline[i)) # Read each property
Datamat.append (Linearr)
Labelmat.append (float (curline[3]))
Return Datamat,labelmat
2. Using the CSV module
Import CSV def File2matrix (): = Open ('./traindata','rb') = Csv.reader (FR) lines.next () // Ignore first line for in lines: ....
3. Using the Pandas module
ImportPandas as PDdefFile2matrix (): Fr= Open ('./traindata.csv','RB') DF= Pd.read_csv (fr,header=0) Datamat= df[['Feature1','Feature2','Feature3']] Labelmat= df['label'] returnDatamat,labelmat
Obviously, if the skilled palmprint pandas will be very simple, so easy.
The end of the paper is shallow, I know this matter to preach ....
Just do it!
Python reads text