This file contains hidden or 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 functools import lru_cache | |
| @lru_cache(maxsize=100) | |
| def fibonacci(n): | |
| # Check that the input is a positive integer | |
| if type(n) != int: | |
| raise TypeError("n must be a positive int") | |
| if n < 1: | |
| raise ValueError("n must be a positive int") | |
This file contains hidden or 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
| def foo(s): | |
| if len(s) <= 0: | |
| return None | |
| else: | |
| output, curr_char, curr_count = '', '', 0 | |
| for idx in range(0, len(s)): | |
| if s[idx] == curr_char: | |
| curr_count += 1 | |
| else: | |
| output += curr_char + str(curr_count) if curr_count > 0 else curr_char |
This file contains hidden or 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 sklearn.linear_model import SGDRegressor | |
| # https://adventuresindatascience.wordpress.com/2014/12/30/minibatch-learning-for-large-scale-data-using-scikit-learn/ | |
| def iter_minibatches(chunksize, numtrainingpoints): | |
| # Provide chunks one by one | |
| chunkstartmarker = 0 | |
| while chunkstartmarker < numtrainingpoints: | |
| chunkrows = range(chunkstartmarker,chunkstartmarker+chunksize) | |
| X_chunk, y_chunk = getrows(chunkrows) |
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| from afinn import Afinn | |
| import spacy | |
| import re | |
| class TargetedSentimentAnalysis(object): | |
| def __init__(self): | |
| self.afinn = Afinn(emoticons=True) |
This file contains hidden or 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
| import argparse | |
| import sys | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--x', type=float, default=1.0, | |
| help='What is the first number?') | |
| parser.add_argument('--y', type=float, default=1.0, | |
| help='What is the second number?') | |
| parser.add_argument('--operation', type=str, default='add', |
This file contains hidden or 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
| import re | |
| # Returns all the emoji in this string. 'text' is a unicode string. | |
| def get_emoji(text): | |
| text = unicode(text, 'ignore') | |
| try: | |
| ranges = re.compile(u'([\U00002600-\U000027BF])|([\U0001f300-\U0001f64F])|([\U0001f680-\U0001f6FF])') | |
| except re.error: | |
| ranges = re.compile(u'([\u2600-\u27BF])|([\uD83C][\uDF00-\uDFFF])|([\uD83D][\uDC00-\uDE4F])|([\uD83D][\uDE80-\uDEFF])') | |
| emojis = (ranges.findall(text)) |
This file contains hidden or 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
| import networkx as nx | |
| from networkx.algorithms import bipartite | |
| B = nx.Graph() | |
| B.add_nodes_from([1,2,3,4], bipartite=0) # Add the node attribute "bipartite" | |
| B.add_nodes_from(['a', 'b', 'c', 'd'], bipartite=1) | |
| B.add_edges_from([(1,'a'), (1,'b'), (2,'b'), (2,'c'), (3,'c'), (4,'a')]) | |
| print 'Is connected? %s' % nx.is_connected(B) |
This file contains hidden or 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 multiprocessing import Pool | |
| def job(x): | |
| return x ** 2 | |
| # End of job(...). | |
| if __name__ == "__main__": | |
| p = Pool(processes=50) | |
| data_1 = p.map(job, range(10)) | |
| data_2 = p.map(job, [99, 111, 7236]) |
This file contains hidden or 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
| du -h <dir> | grep '[0-9\.]\+G' | |
| # e.g.: | |
| # du -h /home/peterparker | grep '[0-9\.]\+G' |
This file contains hidden or 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
| # Snippet for identifying the best classifier from svm, per, KNN based on their respective (accuracy) scores. | |
| idx = np.argmax([svm_accuracy, per_accuracy, knn_accuracy]) | |
| classifiers = {0: 'SVM', 1: 'Perceptron', 2: 'KNN'} | |
| print('Best classifier is {}'.format(classifiers[idx])) |