Skip to content

Instantly share code, notes, and snippets.

@benjamincohen1
Created October 7, 2016 03:11
Show Gist options
  • Save benjamincohen1/7dcc87a6385bd75bf8877eb387236dbc to your computer and use it in GitHub Desktop.
Save benjamincohen1/7dcc87a6385bd75bf8877eb387236dbc to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 6 21:28:37 2016
@author: ben
"""
import numpy
my_dict = {
'ben': [100, 90, 80, 85],
'john': [70, 65, 42],
'mary': [100, 98, 95]
}
names = ['b', 'j', 'm']
scores = [[1,2,3], [4,5,6], [7,8,9]]
for student in my_dict.keys():
scores = my_dict[student]
print student, numpy.mean(scores), numpy.std(scores)
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 6 21:02:26 2016
@author: ben
"""
#guessing game
#have hidden number
# ask user for guess
#tell use if guess is too high or too low
#
#import random
#
#num_to_guess = random.randint(1,100)
#
#user_input = -1
#counter = 0
#
#while (user_input != num_to_guess):
# user_input = float(raw_input('Enter your guess: '))
# counter += 1
# #counter = counter + 1
# if user_input > num_to_guess:
# print("too high!")
# elif user_input < num_to_guess:
# print "too low!"
# else:
# print "you got it"
#
#
##print "This took you " + str(counter) + " tries"
#
#print "This took you {} tries to guess the number {}.".format(counter, num_to_guess)
## same hidden num every time
#
#def myfunc():
# print 'hello!'
# x = 5 + 9
# print x
#
#def crazy_math(first_number, num_to_multiply):
# answer = (first_number * 10) - (4 * num_to_multiply)
# return answer
#
#print crazy_math(10, 4)
import csv
import numpy
import matplotlib.pyplot as plt
myfile = open('/Users/Ben/Desktop/train.csv')
csv_file = csv.DictReader(myfile)
predictions = []
truths = []
def predict(datapoint):
# take in a line from our file
# spit out a single prediction for that line
print datapoint['Name']
age = datapoint['Age']
sex = datapoint['Sex']
pclass = int(datapoint['Pclass'])
if age == '':
return True
else:
age = float(age)
if age <= 15:
return True
#if over 15, we need to decide some other way
else:
if sex == 'female':
return True
else:
return False
# if pclass == 1:
# return True
# else:
# return False
yes = []
no = []
for line in csv_file:
age = line['Age']
if line['Survived'] == '1':
truths.append(True)
else:
truths.append(False)
if age == '':
pass
else:
pclass = line['Pclass']
if line['Survived'] == '1':
yes.append(int(pclass))
else:
no.append(int(pclass))
mypred = predict(line)
predictions.append(mypred)
print '-----'
correct = 0.0
l = len(predictions)
for x in range(l):
if predictions[x] == truths[x]:
correct += 1
print correct/len(predictions), ' correct answers'
print numpy.mean(yes)
print numpy.mean(no)
yes_hist = numpy.histogram(yes)
no_hist = numpy.histogram(no)
for x in range(len(no_hist)):
print yes_hist[x] - no_hist[x]
print 'YES'
plt.hist(yes, bins=3)
plt.show()
print 'NO'
plt.hist(no, bins=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment