Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<drop_column>
%matplotlib inline
warnings.filterwarnings("ignore" )
app_train_domain = app_train_domain.drop('SK_ID_PREV_x',axis=1)
 app_test_domain = app_test_domain.drop('SK_ID_PREV_x',axis=1 )<drop_column>
def encode_data(df):
df['idhogar'] = LabelEncoder().fit_transform(df['idhogar'])
 def feature_importance(forest, X_train):
ranked_list = []
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
print("Feature ranking:")
 for f in range(X_train.shape[1]):
print("%d.feature %d(%f)" %(f + 1, indices[f], importances[indices[f]])+ " - " + X_train.columns[indices[f]])
 ranked_list.append(X_train.columns[indices[f]])
 return ranked_list
app_train_domain = app_train_domain.drop('SK_ID_PREV_y',axis=1)
 app_test_domain = app_test_domain.drop('SK_ID_PREV_y',axis=1 )<train_model>
def do_features(df):
feats_div = [('children_fraction', 'r4t1', 'r4t3'), 
('working_man_fraction', 'r4h2', 'r4t3'), 
('all_man_fraction', 'r4h3', 'r4t3'), 
('human_density', 'tamviv', 'rooms'), 
('human_bed_density', 'tamviv', 'bedrooms'), 
('rent_per_person', 'v2a1', 'r4t3'), 
('rent_per_room', 'v2a1', 'rooms'), 
('mobile_density', 'qmobilephone', 'r4t3'), 
('tablet_density', 'v18q1', 'r4t3'), 
('mobile_adult_density', 'qmobilephone', 'r4t2'), 
('tablet_adult_density', 'v18q1', 'r4t2'),
]
feats_sub = [('people_not_living', 'tamhog', 'tamviv'), 
('people_weird_stat', 'tamhog', 'r4t3')]
for f_new, f1, f2 in feats_div:
df['fe_' + f_new] =(df[f1] / df[f2] ).astype(np.float32)
 for f_new, f1, f2 in feats_sub:
df['fe_' + f_new] =(df[f1] - df[f2] ).astype(np.float32)
 aggs_num = {'age': ['min', 'max', 'mean'],
'escolari': ['min', 'max', 'mean']
}
aggs_cat = {'dis': ['mean']}
for s_ in ['estadocivil', 'parentesco', 'instlevel']:
for f_ in [f_ for f_ in df.columns if f_.startswith(s_)]:
aggs_cat[f_] = ['mean', 'count']
for name_, df_ in [('18', df.query('age >= 18')) ]:
df_agg = df_.groupby('idhogar' ).agg({**aggs_num, **aggs_cat} ).astype(np.float32)
 df_agg.columns = pd.Index(['agg' + name_ + '_' + e[0] + "_" + e[1].upper() for e in df_agg.columns.tolist() ])
 df = df.join(df_agg, how='left', on='idhogar')
 del df_agg
df.drop(['Id'], axis=1, inplace=True)
 return df
def model(features, test_features, encoding = 'ohe', n_folds =5):

 train_ids = features['SK_ID_CURR']
test_ids = test_features['SK_ID_CURR']
labels = features['TARGET']
features = features.drop(columns = ['SK_ID_CURR', 'TARGET'])
 test_features = test_features.drop(columns = ['SK_ID_CURR'])
 if encoding == 'ohe':
features = pd.get_dummies(features)
 test_features = pd.get_dummies(test_features)
 features, test_features = features.align(test_features, join = 'inner', axis = 1)
 cat_indices = 'auto'
elif encoding == 'le':
label_encoder = LabelEncoder()
cat_indices = []
for i, col in enumerate(features):
if features[col].dtype == 'object':
features[col] = label_encoder.fit_transform(np.array(features[col].astype(str)).reshape(( -1,)))
 test_features[col] = label_encoder.transform(np.array(test_features[col].astype(str)).reshape(( -1,)))
 cat_indices.append(i)
 else:
raise ValueError("Encoding must be either 'ohe' or 'le'")
 print('Training Data Shape: ', features.shape)
 print('Testing Data Shape: ', test_features.shape)
 feature_names = list(features.columns)
 features = np.array(features)
 test_features = np.array(test_features)
 k_fold = KFold(n_splits = n_folds, shuffle = True, random_state = 50)
 feature_importance_values = np.zeros(len(feature_names))
