Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<feature_engineering>
import numpy as np
import pandas as pd
import time
from datetime import datetime
from sklearn.model_selection import train_test_split
from google.cloud import storage
from google.cloud import automl_v1beta1 as automl
from google.api_core.gapic_v1.client_info import ClientInfo
from automlwrapper import AutoMLWrapper
if not TRAINING:
f = np.median
models = models[-2:]
env = janestreet.make_env()
th = 0.5
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_mean
pred = np.mean([model(x_tt, training = False ).numpy() for model in models],axis=0)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<import_modules>
PROJECT_ID = 'cloudml-demo'
bucket_name = 'cloudml-demo-lcm'
region = 'us-central1'
dataset_display_name = 'kaggle_tweets'
model_display_name = 'kaggle_starter_model1'
storage_client = storage.Client(project=PROJECT_ID)
 client = automl.AutoMlClient(client_info=ClientInfo())
 print(f'Starting AutoML notebook at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}' )
LabelEncoder,
StandardScaler,
PolynomialFeatures)
 ShuffleSplit,
GridSearchCV,
cross_val_score)
 BaggingClassifier,
VotingClassifier,
RandomForestClassifier)
<load_from_csv>
nlp_train_df = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 nlp_test_df = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 def callback(operation_future):
result = operation_future.result()
train = pandas.read_csv('.. /input/train.csv')
 test = pandas.read_csv('.. /input/test.csv')
 features_numerical = [
'bone_length',
'rotting_flesh',
'hair_length',
'has_soul'
]
features_categorical = ['color']
target = 'type'
le_target = LabelEncoder().fit(train[target])
 labels = le_target.transform(train[target])
 classes = list(le_target.classes_)
 train = train.drop([target, 'id'], axis=1)
 test_ids = test.pop('id')
 train = train.drop(features_categorical, axis=1)
 test = test.drop(features_categorical, axis=1 )<choose_model_class>
nlp_train_df.loc[nlp_train_df['text'].str.contains('fire', na=False, case=False)]
classifiers = {
'blinky': LogisticRegression(
 C=1,
tol=1e-3,
multi_class= 'multinomial',
penalty='l2',
solver='lbfgs' 
),
'pinky': RandomForestClassifier(
 n_estimators=250,
criterion='entropy',
max_depth=5,
min_samples_leaf=8,
min_samples_split=3 
),
'inky': GaussianProcessClassifier(
 kernel=1.0 * RBF(
 length_scale=1.0,
length_scale_bounds=(1e-1, 10.0)
),
warm_start=True 
),
'clyde': XGBClassifier(
 n_estimators=250,
objective="multi:softprob",
max_depth=6,
learning_rate=0.1,
gamma=0,
nthread=6 
),
}<train_model>
nlp_train_df.loc[nlp_train_df['text'].str.contains('fire', na=False, case=False)].target.value_counts()
clf_acc = []
ss = ShuffleSplit(
 n_splits=10,
test_size=0.2,
random_state=0 
)
 estimators = dict()
