Kaggle competition: Предсказание положения космических объектов
train.Fare.loc[50:]<train_model>
%matplotlib inline
warnings.filterwarnings('ignore')
clf = RandomForestClassifier(n_estimators=50, max_features='sqrt')
 clf = clf.fit(train,targets)
 print(clf.feature_importances_ )<categorify>
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
model = SelectFromModel(clf, prefit=True)
 train_reduced = model.transform(train)
 train_reduced.shape<init_hyperparams>
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()
run_gs = False
if run_gs:
parameter_grid = {
'max_depth' : [4, 6, 8],
'n_estimators': [50, 10],
'max_features': ['sqrt', 'auto', 'log2'],
'min_samples_split': [1, 3, 10],
'min_samples_leaf': [1, 3, 10],
'bootstrap': [True, False],
}
forest = RandomForestClassifier()
cross_validation = StratifiedKFold(targets, n_folds=5)
 grid_search = GridSearchCV(forest,
scoring='accuracy',
param_grid=parameter_grid,
cv=cross_validation)
 grid_search.fit(train, targets)
 model = grid_search
parameters = grid_search.best_params_
print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))
else:
parameters = {'bootstrap': False, 'min_samples_leaf': 3, 'n_estimators': 50,
'min_samples_split': 10, 'max_features': 'sqrt', 'max_depth': 6}
model = RandomForestClassifier(**parameters)
 model.fit(train, targets )<compute_test_metric>
df_train = df[df.type == 'train']
df_test = df[df.type == 'test']
compute_score(model, train, targets, scoring='accuracy' )<save_to_csv>
df_train['sat_id'].value_counts()
output = model.predict(test ).astype(int)
 df_output = pd.DataFrame()
aux = pd.read_csv('.. /input/test.csv')
 df_output['PassengerId'] = aux['PassengerId']
df_output['Survived'] = output
df_output[['PassengerId','Survived']].to_csv('output.csv',index=False )<load_from_csv>
def mean_absolute_percentage_error(y_true, y_pred):
return np.mean(np.abs(( y_true - y_pred)/ y_true)) * 100
train = pd.read_csv('.. /input/titanic/train.csv')
 test_x = pd.read_csv('.. /input/titanic/test.csv')
 sub = pd.read_csv('.. /input/titanic/gender_submission.csv')
 df = pd.concat([train,test_x], sort = False)
 df.head()<feature_engineering>
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
CabinFill = df[df['Cabin'].notnull() ]
CabinNull = df[df['Cabin'].isnull() ]
CabinFill['Cabin'] = CabinFill['Cabin'].astype(str ).str[0]
df = pd.concat([CabinFill, CabinNull], sort = False ).sort_values(['PassengerId'])
 df['Sex'] = pd.Categorical(df.Sex ).codes
df['Embarked'] = pd.Categorical(df.Embarked ).codes
df['Cabin'] = pd.Categorical(df.Cabin ).codes
df['FamilySize'] = df['Parch'] + df['SibSp'] + 1
df['IsAlone'] = 0
df.loc[df['FamilySize'] == 1, 'IsAlone'] = 1
df.head(10 )<count_missing_values>
@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
df.isnull().sum()<count_values>
@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 )
df['Title'] = df['Name'].map(lambda name: name.split(',')[1].split('.')[0].strip())
 df['Sur'] = df['Name'].map(lambda name: name.split(',')[0])
 df['Title'].value_counts()<categorify>
with ThreadPool(20)as pool:
fold = pool.map(predictions,range(600))
titles_dummies = pd.get_dummies(df['Title'], prefix='Title')
 sur_dummies = pd.get_dummies(df['Sur'], prefix='Sur')
 df = pd.concat([df, titles_dummies, sur_dummies], axis=1)
 df.drop(['Title', 'Sur'], axis=1, inplace=True)
 df.head()<prepare_x_and_y>
fold = [x[i] for x in fold for i in range(len(x)) ]
kar = pd.DataFrame(fold)
 kar
logloss, accuracy, y_preds = [],[],[]
cv_train = np.zeros(( len(train),))
drop_cols = ['Name','PassengerId','Ticket','Survived','Fare','Cabin','Embarked',
'Age','SibSp','Parch']
train = df[:len(train)]
train_x = train.drop(drop_cols, axis=1)
 train_y = train['Survived']
test = df[len(train):]
test_x = test.drop(drop_cols, axis=1)
 params = {'objective': 'binary', 'seed': 71, 'verbose': 0, 'metrics': 'binary_logloss'}
num_round = 100
categorical_features = []
kf = StratifiedKFold(n_splits=4, shuffle=True, random_state=71)
 for tr_idx, va_idx in kf.split(train_x, train_y):
tr_x, va_x = train_x.iloc[tr_idx], train_x.iloc[va_idx]
tr_y, va_y = train_y.iloc[tr_idx], train_y.iloc[va_idx]
lgb_train = lgb.Dataset(tr_x, tr_y)
 lgb_eval = lgb.Dataset(va_x, va_y)
 model = lgb.train(params, lgb_train, num_boost_round=num_round,
categorical_feature=categorical_features,
valid_names=['train', 'valid'], valid_sets=[lgb_train, lgb_eval],
early_stopping_rounds=20)
 va_pred = model.predict(va_x, num_iteration=model.best_iteration)
 cv_train[va_idx] = va_pred
logloss.append(log_loss(va_y, va_pred))
va_pred =(va_pred > 0.5 ).astype(int)
 accuracy.append(accuracy_score(va_y, va_pred))
y_pred = model.predict(test_x, num_iteration=model.best_iteration)
 y_preds.append(y_pred)
 print(np.mean(logloss))
print(np.mean(accuracy))<compute_test_metric>
predictions = pd.merge(df[df.type == 'test'],kar,how = 'inner',on = 'id')
 predictions.error = predictions.error_p
predictions.info()
y_pred_cv =(cv_train > 0.5 ).astype(int)
 accuracy_score(train_y, y_pred_cv )<data_type_conversions>
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
sub_y_lgb = sum(y_preds)/ len(y_preds)
 sub_y_lgb =(sub_y_lgb > 0.5 ).astype(int)
 sub['sub_y_lgb'] = sub_y_lgb<prepare_x_and_y>
our_preds.to_csv('/kaggle/output',sep=",",index = False )
<data_type_conversions><EOS> 
our_preds.to_csv('/kaggle/working/pred.csv', index = False )