test_predictions = np.zeros(test_features.shape[0])
 out_of_fold = np.zeros(features.shape[0])
 valid_scores = []
train_scores = []
for train_indices, valid_indices in k_fold.split(features):
train_features, train_labels = features[train_indices], labels[train_indices]
valid_features, valid_labels = features[valid_indices], labels[valid_indices]
model = lgb.LGBMClassifier(n_estimators=10000, nthread=4,objective = 'binary',
class_weight = 'balanced', learning_rate = 0.015,
reg_alpha = 0.041545473, reg_lambda = 0.1,
n_jobs = -1, random_state = 50,num_leaves=32,colsample_bytree=.9497036,subsample=.8715623,
max_depth=5,min_split_gain=.0222415,min_child_weight=39.3259775,max_bin=200,num_boost_round=3000,min_data_in_leaf=100,bagging_fraction=0.5,bagging_freq=10)
 model.fit(train_features, train_labels, eval_metric = 'auc',
eval_set = [(valid_features, valid_labels),(train_features, train_labels)],
eval_names = ['valid', 'train'], categorical_feature = cat_indices,
early_stopping_rounds = 100, verbose = 200)
 best_iteration = model.best_iteration_
feature_importance_values += model.feature_importances_ / k_fold.n_splits
test_predictions += model.predict_proba(test_features, num_iteration = best_iteration)[:, 1] / k_fold.n_splits
out_of_fold[valid_indices] = model.predict_proba(valid_features, num_iteration = best_iteration)[:, 1]
valid_score = model.best_score_['valid']['auc']
train_score = model.best_score_['train']['auc']
valid_scores.append(valid_score)
 train_scores.append(train_score)
 gc.enable()
del model, train_features, valid_features
gc.collect()
submission = pd.DataFrame({'SK_ID_CURR': test_ids, 'TARGET': test_predictions})
 feature_importances = pd.DataFrame({'feature': feature_names, 'importance': feature_importance_values})
 valid_auc = roc_auc_score(labels, out_of_fold)
 valid_scores.append(valid_auc)
 train_scores.append(np.mean(train_scores))
fold_names = list(range(n_folds))
fold_names.append('overall')
 metrics = pd.DataFrame({'fold': fold_names,
'train': train_scores,
'valid': valid_scores})
 return submission, feature_importances, metrics<train_model>
def convert_OHE2LE(df):
tmp_df = df.copy(deep=True)
 for s_ in ['pared', 'piso', 'techo', 'abastagua', 'sanitario', 'energcocinar', 'elimbasu',
'epared', 'etecho', 'eviv', 'estadocivil', 'parentesco',
'instlevel', 'lugar', 'tipovivi',
'manual_elec']:
if 'manual_' not in s_:
cols_s_ = [f_ for f_ in df.columns if f_.startswith(s_)]
elif 'elec' in s_:
cols_s_ = ['public', 'planpri', 'noelec', 'coopele']
sum_ohe = tmp_df[cols_s_].sum(axis=1 ).unique()
if 0 in sum_ohe:
print('The OHE in {} is incomplete.A new column will be added before label encoding' 
.format(s_))
col_dummy = s_+'_dummy'
tmp_df[col_dummy] =(tmp_df[cols_s_].sum(axis=1)== 0 ).astype(np.int8)
 cols_s_.append(col_dummy)
 sum_ohe = tmp_df[cols_s_].sum(axis=1 ).unique()
if 0 in sum_ohe:
print("The category completion did not work")
 tmp_cat = tmp_df[cols_s_].idxmax(axis=1)
 tmp_df[s_ + '_LE'] = LabelEncoder().fit_transform(tmp_cat ).astype(np.int16)
 if 'parentesco1' in cols_s_:
cols_s_.remove('parentesco1')
 tmp_df.drop(cols_s_, axis=1, inplace=True)
 return tmp_df
submission, fi, metrics = model(app_train_domain, app_test_domain)
 print('Baseline metrics')
 print(metrics )<save_to_csv>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 test_ids = test.Id
