Kaggle competition: Mercedes-Benz Greener Manufacturing
<SOS> metric: R2Score Kaggle data source: mercedes-benz-greener-manufacturing<drop_column>
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.svm import SVR
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from scipy.stats import uniform, randint
df1 = df1.drop('PassengerID', 'Cabin', 'Name', 'Ticket', 'Title' )<count_missing_values>
df_train = pd.read_csv('/kaggle/input/mercedes-benz-greener-manufacturing/train.csv.zip')
 df_test = pd.read_csv('/kaggle/input/mercedes-benz-greener-manufacturing/test.csv.zip')
 submission = pd.read_csv('/kaggle/input/mercedes-benz-greener-manufacturing/sample_submission.csv.zip' )
for col in df1.columns:
print(col.ljust(20), df1.filter(df1[col].isNull() ).count() )<import_modules>
df_test = df_test.drop(columns=columns)
 df_test
RandomForestClassifier, GBTClassifier 
<categorify>
for column in df_train:
if df_train[column].dtypes == 'object':
X = [df_train[column], df_test[column]]
X = np.hstack(X)
 enc = LabelEncoder()
enc.fit(X)
 df_train[column] = enc.transform(df_train[column])
 df_test[column] = enc.transform(df_test[column])
 df_train
stringIndex = StringIndexer(inputCols=['Sex', 'Embarked'],
outputCols=['SexNum', 'EmbNum'])
 stringIndex_model = stringIndex.fit(df1)
 df1_ = stringIndex_model.transform(df1 ).drop('Sex', 'Embarked')
 df1_.show(4 )<categorify>
X_train = df_train.loc[:, 'X0':]
y_train = df_train.loc[:, 'y']
vec_asmbl = VectorAssembler(inputCols=df1_.columns[1:],
outputCol='features')
 df1_ = vec_asmbl.transform(df1_ ).select('features', 'Survived')
 df1_.show(4, truncate=False )<randomize_order>
param_random = {
'learning_rate': uniform(0.01, 0.1),
'n_estimators': randint(100, 300),
'max_depth': randint(3, 5)
 }
model_gbr = RandomizedSearchCV(GradientBoostingRegressor() , param_random, n_jobs=-1)
 model_gbr.fit(X_train, y_train)
 model_gbr.best_score_
train_df, valid_df = df1_.randomSplit([0.7, 0.3] )<choose_model_class>
param_random = {
'C': uniform(0.1, 3.0),
}
model_svr = RandomizedSearchCV(SVR() , param_random, n_jobs=-1)
 model_svr.fit(X_train, y_train)
 model_svr.best_score_
evaluator = MulticlassClassificationEvaluator(labelCol='Survived',
metricName='accuracy' )<train_model>
param_random = {
'alpha': uniform(0.01, 100),
'l1_ratio': uniform(0.25, 0.75)
 }
model_esn = RandomizedSearchCV(ElasticNet() , param_random, n_jobs=-1, n_iter=100)
 model_esn.fit(X_train, y_train)
 model_esn.best_score_
ridge = LogisticRegression(labelCol='Survived',
maxIter=100,
elasticNetParam=0,
regParam=0.03)
 model = ridge.fit(train_df)
 pred = model.transform(valid_df)
 evaluator.evaluate(pred )<train_model>
X_test = df_test.loc[:, 'X0':]
y_test = 0.5 * model_gbr.predict(X_test)+ 0.1 * model_svr.predict(X_test)+ 0.4 * model_esn.predict(X_test)
 submission['y'] = y_test
submission.to_csv('submssion.csv', index=False )