Kaggle competition: Предсказание положения космических объектов
def get_titles() :
global combined
combined['Title'] = combined['Name'].map(lambda name:name.split(',')[1].split('.')[0].strip())
 Title_Dictionary = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royalty"
}
combined['Title'] = combined.Title.map(Title_Dictionary )<groupby>
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
grouped_train = combined.head(891 ).groupby(['Sex','Pclass','Title'])
 grouped_median_train = grouped_train.median()
grouped_test = combined.iloc[891:].groupby(['Sex','Pclass','Title'])
 grouped_median_test = grouped_test.median()
grouped_median_train<categorify>
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 )
def process_age() :
global combined
def fillAges(row, grouped_median):
if row['Sex']=='female' and row['Pclass'] == 1:
if row['Title'] == 'Miss':
return grouped_median.loc['female', 1, 'Miss']['Age']
elif row['Title'] == 'Mrs':
return grouped_median.loc['female', 1, 'Mrs']['Age']
elif row['Title'] == 'Officer':
return grouped_median.loc['female', 1, 'Officer']['Age']
elif row['Title'] == 'Royalty':
return grouped_median.loc['female', 1, 'Royalty']['Age']
elif row['Sex']=='female' and row['Pclass'] == 2:
if row['Title'] == 'Miss':
return grouped_median.loc['female', 2, 'Miss']['Age']
elif row['Title'] == 'Mrs':
return grouped_median.loc['female', 2, 'Mrs']['Age']
elif row['Sex']=='female' and row['Pclass'] == 3:
if row['Title'] == 'Miss':
return grouped_median.loc['female', 3, 'Miss']['Age']
elif row['Title'] == 'Mrs':
return grouped_median.loc['female', 3, 'Mrs']['Age']
elif row['Sex']=='male' and row['Pclass'] == 1:
if row['Title'] == 'Master':
return grouped_median.loc['male', 1, 'Master']['Age']
elif row['Title'] == 'Mr':
return grouped_median.loc['male', 1, 'Mr']['Age']
elif row['Title'] == 'Officer':
return grouped_median.loc['male', 1, 'Officer']['Age']
elif row['Title'] == 'Royalty':
return grouped_median.loc['male', 1, 'Royalty']['Age']
elif row['Sex']=='male' and row['Pclass'] == 2:
if row['Title'] == 'Master':
return grouped_median.loc['male', 2, 'Master']['Age']
elif row['Title'] == 'Mr':
return grouped_median.loc['male', 2, 'Mr']['Age']
elif row['Title'] == 'Officer':
return grouped_median.loc['male', 2, 'Officer']['Age']
elif row['Sex']=='male' and row['Pclass'] == 3:
if row['Title'] == 'Master':
return grouped_median.loc['male', 3, 'Master']['Age']
elif row['Title'] == 'Mr':
return grouped_median.loc['male', 3, 'Mr']['Age']
combined.head(891 ).Age = combined.head(891 ).apply(lambda r : fillAges(r, grouped_median_train)if np.isnan(r['Age'])
 else r['Age'], axis=1)
 combined.iloc[891:].Age = combined.iloc[891:].apply(lambda r : fillAges(r, grouped_median_test)if np.isnan(r['Age'])
 else r['Age'], axis=1)
 status('age' )<categorify>
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))
def process_names() :
global combined
combined.drop('Name',axis=1,inplace=True)
 titles_dummies = pd.get_dummies(combined['Title'],prefix='Title')
 combined = pd.concat([combined,titles_dummies],axis=1)
 combined.drop('Title',axis=1,inplace=True)
 status('names' )<categorify>
series = sat_pos_train[sat_pos_train['sat_id'] == 150]
n_splits = 100
timeseriesCVscore(series=series, n_splits=n_splits, loss_function=smape_score )
def process_embarked() :
global combined
combined.head(891 ).Embarked.fillna('S', inplace=True)
 combined.iloc[891:].Embarked.fillna('S', inplace=True)
 embarked_dummies = pd.get_dummies(combined['Embarked'],prefix='Embarked')
 combined = pd.concat([combined,embarked_dummies],axis=1)
 combined.drop('Embarked',axis=1,inplace=True)
 status('embarked' )<categorify>
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)
def process_cabin() :
global combined
combined.Cabin.fillna('U', inplace=True)
 combined['Cabin'] = combined['Cabin'].map(lambda c : c[0])
 cabin_dummies = pd.get_dummies(combined['Cabin'], prefix='Cabin')
 combined = pd.concat([combined,cabin_dummies], axis=1)
 combined.drop('Cabin', axis=1, inplace=True)
 status('cabin' )<categorify>
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))
def process_sex() :
global combined
combined['Sex'] = combined['Sex'].map({'male':1,'female':0})
 status('sex' )<categorify>
forecast = np.concatenate(predictions)
 subm = pd.DataFrame({'id': sat_pos_test['id'],
'error': forecast})
 subm.to_csv('submission_3_5.csv', index=False )