submission.to_csv('second_sub.csv', index = False )<set_options>
train['dependency'] = np.sqrt(train['SQBdependency'])
 test['dependency'] = np.sqrt(test['SQBdependency'] )
%matplotlib inline<load_from_csv>
def process_df(df_):
encode_data(df_)
 return do_features(df_)
 train = process_df(train)
 test = process_df(test )
monster_train = pd.read_csv('.. /input/train.csv', index_col='id')
 monster_test = pd.read_csv('.. /input/test.csv', index_col='id' )<randomize_order>
train.loc[train['edjefa'] == "no", "edjefa"] = 0
train.loc[train['edjefa'] == "yes", "edjefa"] = 1
train['edjefa'] = train['edjefa'].astype("int")
 train.loc[train['edjefe'] == "no", "edjefe"] = 0
train.loc[train['edjefe'] == "yes", "edjefe"] = 1
train['edjefe'] = train['edjefe'].astype("int")
 test.loc[test['edjefa'] == "no", "edjefa"] = 0
test.loc[test['edjefa'] == "yes", "edjefa"] = 4
test['edjefa'] = test['edjefa'].astype("int")
 test.loc[test['edjefe'] == "no", "edjefe"] = 0
test.loc[test['edjefe'] == "yes", "edjefe"] = 4
test['edjefe'] = test['edjefe'].astype("int")
 train['edjef'] = np.max(train[['edjefa','edjefe']], axis=1)
 test['edjef'] = np.max(test[['edjefa','edjefe']], axis=1)
 train['v2a1']=train['v2a1'].fillna(0)
 test['v2a1']=test['v2a1'].fillna(0)
 test['v18q1']=test['v18q1'].fillna(0)
 train['v18q1']=train['v18q1'].fillna(0)
 train['rez_esc']=train['rez_esc'].fillna(0)
 test['rez_esc']=test['rez_esc'].fillna(0)
 train.loc[train.meaneduc.isnull() , "meaneduc"] = 0
train.loc[train.SQBmeaned.isnull() , "SQBmeaned"] = 0
test.loc[test.meaneduc.isnull() , "meaneduc"] = 0
test.loc[test.SQBmeaned.isnull() , "SQBmeaned"] = 0
train.loc[(train.v14a == 1)&(train.sanitario1 == 1)&(train.abastaguano == 0), "v14a"] = 0
train.loc[(train.v14a == 1)&(train.sanitario1 == 1)&(train.abastaguano == 0), "sanitario1"] = 0
test.loc[(test.v14a == 1)&(test.sanitario1 == 1)&(test.abastaguano == 0), "v14a"] = 0
test.loc[(test.v14a == 1)&(test.sanitario1 == 1)&(test.abastaguano == 0), "sanitario1"] = 0
msk = np.random.rand(len(monster_train)) < 0.8
monster_train_A = monster_train[msk]
monster_train_B = monster_train[~msk]
print('%d monsters in training subset A' % len(monster_train_A))
print('%d monsters in training subset B' % len(monster_train_B))<predict_on_test>
def train_test_apply_func(train_, test_, func_):
test_['Target'] = 0
xx = pd.concat([train_, test_])
 xx_func = func_(xx)
 train_ = xx_func.iloc[:train_.shape[0], :]
test_ = xx_func.iloc[train_.shape[0]:, :].drop('Target', axis=1)
 del xx, xx_func
return train_, test_
features = ['bone_length', 'rotting_flesh', 'hair_length', 'has_soul']
def how_accurate(classifier, n=5):
accuracy = []
for i in range(n):
classifier.fit(monster_train_A[features], monster_train_A['type'])
 preds = classifier.predict(monster_train_B[features])
 accuracy.append(np.where(preds==monster_train_B['type'], 1, 0 ).sum() / float(len(monster_train_B)))
 return np.mean(accuracy )<import_modules>
