Kaggle competition: Titanic - Machine Learning from Disaster
gbr = GradientBoostingRegressor(n_estimators=4000, learning_rate=0.01, max_depth=3,
subsample=0.9, max_features='sqrt' , random_state=21)
 gbr.fit(X_train, y_train)
 rmsle(gbr, X_test, y_test )<train_model>
titanic_train = pd.read_csv('.. /input/train.csv')
 titanic_test = pd.read_csv('.. /input/test.csv' )
xgboost = XGBRegressor(n_estimators=4000, learning_rate=0.009, max_depth=4,
min_child_weight=2., colsample_bytree=0.8 , subsample=0.69,
gamma=0.01, reg_lambda=1.1, reg_alpha=0.001, random_state=21)
 xgboost.fit(X_train, y_train)
 rmsle(xgboost, X_test, y_test )<train_model>
print("Age broken down by P-class")
 titanic_train.groupby('Pclass' ).mean() [['Age']]
lgbm = LGBMRegressor(n_estimators=1000, learning_rate=0.04, num_leaves=8, max_depth=3,
colsample_bytree=0.1, objective='regression',
random_state=21)
 lgbm.fit(X_train, y_train)
 rmsle(lgbm, X_test, y_test )<define_variables>
titanic_train.loc[titanic_train.Age.isnull() , 'Age'] = titanic_train.groupby('Pclass')['Age'].transform('mean')
 titanic_test.loc[titanic_test.Age.isnull() , 'Age'] = titanic_test.groupby('Pclass')['Age'].transform('mean' )
estimators = [ridge, lasso, elastic, svr, gbr, lgbm, xgboost]
names = ["RidgeCV", "LassoCV", "ElasticNetCV", "SVR", "GBoost", "LGBM", "XGBoost"]<train_model>
titanic_train = titanic_train.drop('Cabin', axis=1)
 titanic_test = titanic_test.drop('Cabin', axis=1 )
dtr = DecisionTreeRegressor(max_depth=10, max_features='sqrt', min_samples_leaf=2)
 ada = AdaBoostRegressor(xgboost, learning_rate=0.1, n_estimators=10, loss='exponential', random_state=11)
 ada.fit(X_train, y_train)
 rmsle(ada, X_test, y_test )<save_to_csv>
titanic_train['Embarked'].fillna(titanic_train['Embarked'].mode() [0], inplace=True)
 titanic_test['Fare'].fillna(titanic_test['Fare'].median() , inplace = True )
features_pure_log.to_csv("FeaturesLog.csv")
 target_log.to_csv("TargetLog.csv" )<train_model>
print('Training Data Null Values')
 print(titanic_train.isnull().sum())
 print("-" * 30)
 print('Test Data Null Values')
 print(titanic_test.isnull().sum() )
ridge.fit(train1, target)
 lasso.fit(train1, target)
 elastic.fit(train1, target)
 svr.fit(train1, target)
 gbr.fit(train1, target)
 lgbm.fit(train1, target)
 xgboost.fit(train1, target)
 ada.fit(train1, target )<split>
titanic_train.groupby('Survived' ).mean() [['Fare']]
test = features_pure_log.iloc[1460:]
test<predict_on_test>
titanic_train.loc[titanic_train['Fare'] > 500, :]
def blend_models(X):
return(0.1 * ridge.predict(X)) +(0.1 * lasso.predict(X)) +(0.1 * elastic.predict(X)) +\ 
(0.05 * svr.predict(X)) +(0.15 * gbr.predict(X)) +(0.1 * lgbm.predict(X)) +\ 
(0.3 * xgboost.predict(X)) +(0.1 * ada.predict(X))
prediction = np.expm1(blend_models(test))
prediction<save_to_csv>
titanic_train.groupby(['Embarked'] ).count()
submit = pd.DataFrame({"Id": np.arange(1461, 2920), "SalePrice": prediction})
 submit.to_csv("submission12a.csv", index=False )<import_modules>
traindf = titanic_train.copy()
testdf = titanic_test.copy()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import skew
import lightgbm as lgb<load_from_csv>
all_data = [traindf, testdf]
submission = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv')
 submission.head()<load_from_csv>
for dat in all_data:
dat.drop(['Name', 'Ticket'], axis=1, inplace=True )
train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 train.head()<load_from_csv>
for dat in all_data:
dat['Fam_Size'] = dat['SibSp'] + dat['Parch']
test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 test.head()<train_model>
traindf = pd.get_dummies(traindf)
 traindf.head()
print('=========== train infomation ===========')
 train.info()
