Skip to content

Instantly share code, notes, and snippets.

@HackerEarthBlog
HackerEarthBlog / Apriori.R
Last active May 17, 2020 10:41
Apriori Algorithm in R
> library(arules)
> data("Adult")
> rules <- apriori(Adult,parameter = list(supp = 0.5, conf = 0.9, target = "rules"))
> summary(rules)
#set of 52 rules
#rule length distribution (lhs + rhs):sizes
# 1 2 3 4
# 2 13 24 13
@HackerEarthBlog
HackerEarthBlog / parameter_tuning.py
Last active June 14, 2019 06:43
Parameter tuning for SVM using Grid Search
from sklearn.model_selection import GridSearchCV
parameters = {'kernel':('linear', 'rbf'), 'C':[1,2,3,4,5,6,7,8,9,10], 'gamma':
[0.01,0.02,0.03,0.04,0.05,0.10,0.2,0.3,0.4,0.5]}
svr = svm.SVC()
grid = GridSearchCV(svr, parameters)
grid.fit(X_train, y_train)
predicted = grid.predict(X_test)
cnf_matrix = confusion_matrix(y_test, predicted)
print(cnf_matrix)
library(e1071)
library(caTools)
data(iris)
iris$spl=sample.split(iris,SplitRatio=0.7)
train=subset(iris, iris$spl==TRUE)
test=subset(iris, iris$spl==FALSE)
x=train[,-5]
y=train[,5]
svm_model <- svm(Species ~ ., data=train)
table(predict(svm_model, test[,-5]), test[,5])
@HackerEarthBlog
HackerEarthBlog / svm_iris.py
Last active April 26, 2020 22:12
SVM with linear kernel and C=1 for Iris Data
from sklearn import datasets, svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
#Split the data into test and train
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# Linear Kernel
@HackerEarthBlog
HackerEarthBlog / buffered_input.py
Last active February 13, 2017 07:13
revamped `BufferedInput` class
class BufferedInput:
def __init__(self):
# the buffer holds the content that has be entered by the user but has not been read by into a variable yet
self.buffer = ''
def getc(self):
self.fill_buffer()
byte = self.buffer[0]
self.buffer = self.buffer[1:]
@HackerEarthBlog
HackerEarthBlog / install_python35.sh
Created February 9, 2017 08:05
how to install python 3.5
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python3.5
@HackerEarthBlog
HackerEarthBlog / python_rpi.py
Created February 9, 2017 07:52
python on raspberry pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
if(GPIO.input(23) ==1):
print(“Button 1 pressed”)
if(GPIO.input(24) == 0):
@HackerEarthBlog
HackerEarthBlog / buffer.py
Created February 1, 2017 06:20
for handling buffered input
class UngetableInput:
def __init__(self, initialBuffer=''):
self.buffer = initialBuffer
def getc(self):
if not self.buffer:
self.buffer = input()
c = self.buffer[0]
self.buffer = self.buffer[1:]
return c
@HackerEarthBlog
HackerEarthBlog / calc.py
Created February 1, 2017 06:17
FIle object to put a character back into the input buffer
from buffer import UngetableInput
token_stream = UngetableInput()
def expression():
val = None
ch = token_stream.getc()
while ch == ' ':
ch = token_stream.getc()
if ch in {'-', '+', '.'} or ch.isdigit():
@HackerEarthBlog
HackerEarthBlog / lisp_parser_grammer
Created February 1, 2017 06:04
function expression for the list interpreter
function expression:
token = next_token()
if token is one of (-, +, 0-9, .):
putback the token
val = number()
elif token is (:
op = operator()
exp1 = expression()
exp2 = expression()
c = next_token()