Skip to content

Instantly share code, notes, and snippets.

def sandwich (Slices, HasPeanutButter, HasJelly):
Bread = Slices/2
HasPeanutButter = HasPeanutButter
HasJelly = HasJelly
if Bread >= 1 & HasPeanutButter == True: #check for enough bread
if Bread >= 2: #assign plural or not plural
Plural = "es"
else:
Plural = ""
@ayota
ayota / gist:9712667
Created March 22, 2014 19:14
python dict contacts
contacts = {'Shannon': {'phone': '202-555-1234', 'twitter': '@svt827', 'github': '@shannonturner' },
'Anupama': {'phone': '410-333-9876','twitter': '@iamtheanupama', 'github':''}}
# for contact in sorted(contacts.keys()):
# print "{0} : {1}".format(contact,contacts[contact]['phone'])
#
# for contact,info in sorted(contacts.items()):
# print "{0} : {1}".format(contact,info)
@ayota
ayota / dedup.py
Last active August 29, 2015 13:58
def deduplicate(file1, file2):
with open(file1,"r") as file_1:
event_1 = file_1.read().split("\n")
with open(file2,"r") as file_2:
event_2 = file_2.read().split("\n")
master_list = list(set(event_1 + event_2))
doubles = []
@ayota
ayota / statesexample.py
Created June 14, 2014 17:06
example of goal 2
codelist = ""
#this one splits the states.csv file
with open("states.csv","r") as states_file:
states = states_file.read().split("\n")
for index, state in enumerate(states):
states[index] = state.split(",")
#this formats the states in a pretty table
@ayota
ayota / gist:4d2a6eb131a842aca531
Last active August 29, 2015 14:14
hwk 3 prob 5 a
norm <- function(x) {
norm <- sqrt(sum(x^2))
return(norm)
}
x.1 <- c(-2,1,0)
x.2 <- c(1,-1,1)
y.1 <- c(0,1,0)
y.2 <- c(1,1,2)
#using distance formula
planesDist <- function(x1, x2, x3, y1, y2, y3) {
dist <- function(s1, s2, s3, s4) {
d <- sqrt(
t(x1 + s1*x2 + s2*x3 - (y1 + s3*y2 + s4*y3)) %*%
(x1 + s1*x2 + s2*x3 - (y1 + s3*y2 + s4*y3)))
return(d)}
start <- cbind(seq(-100,100,.01),seq(-100,100,.01),seq(-100,100,.01),seq(-100,100,.01))
x.dat <- model.matrix(Y ~ ., data=data)[,-1]
set.seed(1)
train <- sample(1:nrow(data), nrow(data)/2)
y.train <- data$Y[train]
x.train <- x.dat[train,]
test <- (-train)
y.test <- data$Y[test]
x.test <- x.dat[test,]
data(prostate)
prostate <- prostate[,1:9]
#scale all the predictors
data.st <- scale(prostate[,-9])
data.st <- sapply(1:8, function(x) data.st[,x]/fxn.4$norm(data.st[,x]))
data.st <- cbind(data.st, prostate[,9])
#roughly unit vectors
sapply(1:9, function(x) fxn.4$norm(data.st[,x]))
data <- read.table("~/Dropbox/numericalmethods/nonlinear.txt", header=TRUE, quote="\"")
#param order: d, r
fxn.4 <- list(
f.x = function(params, data) return( sum((data$y - params[1]*exp(-params[2] * data$x) )^2) ),
grad = function(params, data, f.x, h) {
d.d <- (f.x(params = c(params[1] + h, params[2]), data) - f.x(params, data)) / h
d.r <- (f.x(params = c(params[1], params[2] + h), data) - f.x(params, data)) / h
return(c(d.d,d.r))
},
Elaine Ayo
Math 504 / Homework 10
March 29, 2015
```{r echo=FALSE, message=FALSE}
library(MASS)
library(Matrix)
library(ggplot2)