Kaggle competition: Titanic - Machine Learning from Disaster
from sklearn.metrics import mean_squared_error<import_modules>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 df_combined = pd.concat([df_train, df_test], sort=False )
from sklearn.metrics import mean_squared_error<feature_engineering>
df_train[['Pclass','Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by="Survived", ascending=False )
df['preds'] = df['nota_mat'].mean()<import_modules>
df_train[['Sex','Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by="Survived", ascending=False )
from sklearn.ensemble import RandomForestRegressor<import_modules>
df_train[['SibSp','Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by="Survived", ascending=False )
from sklearn.ensemble import RandomForestRegressor<predict_on_test>
df_train[['Parch','Survived']].groupby(['Parch'], as_index=False ).maean().sort_values(by="Survived", ascending=False )
df_preds = rf.predict(valid[feats] )<compute_test_metric>
df_test['Title'] = df_test['Name'].str.extract('([A-Za-z]+)\.', expand=False )
mean_squared_error(valid['nota_mat'], df_preds)**(1/2 )<predict_on_test>
df_train['Title'] = df_train['Title'].replace(['Capt', 'Col', 'Don', 'Dr', 'Major', 'Jonkheer', 'Sir', 'Rev', 'Dona'], 'Rare')
 df_train['Title'] = df_train['Title'].replace(['Lady', 'Countess', 'Mme'], 'Mrs')
 df_train['Title'] = df_train['Title'].replace(['Ms', 'Mlle'], 'Miss' )
valid_preds=rf.predict(valid[feats] )<compute_test_metric>
df_test['Title'] = df_test['Title'].replace(['Capt', 'Col', 'Don', 'Dr', 'Major', 'Jonkheer', 'Sir', 'Rev', 'Dona'], 'Rare')
 df_test['Title'] = df_test['Title'].replace(['Lady', 'Countess', 'Mme'], 'Mrs')
 df_test['Title'] = df_test['Title'].replace(['Ms', 'Mlle'], 'Miss' )
mean_squared_error(valid['nota_mat'], valid_preds)**(1/2 )<set_options>
df_train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
ts.fillna(-1.0, inplace=True )<data_type_conversions>
df_train['FamSize'] = df_train['Parch'] + df_train['SibSp'] + 1
df_test['FamSize'] = df_test['Parch'] + df_test['SibSp'] + 1
ts['codigo_mun'] = ts['codigo_mun'].str.replace('ID_ID_','')
 ts['codigo_mun'] = ts['codigo_mun'].values.astype('int64' )<data_type_conversions>
df_train[['FamSize','Survived']].groupby(['FamSize'], as_index=False ).mean().sort_values(by="Survived", ascending=False )
for c in ts.columns:
if ts[c].dtype == 'object':
ts[c] = df[c].astype('category' ).cat.codes<set_options>
df_train['IsAlone'] = df_train["FamSize"].map(lambda s: 1 if s==1 else 0)
 df_test['IsAlone'] = df_test["FamSize"].map(lambda s: 1 if s==1 else 0 )
ts.fillna(-1.0, inplace=True )<predict_on_test>
df_train[['IsAlone','Survived']].groupby(['IsAlone'], as_index=False ).mean().sort_values(by="Survived", ascending=False )
ts['nota_mat']=rf.predict(ts[feats] )<save_to_csv>
df_train[['Embarked','Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by="Survived", ascending=False )
ts[['codigo_mun','nota_mat']].to_csv('trabalho.csv', index=False )<set_options>
df_train['Fare'] = df_train['Fare'].fillna(df_train['Fare'].median())
 df_test['Fare'] = df_test['Fare'].fillna(df_test['Fare'].median() )
warnings.simplefilter("ignore")
 np.random.seed(10)
 LEVEL = 'level_4a'<compute_test_metric>
df_train['CategoricalFare'] = pd.qcut(df_train['Fare'], 10)
class SigmoidNeuron:
def __init__(self):
self.w = None
self.b = None
def perceptron(self, x):
return np.dot(x, self.w.T)+ self.b
def sigmoid(self, x):
return 1.0/(1.0 + np.exp(-x))
def grad_w_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)* x
def grad_b_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)
 def grad_w_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred * x
elif y == 1:
return -1 *(1 - y_pred)* x
else:
raise ValueError("y should be 0 or 1")
 def grad_b_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred
elif y == 1:
return -1 *(1 - y_pred)
 else:
raise ValueError("y should be 0 or 1")
 def fit(self, X, Y, epochs=1, learning_rate=1, initialise=True, loss_fn="mse", display_loss=False):
if initialise:
self.w = X.mean(axis=0)
 self.b = 0
if display_loss:
loss = {}
for i in tqdm_notebook(range(epochs), total=epochs, unit="epoch"):
dw = 0
db = 0
for x, y in zip(X, Y):
if loss_fn == "mse":
dw += self.grad_w_mse(x, y)
 db += self.grad_b_mse(x, y)
 elif loss_fn == "ce":
dw += self.grad_w_ce(x, y)
 db += self.grad_b_ce(x, y)
 self.w -= learning_rate * dw
self.b -= learning_rate * db
if display_loss:
Y_pred = self.sigmoid(self.perceptron(X))
if loss_fn == "mse":
loss[i] = mean_squared_error(Y, Y_pred)
 elif loss_fn == "ce":
loss[i] = log_loss(Y, Y_pred)
 if display_loss:
plt.plot(loss.values())
 plt.xlabel('Epochs')
 if loss_fn == "mse":
plt.ylabel('Mean Squared Error')
 elif loss_fn == "ce":
plt.ylabel('Log Loss')
 plt.show()
min_key = min(loss, key=loss.get)
 print(min_key)
 print(loss.get(min_key))
def predict(self, X):
Y_pred = []
for x in X:
y_pred = self.sigmoid(self.perceptron(x))
Y_pred.append(y_pred)
 return np.array(Y_pred )<load_pretrained>
print(df_train[['CategoricalFare','Survived']].groupby(['CategoricalFare'], as_index=False ).mean().sort_values(by="Survived", ascending=False))
languages = ['en','ta', 'hi']
images_train = read_all(".. /input/"+LEVEL+"_train/"+LEVEL+"/"+"background", key_prefix='bgr_')
 for language in languages:
images_train.update(read_all(".. /input/"+LEVEL+"_train/"+LEVEL+"/"+language, key_prefix=language+"_"))
print(len(images_train))
images_test = read_all(".. /input/"+LEVEL+"_test/kaggle_"+LEVEL, key_prefix='')
 print(len(images_test))<normalization>
age_null_count = df_train['Age'].isnull().sum()
age_null_count
scaler = StandardScaler()
X_scaled_train = scaler.fit_transform(X_train)
 X_scaled_test = scaler.transform(X_test )<train_model>
df_train['Age'] = df_train['Age'].fillna(age_median)
 df_train['CategoricalAge'] = pd.cut(df_train['Age'], 8)
 print(df_train[['CategoricalAge', 'Survived']].groupby(['CategoricalAge'], as_index=False ).mean() )
sn_ce = SigmoidNeuron()
sn_ce.fit(X_scaled_train, Y_train, epochs=100, learning_rate=1,loss_fn="ce", display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=200, learning_rate=0.1,initialise=False,loss_fn="ce", display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=200, learning_rate=0.01,initialise=False,loss_fn="ce", display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=400, learning_rate=0.001,initialise=False,loss_fn="ce", display_loss=True)
 sn_ce.fit(X_scaled_train, Y_train, epochs=500, learning_rate=0.0001,initialise=False,loss_fn="ce", display_loss=True )<predict_on_test>
df_test['Age'] = df_test['Age'].fillna(age_median)
def print_accuracy(sn):
Y_pred_train = sn.predict(X_scaled_train)
 Y_pred_binarised_train =(Y_pred_train >= 0.5 ).astype("int" ).ravel()
accuracy_train = accuracy_score(Y_pred_binarised_train, Y_train)
 print("Train Accuracy : ", accuracy_train)
 print("-"*50 )<compute_test_metric>
df_train['Title'] = df_train['Title'].map({"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 0} ).astype(int )
print_accuracy(sn_ce )<save_to_csv>
df_test['Title'] = df_test['Title'].map({"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 0} ).astype(int )
Y_pred_test = sn_ce.predict(X_scaled_test)
 Y_pred_binarised_test =(Y_pred_test >= 0.5 ).astype("int" ).ravel()
submission = {}
submission['ImageId'] = ID_test
submission['Class'] = Y_pred_binarised_test
submission = pd.DataFrame(submission)
 submission = submission[['ImageId', 'Class']]
submission = submission.sort_values(['ImageId'])
 submission.to_csv("submisision.csv", index=False )<compute_test_metric>
df_train['Sex'] = df_train['Sex'].map({'female' : 0, 'male' : 1} ).astype(int)
 df_train.head()
def smape_score(y_true, y_pred):
return np.mean(2 * np.abs(y_true - y_pred)/ 
(np.abs(y_true)+ np.abs(y_pred)))* 100<load_from_csv>
df_test['Sex'] = df_test['Sex'].map({'female' : 0, 'male' : 1} ).astype(int)
 df_test.head()
sat_pos = pd.read_csv('/kaggle/input/sputnik/train.csv', sep=',')
 sat_pos['epoch'] = pd.to_datetime(sat_pos.epoch, format='%Y-%m-%d %H:%M:%S')
 sat_pos.index = sat_pos.epoch
sat_pos.drop('epoch', axis=1, inplace=True)
 sat_pos['error'] = np.linalg.norm(sat_pos[['x', 'y', 'z']].values -
sat_pos[['x_sim', 'y_sim', 'z_sim']].values, axis=1)
 sat_pos.drop(['x', 'y', 'z', 'x_sim', 'y_sim', 'z_sim'], axis=1, inplace=True)
 np.array_equal(np.unique(sat_pos['sat_id']), np.arange(0,600))
sat_set = np.arange(0,600)
 sat_set.shape[0]
sat_pos_train = sat_pos[sat_pos['type'] == 'train']
sat_pos_test = sat_pos[sat_pos['type'] == 'test']
sat_pos_train.drop('type', axis=1, inplace=True )<find_best_model_class>
df_train.loc[df_train['Embarked'] == 'Empty']
df_train['Embarked'] = df_train['Embarked'].fillna("Empty")
 df_train['Embarked'] = df_train['Embarked'].map({'S' : 0, 'C' : 1, 'Q' : 2, 'Empty' : 0}, na_action='ignore' ).astype(int)
 df_train.head()
def timeseriesCVscore(series, n_splits, loss_function):
errors = []
tscv = TimeSeriesSplit(n_splits=n_splits)
 for train, test in tscv.split(series.error.values):
x_true = series.error.values[train]
y_true = series.error.values[test]
l_train = len(x_true)
 l_test = len(y_true)
 lags_errors = []
lag_period = 24
n_steps = l_test // lag_period
remainder = l_test % lag_period
for step in np.arange(1, n_steps + 1):
lag_error =(series.error.shift(lag_period)+
step *(series.error.shift(lag_period)-
series.error.shift(2*lag_period)))[l_train:l_train+lag_period]
lags_errors.append(lag_error)
 if remainder != 0:
lag_error =(series.error.shift(lag_period)+ 
(n_steps + 1)*(series.error.shift(lag_period)-
series.error.shift(2*lag_period)))[l_train:l_train+remainder]
lags_errors.append(lag_error)
 y_pred = np.concatenate(lags_errors)
 error = loss_function(y_pred, y_true)
 errors.append(error)
 return np.mean(np.array(errors))<compute_train_metric>
df_test['Embarked'] = df_test['Embarked'].fillna("Empty")
 df_test.loc[df_test['Embarked'] == 'Empty']
df_test['Embarked'] = df_test['Embarked'].map({'S' : 0, 'C' : 1, 'Q' : 2, 'Empty' : 0}, na_action='ignore' ).astype(int)
 df_test.head()
series = sat_pos_train[sat_pos_train['sat_id'] == 150]
n_splits = 100
timeseriesCVscore(series=series, n_splits=n_splits, loss_function=smape_score )<define_variables>
df_train['CategoricalAge'] = 0
df_train.loc[df_train['Age'] <= 10.368, 'CategoricalAge'] = 0
df_train.loc[(df_train['Age'] > 10.368)&(df_train['Age'] <= 20.315), 'CategoricalAge'] = 1
df_train.loc[(df_train['Age'] > 20.315)&(df_train['Age'] <= 30.263), 'CategoricalAge'] = 2
df_train.loc[(df_train['Age'] > 30.263)&(df_train['Age'] <= 40.21), 'CategoricalAge'] = 3
df_train.loc[(df_train['Age'] > 40.21)&(df_train['Age'] <= 50.158), 'CategoricalAge'] = 4
df_train.loc[(df_train['Age'] > 50.158)&(df_train['Age'] <= 60.105), 'CategoricalAge'] = 5
df_train.loc[(df_train['Age'] > 60.105)&(df_train['Age'] <= 70.052), 'CategoricalAge'] = 6
df_train.loc[df_train['Age'] > 70.052, 'CategoricalAge'] = 7
df_train.head()
train_lengths = []
test_lengths = []
for sat_i in tqdm(sat_set):
series = sat_pos[sat_pos['sat_id'] == sat_i]
l_train = len(series[series['type'] == 'train'])
 l_test = len(series[series['type'] == 'test'])
 train_lengths.append(l_train)
 test_lengths.append(l_test)
 len_train_max = max(train_lengths)
<statistical_test>
df_test['CategoricalAge'] = 0
df_test.loc[df_test['Age'] <= 10.368, 'CategoricalAge'] = 0
df_test.loc[(df_test['Age'] > 10.368)&(df_test['Age'] <= 20.315), 'CategoricalAge'] = 1
df_test.loc[(df_test['Age'] > 20.315)&(df_test['Age'] <= 30.263), 'CategoricalAge'] = 2
df_test.loc[(df_test['Age'] > 30.263)&(df_test['Age'] <= 40.21), 'CategoricalAge'] = 3
df_test.loc[(df_test['Age'] > 40.21)&(df_test['Age'] <= 50.158), 'CategoricalAge'] = 4
df_test.loc[(df_test['Age'] > 50.158)&(df_test['Age'] <= 60.105), 'CategoricalAge'] = 5
df_test.loc[(df_test['Age'] > 60.105)&(df_test['Age'] <= 70.052), 'CategoricalAge'] = 6
df_test.loc[df_test['Age'] > 70.052, 'CategoricalAge'] = 7
df_test.head()
predictions = []
for sat_i in tqdm(sat_set):
series = sat_pos[sat_pos['sat_id'] == sat_i]
l_train = len(series[series['type'] == 'train'])
 l_test = len(series[series['type'] == 'test'])
 lags_errors = []
lag_period = 24
n_steps = l_test // lag_period
remainder = l_test % lag_period
for step in np.arange(1, n_steps + 1):
lag_error =(series.error.shift(lag_period)+
step *(series.error.shift(lag_period)-
series.error.shift(2*lag_period)))[l_train:l_train+lag_period]
lags_errors.append(lag_error)
 if remainder != 0:
lag_error =(series.error.shift(lag_period)+ 
(n_steps + 1)*(series.error.shift(lag_period)-
series.error.shift(2*lag_period)))[l_train:l_train+remainder]
lags_errors.append(lag_error)
 predictions.append(np.concatenate(lags_errors))<save_to_csv>
df_train['CategoricalFare'] = 0
df_train.loc[df_train['Fare'] <= 7.55, 'CategoricalFare'] = 0
df_train.loc[(df_train['Fare'] > 7.55)&(df_train['Fare'] <= 7.854), 'CategoricalFare'] = 1
df_train.loc[(df_train['Fare'] > 7.854)&(df_train['Fare'] <= 8.05), 'CategoricalFare'] = 2
df_train.loc[(df_train['Fare'] > 8.05)&(df_train['Fare'] <= 10.5), 'CategoricalFare'] = 3
df_train.loc[(df_train['Fare'] > 10.5)&(df_train['Fare'] <= 14.454), 'CategoricalFare'] = 4
df_train.loc[(df_train['Fare'] > 14.454)&(df_train['Fare'] <= 21.679), 'CategoricalFare'] = 5
df_train.loc[(df_train['Fare'] > 21.679)&(df_train['Fare'] <= 27.0), 'CategoricalFare'] = 6
df_train.loc[(df_train['Fare'] > 27.0)&(df_train['Fare'] <= 39.688), 'CategoricalFare'] = 7
df_train.loc[(df_train['Fare'] > 39.688)&(df_train['Fare'] <= 77.958), 'CategoricalFare'] = 8
df_train.loc[df_train['Fare'] > 77.958, 'CategoricalFare'] = 9
df_train.head()
forecast = np.concatenate(predictions)
 subm = pd.DataFrame({'id': sat_pos_test['id'],
'error': forecast})
 subm.to_csv('submission_3_5.csv', index=False )<set_options>
df_test['CategoricalFare'] = 0
df_test.loc[df_test['Fare'] <= 7.55, 'CategoricalFare'] = 0
df_test.loc[(df_test['Fare'] > 7.55)&(df_test['Fare'] <= 7.854), 'CategoricalFare'] = 1
df_test.loc[(df_test['Fare'] > 7.854)&(df_test['Fare'] <= 8.05), 'CategoricalFare'] = 2
df_test.loc[(df_test['Fare'] > 8.05)&(df_test['Fare'] <= 10.5), 'CategoricalFare'] = 3
df_test.loc[(df_test['Fare'] > 10.5)&(df_test['Fare'] <= 14.454), 'CategoricalFare'] = 4
df_test.loc[(df_test['Fare'] > 14.454)&(df_test['Fare'] <= 21.679), 'CategoricalFare'] = 5
df_test.loc[(df_test['Fare'] > 21.679)&(df_test['Fare'] <= 27.0), 'CategoricalFare'] = 6
df_test.loc[(df_test['Fare'] > 27.0)&(df_test['Fare'] <= 39.688), 'CategoricalFare'] = 7
df_test.loc[(df_test['Fare'] > 39.688)&(df_test['Fare'] <= 77.958), 'CategoricalFare'] = 8
df_test.loc[df_test['Fare'] > 77.958, 'CategoricalFare'] = 9
df_test.head()
warnings.filterwarnings('ignore')
 %matplotlib inline
def rmse(y_actual, y_predicted):
rmse = sqrt(mean_squared_error(y_actual, y_predicted))
return rmse
def mean_absolute_percentage_error(y_true, y_pred):
return np.mean(np.abs(( y_true - y_pred)/ y_true)) * 100 
<load_from_csv>
drop_elements = ['Name', 'Ticket', 'Cabin', 'Age', 'Fare', 'Parch', 'SibSp', 'IsAlone']
df_train = df_train.drop(drop_elements, axis = 1)
 df_test = df_test.drop(drop_elements, axis = 1 )
raw_data = pd.read_csv('/kaggle/input/sputnik/train.csv', sep =',')
 raw_data.head(2 )<feature_engineering>
train = df_train.copy()
train = train.drop('PassengerId', axis=1)
 train.head()
def make_coord(df,coord,coord_sim):
ts_x=df[coord]
ts_x_sim = df[coord_sim]
eps = np.mean(df.epoch-df.epoch.shift(1)) /5
bad_num_plus=[]
[bad_num_plus.append(i)for i in range(1,df.shape[0])if(df.epoch[i]-df.epoch[i-1])<eps]
ts_x.drop(ts_x.index[bad_num_plus], inplace=True)
 if len(bad_num_plus)>0:
val_x=ts_x_sim.values[:-len(bad_num_plus)]
else:
val_x=ts_x_sim.values
ts_x_sim = pd.Series(val_x)
 ts_x_sim.index=ts_x.index
d_x = ts_x - ts_x_sim
train = d_x[df.type=='train']
val = d_x[df.type=='test']
fit1 = ExponentialSmoothing(np.asarray(train), seasonal_periods=24, seasonal='add' ).fit()
forecast = pd.Series(fit1.forecast(len(val)))
 forecast.index = val.index
c=1.1
x_pd = train.append(c*forecast)
 x_pd = x_pd + ts_x_sim
x_new=np.array(x_pd.values)
 bad_num=np.array(bad_num_plus)-1
for i in bad_num:
x_new = np.insert(x_new,i,x_new[i])
 x_new = pd.Series(x_new)
 x_new.index = df.index
df[coord+'_new']=x_new
return df<feature_engineering>
test = df_test.copy()
test = test.drop('PassengerId', axis=1)
 test.head()
def make_coord(df,coord,coord_sim):
ts_x=df[coord]
ts_x_sim = df[coord_sim]
eps = np.mean(df.epoch-df.epoch.shift(1)) /5
bad_num_plus=[]
[bad_num_plus.append(i)for i in range(1,df.shape[0])if(df.epoch[i]-df.epoch[i-1])<eps]
ts_x.drop(ts_x.index[bad_num_plus], inplace=True)
 if len(bad_num_plus)>0:
val_x=ts_x_sim.values[:-len(bad_num_plus)]
else:
val_x=ts_x_sim.values
ts_x_sim = pd.Series(val_x)
 ts_x_sim.index=ts_x.index
d_x = ts_x - ts_x_sim
train = d_x[df.type=='train']
val = d_x[df.type=='test']
fit1 = ExponentialSmoothing(np.asarray(train), seasonal_periods=24, seasonal='add' ).fit()
forecast = pd.Series(fit1.forecast(len(val)))
 forecast.index = val.index
c_1=(max(train[-24:])-np.mean(train[-24:])) /(max(train[-48:-24])-np.mean(train[-24:]))
c_2=(min(train[-24:])-np.mean(train[-24:])) /(min(train[-48:-24])-np.mean(train[-24:]))
for j in range(len(forecast)) :
if forecast[j]>np.mean(forecast[-48:]):
forecast[j]= np.mean(forecast[-48:])+(forecast[j]-np.mean(forecast[-48:])) *(c_1**(1+j/24))
else:
forecast[j]= np.mean(forecast[-48:])+(forecast[j]-np.mean(forecast[-48:])) *(c_2**(1+j/24))
x_pd = train.append(forecast)
 x_pd = x_pd + ts_x_sim
x_new=np.array(x_pd.values)
 bad_num=np.array(bad_num_plus)-1
for i in bad_num:
x_new = np.insert(x_new,i,x_new[i])
 x_new = pd.Series(x_new)
 x_new.index = df.index
df[coord+'_new']=x_new
return df<feature_engineering>
from sklearn.preprocessing import StandardScaler
raw_train = raw_data
ids=[]
n_err=[]
err_by_obj=[]
for i in range(600):
df = raw_train[raw_train.sat_id==i]
df['error'] = np.linalg.norm(df[['x', 'y', 'z']].values - df[['x_sim', 'y_sim', 'z_sim']].values, axis=1)
 df.sort_values('epoch', axis = 0, inplace=True)
 df.epoch = pd.to_datetime(df.epoch, format='%Y-%m-%dT%H:%M:%S')
 df.index = df.epoch
df = make_coord(df,'x','x_sim')
 df = make_coord(df,'y','y_sim')
 df = make_coord(df,'z','z_sim')
 df['new_error'] = np.linalg.norm(df[['x_new', 'y_new', 'z_new']].values - df[['x_sim', 'y_sim', 'z_sim']].values, axis=1)
 for j in range(df.shape[0]):
ids.append(df.id[j])
 n_err.append(df.new_error[j])
<feature_engineering>
X_train = train.drop('Survived', axis=1 ).astype(float)
 y_train = df_train['Survived']
X_test = test.astype(float )
raw_train['new_error']=n_err
raw_train.head(2 )<save_to_csv>
scaler = StandardScaler()
scaler.fit(X_train )
res = raw_train[['id','new_error','type']]
res.columns = ['id', 'error','type']
res=res[res.type=='test']
res=res[['id','error']]
res.to_csv('my_sub_sec.csv', index = False, header=True )<set_options>
X_train = scaler.transform(X_train )
%matplotlib inline
warnings.filterwarnings('ignore')
<categorify>
X_test = scaler.transform(X_test )
def illustration(func):

 mutex = ThreadLock()
n_thread = ThreadValue('i',0)
 @functools.wraps(func)
 def wrapper(*args, **argv):
result = func(*args, **argv)
 with mutex:
nonlocal n_thread
n_thread.value +=1
if n_thread.value % 5 ==0:
print(f"\r{n_thread.value} objects are processed...",end ='',flush = True)
 return result
return wrapper<load_from_csv>
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
data_root = "/kaggle/input/wwwkagglecomborisgruzdev/"
df = pd.read_csv(data_root+'/train.csv',sep = ',')
 df.sort_values(['sat_id','epoch'],axis = 0,inplace =True)
 df['epoch'] = pd.to_datetime(df.epoch,format='%Y-%m-%d %H:%M:%S')
 df.index = df.epoch
df.drop('epoch', axis = 1, inplace = True)
 df['error'] = np.linalg.norm(df[['x', 'y', 'z']].values - df[['x_sim', 'y_sim', 'z_sim']].values, axis=1)
 df.head()<filter>
logreg = LogisticRegression(solver='lbfgs')
 logreg.fit(X_train, y_train)
 Y_pred = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, y_train)* 100, 3)
 acc_log
df_train = df[df.type == 'train']
df_test = df[df.type == 'test']<count_values>
svc = SVC(gamma='scale')
 svc.fit(X_train, y_train)
 Y_pred = svc.predict(X_test)
 acc_svc = round(svc.score(X_train, y_train)* 100, 3)
 acc_svc
df_train['sat_id'].value_counts()<compute_test_metric>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, y_train)* 100, 3)
 acc_knn
def mean_absolute_percentage_error(y_true, y_pred):
return np.mean(np.abs(( y_true - y_pred)/ y_true)) * 100<compute_train_metric>
gaussian = GaussianNB()
gaussian.fit(X_train, y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, y_train)* 100, 3)
 acc_gaussian
def predict_with_season_and_trend(train,test,period = 24):
count = test.shape[0]//period
res = test.shape[0]%period
os1 = train.iloc[-period:].error.values.tolist() *count
if res != 0:
os1 += train.iloc[-period:-(period-res)].error.values.tolist()
seas = np.array(os1)
 Mm = 48
mm = 25
trend = train.error[-Mm:].rolling(window=24 ).mean()
s,*_ = linregress(np.arange(mm),trend.dropna().values)
 slope = s*np.arange(0,test.shape[0])
 seas+=slope
return seas<predict_on_test>
perceptron = Perceptron(max_iter=1000, tol=0.001)
 perceptron.fit(X_train, y_train)
 Y_pred = perceptron.predict(X_test)
 acc_perceptron = round(perceptron.score(X_train, y_train)* 100, 3)
 acc_perceptron
@illustration
def predictions(indices):
dff = df[df.sat_id == indices]
df_traning = dff[dff.type == 'train']
df_testing = dff[dff.type == 'test']
pred = predict_with_season_and_trend(df_traning,df_testing,period = 24)
 a = [{'id' : df_testing["id"].values[i],
'error_p' : pred[i]} for i in range(df_testing.shape[0])]
return a<predict_on_test>
linear_svc = LinearSVC(max_iter=10000)
 linear_svc.fit(X_train, y_train)
 Y_pred = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, y_train)* 100, 3)
 acc_linear_svc
@illustration
def test_pred(indices):
t_df = df_train[df_train.sat_id == indices]
sh = np.int64(t_df.shape[0]*0.7)
 df_traning = t_df.iloc[:sh]
df_testing = t_df.iloc[sh:]
pred = predict_with_season_and_trend(df_traning,df_testing,period = 24)
 df_testing['pred_error'] = pred
return mean_absolute_percentage_error(df_testing.error,df_testing.pred_error )<categorify>
sgd = SGDClassifier(max_iter=10000, tol=0.001)
 sgd.fit(X_train, y_train)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X_train, y_train)* 100, 3)
 acc_sgd
with ThreadPool(20)as pool:
fold = pool.map(predictions,range(600))<create_dataframe>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, y_train)
 Y_pred = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, y_train)* 100, 3)
 acc_decision_tree
