Skip to content

Instantly share code, notes, and snippets.

@ngopal
ngopal / REPL.R
Created April 18, 2017 04:52
How to write a REPL in R
REPL <- function() {
while(T) {
print(eval(parse(text=readline("NG>>"))))
}
}
REPL()
@ngopal
ngopal / sgdlinreg.java
Created March 24, 2017 17:06
Stochastic Gradient Descent, but this time in Java.
package com.nikhilgopal.spark;
/**
* Created by nikhilgopal on 3/24/17.
*/
public class SGDLinReg {
public static void main(String[] args) {
double[] coefficients = {0.4, 0.8};
double[][] dataset = {
{1.0, 1.0},
@ngopal
ngopal / spam.clisp
Created March 23, 2017 00:46
A first attempt at creating bayesian a spam filter from stratch; Trying to do this in lisp as a learning exercise; Definitely still a work in progress...
;; A first attempt at creating bayesian a spam filter from stratch
;; Trying to do this in lisp as a learning exercise
;; Definitely still a work in progress...
(defparameter *spam* '(this is a spam document that is fake news))
(defparameter *ham* '(this is nikhils document))
(defparameter *testdoc* '(this is a test document that is meant to be fake))
(setq spam-table (make-hash-table))
(setq ham-table (make-hash-table))
@ngopal
ngopal / stocktwitdash.R
Created February 14, 2017 19:43
The beginnings of a stock twit dashboard.
library(syuzhet)
library(igraph)
library(RCurl)
plotDashBoard <- function(ticker) {
twits <- getURL(paste("https://api.stocktwits.com/api/2/streams/symbol/",ticker,".json", sep=""))
twts.tab <- fromJSON(twits)
# Sentiment
twt.sent <- get_nrc_sentiment(twts.tab$messages$body)
@ngopal
ngopal / wines.R
Last active February 10, 2017 21:39
A very quick and surface-level analysis of wines using the wines.com API
library(rjson)
apikey = 'REDACTED' # please get one from wines.com
url <- paste('http://services.wine.com/api/beta2/service.svc/json/catalog?filter=categories(614)&offset=0&size=100&apikey=',apikey,sep="")
document <- fromJSON(file=url, method='C')
# I am pulling down light and crispy white wine data above
ds <- matrix(0, 100, 3)
for (i in 1:length(document$Products$List)) {
ds[i,] <- c(as.numeric(document$Products$List[[i]]$PriceRetail),
as.numeric(document$Products$List[[i]]$Ratings$HighestScore),
@ngopal
ngopal / general_assembly.ipynb
Last active January 21, 2017 03:09
removed an annoying extra cell.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
library(randomForest)
library(RJSONIO)
library(ROCR)
# Research Questions
# 1. What are the ranked importances of node encodings?
# 2. What are the ranked importances of edge encodings?
# 3. How important is network structure to noticeability?
# 3a. Where do participants tend to click?
library(igraph)
# Random Graph Set Space Exploration
plot_densityXsize <- function(dens,size) {
numer = 1;
par(mfrow=c(length(dens), length(size)))
for (d in dens) {
for (s in size) {
cat(paste("rg",numer," <- random.graph.game(",s,",",d,")",sep=''), '\n')
assign(paste("rg",numer,sep=''),eval(parse(text=paste("random.graph.game(",s,",",d,")",sep=''))), envir = .GlobalEnv)
aim2d <- matrix(rbind(
c(2, 5),
c(30, 25),
c(8, 3),
c(3, 2),
c(5, 1),
c(27, 11),
c(12, 15),
c(9, 14)
), 8, 2)
@ngopal
ngopal / toCytojs.R
Created June 18, 2016 20:18
Convert iGraph to CytoscapeJS elements
function toCytojs(g) {
s = c()
s = append(s,"[")
for (i in 1:length(V(g))) {
s = append(s, paste("{ data: { id: \'",i,"\' } },", sep=''))
}
temp <- get.edgelist(g)
for (i in 1:length(E(g))) {
if (i == length(E(g))) {
s = append(s, paste("{ data: { id: \'",i+length(V(g)),"\', source: \'",temp[i,1],"\', target: \'",temp[i,2],"\' } }", sep=''))