Skip to content

Instantly share code, notes, and snippets.

View escuccim's full-sized avatar

Eric A. Scuccimarra escuccim

View GitHub Profile
@escuccim
escuccim / gist:f358688d9412d4bd423275b11382d263
Created August 3, 2018 09:41
Code to see what features are not used by any models in sklearn VotingClassifier
# plot feature importance for sklearn decision trees
def feature_importance(forest, X_train, display_results=True):
ranked_list = []
zero_features = []
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
if display_results:
@escuccim
escuccim / mean_encode.py
Created June 7, 2018 11:21
mean encoding in python
# mean encoding
means = X_tr.groupby(col).target.mean()
train_new[col+'_target_mean'] = train_new[col].map(means)
val_new[col+'_target_mean'] = val_new[col].map(means)
@escuccim
escuccim / flatten.py
Created April 17, 2018 09:01
Python Function to Flatten Nested Lists
# this works just like "unlist" in R
def flatten(l):
out = []
for item in l:
if isinstance(item, (list, tuple)):
out.extend(flatten(item))
else:
out.append(item)
return out
@escuccim
escuccim / synccolumns.py
Created January 28, 2018 10:55
Code to sync columns for two pandas dataframes
# For two dataframes X1 and X2, sync columns in X2 to be same as X1 in same order.
missing_cols = set( X1.columns ) - set( X2.columns )
for c in missing_cols:
X2[c] = 0
X2 = X2[X1.columns]
@escuccim
escuccim / gist:e8c49993616468e6305e062e4d5765b2
Created April 12, 2017 07:59
MySQL Query to delete duplicate rows
DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name
@escuccim
escuccim / unaccent.php
Created April 12, 2017 07:49
PHP Function to Remove Accents from Strings
function unaccent($string)
{
return preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);~i', '$1', htmlentities($string, ENT_COMPAT, 'UTF-8'));
}
@escuccim
escuccim / BackupDB.php
Created January 30, 2017 15:04
Laravel Command to Backup File to Amazon S3
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Aws\Laravel\AwsFacade as AWS;
class BackupDB extends Command
{
/**
@escuccim
escuccim / metadata.php
Last active January 11, 2017 11:21
Laravel MetaData translation file
return [
'default-title' => 'Default page title',
'default-description' => 'My whatever whatever web site default description text',
'/termsandconditions-title' => 'Terms and Conditions',
'/termsandconditions-description' => 'Terms and conditions for the website',
'/privacypolicy-title' => 'Privacy Policy',
'/privacypolicy-description' => 'Privacy policy for the website',
];
@escuccim
escuccim / helpers.php
Last active January 11, 2017 11:20
Laravel helper function to get translated page title and description from lang file metadata.php
function getMetaData($type){
// set a default so it doesn't break phpunit
if(isset($_SERVER['REQUEST_URI']))
$page = $_SERVER['REQUEST_URI'];
else
$page = "/";
// if we are looking at a blog article just use the default for the main blog page
if(strpos($page, 'blog') !== false) {
$page = '/blog';
@escuccim
escuccim / helpers.php
Created January 10, 2017 12:32
Laravel Helper to set language from session
function setLanguage(){
// set the app locale
App::setLocale( session('locale') ? session('locale') : config('app.locale'));
// set PHP locale for formatting dates
if(App::getLocale() == 'fr')
setlocale(LC_TIME, 'fr_CH.UTF-8');
}