Kaggle competition: Quora Insincere Questions Classification
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<train_model>
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import tensorflow as tf
from sklearn.model_selection import train_test_split
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )<predict_on_test>
zip_ref = zipfile.ZipFile('/kaggle/input/quora-insincere-questions-classification/embeddings.zip', 'r')
 print(zip_ref.namelist())
 embeddings = zip_ref.open('glove.840B.300d/glove.840B.300d.txt', 'r' )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
def get_coefs(word,*arr):
return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.decode().split(" ")) for o in embeddings )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_submission.csv",index=False )<import_modules>
train_data = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')
 test_data = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')
 print('训练集维度：
',train_data.shape)
 print('测试集维度：
',test_data.shape)
 train_data.sample(5 )
import matplotlib.pyplot as plt<load_from_csv>
train_input = list(train_data['question_text'])
 train_label = list(train_data['target'])
 test_input = list(test_data['question_text'] )
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )<load_from_csv>
stop=stopwords.words('english')
 def remove_stop_words(x):
for word in stop:
token = " " + word + " "
if(x.find(token)!= -1):
x = x.replace(token, " ")
 return x
train_input_rsw = list(map(remove_stop_words, train_input))
test_input_rsw = list(map(remove_stop_words, test_input))
sample_submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )<prepare_x_and_y>
max_features=100000
embed_size = 300
max_length = 60
X_train = train.drop(['label'], axis = 1)
 y_train = train['label']
X_test = test
X_train = X_train / 255.0
X_test = X_test / 255.0<categorify>
tokenizer=Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(train_input_rsw)
 word_index = tokenizer.word_index
n_words=min(max_features,len(word_index))
embedding_matrix = np.zeros(( n_words+1, 300))
for word, i in word_index.items() :
if i >= max_features:
continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
X_train=X_train.values
X_test=X_test.values
test=test.values
X_train=X_train.reshape(X_train.shape[0], 28, 28, 1)
 X_test=X_test.reshape(X_test.shape[0], 28, 28, 1)
 test=test.reshape(test.shape[0] , 28 , 28 , 1 )<import_modules>
sequences = tokenizer.texts_to_sequences(train_input_rsw)
 train_input_padded = pad_sequences(sequences, maxlen=max_length, padding='post', truncating='post')
 print(train_input_padded.shape)
 sequences = tokenizer.texts_to_sequences(test_input_rsw)
 test_input_padded = pad_sequences(sequences, maxlen=max_length, padding='post', truncating='post')
 print(test_input_padded.shape )
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping<define_variables>
train_text, cv_text, train_target, cv_target = train_test_split(train_input_padded, train_label, test_size = 0.1, random_state=2 )
input_shape = X_train[0].shape<choose_model_class>
from keras.models import Sequential
from keras.layers import Embedding,Bidirectional,LSTM,Dropout,Conv1D,MaxPooling1D,Dense
early_stop = EarlyStopping(monitor = 'val_loss', mode = 'min', verbose = 2, patience = 0 )<choose_model_class>
lstm=Sequential()
lstm.add(Embedding(n_words+1,300,input_length=max_length,weights=[embedding_matrix], trainable=False))
lstm.add(Bidirectional(LSTM(256,return_sequences=True)))
 lstm.add(Dropout(0.2))
lstm.add(Conv1D(100,5,activation='relu'))
lstm.add(MaxPooling1D(pool_size=4))
lstm.add(LSTM(128))
lstm.add(Dropout(0.4))
lstm.add(Dense(1,activation='sigmoid'))
lstm.summary()
lstm.compile(loss='binary_crossentropy',optimizer='adam', metrics=['acc'])
model1 = Sequential()
model1.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'valid', activation = 'relu', input_shape = input_shape))
model1.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model1.add(MaxPool2D(pool_size=(2,2)))
 model1.add(Dropout(0.25))
model1.add(Conv2D(filters = 96, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model1.add(Conv2D(filters = 128, kernel_size =(5,5), padding = 'valid', activation = 'relu'))
model1.add(MaxPool2D(pool_size=(3,3)))
 model1.add(Dropout(0.33))
model1.add(Flatten())
 model1.add(Dense(128, activation='relu'))
model1.add(Dropout(0.50))
model1.add(Dense(10, activation='softmax')) 
<choose_model_class>
del embeddings_index
gc.collect()
model1.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] )<train_model>
history=lstm.fit(np.array(train_text), np.array(train_target), epochs = 5, validation_data=(np.array(cv_text),np.array(cv_target)) , batch_size=1024,verbose=1 )
model1.fit(X_train, y_train, epochs = 100, batch_size = 64, verbose = 1, callbacks = [early_stop], shuffle = True )<predict_on_test>
cv_predictions = lstm.predict(cv_text, batch_size=512)
 thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 result = f1_score(cv_target,(cv_predictions>thresh ).astype(int))
thresholds.append([thresh, result])
 print("F1 score at threshold {} is {}".format(thresh, result))
thresholds.sort(key=lambda x: x[1], reverse=True)
 print("Best value {}".format(thresholds[0]))
best_thresh = thresholds[0]
predictions1 = model1.predict(X_test )<choose_model_class>
predictions = lstm.predict(cv_text)
 predictions = np.around(predictions ).astype(int)
 df = pd.DataFrame({'pred': predictions.flatten() , 'actual': cv_target})
 df.head()
pd.crosstab(df['pred'], df['actual'], margins=True )
model2 = Sequential()
model2.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'valid', activation = 'relu', input_shape = input_shape))
model2.add(MaxPool2D(pool_size=(2,2)))
 model2.add(Dropout(0.25))
model2.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model2.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model2.add(MaxPool2D(pool_size=(2,2)))
 model2.add(Dropout(0.33))
model2.add(Conv2D(filters = 128, kernel_size =(3,3), padding = 'valid', activation = 'relu'))
model2.add(MaxPool2D(pool_size=(2,2)))
 model2.add(Dropout(0.4))
model2.add(Flatten())
 model2.add(Dense(1024, activation='relu'))
model2.add(Dropout(0.50))
model2.add(Dense(10, activation='softmax'))<choose_model_class>
predictions = lstm.predict(test_input_padded )
model2.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] )<train_model>
predictions1 =(predictions>best_thresh ).astype(int )
<predict_on_test><EOS> 
predictions =(predictions>best_thresh ).astype(int)
 submission = pd.DataFrame({'qid': test_data.qid, 'prediction': predictions1[:,0]})
 submission.to_csv('submission.csv', index=False )