Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<features_selection>
%matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
m = 40
run_opt(path_df, m)
 path = path_df['Path'].values<save_to_csv>
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 train = pd.read_csv("/kaggle/input/titanic/train.csv" )
def make_submission(name, path):
pd.DataFrame({'Path': path} ).to_csv(f'{name}.csv', index=False )<compute_test_metric>
total = train.isnull().sum().sort_values(ascending=False)
 percent = round(train.isnull().sum() /train.isnull().count() *100, 1 ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', '%'])
 missing_data.head(5 )
make_submission("path" + str(int(score_path(path))), path )<define_search_space>
train_ml = train.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1)
 train_ml["Embarked"] = train_ml["Embarked"].fillna("S")
 mean = train_ml["Age"].mean()
std = train_ml["Age"].std()
is_null = train_ml["Age"].isnull().sum()
rand_age = np.random.randint(mean - std, mean + std, size = is_null)
 age_slice = train_ml["Age"].copy()
age_slice[np.isnan(age_slice)] = rand_age
train_ml["Age"] = age_slice
train_ml.info()
%%sh
cat > main.cpp <<'EOF'
using namespace std;
constexpr int maxShift = 25; //5
constexpr int cntNeighbors = 100;
constexpr int cntCities = 197769;
class GlobalIndex {
public:
array<double, 10> penalty_coefficients{1.1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
vector<vector<int>> neighbors;
bitset<cntCities> primes;
array<double, cntCities> x, y;
GlobalIndex()
: neighbors(cntCities+1, vector<int>(100)) {
primes.set() ;
primes[0] = primes[1] = false;
for(int p = 2; p * p <= cntCities; ++p)if(primes[p])
 for(int i = p*2; i < cntCities; i += p)
 primes[i] = false;
// Read coordinates
ifstream file(".. /input/traveling-santa-2018-prime-paths/cities.csv");
assert(file && "cities.csv");
file.ignore(numeric_limits<streamsize>::max() , '
');
char delimiter;
for(int i; file >> i >> delimiter >> x[i] >> delimiter >> y[i];);
// Read neighbors for not depends from libboost
ifstream file2(".. /input/list-100-neighbors-each-cities/neighbors100.bin", ios::binary);
assert(file2 && "neighbors100.bin");
for(auto& arr : neighbors)
 file2.read(reinterpret_cast<char*>(arr.data()), arr.size() *sizeof(arr[0])) ;
}
inline double dist(int l, int r)const {
const double dx = x[l] - x[r];
const double dy = y[l] - y[r];
return sqrt(dx*dx + dy*dy);
}
inline double dist(int l, int r, int i)const {
if(primes[l])return dist(l, r);
return penalty_coefficients[(i+1)%10] * dist(l, r);
}
};
const GlobalIndex gi;
class Index {
public:
explicit Index(const vector<int>& ids){
positions.resize(ids.size());
for(auto& diff_distance : diff_distances)
 diff_distance.resize(ids.size());
for(auto& diff_distance : rdiff_distances)
 diff_distance.resize(ids.size());
pure_dist.resize(ids.size() -1);
Update(ids);
}
void Update(const vector<int>& ids){
for(int i = 0; i < ids.size() ; ++i)
 positions[ids[i]] = i;
for(int i = 0; i < pure_dist.size() ; ++i)
 pure_dist[i] = gi.dist(ids[i], ids[i+1]);
for(int j = 0; j < diff_distances.size() ; ++j)
 for(int i = 1; i < diff_distances[0].size() ; ++i){
double value = pure_dist[i-1];
if(!gi.primes[ids[i-1]])
 value *= gi.penalty_coefficients[(i+j)%10];
diff_distances[j][i] = diff_distances[j][i-1] + value;
}
for(int j = 0; j < rdiff_distances.size() ; ++j)
 for(int i = 1, sz = rdiff_distances[0].size() ; i < sz; ++i){
double value = pure_dist[i-1];
if(!gi.primes[ids[i]])
 value *= gi.penalty_coefficients[(sz-i+j+1)%10];
rdiff_distances[j][i] = rdiff_distances[j][i-1] + value;
}
}
array<vector<double>, 10> diff_distances;
array<vector<double>, 10> rdiff_distances;
vector<int> positions;
vector<double> pure_dist;
inline double getScore(const vector<int>& ids, int beg, int end)const {
return diff_distances[0][end] - diff_distances[0][beg];
}
inline double getScoreReverse(const vector<int>& ids, int beg, int end, int shift = 0)const {
int i =(beg + end + shift)%10;
return rdiff_distances[i][end] - rdiff_distances[i][beg];
}
inline double getScore(const vector<int>& ids, int beg, int end, int shift)const {
if(beg <= end){
int i = shift ?(cntCities + shift + 1)%10 : 0;
return diff_distances[i][end] - diff_distances[i][beg];
}
return getScoreReverse(ids, end, beg, shift);
}
double getScore() const {
return diff_distances[0].back() ;
}
};
auto loadIds(const string& name){
vector<int> result;
ifstream file(name);
assert(file);
file.ignore(numeric_limits<streamsize>::max() , '
');
int id;
while(file >> id)
 result.emplace_back(id);
return result;
}
void saveIds(const vector<int>& ids, const string& name){
ofstream file(name);
file << "Path";
for(const auto& id : ids)
 file << endl << id;
}
auto compareReverse = [](const Index& index, const vector<int>& ids, int beg, int end){
if(end - beg < 3)
 return numeric_limits<double>::lowest() ;
double sum = index.getScore(ids, beg, end);
sum -= index.getScoreReverse(ids, beg + 1, end - 1);
sum -= gi.dist(ids[beg], ids[end - 1], beg);
sum -= gi.dist(ids[beg + 1], ids[end], end - 1);
return sum;
};
auto compareShiftRightN = [](const int n){
return [n](const Index& index, const vector<int>& ids, int beg, int end){
if(end >= ids.size() - 1 || end - beg < n + 1)
 return numeric_limits<double>::lowest() ;
double sum = index.getScore(ids, beg, end + 1);
sum -= index.getScore(ids, beg + 1, end - n, n);
for(auto i = 0; i < n - 1; ++i)
 sum -= gi.dist(ids[end - i], ids[end - i - 1], beg + i + 1);
sum -= gi.dist(ids[beg], ids[end], beg);
sum -= gi.dist(ids[end - n + 1], ids[beg + 1], beg+n);
sum -= gi.dist(ids[end - n], ids[end + 1], end);
return sum;
};
};
auto compareShiftLeftN = [](const int n){
return [n](const Index& index, const vector<int>& ids, int beg, int end){
if(beg == 0 || end - beg < n + 1)
 return numeric_limits<double>::lowest() ;
double sum = index.getScore(ids, beg - 1, end);
sum -= index.getScore(ids, beg+n, end - 1, -n);
for(auto i = 0; i < n - 1; ++i)
 sum -= gi.dist(ids[beg + 1 + i], ids[beg + i], end - 2 - i);
sum -= gi.dist(ids[beg - 1], ids[beg + n] , beg - 1);
sum -= gi.dist(ids[end - 1], ids[beg + n - 1], end - n - 1);
sum -= gi.dist(ids[beg] , ids[end] , end - 1);
return sum;
};
};
struct Result {
Result(int l, int r): beg(min(l ,r)) , end(max(l, r)) {}
double dist;
int beg, end;
friend bool operator<(const Result& l, const Result& r){return l.dist < r.dist;}
};
auto bestPairNeighbors = [](const Index& index, const vector<int>& ids, auto comp){
Result best(0, cntCities);
best.dist = comp(index, ids, best.beg, best.end);
for(int it = 0; it < ids.size() ; ++it)
 for(int i = 0; i < cntNeighbors; ++i){
Result tmp(it, index.positions[gi.neighbors[ids[it]][i]]);
tmp.dist = comp(index, ids, tmp.beg, tmp.end);
best = max(tmp, best);
}
return best;
};
auto shiftLeftN = [](const int n){
return [n](vector<int>& ids, int beg, int end){
rotate(begin(ids)+ beg, begin(ids)+ beg + n, begin(ids)+end);
reverse(begin(ids)+ end - n, begin(ids)+ end);
};
};
auto shiftRightN = [](const int n){
return [n](vector<int>& ids, int beg, int end){
auto it = rbegin(ids)+ ids.size() - 1;
rotate(it - end, it - end + n, it - beg);
reverse(begin(ids)+ beg + 1, begin(ids)+ beg + n + 1);
};
};
void reverseMover(vector<int>& ids, int beg, int end){
reverse(begin(ids)+ beg + 1, begin(ids)+ end);
}
template<class Func>
void fastMover(vector<int>& ids, Func func){
cout << fixed << setprecision(4);
vector<function<double(const Index&, const vector<int>&, int, int)>> comparators{compareReverse};
vector<function<void(vector<int>&, int, int)>> movers{reverseMover};
for(int i = 1; i <= maxShift; ++i){
comparators.emplace_back(compareShiftLeftN(i)) ;
comparators.emplace_back(compareShiftRightN(i)) ;
movers.emplace_back(shiftLeftN(i)) ;
movers.emplace_back(shiftRightN(i)) ;
}
Index index(ids);
while(true){
vector<Result> results;
vector<future<Result>> futures;
for(auto& comp : comparators)
 futures.emplace_back(async(func, index, ids, comp)) ;
for(auto& item : futures)
 results.emplace_back(item.get());
auto best = max_element(begin(results), end(results)) - begin(results);
if(results[best].dist < 0.0001)
 return;
movers[best](ids, results[best].beg, results[best].end);
index.Update(ids);
cout << "Score: " << index.getScore() << setw(10)<< -results[best].dist << endl;
}
}
int main(int argc, char** argv){
if(argc < 2){
cerr << "Usage: " << argv[0] << " <input.tour> <output.tour>" << endl;
return 1;
}
auto ids = loadIds(argv[1]);
fastMover(ids, bestPairNeighbors);
saveIds(ids, argv[2]);
return 0;
}
EOF<set_options>
train_ml.loc[train_ml["Sex"] == "male", "Sex"] = 0
train_ml.loc[train_ml["Sex"] == "female", "Sex"] = 1
train_ml["Sex"] = train_ml["Sex"].astype(int)
 train_ml.loc[train_ml["Embarked"] == "S", "Embarked"] = 0
train_ml.loc[train_ml["Embarked"] == "C", "Embarked"] = 1
train_ml.loc[train_ml["Embarked"] == "Q", "Embarked"] = 2
train_ml["Embarked"] = train_ml["Embarked"].astype(int)
 train_ml.head()
!g++ -pthread --std=c++17 -Ofast -o main main.cpp<define_variables>
train_ml['family_members'] = train_ml.SibSp + train_ml.Parch
%%bash
time./main.. /input/gpu-shuffle-numba-cuda/submission_1515558.3917849772.csv./submission.csv<feature_engineering>
total = test.isnull().sum().sort_values(ascending=False)
 percent = round(test.isnull().sum() /test.isnull().count() *100, 1 ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', '%'])
 missing_data.head(5 )
def factorial(n):
return int(scipy.special.gamma(n+1))<define_variables>
test_ml = test.drop(['Name', 'Ticket', 'Cabin'], axis=1)
 mean = test_ml["Age"].mean()
std = test_ml["Age"].std()
is_null = test_ml["Age"].isnull().sum()
rand_age = np.random.randint(mean - std, mean + std, size = is_null)
 age_slice = test_ml["Age"].copy()
age_slice[np.isnan(age_slice)] = rand_age
test_ml["Age"] = age_slice
for k in range(8, 21): print(k, factorial(k))<set_options>
test_ml[test_ml.isnull().any(axis=1)]
test_ml["Fare"] = test_ml["Fare"].fillna(test_ml.groupby('Pclass' ).mean() ['Fare'][3])
 test_ml.info()
!g++ --std=c++17 -Wall -g -Ofast -mtune=native -march=native -fopenmp -DNDEBUG -o dp dp.cc<load_from_csv>
test_ml.loc[test_ml["Sex"] == "male", "Sex"] = 0
test_ml.loc[test_ml["Sex"] == "female", "Sex"] = 1
test_ml["Sex"] = test_ml["Sex"].astype(int)
 test_ml.loc[test_ml["Embarked"] == "S", "Embarked"] = 0
test_ml.loc[test_ml["Embarked"] == "C", "Embarked"] = 1
test_ml.loc[test_ml["Embarked"] == "Q", "Embarked"] = 2
test_ml["Embarked"] = test_ml["Embarked"].astype(int )
%%bash
time./dp 18 <.. /input/not-a-3-and-3-halves-opt/1515562.8360910178.csv > submission.csv<import_modules>
test_ml['age_categories'] = pd.cut(test_ml.Age, [0, 11, 18, 23, 27, 33, 40, 50, np.inf],
labels=[0, 1, 2, 3, 4, 5, 6,7], include_lowest=True, right=False ).astype(int)
 test_ml['fare_categories'] = pd.cut(test_ml.Fare, [0, 8, 14, 31, 99, 250, np.inf],
labels=[0, 1, 2, 3, 4, 5], include_lowest=True, right=False ).astype(int )
import numpy as np
import pandas as pd
import numba
from sympy import isprime, primerange
from math import sqrt
from sklearn.neighbors import KDTree
from tqdm import tqdm_notebook as tqdm
from itertools import combinations, permutations
from functools import lru_cache<load_from_csv>
test_ml['family_members'] = test_ml.SibSp + test_ml.Parch
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv', index_col=['CityId'])
 XY = np.stack(( cities.X.astype(np.float32), cities.Y.astype(np.float32)) , axis=1)
 is_not_prime = np.array([0 if isprime(i)else 1 for i in cities.index], dtype=np.int32 )<compute_test_metric>
X_test = test_final.drop('PassengerId', axis=1 )
@numba.jit('f8(i8, i8, i8)', nopython=True, parallel=False)
 def cities_distance(offset, id_from, id_to):
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 if offset % 10 == 9 and is_not_prime[id_from]:
return 1.1 * distance
return distance
@numba.jit('f8(i4, i8[:])', nopython=True, parallel=False)
 def score_chunk(offset, chunk):
pure_distance, penalty = 0.0, 0.0
penalty_modulo = 9 - offset % 10
for path_index in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[path_index], chunk[path_index+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if path_index % 10 == penalty_modulo and is_not_prime[id_from]:
penalty += distance
return pure_distance + 0.1 * penalty
@numba.jit('f8(i8[:])', nopython=True, parallel=False)
 def score_path(path):
return score_chunk(0, path)
 @numba.jit
def chunk_scores(chunk):
scores = np.zeros(10)
 pure_distance = 0
for i in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[i], chunk[i+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if is_not_prime[id_from]:
scores[9-i%10] += distance
scores *= 0.1
scores += pure_distance
return scores<compute_train_metric>
X_train = train_ml[['Pclass', 'Sex', 'Embarked', 'age_categories', 'fare_categories', 'family_members']]
Y_train = train_ml.Survived
@numba.jit('f8(i8, i8, i8[:], i8[:], i8[:], i8, f8[:,:], i8[:])', nopython=True, parallel=False)
 def score_compound_chunk(offset, head, firsts, lasts, lens, tail, scores, indexes):
score = 0.0
last_city_id = head
for i in numba.prange(len(indexes)) :
index = indexes[i]
first, last, chunk_len = firsts[index], lasts[index], lens[index]
score += cities_distance(offset, last_city_id, first)
 score += scores[index,(offset + 1)% 10]
last_city_id = last
offset += chunk_len
return score + cities_distance(offset, last_city_id, tail)
 @numba.jit('i8(i8, i8, i8[:], i8[:], i8[:], i8, f8[:,:], i8[:,:], f8)', nopython=True, parallel=False)
 def best_score_permutation_index(offset, head, firsts, lasts, lens, tail, scores, indexes, best_score):
best_index = -1
for i in numba.prange(len(indexes)) :
score = score_compound_chunk(offset, head, firsts, lasts, lens, tail, scores, indexes[i])
 if score < best_score:
best_index, best_score = i, score
return best_index<statistical_test>
log_reg = LogisticRegression()
log_reg.fit(X_train, Y_train)
 log_reg_acc = round(cross_val_score(log_reg, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = log_reg.predict(X_test )
kdt = KDTree(XY)
 fives = set()
for i in tqdm(cities.index):
dists, neibs = kdt.query([XY[i]], 9)
 for comb in combinations(neibs[0], 5):
if all(comb):
fives.add(tuple(sorted(comb)))
 neibs = kdt.query_radius([XY[i]], 10, count_only=False, return_distance=False)
 for comb in combinations(neibs[0], 5):
if all(comb):
fives.add(tuple(sorted(comb)))
 print(f'{len(fives)} cities fives are selected.')
 @numba.jit('f8(i8[:])', nopython=True, parallel=False)
 def sum_distance(ids):
res = 0
for i in numba.prange(len(ids)) :
for j in numba.prange(i + 1, len(ids)) :
res += cities_distance(0, ids[i], ids[j])
 return res
fives = np.array(list(fives))
distances = np.array(list(map(sum_distance, tqdm(fives))))
order = distances.argsort()
fives = fives[order]<load_from_csv>
knn = KNeighborsClassifier()
knn.fit(X_train, Y_train)
 knn_acc = round(cross_val_score(knn, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = knn.predict(X_test )
path = np.array(pd.read_csv('.. /input/shame-on-me/1515656.5970491648.csv' ).Path )<statistical_test>
gaussian_nb = GaussianNB()
gaussian_nb.fit(X_train, Y_train)
 gaussian_nb_acc = round(cross_val_score(gaussian_nb, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = gaussian_nb.predict(X_test )
@lru_cache(maxsize=None)
 def indexes_permutations(n):
return np.array(list(map(list, permutations(range(n)))))
 path_index = np.argsort(path[:-1])
 print(f'Total score is {score_path(path):.2f}.')
 for _ in range(2):
for ids in tqdm(fives[:2 * 10**6]):
i1, i2, i3, i4, i5 = np.sort(path_index[ids])
 head, tail = path[i1-1], path[i5+1]
chunks = [path[i1:i1+1], path[i1+1:i2], path[i2:i2+1], path[i2+1:i3],
path[i3:i3+1], path[i3+1:i4], path[i4:i4+1], path[i4+1:i5], path[i5:i5+1]]
chunks = [chunk for chunk in chunks if len(chunk)]
scores = np.array([chunk_scores(chunk)for chunk in chunks])
 lens = np.array([len(chunk)for chunk in chunks])
 firsts = np.array([chunk[0] for chunk in chunks])
 lasts = np.array([chunk[-1] for chunk in chunks])
 best_score = score_compound_chunk(i1-1, head, firsts, lasts, lens, tail, scores, indexes_permutations(len(chunks)) [0])
 index = best_score_permutation_index(i1-1, head, firsts, lasts, lens, tail, scores, indexes_permutations(len(chunks)) , best_score)
 if index > 0:
perm = [chunks[i] for i in indexes_permutations(len(chunks)) [index]]
path[i1-1:i5+2] = np.concatenate([[head], np.concatenate(perm), [tail]])
 path_index = np.argsort(path[:-1])
 print(f'New total score is {score_path(path):.3f}.Permutating path at indexes {i1}, {i2}, {i3}, {i4}, {i5}.' )<save_to_csv>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 decision_tree_acc = round(cross_val_score(decision_tree, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = decision_tree.predict(X_test )
def make_submission(name, path):
pd.DataFrame({'Path': path} ).to_csv(f'{name}.csv', index=False)
 make_submission(score_path(path), path )<import_modules>
random_forest = RandomForestClassifier()
random_forest.fit(X_train, Y_train)
 random_forest_acc = round(cross_val_score(random_forest, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = random_forest.predict(X_test )
import numpy as np
import pandas as pd
import numba
from sympy import isprime, primerange
from math import sqrt
from sklearn.neighbors import KDTree
from tqdm import tqdm
from itertools import combinations<load_from_csv>
model_score = pd.DataFrame({'Model': ['Logistic regression', 'K-nearest Neighbors', 'Gaussian Naïve Bayes', 'Decision Tree', 'Random Forest'],
'Score': [log_reg_acc, knn_acc, gaussian_nb_acc, decision_tree_acc, random_forest_acc]})
 model_score.sort_values(by='Score', ascending=False ).reset_index().drop('index', axis=1 )
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv', index_col=['CityId'])
 XY = np.stack(( cities.X.astype(np.float32), cities.Y.astype(np.float32)) , axis=1)
 is_not_prime = np.array([0 if isprime(i)else 1 for i in cities.index], dtype=np.int32 )<compute_test_metric>
feature_importance = pd.DataFrame({'Feature':X_train.columns,'Importance':np.round(random_forest.feature_importances_,3)})
 feature_importance.sort_values('Importance',ascending=False ).reset_index().drop('index', axis=1 )
@numba.jit('f8(i8, i8, i8)', nopython=True, parallel=False)
 def cities_distance(offset, id_from, id_to):
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 if offset % 10 == 9 and is_not_prime[id_from]:
return 1.1 * distance
return distance
@numba.jit('f8(i4, i8[:])', nopython=True, parallel=False)
 def score_chunk(offset, chunk):
pure_distance, penalty = 0.0, 0.0
penalty_modulo = 9 - offset % 10
for path_index in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[path_index], chunk[path_index+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if path_index % 10 == penalty_modulo and is_not_prime[id_from]:
penalty += distance
return pure_distance + 0.1 * penalty
@numba.jit('f8(i8[:])', nopython=True, parallel=False)
 def score_path(path):
return score_chunk(0, path )<choose_model_class>
random_forest.get_params()
kdt = KDTree(XY )<load_from_csv>
n_estimators = [int(x)for x in np.linspace(start = 100, stop = 1000, num = 10)]
max_features = ['auto', 'sqrt', 'log2']
max_depth = [int(x)for x in np.linspace(10, 110, num = 11)]
max_depth.append(None)
 min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]
random_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
random_grid
path = np.array(pd.read_csv('.. /input/traveling-santa-lkh-solution/pure1502650.csv' ).Path)
 initial_score = score_path(path )<compute_test_metric>
random_forest_random = RandomizedSearchCV(estimator = random_forest, param_distributions = random_grid, n_iter = 100, verbose=2, cv = 3, random_state=42, n_jobs = -1)
 random_forest_random.fit(X_train, Y_train )
path_index = np.argsort(path[:-1])
 total_score = initial_score
print(f'Total score is {total_score:.2f}.')
 for _ in range(3):
for step,(id1, id2)in enumerate(tqdm(pairs), 1):
if step % 10**6 == 0:
new_total_score = score_path(path)
 print(f'Score: {new_total_score:.2f}; improvement over last 10^6 steps: {total_score - new_total_score:.2f}; total improvement: {initial_score - new_total_score:.2f}.')
 total_score = new_total_score
i, j = path_index[id1], path_index[id2]
i, j = min(i, j), max(i, j)
 chunk, reversed_chunk = path[i-1:j+2], np.concatenate([path[i-1:i], path[j:i-1:-1], path[j+1:j+2]])
 chunk_score, reversed_chunk_score = score_chunk(i-1, chunk), score_chunk(i-1, reversed_chunk)
 if j - i > 2:
chunk_abc = np.concatenate([path[i-1:i+1], path[j:i:-1], path[j+1:j+2]])
 chunk_acb = np.concatenate([path[i-1:i], path[j:j+1], path[i:j], path[j+1:j+2]])
 chunk_abcb = np.concatenate([path[i-1:i+1], path[j:j+1], path[i+1:j], path[j+1:j+2]])
 abc_score, acb_score, abcb_score = map(lambda chunk: score_chunk(i-1, chunk), [chunk_abc, chunk_acb, chunk_abcb])
 for chunk, score, name in zip(( chunk_abc, chunk_acb, chunk_abcb),(abc_score, acb_score, abcb_score),('abc', 'acb', 'abcb')) :
if score < chunk_score:
path[i-1:j+2] = chunk
path_index = np.argsort(path[:-1])
 chunk_score = score
if reversed_chunk_score < chunk_score:
path[i-1:j+2] = reversed_chunk
path_index = np.argsort(path[:-1] )<compute_test_metric>
random_forest_random.best_params_
print(f'Total improvement is {initial_score - total_score:.2f}.' )<save_to_csv>
random_forest_2 = random_forest_random.best_estimator_
random_forest_2.fit(X_train, Y_train)
 random_forest_acc_2 = round(cross_val_score(random_forest_2, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 model_score = model_score.append(pd.DataFrame({'Model': ['Random Forest 2'], 'Score': [random_forest_acc_2]}))
model_score.sort_values(by='Score', ascending=False ).reset_index().drop('index', axis=1 )
def make_submission(name, path):
pd.DataFrame({'Path': path} ).to_csv(f'{name}.csv', index=False )<compute_test_metric>
param_grid = {
'bootstrap': [True],
'max_depth': [20, 40, 60, 80, 100],
'max_features': ['log2'],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [4, 5, 6],
'n_estimators': [100, 200, 300, 700, 1000]
}
random_forest_grid_search = GridSearchCV(estimator = random_forest, param_grid = param_grid, cv = 3, n_jobs = -1, verbose = 2)
 random_forest_grid_search.fit(X_train, Y_train )
make_submission(score_path(path), path )<import_modules>
random_forest_grid_search.best_params_
import numpy as np
import pandas as pd
import numba
from sympy import isprime, primerange
from math import sqrt
from sklearn.neighbors import KDTree
from tqdm import tqdm
from itertools import combinations<load_from_csv>
random_forest_3 = random_forest_grid_search.best_estimator_
random_forest_3.fit(X_train, Y_train)
 random_forest_acc_3 = round(cross_val_score(random_forest_3, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 model_score = model_score.append(pd.DataFrame({'Model': ['Random Forest 3'], 'Score': [random_forest_acc_3]}))
model_score.sort_values(by='Score', ascending=False ).reset_index().drop('index', axis=1 )
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv', index_col=['CityId'])
 XY = np.stack(( cities.X.astype(np.float32), cities.Y.astype(np.float32)) , axis=1)
 is_not_prime = np.array([0 if isprime(i)else 1 for i in cities.index], dtype=np.int32 )<compute_test_metric>
predictions = cross_val_predict(random_forest_3, X_train, Y_train, cv=3)
 confusion_matrix(Y_train, predictions )
@numba.jit('f8(i8, i8, i8)', nopython=True, parallel=False)
 def cities_distance(offset, id_from, id_to):
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 if offset % 10 == 9 and is_not_prime[id_from]:
return 1.1 * distance
return distance
@numba.jit('f8(i4, i8[:])', nopython=True, parallel=False)
 def score_chunk(offset, chunk):
pure_distance, penalty = 0.0, 0.0
penalty_modulo = 9 - offset % 10
for path_index in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[path_index], chunk[path_index+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if path_index % 10 == penalty_modulo and is_not_prime[id_from]:
penalty += distance
return pure_distance + 0.1 * penalty
@numba.jit('f8(i8[:])', nopython=True, parallel=False)
 def score_path(path):
return score_chunk(0, path )<choose_model_class>
print("Precision:", precision_score(Y_train, predictions))
print("Recall:",recall_score(Y_train, predictions))
kdt = KDTree(XY )<load_from_csv>
y_scores = random_forest_3.predict_proba(X_train)
 y_scores = y_scores[:,1]
r_a_score = roc_auc_score(Y_train, y_scores)
 print("ROC-AUC-Score:", r_a_score )
path = np.array(pd.read_csv('.. /input/gpu-shuffle-numba-cuda/submission.csv' ).Path)
 initial_score = score_path(path )<compute_test_metric>
final_predictions = random_forest_3.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_final.PassengerId, 'Survived': final_predictions})
 output.to_csv('random_forest_model.csv', index=False )