print('
=========== test infomation ===========')
 test.info()<concatenate>
testdf = pd.get_dummies(testdf)
 testdf.head()
data = pd.concat([train, test])
 data.shape<categorify>
from sklearn.metrics import confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
cat_data = pd.get_dummies(data.loc[:, cat_cols], drop_first=True)
 cat_data.head()<drop_column>
X = traindf.drop(columns=['PassengerId', 'Survived'], axis=1)
 y = traindf['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30 )
num_data = data.loc[:,data.dtypes != 'object'].drop('Id', axis=1)
 num_data.head()<concatenate>
results = pd.DataFrame(columns=['Validation'], index=['Logistic Regression', 'Support Vector Machine', 'KNN', 'Random Forest'] )
optimized_data = pd.concat([data['Id'], cat_data, log_num_data], axis=1)
 optimized_data.head()<prepare_x_and_y>
def log_reg(X_train, X_test, y_train, y_test):
logmodel = LogisticRegression(C=.01)
 logmodel.fit(X_train, y_train)
 predictions = logmodel.predict(X_test)
 print(accuracy_score(y_test, predictions))
return accuracy_score(y_test, predictions )
train = optimized_data[:train.shape[0]]
test = optimized_data[train.shape[0]:].drop(['Id', 'SalePrice'], axis=1)
 X_train = train.drop(['Id', 'SalePrice'], axis=1)
 y_train = train['SalePrice']<train_model>
LR_preds = log_reg(X_train, X_test, y_train, y_test)
 results.loc['Logistic Regression', 'Validation'] = LR_preds
lgb_train = lgb.Dataset(X_train, y_train)
 params = {
'task' : 'train',
'boosting_type' : 'gbdt',
'objective' : 'regression',
'metric' : {'l2'},
'num_leaves' : 40,
'learning_rate' : 0.1,
'feature_fraction' : 0.9,
'bagging_fraction' : 0.8,
'bagging_freq': 5,
'verbose' : 0
}
gbm = lgb.train(params, lgb_train)
 pred = gbm.predict(test )<save_to_csv>
def svm(X_train, X_test, y_train, y_test):
c_vals = list(range(1, 100))
accuracy = [0 for i in range(99)]
for i, c in enumerate(c_vals):
svc_model = SVC(C=c)
 svc_model.fit(X_train, y_train)
 predictions = svc_model.predict(X_test)
 accuracy[i] = accuracy_score(y_test, predictions)
 print("Best C Value:", c_vals[accuracy.index(max(accuracy)) ])
 print(accuracy)
 print("Prediction Accuracy: ", max(accuracy))
return max(accuracy)
pred = np.expm1(pred)
 results = pd.Series(pred, name='SalePrice')
 submission = pd.concat([submission['Id'], results], axis=1)
 submission.to_csv('submission.csv', index=False)
 submission.head()<set_options>
svm_preds = svm(X_train, X_test, y_train, y_test)
 results.loc['Support Vector Machine', 'Validation'] = svm_preds
results.head()
%matplotlib inline
plt.style.use('fivethirtyeight')
 warnings.simplefilter('ignore')
 print('Setup complete' )<load_from_csv>
def knn(X_train, X_test, y_train, y_test):
scaler = StandardScaler()
scaler.fit(X_train)
 X_train = scaler.transform(X_train)
 X_test = scaler.transform(X_test)
 ks = [i + 1 for i in range(20)]
accuracy = [0 for i in range(20)]
for i, k in enumerate(ks):
knn = KNeighborsClassifier(n_neighbors = k)
 knn.fit(X_train, y_train)
 predictions = knn.predict(X_test)
 accuracy[i] = accuracy_score(y_test, predictions)
 print(ks)
 print(accuracy)
 print("Best k Value:", ks[accuracy.index(max(accuracy)) ])
 print("Prediction Accuracy: ", max(accuracy))
return max(accuracy )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 train.head()<create_dataframe>
knn_preds = knn(X_train, X_test, y_train, y_test)
 results.loc['KNN', 'Validation'] = knn_preds
results.head()
num_features = [col for col in train.columns if train[col].dtype in ['int64', 'float64']]
num_features.remove('Id')
 num_features.remove('SalePrice')
 num_analysis = train[num_features].copy()
for col in num_features:
if num_analysis[col].isnull().sum() > 0:
num_analysis[col] = SimpleImputer(strategy='median' ).fit_transform(num_analysis[col].values.reshape(-1,1))
clf = ExtraTreesRegressor(random_state=42)
 h = clf.fit(num_analysis, train.SalePrice)
 feature_imp = pd.DataFrame(sorted(zip(clf.feature_importances_,num_features)) , columns=['Value','Feature'])
 plt.figure(figsize=(16,10))
sns.barplot(x='Value', y='Feature', data=feature_imp.sort_values(by='Value', ascending=False))
plt.title('Most important numerical features with ExtraTreesRegressor')
 del clf, h;<categorify>
from sklearn.decomposition import PCA
<categorify><EOS> 
scaler = StandardScaler()
scaler.fit(X)
 test_feats = testdf.drop('PassengerId', axis=1)
 X = scaler.transform(X)
 test_feats = scaler.transform(test_feats)
 pca = PCA(n_components = 4)
 pca.fit(X)
 x_train_pca = pca.transform(X)
 x_test_pca = pca.transform(test_feats)
 svc_model = SVC(C = 1)
 svc_model.fit(x_train_pca, y)
 svm_predictions = svc_model.predict(x_test_pca)
 output = pd.DataFrame({ 'PassengerId' : testdf['PassengerId'], 'Survived': svm_predictions })
 output.to_csv('titanic-predictions-svm-pca.csv', index=False)
 output