print('
Results:')
 for i,(train_index, val_index)in enumerate(ss.split(train, labels), start=1):
X_train, X_val = train.as_matrix() [train_index], train.as_matrix() [val_index]
y_train, y_val = labels[train_index], labels[val_index]
for name, clf in classifiers.items() :
pipeline = Pipeline([ 
('features', combined_features), 
(name, clf),
])
 estimator = pipeline.fit(X_train, y_train)
 estimators['%s_%s' %(name, i)] = estimator
train_predictions = estimator.predict(X_val)
 acc = metrics.accuracy_score(y_val, train_predictions)
 print('acc: {:.2%} | [split {}] {}'.format(acc, i, name))
clf_acc.append([name, i, acc])
 clf_acc = pandas.DataFrame(clf_acc, columns=['classifier', 'split', 'acc'] )<find_best_params>
def upload_blob(bucket_name, source_file_name, destination_blob_name):

 bucket = storage_client.get_bucket(bucket_name)
 blob = bucket.blob(destination_blob_name)
 blob.upload_from_filename(source_file_name)
 print('File {} uploaded to {}'.format(
 source_file_name,
'gs://' + bucket_name + '/' + destination_blob_name))
def download_to_kaggle(bucket_name,destination_directory,file_name,prefix=None):

 os.makedirs(destination_directory, exist_ok = True)
 full_file_path = os.path.join(destination_directory, file_name)
 blobs = storage_client.list_blobs(bucket_name,prefix=prefix)
 for blob in blobs:
blob.download_to_filename(full_file_path )
clf_cv_acc = clf_acc[['classifier', 'acc']] \ 
.groupby('classifier')\ 
.mean() \ 
.sort_values(by='acc', ascending=False
).reset_index()
print('
Mean classifier accuracy:
%r' %(clf_cv_acc.head(10)))
 top_estimators_acc = clf_cv_acc.head(3 ).classifier.values
vc_estimators = {k: classifiers[k] for k in top_estimators_acc}<randomize_order>
bucket = storage.Bucket(storage_client, name=bucket_name)
 if not bucket.exists() :
bucket.create(location=region )
ss = ShuffleSplit(
 n_splits=3,
test_size=0.1,
random_state=0 
)
 max_w = 3
weight_scores = []
w = range(1, max_w + 1)
 for weights in itertools.product(w, w, w):
if(len(set(weights)) != 1)or(int(sum(weights)) == 3):
eclf = VotingClassifier(
 estimators=vc_estimators.items() ,
voting='soft',
weights=weights 
)
 for i,(train_index, val_index)in enumerate(ss.split(train, labels), start=1):
X_train, X_val = train.as_matrix() [train_index], train.as_matrix() [val_index]
y_train, y_val = labels[train_index], labels[val_index]
estimator = eclf.fit(X_train, y_train)
 acc = metrics.accuracy_score(y_val, estimator.predict(X_val))
weight_scores.append([weights, i, acc])
 print('%s: [split %s] %s' %(weights, i, acc))
cols = ['weights', 'splits', 'acc']
df = pandas.DataFrame(weight_scores, columns=cols)
 df = df[['weights', 'acc']] \ 
.groupby('weights')\ 
.mean() \ 
.sort_values(by='acc', ascending=False
).reset_index()
print('
%r' % df.head())
 weights, _ = df.as_matrix() [0]<save_to_csv>
nlp_train_df[['text','target']].to_csv('train.csv', index=False, header=False )
eclf = VotingClassifier(
 estimators=vc_estimators.items() ,
voting='soft',
weights=weights 
)
 eclf.fit(X_train, y_train)
 test_predictions = eclf.predict_proba(test.as_matrix())
 test_predictions = [le_target.classes_[numpy.argmax(i)] for i in test_predictions]
submission = pandas.DataFrame(test_predictions, columns=[target])
 submission.insert(0, 'id', test_ids)
 submission.to_csv('cherries.csv', index=False )<import_modules>
training_gcs_path = 'uploads/kaggle_getstarted/full_train.csv'
upload_blob(bucket_name, 'train.csv', training_gcs_path )
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsOneClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
import seaborn as sns<load_from_csv>
amw = AutoMLWrapper(client=client,
project_id=PROJECT_ID,
bucket_name=bucket_name,
region='us-central1',
dataset_display_name=dataset_display_name,
model_display_name=model_display_name)
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
print(f'Getting dataset ready at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}')
 if not amw.get_dataset_by_display_name(dataset_display_name):
print('dataset not found')
 amw.create_dataset()
amw.import_gcs_data(training_gcs_path)
 amw.dataset
print(f'Dataset ready at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}')
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<drop_column>
print(f'Getting model trained at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}')
 if not amw.get_model_by_display_name() :
print(f'Training model at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}')
 amw.train_model()
print(f'Model trained.Ensuring model is deployed at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}')
 amw.deploy_model()
amw.model
print(f'Model trained and deployed at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}')
types = train["type"]
indexes_test = test["id"]
colors_train = train.color
colors_test = test.color
ids = test.id
train.drop(["type","id", 'color'], axis=1, inplace=True)
 test.drop(["id", 'color'], axis=1, inplace=True )<feature_engineering>
amw.model_full_path
train_eng = train.copy(deep=True)
 train_eng['hairXsoul'] = train_eng.hair_length * train_eng.has_soul
train_eng['hairXbone'] = train_eng.hair_length * train_eng.bone_length
train_eng['boneXsoul'] = train_eng.bone_length * train_eng.has_soul
test_eng = test.copy(deep=True)
 test_eng['hairXsoul'] = test_eng.hair_length * test_eng.has_soul
test_eng['hairXbone'] = test_eng.hair_length * test_eng.bone_length
test_eng['boneXsoul'] = test_eng.bone_length * test_eng.has_soul 
<categorify>
print(f'Begin getting predictions at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}')
 prediction_client = automl.PredictionServiceClient()
amw.set_prediction_client(prediction_client)
 predictions_df = amw.get_predictions(nlp_test_df,
input_col_name='text',
limit=None,
threshold=0.5,
verbose=False)
 print(f'Finished getting predictions at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}' )
dummies = pd.get_dummies(types)
 y = np.argmax(dummies.as_matrix() , 1)
 labels = pd.DataFrame(y, columns=['monster'] )<choose_model_class>
submission_df = pd.concat([nlp_test_df['id'], predictions_df['class']], axis=1)
 submission_df.head()
models = []
scores = []
predictions = []
estimators = [LogisticRegression(penalty='l2',
tol=0.01,
C=1000.0,
solver='liblinear',
max_iter=10000,
multi_class='ovr'),
LogisticRegression() ,
RandomForestClassifier(n_estimators=500),
MLPClassifier(max_iter=1000,
solver='lbfgs',
hidden_layer_sizes=[16])
 ]
for estimator in estimators:
print(estimator)
 for color in colors_train.unique() :
X = train_eng[colors_train!=color].values
y = labels[colors_train!=color]
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3)
 clf = OneVsOneClassifier(estimator)
 clf.fit(X_train, y_train.values.ravel())
 score = accuracy_score(y_test,
clf.predict(X_test))
scores.append(score)
 print(score)
 predictions.append(clf.predict(test_eng))
print('
')
 print("{m} +/- {sd}".format(m=np.mean(scores), sd=np.std(scores)*2))<prepare_output>
submission_df = submission_df.rename(columns={'class':'target'})
 submission_df.head()
<count_unique_values><EOS> 
submission_df.to_csv("submission.csv", index=False, header=True )