Kaggle competition: LANL Earthquake Prediction
df_target_g=df_target[['group_id','surface_id']].groupby(by='group_id' ).count().reset_index()
df_target_g.head()<count_unique_values>
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.preprocessing import StandardScaler
from sklearn.svm import NuSVR
from sklearn.metrics import mean_absolute_error
print('Groups Nr Max Min',yg.nunique() ,yg.max() ,yg.min() )<feature_engineering>
train = pd.read_csv('.. /input/train.csv', dtype={'acoustic_data': np.int16, 'time_to_failure': np.float64} )
target['surface'] = le.fit_transform(target['surface'] )<normalization>
pd.options.display.precision = 15
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 *(w * x + y * z)
 t1 = +1.0 - 2.0 *(x * x + y * y)
 X = math.atan2(t0, t1)
 t2 = +2.0 *(w * y - z * x)
 t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.asin(t2)
 t3 = +2.0 *(w * z + x * y)
 t4 = +1.0 - 2.0 *(y * y + z * z)
 Z = math.atan2(t3, t4)
 return X, Y, Z<feature_engineering>
rows = 150_000
segments = int(np.floor(train.shape[0] / rows))
X_train = pd.DataFrame(index=range(segments), dtype=np.float64,
columns=['ave', 'std', 'max', 'min'])
 y_train = pd.DataFrame(index=range(segments), dtype=np.float64,
columns=['time_to_failure'])
 for segment in tqdm(range(segments)) :
seg = train.iloc[segment*rows:segment*rows+rows]
x = seg['acoustic_data'].values
y = seg['time_to_failure'].values[-1]
y_train.loc[segment, 'time_to_failure'] = y
X_train.loc[segment, 'ave'] = x.mean()
X_train.loc[segment, 'std'] = x.std()
X_train.loc[segment, 'max'] = x.max()
X_train.loc[segment, 'min'] = x.min()
def fe_step0(actual):
actual['norm_quat'] =(actual['orientation_X']**2 + actual['orientation_Y']**2 + actual['orientation_Z']**2 + actual['orientation_W']**2)
 actual['mod_quat'] =(actual['norm_quat'])**0.5
actual['norm_X'] = actual['orientation_X'] / actual['mod_quat']
actual['norm_Y'] = actual['orientation_Y'] / actual['mod_quat']
actual['norm_Z'] = actual['orientation_Z'] / actual['mod_quat']
actual['norm_W'] = actual['orientation_W'] / actual['mod_quat']
return actual<feature_engineering>
scaler = StandardScaler()
scaler.fit(X_train)
 X_train_scaled = scaler.transform(X_train )
tr = fe_step0(tr)
 te = fe_step0(te )<concatenate>
svm = NuSVR()
svm.fit(X_train_scaled, y_train.values.flatten())
 y_pred = svm.predict(X_train_scaled )
def fe_step1(actual):

 x, y, z, w = actual['norm_X'].tolist() , actual['norm_Y'].tolist() , actual['norm_Z'].tolist() , actual['norm_W'].tolist()
nx, ny, nz = [], [], []
for i in range(len(x)) :
xx, yy, zz = quaternion_to_euler(x[i], y[i], z[i], w[i])
 nx.append(xx)
 ny.append(yy)
 nz.append(zz)
 actual['euler_x'] = nx
actual['euler_y'] = ny
actual['euler_z'] = nz
return actual<feature_engineering>
score = mean_absolute_error(y_train.values.flatten() , y_pred)
 print(f'Score: {score:0.3f}' )
def feat_eng(data):
data['totl_anglr_vel'] =(data['angular_velocity_X']**2 + data['angular_velocity_Y']**2 + data['angular_velocity_Z']**2)** 0.5
data['totl_linr_acc'] =(data['linear_acceleration_X']**2 + data['linear_acceleration_Y']**2 + data['linear_acceleration_Z']**2)**0.5
data['totl_xyz'] =(data['orientation_X']**2 + data['orientation_Y']**2 + data['orientation_Z']**2)**0.5
data['totl_angle'] = np.sqrt(data['euler_x'] ** 2 + data['euler_y'] ** 2 + data['euler_z'] ** 2)
 data['linear_acceleration_X_abs'] = data['linear_acceleration_X'].where(data['linear_acceleration_X']>=0, - data['linear_acceleration_X'])
 data['linear_acceleration_Y_abs'] = data['linear_acceleration_Y'].where(data['linear_acceleration_Y']>=0, - data['linear_acceleration_Y'])
 data['linear_acceleration_Z_abs'] = data['linear_acceleration_Z'].where(data['linear_acceleration_Z']>=0, - data['linear_acceleration_Z'])
 data['acc_vs_vel'] = data['totl_linr_acc'] / data['totl_anglr_vel']
data['angle_vs_acc'] = data['totl_angle'] / data['totl_linr_acc']
data['angle_vs_vel'] = data['totl_angle'] / data['totl_anglr_vel']
data['acc_int_vel'] = data['totl_linr_acc'] * data['totl_anglr_vel']
data['angle_int_acc'] = data['totl_angle'] * data['totl_linr_acc']
data['angle_int_vel'] = data['totl_angle'] * data['totl_anglr_vel']
data['angle_ints_vel_acc'] = data['totl_angle'] * data['totl_anglr_vel'] * data['totl_linr_acc']
def mean_change_of_abs_change(x):
return np.mean(np.diff(np.abs(np.diff(x))))
df = pd.DataFrame()
for col in data.columns:
if col in ['row_id','series_id','measurement_number']:
continue
df[col + '_mean'] = data.groupby(['series_id'])[col].mean()
df[col + '_median'] = data.groupby(['series_id'])[col].median()
df[col + '_max'] = data.groupby(['series_id'])[col].max()
df[col + '_min'] = data.groupby(['series_id'])[col].min()
df[col + '_std'] = data.groupby(['series_id'])[col].std()
df[col + '_range'] = df[col + '_max'] - df[col + '_min']
df[col + '_maxtoMin'] = df[col + '_max'] / df[col + '_min']
df[col + '_mean_abs_chg'] = data.groupby(['series_id'])[col].apply(lambda x: np.mean(np.abs(np.diff(x))))
df[col + '_mean_change_of_abs_change'] = data.groupby('series_id')[col].apply(mean_change_of_abs_change)
 df[col + '_abs_max'] = data.groupby(['series_id'])[col].apply(lambda x: np.max(np.abs(x)))
 df[col + '_abs_min'] = data.groupby(['series_id'])[col].apply(lambda x: np.min(np.abs(x)))
 df[col + '_abs_avg'] =(df[col + '_abs_min'] + df[col + '_abs_max'])/2
return df<feature_engineering>
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id' )
tr = fe_step1(tr)
 te = fe_step1(te )<feature_engineering>
X_test = pd.DataFrame(columns=X_train.columns, dtype=np.float64, index=submission.index)
%%time
tr = feat_eng(tr)
 te = feat_eng(te)
 tr.head()<normalization>
for seg_id in X_test.index:
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 x = seg['acoustic_data'].values
X_test.loc[seg_id, 'ave'] = x.mean()
X_test.loc[seg_id, 'std'] = x.std()
X_test.loc[seg_id, 'max'] = x.max()
X_test.loc[seg_id, 'min'] = x.min()
<concatenate><EOS> 
X_test_scaled = scaler.transform(X_test)
 submission['time_to_failure'] = svm.predict(X_test_scaled)
 submission.to_csv('submission.csv' )