Skip to content

Instantly share code, notes, and snippets.

#train the network
dp_model <- mx.model.FeedForward.create(symbol = out
,X = train.x
,y = train.y
,ctx = mx.cpu()
,num.round = 100
,eval.metric = mx.metric.accuracy
,array.batch.size = 50
,learning.rate = 0.005)
#predict on test
pred_dp <- predict(dp_model,test.x)
str(pred_dp) #contains 2 rows and 16281 columns
#transpose the pred matrix
pred.val <- max.col(t(pred_dp))-1
@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()
@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 / 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 / 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 / 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 / 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 / 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
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])