Skip to content

Instantly share code, notes, and snippets.

View isayev's full-sized avatar
👨‍💻
busy writing proposals

Olexandr Isayev isayev

👨‍💻
busy writing proposals
View GitHub Profile
@isayev
isayev / gist:a794323453600d0a44c327968e708ab1
Created July 28, 2019 19:04
find a line number where is a pattern in a file
grep -n "pattern" file.csv | awk -F ":" '{print $1}'
@isayev
isayev / sort-by-length.sh
Created October 7, 2015 02:03
sort lines in file by their length
cat input.txt | awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2- > input-sorted.txt
# reverse order
cat input.txt | awk '{ print length, $0 }' | sort -n -r | cut -d" " -f2- > input-sorted-rev.txt
@isayev
isayev / gist:f83e507a8ea841bd6cbc
Last active April 17, 2018 19:36
reverse column order in pandas
data[data.columns[::-1]]
def pca(X, npc):
n_samples, n_features = X.shape
Xmean = np.mean(X, axis=0)
U, s, Vt = scipy.sparse.linalg.svds(X - Xmean, k=npc)
order = np.argsort(-s) # sort s in descending order
# svds returns U, s, Vt sorder in ascending order. We want descending
s = s[order]
W = Vt[order,:]
@isayev
isayev / csv_split.py
Created April 28, 2014 17:15
splits file returns the files splitted and the number of files(current_piece)
import csv
import os
import pprint
import datetime
#splits file returns the files splitted and the number of files(current_piece)
import csv
def splits(filehandler, delimiter=',', row_limit=100000,
output_name_template='output_%s.csv', output_path='.', keep_headers=True):
"""
@isayev
isayev / plotsparse.py
Created April 28, 2014 04:40
plot sparse matrix pattern
import matplotlib.pyplot as plt
from scipy.sparse import coo_matrix
def plot_coo_matrix(m):
if not isinstance(m, coo_matrix):
m = coo_matrix(m)
fig = plt.figure()
ax = fig.add_subplot(111, axisbg='black')
ax.plot(m.col, m.row, 's', color='white', ms=1)
ax.set_xlim(0, m.shape[1])
@isayev
isayev / brakets2underscores.sh
Created April 25, 2014 16:17
Shell script to rename files with brackets & white space to underscores
find . -depth -name '*(*' | while IFS= read -r f ; do mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr '(' _)" ; done
find . -depth -name '*)*' | while IFS= read -r f ; do mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr ')' _)" ; done
find . -depth -name '* *' | while IFS= read -r f ; do mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr ' ' _)" ; done