fold = [x[i] for x in fold for i in range(len(x)) ]
kar = pd.DataFrame(fold)
 kar<merge>
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, y_train)
 Y_pred = random_forest.predict(X_test)
 random_forest.score(X_train, y_train)
 acc_random_forest = round(random_forest.score(X_train, y_train)* 100, 3)
 acc_random_forest
predictions = pd.merge(df[df.type == 'test'],kar,how = 'inner',on = 'id')
 predictions.error = predictions.error_p
predictions.info()<load_from_csv>
MLP_clf = MLPClassifier(activation='tanh', solver='lbfgs', alpha=1e-7, hidden_layer_sizes=(50,50), random_state=1, warm_start=True)
 MLP_clf.fit(X_train, y_train)
 Y_pred = MLP_clf.predict(X_test)
 acc_MLP_clf = round(MLP_clf.score(X_train, y_train)* 100, 3)
 acc_MLP_clf
new_df = pd.read_csv(data_root+'sub.csv',sep = ',')
 our_preds = pd.merge(predictions,new_df,how = 'inner',on = 'id')
 our_preds = our_preds[['id','error_x']]
our_preds.rename(columns={'error_x': 'error'}, inplace=True)
 our_preds<save_to_csv>
adaboost = AdaBoostClassifier()
adaboost.fit(X_train, y_train)
 Y_pred = adaboost.predict(X_test)
 acc_adaboost = round(adaboost.score(X_train, y_train)* 100, 3)
 acc_adaboost
our_preds.to_csv('/kaggle/output',sep=",",index = False )<save_to_csv>
gboost = GradientBoostingClassifier()
gboost.fit(X_train, y_train)
 Y_pred = gboost.predict(X_test)
 acc_gboost = round(gboost.score(X_train, y_train)* 100, 3)
 acc_gboost
our_preds.to_csv('/kaggle/working/pred.csv', index = False )<set_options>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree', 'MLPClassifier', 'AdaBoostClassifier', 'GradientBoostingClassifier'],
'Score': [acc_svc, acc_knn, acc_log,
acc_random_forest, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree, acc_MLP_clf, acc_adaboost, acc_gboost]})
 models.sort_values(by='Score', ascending=False )
cf.set_config_file(offline=True)
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
Y_pred = random_forest.predict(X_test )
root = Path('.. /input/great-energy-predictor-shootout-i')
 df_submit = pd.read_csv(root/'sample_submission.csv')
 df_submit<data_type_conversions>
submission = pd.DataFrame({
"PassengerId": df_test["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submission.csv', index=False )