import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
train = pd.read_csv("./dataset/train.csv").fillna('')
train
test = pd.read_csv("./dataset/test.csv").fillna('')
test
ax = plt.subplot()
n0 = train[train.target == 0].shape[0]
n1 = train[train.target == 1].shape[0]
ax.pie([n0, n1], labels = ('Not Disaster Tweets: ' + str(n0), 'Disaster Tweets: ' + str(n1)))
ax.set_xlabel('Disaster Rate = ' + str(n1 / (n0 + n1)))
plt.show()
Categorize the field keyword.
from sklearn.preprocessing import LabelEncoder
le_keyword = LabelEncoder()
le_keyword.fit(np.concatenate((np.array(train.keyword), np.array(test.keyword))))
train.keyword = le_keyword.transform(train.keyword) / len(le_keyword.classes_)
test.keyword = le_keyword.transform(test.keyword) / len(le_keyword.classes_)
Categorize the field location.
from sklearn.preprocessing import LabelEncoder
le_location = LabelEncoder()
le_location.fit(np.concatenate((np.array(train.location), np.array(test.location))).tolist())
train.location = le_location.transform(train.location) / len(le_location.classes_)
test.location = le_location.transform(test.location) / len(le_location.classes_)
Now we extract links from the field text and try to figure out the pattern between the number of links and whether it is about disasters.
import re
def convert_links(data):
if not 'link_count' in data.columns:
for i in range(0, data.shape[0]):
text = data.loc[i, 'text']
urls = re.findall("https?:\/\/[a-zA-Z0-9\/\.]+", text)
for url in urls:
text = text.replace(url, '[link]')
data.loc[i, 'link_count'] = len(urls)
data.loc[i, 'text'] = text
convert_links(train)
convert_links(test)
fig, axes = plt.subplots(2)
for c in train.link_count.unique():
axes[0].bar(c, train[(train.link_count == c) & (train.target == 1)].shape[0] / train[(train.link_count == c)].shape[0])
for c in test.link_count.unique():
axes[1].bar(c, test[test.link_count == c].shape[0])
axes[1].set_xlabel('Link Count')
axes[0].set_ylabel('Disaster Rate (train)')
axes[1].set_ylabel('Number of Instances (test)')
plt.show()
import re
def convert_ats(data):
if not 'at_count' in data.columns:
for i in range(0, data.shape[0]):
text = data.loc[i, 'text']
ats = re.findall("\@[^\s:]+", text)
for at in ats:
text = text.replace(at, '@xxx')
data.loc[i, 'at_count'] = len(ats)
data.loc[i, 'text'] = text
convert_ats(train)
convert_ats(test)
fig, axes = plt.subplots(2)
for c in train.at_count.unique():
axes[0].bar(c, train[(train.at_count == c) & (train.target == 1)].shape[0] / train[(train.at_count == c)].shape[0])
for c in test.at_count.unique():
axes[1].bar(c, test[test.at_count == c].shape[0])
axes[1].set_xlabel('At Count')
axes[0].set_ylabel('Disaster Rate (train)')
axes[1].set_ylabel('Num of Instances (test)')
plt.show()
import re
def convert_tags(data):
if not 'tag_count' in data.columns:
for i in range(0, data.shape[0]):
text = data.loc[i, 'text']
tags = re.findall("\#[^\s:]+", text)
for tag in tags:
text = text.replace(tag, tag[1:])
data.loc[i, 'tag_count'] = len(tags)
data.loc[i, 'text'] = text
convert_tags(train)
convert_tags(test)
fig, axes = plt.subplots(2)
for c in train.tag_count.unique():
axes[0].bar(c, train[(train.tag_count == c) & (train.target == 1)].shape[0] / train[(train.tag_count == c)].shape[0])
for c in test.tag_count.unique():
axes[1].bar(c, test[(test.tag_count == c)].shape[0])
axes[1].set_xlabel('Tag Count')
axes[0].set_ylabel('Disaster Rate (train)')
axes[1].set_ylabel('Num of Instances (test)')
plt.show()
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
vectorizer.fit(train.text)
vectorizer.fit(test.text)
train_x = pd.concat([
train[['keyword', 'location', 'link_count', 'at_count', 'tag_count']],
pd.DataFrame(vectorizer.transform(train.text).toarray())], axis = 1, sort=False)
test_x = pd.concat([
test[['keyword', 'location', 'link_count', 'at_count', 'tag_count']],
pd.DataFrame(vectorizer.transform(test.text).toarray())], axis = 1, sort=False)
train_y = train.target
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import plot_roc_curve
x1, x2, y1, y2 = train_test_split(train_x, train_y)
rfc = RandomForestClassifier()
svm = SVC()
rfc.fit(x1, y1)
svm.fit(x1, y1)
ax = plt.subplot()
plot_roc_curve(rfc, x2, y2, ax = ax)
plot_roc_curve(svm, x2, y2, ax = ax)
plt.show()
svm = SVC()
svm.fit(train_x, train_y)
prediction = pd.DataFrame({ 'id': test.id, 'target': svm.predict(test_x) })
prediction
prediction.to_csv('./dataset/submission.csv', index = False)