Skip to content

Instantly share code, notes, and snippets.

View 0xchamin's full-sized avatar
🎯
Focusing

Chamin Nalinda 0xchamin

🎯
Focusing
View GitHub Profile
@0xchamin
0xchamin / quadtree.java
Created March 3, 2023 21:52 — forked from AbhijeetMajumdar/quadtree.java
Quadtree Java implementation
A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are most often used to
partition a two-dimensional space by recursively subdividing it into four quadrants or regions. The regions may be square or
rectangular, or may have arbitrary shapes.
More on
https://en.wikipedia.org/wiki/Quadtree
Thanks to Jim for such an excellent visual representation on http://jimkang.com/quadtreevis/
[{'sa': '10.0.2.15',
'da': '104.244.42.193',
'pr': 6,
'sp': 49188,
'dp': 443,
'bytes_out': 21166,
'num_pkts_out': 37,
'bytes_in': 44703,
'num_pkts_in': 71,
'time_start': 158.474052,
@0xchamin
0xchamin / json_error.json
Created July 21, 2018 22:08
Cisco Joy JSON parse error
{"sa":"10.0.2.20","da":"195.113.214.245","pr":6,"sp":58178,"dp":443,"bytes_out":591,"num_pkts_out":10,"bytes_in":20706,"num_pkts_in":21,"time_start":2969087.103130,"time_end":2969087.310696,"packets":[{"b":88,"dir":">","ipt":1},{"b":1420,"dir":"<","ipt":23},{"b":1420,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":1027,"dir":"<","ipt":0},{"b":208,"dir":">","ipt":1},{"b":71,"dir":"<","ipt":22},{"b":295,"dir":">","ipt":2},{"b":1420,"dir":"<","ipt":154},{"b":1420,"dir":"<","ipt":0},{"b":1216,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":1232,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":1420,"dir":"<","ipt":0},{"b":120,"dir":"<","ipt":0}],"ip":{"out":{"ttl":128,"id":[25129,25130,25131,25132,25133,25134,25135,25136,25137,25138]},"in":{"ttl":64,"id":[60447,60448,60449,60450,60451,60452,60453,60454,60455,60456,60457,60458,60459,60460,60461,60462,60463,60464,60465,60466,60467]}},"t
import numpy as np
label = {0:'negative', 1:'positive'}
example = ['I love this movie']
X = vect.transform(example)
print('Prediction: %s\nProbability: %.2f%%' %\
(label[clf.predict(X)[0]],
np.max(clf.predict_proba(X))*100))
import pickle
import re
import os
from vectorizer import vect
clf = pickle.load(open(os.path.join('pkl_objects', 'classifier.pkl'), 'rb'))
from sklearn.feature_extraction.text import HashingVectorizer
import re
import os
import pickle
cur_dir = os.path.dirname(__file__)
stop = pickle.load(open(
os.path.join(cur_dir,
'pkl_objects',
'stopwords.pkl'), 'rb'))
import pickle
import os
dest = os.path.join('movieclassifier', 'pkl_objects')
if not os.path.exists(dest):
os.makedirs(dest)
pickle.dump(stop,
open(os.path.join(dest, 'stopwords.pkl'), 'wb'),
protocol = 4)
@0xchamin
0xchamin / test_set.py
Created May 20, 2018 17:31
Movie reviews test set evaluation
X_test, y_test = get_minibatch(doc_stream, size=5000)
X_test = vect.transform(X_test)
print('Accuracy: %.3f' % clf.score(X_test, y_test))
@0xchamin
0xchamin / movie_classifier_out_of_core_learning.py
Last active May 20, 2018 07:34
Create movie classifier from out of core learning
import numpy as np
import re
from nltk.corpus import stopwords
from distutils.version import LooseVersion as Version
from sklearn import __version__ as sklearn_version
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.linear_model import SGDClassifier
import numpy as np
import re
from nltk.corpus import stopwords
stop = stopwords.words('english')
def tokenizer(text):
text = re.sub('<[^>]*>', '', text)
emoticons = re.findall('(?::|;|=)(?:-)?(?:\)|\(|D|P)', text.lower())
text = re.sub('[\W]+', ' ', text.lower()) +\