Kaggle competition: Titanic - Machine Learning from Disaster
<load_from_csv><EOS> 


plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def readCsv(file):
with open(file, encoding='utf8', mode='r')as f:
return pd.read_csv(f)
 def seeNorm(series):
sns.distplot(series)
 print('偏度系数', series.skew())
 print('峰度系数', series.kurt())
 def drawHeatMap(dataset, target, k=-1):
corrmat = dataset.corr()
cols = dataset.columns
if -1 != k:
cols = corrmat.nlargest(k, target)[target].index
cm = np.corrcoef(dataset[cols].values.T)
 sns.set(font_scale=1.25)
 hm = sns.heatmap(cm, cbar=True, annot = True, square=True, fmt='.2f', annot_kws={'size': 10}, yticklabels=cols.values,
xticklabels=cols.values, cmap='coolwarm')
 plt.show()
return cols
def drawPairplot(dataset, cols):
sns.set()
sns.pairplot(dataset[cols], size=2.5)
 plt.show()
def seeMissing(dataset):
total = dataset.isnull().sum()
percent =(total/dataset.isnull().count())*100
total = total.drop(total[total == 0].index)
 percent = percent.drop(percent[percent == 0].index)
 total = total.sort_values(ascending=False)
 percent = percent.sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Missing Ratio'])
 print(missing_data.head(20))
f, ax = plt.subplots(figsize=(15,12))
plt.xticks(rotation='90')
 if percent.count() != 0:
sns.barplot(x=percent.index, y =percent)
 plt.xlabel('Features', fontsize=15)
 plt.ylabel('Percent of missing values', fontsize=15)
 plt.title('Percent missing data by feature', fontsize=15)
 def drawScatter(dataset, colx, coly):

 fig, ax = plt.subplots()
ax.scatter(x=dataset[colx], y = dataset[coly])
 plt.xlabel(colx, fontsize=13)
 plt.ylabel(coly, fontsize=13)
 plt.show()
def drawDist(dataset, col, title=''):
print('skewness', dataset[col].skew())
 print('kurt', dataset[col].kurt())
 sns.distplot(dataset[col], fit = norm)
(mu, sigma)= norm.fit(dataset[col])
 print('mu = {:.2f} and sigma = {:.2f}'.format(mu, sigma))
plt.legend(['Normal dist.( $\mu=$ {:.2f} and $\sigma=$ {:.2f}'.format(mu, sigma)], loc='best')
 plt.ylabel('Frequency')
 plt.title('{} distribution'.format(title))
plt.show()
def drawQQ(dataset, col):
fig = plt.figure()
res = stats.probplot(dataset[col], plot=plt)
 plt.show()
def drawBox(dataset, xcol, ycol):
plt.figure(figsize=(18, 8))
sns.boxplot(x=dataset[xcol], y=dataset[ycol])
 def delCol(dataset, col):
dataset.drop(columns=col, axis=1, inplace=True)
 class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis=1)
 class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis = 1)
 class DataMining() :
def __init__(self):
dirname = '/kaggle/input/titanic'
self.train = readCsv('{}/train.csv'.format(dirname))
self.test = readCsv('{}/test.csv'.format(dirname))
self.idName = 'PassengerId'
self.targetName = 'Survived'
self.trainX = self.train.drop([self.idName, self.targetName], axis = 1)
 self.trainY = self.train[self.targetName]
self.testId = self.test[self.idName]
self.testX = self.test.drop(self.idName, axis = 1)
 def preprocessing(self):
self.fillMissing()
self.mergeDataset()
self.dataset = self.dataset.fillna(np.nan)
 self.dataset.drop(['Name'], axis=1, inplace=True)
 print(self.dataset.head())
 self.labelEncoding(['Sex', 'Ticket', 'Cabin', 'Embarked'])
 self.splitDataset()
def mergeDataset(self):
self.train_len = self.trainX.shape[0]
self.dataset = pd.concat([self.trainX, self.testX], axis=0 ).reset_index(drop=True)
 def splitDataset(self):
self.trainX = self.dataset[:self.train_len]
self.testX = self.dataset[self.train_len :]
self.trainY = self.trainY.astype(int)
 def fillMedian(self, col, kind='train'):
if kind != 'train':
self.testX[col] = self.testX[col].fillna(self.testX[col].median())
 else:
self.trainX[col] = self.trainX[col].fillna(self.trainX[col].median())
 def fillMode(self, col, kind='train'):
if kind != 'train':
self.testX[col] = self.testX[col].fillna(self.testX[col].mode() [0])
 else:
self.trainX[col] = self.trainX[col].fillna(self.trainX[col].mode() [0])
 def fillMissing(self):
self.fillMedian('Age')
 self.fillMode('Cabin')
 self.fillMode('Embarked')
 self.fillMode('Cabin', 'test')
 self.fillMode('Fare', 'test')
 self.fillMode('Age', 'test')
 def labelEncoding(self, col):
enc = LabelEncoder()
self.dataset[col] = self.dataset[col].apply(enc.fit_transform)
 def clearOutlier(self):
feats = self.train.dtypes[self.train.dtypes != 'object'].index
print(feats)
 for f in feats:
Q1 = self.train[f].quantile (.25)
 Q3 = self.train[f].quantile (.75)
 IQR = Q3 - Q1
threshold = 1.5 * IQR
self.train[(self.train[f] < Q1 - threshold)|(self.train[f] > Q3 + threshold)] = np.nan
def exploring(self):
self.preprocessing()
print('
train missing')
 print(self.trainX.isnull().sum())
 print('
test missing')
 print(self.testX.isnull().sum())
 print(self.dataset.head())
 tmp = pd.concat([self.trainX, self.trainY], axis=1 ).reset_index(drop=True)
 drawHeatMap(tmp, self.targetName, -1)
 def score(self, model, name=''):
n_folds = 9
kf = KFold(n_splits= n_folds, random_state=1, shuffle=True)
 score = cross_val_score(model, self.trainX.values, self.trainY, cv=kf)
 print("{}score: {:.2f}%".format(name, score.mean() *100.0))
def predict(self):
self.ensemble()
self.wrapAns()
def ensemble(self):
all_ans = []
for i in range(len(self.models)) :
self.models[i].fit(self.trainX, self.trainY)
 all_ans.append(self.models[i].predict(self.testX))
self.testY = all_ans[1]
def wrapAns(self):
submission = pd.DataFrame()
submission[self.idName] = self.testId
submission[self.targetName] = self.testY.astype('int')
 save_dir = '/kaggle/working'
submission.to_csv('{}/submission.csv'.format(save_dir), index=False)
 print('the result has been written into the file "submission.csv"')
 def modeling(self):
lr = LogisticRegression(random_state=1)
 rf = RandomForestClassifier(random_state=1, n_estimators=200, criterion = 'entropy', min_samples_leaf=1, min_samples_split=4)
 gboost = GradientBoostingClassifier(learning_rate=0.2,max_depth = 4,n_estimators =100)
 self.models = [
rf,
gboost
]
def evaluate(self):
for model in self.models:
self.score(model)
 dm = DataMining()
dm.preprocessing()
dm.modeling()
dm.predict()