Kaggle competition: Предсказание положения космических объектов
def process_pclass() :
global combined
pclass_dummies = pd.get_dummies(combined['Pclass'], prefix="Pclass")
 combined = pd.concat([combined,pclass_dummies],axis=1)
 combined.drop('Pclass',axis=1,inplace=True)
 status('pclass' )<drop_column>
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
combined.drop('PassengerId', inplace=True, axis=1 )<feature_engineering>
raw_data = pd.read_csv('/kaggle/input/sputnik/train.csv', sep =',')
 raw_data.head(2 )
def process_ticket() :
global combined
def cleanTicket(ticket):
ticket = ticket.replace('.','')
 ticket = ticket.replace('/','')
 ticket = ticket.split()
ticket = map(lambda t : t.strip() , ticket)
 ticket = list(filter(lambda t : not t.isdigit() , ticket))
if len(ticket)> 0:
return ticket[0]
else:
return 'XXX'
combined['Ticket'] = combined['Ticket'].map(cleanTicket)
 tickets_dummies = pd.get_dummies(combined['Ticket'], prefix='Ticket')
 combined = pd.concat([combined, tickets_dummies], axis=1)
 combined.drop('Ticket', inplace=True, axis=1)
 status('ticket' )<feature_engineering>
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
def process_family() :
global combined
combined['FamilySize'] = combined['Parch'] + combined['SibSp'] + 1
combined['Singleton'] = combined['FamilySize'].map(lambda s: 1 if s == 1 else 0)
 combined['SmallFamily'] = combined['FamilySize'].map(lambda s: 1 if 2<=s<=4 else 0)
 combined['LargeFamily'] = combined['FamilySize'].map(lambda s: 1 if 5<=s else 0)
 status('family' )<count_missing_values>
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
pd.isnull(combined ).sum()<compute_train_metric>
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])
def compute_score(clf, X, y, scoring='accuracy'):
xval = cross_val_score(clf, X, y, cv = 5, scoring=scoring)
 return np.mean(xval )<load_from_csv>
raw_train['new_error']=n_err
raw_train.head(2 )
def recover_train_test_target() :
global combined
train0 = pd.read_csv('.. /input/train.csv')
 targets = train0.Survived
train = combined.head(891)
 test = combined.iloc[891:]
return train, test, targets<filter>
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 )