Skip to content

Instantly share code, notes, and snippets.

View vaclavcadek's full-sized avatar

Václav Čadek vaclavcadek

  • Stealth
  • Prague, Czech Republic
View GitHub Profile
@vaclavcadek
vaclavcadek / FooBar.java
Created May 24, 2013 13:05
Annotation example
package com.example;
public FooBar {
@WiredMessage(mapping="BUTTONS-FOO")
private String foo = "foo";
// clear selected button label
@WiredMessage(mapping="BUTTONS-BAR")
private String bar = "bar";
@vaclavcadek
vaclavcadek / remove_punctuation.py
Created April 28, 2014 12:44
Remove punctuation from string
import string
'!Hello,'.strip(string.punctuation)
@vaclavcadek
vaclavcadek / pillow.py
Created July 10, 2014 15:36
Download image from url, make polyline and convert it to base64
from base64 import b64encode
from PIL import Image, ImageDraw
import requests
from StringIO import StringIO
url = 'http://jssgallery.org/other_artists/andy_warhol/campbells.jpg'
response = requests.get(url)
img = Image.open(StringIO(response.content))
draw = ImageDraw.Draw(img)
@vaclavcadek
vaclavcadek / arst-orari-scraper.py
Created August 5, 2014 13:17
Simple web scraper for collecting all ARST schedules (for offline use :))
import urllib2
from bs4 import BeautifulSoup
import os
html_doc_example = """
<html><head><title>ARST - Orari</title></head>
<div onclick="openClose('a1')" class="mainExpand"><h2><a style="cursor:pointer; src="pdfmini.jpg"/>ABBASANTA</h2></div>
<div id="a1" class="texter">
<a href="402.pdf" target="_blank"><img src="pdfmini.jpg" />402 - ABBASANTA-GHILARZA-AIDOMAGGIORE-NORBELLO-ABBASANTA</a></br>
@vaclavcadek
vaclavcadek / ajax.js
Created January 20, 2015 14:27
Simple XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open('GET', '/media/resources/gamedev/weapon.json', true);
xhr.onload = function() {
JSON.parse(this.responseText);
};
xhr.send();
@vaclavcadek
vaclavcadek / comparing_runtimes.py
Last active August 29, 2015 14:25
Comparison of runtime behavior of three approaches: pure Python, naive NumPy and good NumPy
import timeit
normal_py_sec = timeit.timeit('sum(x*x for x in xrange(1000))', number=10000)
naive_np_sec = timeit.timeit('sum(na*na)', setup="import numpy as np; na=np.arange(1000)", number=10000)
good_np_sec = timeit.timeit('na.dot(na)', setup="import numpy as np; na=np.arange(1000)", number=10000)
print("Normal Python: %f sec" % normal_py_sec)
print("Naive NumPy: %f sec" % naive_np_sec)
print("Good NumPy: %f sec" % good_np_sec)
@vaclavcadek
vaclavcadek / df_correlation.py
Created August 4, 2015 13:11
Plot the correlation matrix (Spearman's) of values within the dataframe
import numpy as np
def plot_correlation(dataframe, title='', corr_type=''):
lang_names = dataframe.columns.tolist()
tick_indices = np.arange(0.5, len(lang_names) + 0.5)
plt.figure(figsize=(12,7))
plt.pcolor(dataframe.values, cmap='RdBu', vmin=-1, vmax=1)
colorbar = plt.colorbar()
colorbar.set_label(corr_type)
plt.title(title)
@vaclavcadek
vaclavcadek / vizualize_tree.py
Last active August 29, 2015 14:26
Vizualize sklearn decision tree using graphviz
from sklearn.externals.six import StringIO
from sklearn import tree
from graphviz import Source
from IPython.display import SVG
def vizualize_tree(model, figsize=(14,8)):
out = StringIO()
tree.export_graphviz(model, out_file=out)
graph = Source(out.getvalue())
graph.format = 'svg'
@vaclavcadek
vaclavcadek / feature_selection_cv.py
Created August 7, 2015 08:03
Feature selection with cross-validation
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.cross_validation import StratifiedKFold
from sklearn.feature_selection import RFECV
from sklearn.datasets import make_classification
# Create the RFE object and compute a cross-validated score.
svc = SVC(kernel='linear')
# The "accuracy" scoring is proportional to the number of correct
# classifications
@vaclavcadek
vaclavcadek / generate_bit_strings.py
Last active August 29, 2015 14:27
Generate all bit string permatation
for bits in product([0, 1], repeat=4):
print("".join(str(b) for b in bits)