This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
startTime = datetime.now() | |
#do something | |
print datetime.now() - startTime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#deleting a char directly in csv | |
infile = "test.csv" | |
outfile = "test_edit.csv" | |
delete_list = ["b'", "'"] | |
fin = open(infile) | |
fout = open(outfile, "w+") | |
for line in fin: | |
for word in delete_list: | |
line = line.replace(word, "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df.drop(df.std()[(df.std() == 0)].index, axis=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# and save to csv | |
train = np.load('ecs171train.npy') | |
test = np.load('ecs171train.npy') | |
#Use atom and terminal to load the whole dataset if it's over 200+MB | |
train = train[:51000] | |
test = test[:51000] | |
np.savetxt('test.csv', test, fmt='%s') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Pandas deleting duplicate columns by value | |
df = pd.read_csv('data/test.csv') | |
df = df.loc[:, ~df.T.duplicated()] | |
df.to_csv('data/test.csv') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clf = xgb.sklearn.XGBClassifier( | |
objective="binary:logistic", | |
learning_rate=0.05, | |
seed=9616, | |
max_depth=20, | |
gamma=10, | |
n_estimators=500) | |
clf.fit(X_train, Y_train, early_stopping_rounds=20, eval_metric="auc", eval_set=eval_set, verbose=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y_train['loss'] = (y_train['loss'] > 0.0).astype(int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Prepend a line at the top of the file | |
def line_prepender(filename, line): | |
with open(filename, 'r+') as f: | |
content = f.read() | |
f.seek(0, 0) | |
f.write(line.rstrip('\r\n') + '\n' + content) | |
infile = "c_test.csv" | |
outfile = "complete_test.csv" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Check if all elements in pandas/numpy are finite. notnull = isfinite | |
Pandas: | |
X_train.notnull().values.all() | |
np.isfinite(X_train).all() | |
2. Check if any elements in Pandas is Na. | |
Pandas: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Substitute the NaN's in a dataframe with values from another dataframe | |
If you have two DataFrames of the same shape, then: | |
df[df.isnull()] = d2 | |
2.Replace values in a dataframe with values from another dataframe by conditions |
OlderNewer