Quora Question Pairs

官方比赛链接:https://www.kaggle.com/c/quora-question-pairs

here is some tips:

pandas读取数据的问题

dataframe=pd.read_csv('csvfile')

question1=list(dataframe['question1'])

question1中某些数据会被转换为float格式,需要转为str格式。加上下面这句:

question1=[str(i) for i in question1]

keras分词器Tokenizer

Tokenizer是一个用于向量化文本,或将文本转换为序列(即单词在字典中的下标构成的列表,从1算起)的类。 示例程序:

import keras.preprocessing.text as T

from keras.preprocessing.text import Tokenizer

text1='some thing to eat'

text2='some thing to drink'

texts=[text1,text2]

print T.text_to_word_sequence(text1) #['some', 'thing', 'to', 'eat']

print T.one_hot(text1,10) #[7, 9, 3, 4]

print T.one_hot(text2,10) #[7, 9, 3, 1]

tokenizer = Tokenizer(num_words=10)

tokenzier.fit_on_text(texts)

print tokenizer.word_count #[('some', 2), ('thing', 2), ('to', 2), ('eat', 1), ('drink', 1)]

print tokenizer.word_index #{'some': 1, 'thing': 2,'to': 3 ','eat': 4, drink': 5}

print tokenizer.word_docs #{'some': 2, 'thing': 2, 'to': 2, 'drink': 1, 'eat': 1}

print tokenizer.index_docs #{1: 2, 2: 2, 3: 2, 4: 1, 5: 1}

print tokenizer.text_to_sequences(texts) #[[1, 2, 3, 4], [1, 2, 3, 5]]

print tokenizer.text_to_matrix(texts) #

[[ 0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],

[ 0., 1., 1., 1., 0., 1., 0., 0., 0., 0.]]

保存数组为numpy的npy二进制格式

np.save(npyfile, array)

array=np.load(npyfile)

with open(jsonfile, 'w') as f:

json.dump({'nb_words': nb_words}, f)

with open(NB_WORDS_DATA_FILE, 'r') as f:

nb_words = json.load(f)['nb_words']

Copyright © 2022 九州天命装备站 - 装备获取&角色养成活动 All Rights Reserved.