train, test = train_test_apply_func(train, test, convert_OHE2LE )
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.neural_network import MLPClassifier<choose_model_class>
cols_2_ohe = ['eviv_LE', 'etecho_LE', 'epared_LE', 'elimbasu_LE',
'energcocinar_LE', 'sanitario_LE', 'manual_elec_LE',
'pared_LE']
cols_nums = ['age', 'meaneduc', 'dependency',
'hogar_nin', 'hogar_adul', 'hogar_mayor', 'hogar_total',
'bedrooms', 'overcrowding']
def convert_geo2aggs(df_):
tmp_df = pd.concat([df_[(['lugar_LE', 'idhogar']+cols_nums)],
pd.get_dummies(df_[cols_2_ohe],
columns=cols_2_ohe)],axis=1)
 geo_agg = tmp_df.groupby(['lugar_LE','idhogar'] ).mean().groupby('lugar_LE' ).mean().astype(np.float32)
 geo_agg.columns = pd.Index(['geo_' + e for e in geo_agg.columns.tolist() ])
 del tmp_df
return df_.join(geo_agg, how='left', on='lugar_LE')
 train, test = train_test_apply_func(train, test, convert_geo2aggs )
knn_clf = KNeighborsClassifier(n_neighbors=6)
 tree_clf = DecisionTreeClassifier(max_depth=5)
 forest_clf = RandomForestClassifier(max_depth=2, n_estimators=100, max_features=1)
 gaussian_clf = GaussianProcessClassifier(4.0 * RBF(1.0), warm_start=True, n_jobs=-1)
 neural_clf = MLPClassifier(alpha=1, max_iter=400)
 clfs = {'K Nearest Neighbors': knn_clf,
'Decision Tree': tree_clf,
'Random Forest': forest_clf,
'Gassian Process': gaussian_clf,
'Neural Network': neural_clf}<compute_test_metric>
train['num_over_18'] = 0
train['num_over_18'] = train[train.age >= 18].groupby('idhogar' ).transform("count")
 train['num_over_18'] = train.groupby("idhogar")["num_over_18"].transform("max")
 train['num_over_18'] = train['num_over_18'].fillna(0)
 test['num_over_18'] = 0
test['num_over_18'] = test[test.age >= 18].groupby('idhogar' ).transform("count")
 test['num_over_18'] = test.groupby("idhogar")["num_over_18"].transform("max")
 test['num_over_18'] = test['num_over_18'].fillna(0)
 def extract_features(df):
df['bedrooms_to_rooms'] = df['bedrooms']/df['rooms']
df['rent_to_rooms'] = df['v2a1']/df['rooms']
df['tamhog_to_rooms'] = df['tamhog']/df['rooms']
df['r4t3_to_tamhog'] = df['r4t3']/df['tamhog']
df['r4t3_to_rooms'] = df['r4t3']/df['rooms']
df['v2a1_to_r4t3'] = df['v2a1']/df['r4t3']
df['v2a1_to_r4t3'] = df['v2a1']/(df['r4t3'] - df['r4t1'])
 df['hhsize_to_rooms'] = df['hhsize']/df['rooms']
df['rent_to_hhsize'] = df['v2a1']/df['hhsize']
df['rent_to_over_18'] = df['v2a1']/df['num_over_18']
df.loc[df.num_over_18 == 0, "rent_to_over_18"] = df[df.num_over_18 == 0].v2a1
df['dependency_yes'] = df['dependency'].apply(lambda x: 1 if x == 'yes' else 0)
 df['dependency_no'] = df['dependency'].apply(lambda x: 1 if x == 'no' else 0)
 extract_features(train)
 extract_features(test )
accuracy = {clf: how_accurate(clfs[clf], n=20)for clf in clfs}<define_variables>
X = train.query('parentesco1==1')
 y = X['Target'] - 1
X = X.drop(['Target'], axis=1)
 np.random.seed(seed=None)
 train2 = X.copy()
train_hhs = train2.idhogar
households = train2.idhogar.unique()
cv_hhs = np.random.choice(households, size=int(len(households)* 0.1), replace=False)
 cv_idx = np.isin(train2.idhogar, cv_hhs)
 X_test = train2[cv_idx]
