Kaggle competition: TrackML Particle Tracking Challenge
<SOS> metric: TrackML Kaggle data source: trackml-particle-identification<compute_test_metric>
import numpy as np
mask_results = training_set["Sex"] == "male"
true_positive = len(training_set[true_pos & pred_pos & mask_results])
 false_positive = len(training_set[true_false & pred_pos & mask_results])
 true_negative = len(training_set[true_false & pred_false & mask_results])
 false_negative = len(training_set[true_pos & pred_false & mask_results])
 print(f"Accuracy: {(training_set['true'] == training_set['pred'] ).sum() / len(training_set):.2%}")
 print(f"True positives: {true_positive}({true_positive /(true_positive + false_negative):.0%})")
 print(f"False positives: {false_positive}")
 print(f"True negatives: {true_negative}({true_negative /(true_negative + false_positive):.0%})")
 print(f"False negatives: {false_negative}" )<filter>
x = np.array([(62,397,103),(82,347,107),(93,288,120), 
(94,266,128),(65,163,169),(12,102,198), 
(48,138,180),(77,187,157),(85,209,149),(89,316,112)] )
mask_results = training_set["Sex"] == "male"
mask_results2 = training_set["Name"].str.contains("Master.")
 true_positive = len(training_set[true_pos & pred_pos & mask_results & ~mask_results2])
 false_positive = len(training_set[true_false & pred_pos & mask_results & ~mask_results2])
 true_negative = len(training_set[true_false & pred_false & mask_results & ~mask_results2])
 false_negative = len(training_set[true_pos & pred_false & mask_results & ~mask_results2])
 print(f"Accuracy: {(training_set['true'] == training_set['pred'] ).sum() / len(training_set):.2%}")
 print(f"True positives: {true_positive}({true_positive /(true_positive + false_negative):.0%})")
 print(f"False positives: {false_positive}")
 print(f"True negatives: {true_negative}({true_negative /(true_negative + false_positive):.0%})")
 print(f"False negatives: {false_negative}" )<load_from_csv>
x = x - xm
test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )<merge>
v, s, t = np.linalg.svd(x,full_matrices=True )
test_data_prepared = prepare_data(test_data)
 test_data_prepared = test_data_prepared.merge(data_wcg_name, how="left", on="wcg_surname")
 test_data_prepared["wcg_name_all_died"]
test_data_prepared["wcg_name_all_survived"]
test_data_prepared = test_data_prepared.merge(data_wcg_ticket, how="left", on="wcg_ticket")
 test_data_prepared["wcg_ticket_all_died"]
test_data_prepared["wcg_ticket_all_survived"]
for i, row in test_data_prepared.iterrows() :
if(( test_data_prepared.loc[i, "wcg_name_all_died"] == 1)or(test_data_prepared.loc[i, "wcg_ticket_all_died"] == 1)) :
test_data_prepared.loc[i, "all_died"] = 1
elif(( test_data_prepared.loc[i, "wcg_name_all_died"] == 0)or(test_data_prepared.loc[i, "wcg_ticket_all_died"] == 0)) :
test_data_prepared.loc[i, "all_died"] = 0
else:
test_data_prepared.loc[i, "all_died"] = np.nan
if(( test_data_prepared.loc[i, "wcg_name_all_survived"] == 1)or(test_data_prepared.loc[i, "wcg_ticket_all_survived"] == 1)) :
test_data_prepared.loc[i, "all_survived"] = 1
elif(( test_data_prepared.loc[i, "wcg_name_all_survived"] == 0)or(test_data_prepared.loc[i, "wcg_ticket_all_survived"] == 0)) :
test_data_prepared.loc[i, "all_survived"] = 0
else:
test_data_prepared.loc[i, "all_survived"] = np.nan
test_data_prepared.drop(["wcg_surname", "wcg_ticket", "wcg_name_all_died", "wcg_name_all_survived", "wcg_ticket_all_died", "wcg_ticket_all_survived"], axis=1, inplace=True )<predict_on_test>
sigma1 = s[0]
sigma2 = s[1]
sigma3 = s[2]
v1 = t[0]
v2 = t[1]
v3 = t[2]
test_data_prepared_scaled = scaler.transform(test_data_prepared)
 y_test = gridsearch.predict(test_data_prepared_scaled )<save_to_csv>
Z = np.zeros(( x.shape[0],10), np.float32)
 Z[:,0] = x[:,0]**2
Z[:,1] = 2*x[:,0]*x[:,1]
Z[:,2] = 2*x[:,0]*x[:,2]
Z[:,3] = 2*x[:,0]
Z[:,4] = x[:,1]**2
Z[:,5] = 2*x[:,1]*x[:,2]
Z[:,6] = 2*x[:,1]
Z[:,7] = x[:,2]**2
Z[:,8] = 2*x[:,2]
Z[:,9] = 1
submission = pd.DataFrame(y_test.copy())
 submission['PassengerId'] = pd.read_csv("/kaggle/input/titanic/test.csv")['PassengerId'].copy()
submission.rename(columns={0:'Survived'}, inplace=True)
 submission = submission[['PassengerId', 'Survived']]
submission.to_csv("submission.csv", index=False )<set_options>
v, s, t = np.linalg.svd(Z,full_matrices=True)
 smallest_value = np.min(np.array(s))
smallest_index = np.argmin(np.array(s))
T = np.array(t)
 T = T[smallest_index,:]
S = np.zeros(( 4,4),np.float32)
 S[0,0] = T[0]
S[0,1] = S[1,0] = T[1]
S[0,2] = S[2,0] = T[2]
S[0,3] = S[3,0] = T[3]
S[1,1] = T[4]
S[1,2] = S[2,1] = T[5]
S[1,3] = S[3,1] = T[6]
S[2,2] = T[7]
S[2,3] = S[3,2] = T[8]
S[3,3] = T[9]
norm = np.linalg.norm(np.dot(Z,T), ord=2)**2
print(norm )
warnings.filterwarnings("ignore" )<load_from_csv>
%matplotlib inline
train = pd.read_csv("input/digit-recognizer/train.csv")
 test = pd.read_csv("input/digit-recognizer/test.csv")
 submisssion = pd.read_csv("input/digit-recognizer/sample_submission.csv" )<prepare_x_and_y>
path_to_train = ".. /input/train_1"
X = train.iloc[:,1:]/255.
 y = train.iloc[:,0]
test = test/255 .<split>
event_prefix = "event000001000"
y = to_categorical(y)
 Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size =0.3,random_state=29 )<data_type_conversions>
hits, cells, particles, truth = load_event(os.path.join(path_to_train, event_prefix))
mean_px = Xtrain.mean().astype(np.float32)
 std_px = Xtrain.std().astype(np.float32)
 def standardize(x):
return(x-mean_px)/std_px<choose_model_class>
def create_one_event_submission(event_id, hits, labels):
sub_data = np.column_stack(( [event_id]*len(hits), hits.hit_id.values, labels))
submission = pd.DataFrame(data=sub_data, columns=["event_id", "hit_id", "track_id"] ).astype(int)
 return submission
<train_model><EOS> 
submission = create_one_event_submission(0, hits, labels)
 score = score_event(truth, submission)
 print("Your score: ", score )