This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Elaine Ayo | |
| Math 504 / Homework 10 | |
| March 29, 2015 | |
| ```{r echo=FALSE, message=FALSE} | |
| library(MASS) | |
| library(Matrix) | |
| library(ggplot2) |
OlderNewer