y_test = y[cv_idx]
X_train = train2[~cv_idx]
y_train = y[~cv_idx]
upper_quantile = 0.98
lower_quantile =.02<feature_engineering>
extra_drop_features = ['mobilephone',
'hogar_total',
'agesq',
'hacapo',
'tamhog',
'agg18_estadocivil3_COUNT',
'agg18_estadocivil4_COUNT',
'v14a',
'agg18_estadocivil1_MEAN',
'fe_people_weird_stat',
'num_over_18',
'r4t3_to_tamhog',
'agg18_parentesco4_COUNT',
'agg18_estadocivil5_COUNT',
'agg18_parentesco7_COUNT',
'agg18_parentesco2_COUNT',
'agg18_parentesco5_COUNT',
'agg18_parentesco1_COUNT',
'agg18_parentesco12_COUNT',
'agg18_parentesco11_COUNT',
'agg18_parentesco10_COUNT',
'agg18_instlevel9_COUNT',
'agg18_parentesco6_COUNT',
'agg18_instlevel8_COUNT',
'agg18_instlevel7_COUNT',
'agg18_instlevel6_COUNT',
'agg18_estadocivil6_COUNT',
'agg18_parentesco8_COUNT',
'agg18_instlevel5_COUNT',
'agg18_instlevel4_COUNT',
'agg18_parentesco9_COUNT',
'agg18_instlevel3_COUNT',
'agg18_instlevel2_COUNT',
'dependency_yes',
'agg18_instlevel1_COUNT',
'agg18_parentesco3_COUNT',
'agg18_estadocivil7_COUNT',
'dependency_no'] + ['male', 'escolari', 'v18q', 'parentesco1'] + ['female', 'overcrowding', 'agg18_estadocivil2_COUNT'] + [
'hacdor',
'instlevel_LE',
'parentesco_LE',
'age',
'estadocivil_LE',
'dis',
'abastagua_LE',
'hhsize',
'v18q1',
'agg18_parentesco12_MEAN',
'rez_esc',
'refrig',
'agg18_parentesco11_MEAN',
'agg18_instlevel9_MEAN',
'agg18_estadocivil6_MEAN',
'agg18_instlevel6_MEAN',
'hogar_mayor',
'SQBage'] + [
'geo_pared_LE_0',
'geo_pared_LE_5',
'geo_manual_elec_LE_3',
'geo_energcocinar_LE_2',
'geo_hogar_mayor',
'geo_energcocinar_LE_0',
'geo_pared_LE_7',
'geo_pared_LE_6',
'agg18_parentesco10_MEAN',
'geo_energcocinar_LE_1',
'geo_pared_LE_3',
'geo_elimbasu_LE_4',
'geo_epared_LE_2',
'agg18_parentesco8_MEAN',
'geo_epared_LE_0',
'geo_etecho_LE_2',
'agg18_parentesco6_MEAN',
'agg18_parentesco4_MEAN',
'geo_manual_elec_LE_2'] + ['geo_sanitario_LE_3',
'geo_eviv_LE_0',
'agg18_parentesco7_MEAN',
'geo_manual_elec_LE_4',
'geo_pared_LE_2',
'geo_sanitario_LE_0',
'geo_pared_LE_4',
'geo_hogar_nin',
'geo_elimbasu_LE_2']
for col in monster_train_A.columns.tolist() [:4]:
upper_limit = monster_train_A[col].quantile(upper_quantile)
 lower_limit = monster_train_A[col].quantile(lower_quantile)
 monster_train_A.loc[monster_train_A[col]>upper_limit, col] = np.nan
monster_train_A.loc[monster_train_A[col]<lower_limit, col] = np.nan
monster_train_A = monster_train_A.dropna()<set_options>
lgb_drop_cols = extra_drop_features + ["idhogar"]
try:
X_train.drop(lgb_drop_cols, axis=1, inplace=True)
 X_test.drop(lgb_drop_cols, axis=1, inplace=True)
 except:
print("Error dropping columns" )
%matplotlib inline 
<load_from_csv>
opt_parameters = {'colsample_bytree': 0.93, 'min_child_samples': 56, 'num_leaves': 19, 'subsample': 0.84}
def evaluate_macroF1_lgb(truth, predictions):
pred_labels = predictions.reshape(len(np.unique(truth)) ,-1 ).argmax(axis=0)
 f1 = f1_score(truth, pred_labels, average='macro')
 return('macroF1', f1, True)
 fit_params={"early_stopping_rounds":300,
"eval_metric" : evaluate_macroF1_lgb,
"eval_set" : [(X_train,y_train),(X_test,y_test)],
'eval_names': ['train', 'valid'],
'verbose': False,
'categorical_feature': 'auto'}
def learning_rate_power_0997(current_iter):
base_learning_rate = 0.1
min_learning_rate = 0.02
lr = base_learning_rate * np.power (.995, current_iter)
 return max(lr, min_learning_rate)
 fit_params['verbose'] = 200
