thesis-anne/CsvHandler.py

53 lines
1.7 KiB
Python
Raw Normal View History

2018-09-05 12:08:13 +00:00
'''
Csv Handler
===========
CsvHandler writes articles' information to csv file and reads it.
'''
import csv
2018-09-26 08:30:17 +00:00
import numpy as np
2018-09-05 12:08:13 +00:00
import pandas as pd
2018-09-10 08:38:24 +00:00
class CsvHandler:
2018-09-05 12:08:13 +00:00
def read_csv(csv_file):
2018-09-17 12:47:50 +00:00
df = pd.read_csv(csv_file,
sep='|',
header=0,
engine='python',
2018-09-05 12:08:13 +00:00
usecols=[1,2,4], #use only 'Title', 'Text' and 'Label'
2018-09-17 12:47:50 +00:00
decimal='.',
2018-09-05 12:08:13 +00:00
quotechar='\'',
#nrows = 200,
quoting=csv.QUOTE_NONE)
return df
2018-09-17 12:47:50 +00:00
2018-09-05 12:08:13 +00:00
def write_csv(df, file_name):
df.to_csv(file_name, sep='|')
2018-09-26 08:30:17 +00:00
print('# saved {} article(s) in {}'.format(len(df), file_name))
def select_randoms(df, n):
'''selects n random samples from dataset.
params: df DataFrame to select items from,
n number of items to select randomly,
returns new DataFrame with only selected items
'''
# new empty DataFrame
df_samples = pd.DataFrame(columns=['rands','title','text','label'])
# initialize random => reproducible sequence
np.random.seed(5)
# pseudorandom float -1.0 <= x <= 1.0 for every sample
pd.Series()
# add new column 'Random'
df['Random'] = pd.Series(np.random.randn(len(df)), index=df.index)
# sort DataFrame by random numbers
df = df.sort_values('Random')
# return first n elements of randomly sorted dataset
return df.iloc[0:n]
if __name__ == '__main__':
df = CsvHandler.read_csv('classification_labelled_corrected.csv')
df_new = CsvHandler.select_randoms(df, 10)
CsvHandler.write_csv(df_new, 'samples_10.csv')