Kaggle competition: Mercedes-Benz Greener Manufacturing
<SOS> metric: R2Score Kaggle data source: mercedes-benz-greener-manufacturing<feature_engineering>
%matplotlib inline
InteractiveShell.ast_node_interactivity='all'
data = [train,test]
for dataset in data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1<feature_engineering>
train = pd.read_csv('.. /input/mercedes-benz-greener-manufacturing/train.csv', index_col='ID')
 test = pd.read_csv('.. /input/mercedes-benz-greener-manufacturing/test.csv', index_col='ID')
 sub = pd.read_csv('.. /input/mercedes-benz-greener-manufacturing/sample_submission.csv' )
data = [train,test]
for dataset in data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1<drop_column>
y = train['y']
train.drop('y', axis=1, inplace=True )
train = train.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test = test.drop(['Parch', 'SibSp', 'FamilySize'], axis=1 )<drop_column>
def missing_data(data):
total = data.isnull().sum()
percent =(data.isnull().sum() /data.isnull().count() *100)
 tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 types = []
for col in data.columns:
dtype = str(data[col].dtype)
 types.append(dtype)
 tt['Types'] = types
return(np.transpose(tt))
train_y=train['Survived']
train_ft=train.drop('Survived',axis=1)
 kf = StratifiedKFold(n_splits=10)
 print(train_ft.head())
 print(train_y.head() )<train_on_grid>
def get_num_cat_cols(df):

 cat_vars = [col for col in df.columns if df[col].dtype == 'object']
num_vars = [col for col in df.columns if df[col].dtype != 'object']
return cat_vars, num_vars
svc = SVC(C = 45, gamma = 0.03)
 svc.fit(train_ft, train_y)
 acc_SVM = cross_val_score(svc,train_ft,train_y,cv=kf)
 print(acc_SVM.mean() )<predict_on_test>
cat_vars, num_vars = get_num_cat_cols(train)
 print('Categorical variables: ', cat_vars)
 print('Numeric variables: ', num_vars )
predictions = svc.predict(test)
 print(predictions )<save_to_csv>
train[num_vars].head()
train['1_count'] = train[num_vars].sum(axis=1)
 test['1_count'] = test[num_vars].sum(axis=1)
 train.head(3 )
submission = pd.DataFrame({ 'PassengerId': test2['PassengerId'],
'Survived': predictions })
 submission.to_csv('submission.csv', index = False )<import_modules>
train['1_count_percent'] =(train[num_vars].sum(axis=1)* 100)/ len(num_vars)
 test['1_count_percent'] =(test[num_vars].sum(axis=1)* 100)/ len(num_vars)
 train.head()
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
cols_with_less_data = [c for c in num_vars if train[c].nunique() <2]
train.drop(cols_with_less_data, axis=1, inplace=True)
 test.drop(cols_with_less_data, axis=1, inplace=True)
df = pd.read_csv('.. /input/titanic/train.csv')
 df.head()<filter>
gc.collect()
df.columns[df.isna().any() ]<data_type_conversions>
[print('There are {1} unique values in {0} column'.format(c, train[c].nunique())) for c in cat_vars];
df['Age'].fillna(df['Age'].mean() , inplace=True)
 df['Embarked'].fillna('S', inplace=True)
 df.set_index('PassengerId', inplace=True)
 df['Sex'] = df['Sex'].astype('category')
 df['Sex'] = df['Sex'].cat.codes
df['Embarked'] = df['Embarked'].astype('category')
 df['Embarked'] = df['Embarked'].cat.codes
df.head()<categorify>
cat_vars, num_vars = get_num_cat_cols(train )
def convertRanges(data):
_, bins = pd.qcut(data, 5, retbins=True)
 for i in range(len(data.values)) :
x = data.values[i]
if(x>=bins[0] and x<bins[1]):
x = 1
elif(x>=bins[1] and x<bins[2]):
x = 2
elif(x>=bins[2] and x<bins[3]):
x = 3
elif(x>=bins[3] and x<bins[4]):
x = 4
elif(x>=bins[4] and x<=bins[5]):
x = 5
data.values[i] = x
return data<feature_engineering>
def label_encode_columns(df):

 cat_cols, _ = get_num_cat_cols(df)
 le = LabelEncoder()
for c in cat_cols:
le.fit(df[c])
 df[c] = le.transform(df[c])
 return df
df['Age'] = convertRanges(df['Age'])
 df['Fare'] = convertRanges(df['Fare'])
 df.head()<prepare_x_and_y>
train = label_encode_columns(train)
 test = label_encode_columns(test)
 train[cat_vars].head()
test[cat_vars].head()
classifiers = [
KNeighborsClassifier(3),
SVC(kernel="linear", C=0.025),
SVC(gamma=2, C=1),
GaussianProcessClassifier(1.0 * RBF(1.0)) ,
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
MLPClassifier(alpha=1, max_iter=1000),
AdaBoostClassifier() ,
GaussianNB() ,
QuadraticDiscriminantAnalysis() ]
modelAccuracy = []
for model in classifiers:
kf = KFold(n_splits=10, shuffle=False)
 X = df.iloc[:,1:].values
y = df.iloc[:,0].values
scores = []
for train_index, test_index in kf.split(df.values):
X_train, X_test, y_train, y_test = X[train_index], X[test_index], y[train_index], y[test_index]
model.fit(X_train, y_train)
 scores.append(model.score(X_test, y_test))
modelAccuracy.append(np.mean(scores))
maxIndex = np.argmax(modelAccuracy)
 model = classifiers[maxIndex]
model.fit(X, y )<load_from_csv>
train_x, val_x, train_y, val_y = train_test_split(train, y, test_size=0.3 )
df = pd.read_csv('.. /input/titanic/test.csv')
 df.head()<filter>
important_features = ['X314','X315','X119','X118','X263','X136','X29','X279','X5','X232','X76','X54','X189','X47','X104','X8','1_count_percent','1_count','X2',
'X3','X275','X65','X26','X6','X127','X1','X267','X311','X283','X0','X77','X341','X287','X13','X241','X46','X162','X117','X82','X105']
<data_type_conversions><EOS> 
sub['y'] = reg.predict(test[important_features])
 sub.to_csv('quick_and_dirty_Random_forest.csv', index=False);