fit_params['callbacks'] = [lgb.reset_parameter(learning_rate=learning_rate_power_0997)]
df_train = pd.read_csv('.. /input/train.csv')
 df_train['dataset'] = 'train'
df_test = pd.read_csv('.. /input/test.csv')
 df_test['dataset'] = 'test'
test_len = len(df_test)
 df = pd.concat([df_train, df_test], axis=0, ignore_index=True, sort=False)
 df_len = len(df )<categorify>
def _parallel_fit_estimator(estimator, X, y, sample_weight=None, **fit_params):

 if sample_weight is not None:
estimator.fit(X, y, sample_weight=sample_weight, **fit_params)
 else:
estimator.fit(X, y, **fit_params)
 return estimator
class VotingClassifierLGBM(VotingClassifier):

 def fit(self, X, y, sample_weight=None, **fit_params):
if isinstance(y, np.ndarray)and len(y.shape)> 1 and y.shape[1] > 1:
raise NotImplementedError('Multilabel and multi-output'
' classification is not supported.')
 if self.voting not in('soft', 'hard'):
raise ValueError("Voting must be 'soft' or 'hard'; got(voting=%r)"
% self.voting)
 if self.estimators is None or len(self.estimators)== 0:
raise AttributeError('Invalid `estimators` attribute, `estimators`'
' should be a list of(string, estimator)'
' tuples')
 if(self.weights is not None and
len(self.weights)!= len(self.estimators)) :
raise ValueError('Number of classifiers and weights must be equal'
'; got %d weights, %d estimators'
%(len(self.weights), len(self.estimators)))
 if sample_weight is not None:
for name, step in self.estimators:
if not has_fit_parameter(step, 'sample_weight'):
raise ValueError('Underlying estimator '%s' does not'
' support sample weights.' % name)
 names, clfs = zip(*self.estimators)
 self._validate_names(names)
 n_isnone = np.sum([clf is None for _, clf in self.estimators])
 if n_isnone == len(self.estimators):
raise ValueError('All estimators are None.At least one is '
'required to be a classifier!')
 self.le_ = LabelEncoder().fit(y)
 self.classes_ = self.le_.classes_
self.estimators_ = []
transformed_y = self.le_.transform(y)
 self.estimators_ = Parallel(n_jobs=self.n_jobs )(
 delayed(_parallel_fit_estimator )(clone(clf), X, transformed_y,
sample_weight=sample_weight, **fit_params)
 for clf in clfs if clf is not None)
 return self
def get_xyz(filename):
row = []
xyz = []
lattice = []
with open(filename)as f:
for line in f.readlines() :
row = line.split()
if row[0] == 'atom':
xyz.append(( np.array(row[1:4], dtype=np.float), row[4]))
elif row[0] == 'lattice_vector':
lattice.append(np.array(row[1:4], dtype=np.float))
return xyz, lattice<compute_test_metric>
clfs = []
for i in range(10):
clf = lgb.LGBMClassifier(max_depth=-1, learning_rate=0.1, objective='multiclass',
random_state=314+i, silent=True, metric='None',
n_jobs=4, n_estimators=5000, class_weight='balanced')
 clf.set_params(**opt_parameters)
 clfs.append(( 'lgbm{}'.format(i), clf))
vc = VotingClassifierLGBM(clfs, voting='soft')
 del clfs
_ = vc.fit(X_train, y_train, **fit_params)
 clf_final = vc.estimators_[0]
def get_sine_matrix(xyz, lattice):
n_atom = len(xyz)
 distance_matrix = np.ones(( n_atom, n_atom))
A = np.transpose(lattice)
 B = LA.inv(A)
 for i in range(n_atom):
for j in range(i):
r_ij = np.dot(B, xyz[i][0] - xyz[j][0])
 sin_sq_r =(np.sin(np.pi * r_ij)) **2
distance = LA.norm(np.dot(A, sin_sq_r))
distance_matrix[i, j], distance_matrix[j, i] = distance, distance
labels = np.transpose(xyz)[1]
labels = labels.reshape(-1,1)
 for at, charge in zip(['O', 'Al', 'Ga', 'In'], [8, 13, 31, 49]):
labels = np.where(labels==at, charge, labels)
 charge_matrix = np.dot(labels, np.transpose(labels)).astype(np.float)
 charge_matrix -= np.diag(np.diag(charge_matrix))
charge_matrix += np.diag(0.5 * labels**2.4 ).astype(float)
 sine_matrix = charge_matrix / distance_matrix
return sine_matrix<normalization>
global_score = f1_score(y_test, clf_final.predict(X_test), average='macro')
 vc.voting = 'soft'
global_score_soft = f1_score(y_test, vc.predict(X_test), average='macro')
 vc.voting = 'hard'
global_score_hard = f1_score(y_test, vc.predict(X_test), average='macro')
 print('Validation score of a single LGBM Classifier: {:.4f}'.format(global_score))
print('Validation score of a VotingClassifier on 3 LGBMs with soft voting strategy: {:.4f}'.format(global_score_soft))
print('Validation score of a VotingClassifier on 3 LGBMs with hard voting strategy: {:.4f}'.format(global_score_hard))
def get_eigenspectrum(matrix):
spectrum = LA.eigvalsh(matrix)
 spectrum = np.sort(spectrum)[::-1]
return spectrum<normalization>
print('Validation score of a single LGBM Classifier: {:.4f}'.format(global_score))
print('Validation score of a VotingClassifier on 3 LGBMs with soft voting strategy: {:.4f}'.format(global_score_soft))
print('Validation score of a VotingClassifier on 3 LGBMs with hard voting strategy: {:.4f}'.format(global_score_hard))
spectrum_list = []
for index in range(df_len):
dataset_label = df.dataset.values[index]
row_id = df.id.values[index]
filename = ".. /input/{}/{}/geometry.xyz".format(dataset_label, row_id)
 xyz, lattice = get_xyz(filename)
 sine_matrix = get_sine_matrix(xyz, lattice)
 spectrum = get_eigenspectrum(sine_matrix)
 spectrum_list.append(spectrum )<data_type_conversions>
ranked_features = feature_importance(clf_final, X_train )
spectrum_df = pd.DataFrame(spectrum_list ).astype(np.float)
 spectrum_df = spectrum_df.fillna(0 )<normalization>
y_subm = pd.DataFrame()
y_subm['Id'] = test_ids
ss = StandardScaler()
spectrum_std_df = pd.DataFrame(ss.fit_transform(spectrum_df.values))
pca = PCA(n_components=80)
 spectrum_pca_df = pd.DataFrame(pca.fit_transform(spectrum_std_df.values))
spectrum_pca_df.head()<data_type_conversions>
y_subm['Target'] = clf_final.predict(test[X_train.columns])+ 1
vc.voting = 'soft'
y_subm_soft = y_subm.copy(deep=True)
 y_subm_soft['Target'] = vc.predict(test[X_train.columns])+ 1
vc.voting = 'hard'
y_subm_hard = y_subm.copy(deep=True)
 y_subm_hard['Target'] = vc.predict(test[X_train.columns])+ 1
<categorify><EOS> 
now = datetime.now()
sub_file = 'submission_LGB_{:.4f}_{}.csv'.format(global_score, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_soft = 'submission_soft_LGB_{:.4f}_{}.csv'.format(global_score_soft, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_hard = 'submission_hard_LGB_{:.4f}_{}.csv'.format(global_score_hard, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_0forNonHeads = 'submission_0forNonHead_LGB_{:.4f}_{}.csv'.format(global_score, str(now.strftime('%Y-%m-%d-%H-%M')))
 y_subm.to_csv(sub_file, index=False)
 y_subm_soft.to_csv(sub_file_soft, index=False)
 y_subm_hard.to_csv(sub_file_hard, index=False )