Created
June 9, 2017 03:05
-
-
Save benjamincohen1/6806c8b449b84952f45faffb952f9078 to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Jun 1 20:49:45 2017 | |
@author: ben | |
""" | |
#import random | |
# | |
## write a guessing game | |
# | |
#target_number = random.randint(1,100)# random number between 1 and 100 | |
# | |
#guess = -1 | |
# | |
#while guess != target_number: | |
# guess = input('Enter your number: ') | |
# | |
# if guess == target_number: | |
# print "Correct!" | |
# elif guess < target_number: | |
# print "You guessed too low" | |
# else: | |
# print "Your guess was too high" | |
def mean(mynumbers): | |
return sum(mynumbers)/float(len(mynumbers)) | |
# | |
#def print_student_summary(nums): | |
# print 'Biggest number is: ' + str(max(nums)) | |
# print 'Smallest number is: ' + str(min(nums)) | |
# print 'Mean number is: ' + str(mean(nums)) | |
# | |
# print sorted(nums) | |
#grades = { | |
# 'Ben': [80, 90, 100], | |
# 'Zach': [70, 85, 50], | |
# 'Meghan': [100, 90, 15, 90] | |
# | |
#} | |
#for key in grades.keys(): | |
# print 'Report for: ' + key | |
# print_student_summary(grades[key]) | |
# | |
class Student(object): | |
def __init__(self, name, grades, school): | |
self.name = name | |
self.grades = grades | |
self.school = school | |
def print_student_summary(self): | |
print 'Biggest number is: ' + str(max(self.grades)) | |
print 'Smallest number is: ' + str(min(self.grades)) | |
print 'Mean number is: ' + str(mean(self.grades)) | |
print sorted(self.grades) | |
class TestSchoolStudent(Student): | |
def __init__(self, name, grades): | |
self.school = 'My Test School' | |
super(TestSchoolStudent, self).__init__(name, grades, self.school) | |
ben = TestSchoolStudent('Ben Cohen', [100, 96, 72]) | |
ben.print_student_summary() | |
print ben.school |
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Jun 8 21:46:30 2017 | |
@author: ben | |
""" | |
import csv | |
from first_class import mean | |
import matplotlib.pyplot as plt | |
myfile = open('train.csv') | |
def predict(datapoint): | |
# take in a datapoint, return a prediction | |
# of whether or not that passenger survived | |
pclass = int(datapoint['Pclass']) | |
age = fix_age(datapoint['Age']) | |
if age < 10: | |
return True | |
else: | |
if pclass == 1: | |
return True | |
else: | |
return False | |
def fix_age(value): | |
if value == '': | |
return 29.5 | |
else: | |
return float(value) | |
#yesses = [] | |
#nos = [] | |
predictions = [] | |
corrects = [] | |
mycsvreader = csv.DictReader(myfile) | |
for line in mycsvreader: | |
survived= line['Survived'] | |
# pclass = line['Pclass'] | |
age = fix_age(line['Age']) | |
if survived == '1': | |
corrects.append(True) | |
# yesses.append(int(age)) | |
else: | |
corrects.append(False) | |
# nos.append(int(age)) | |
predictions.append(predict(line)) | |
#plt.hist(nos) | |
#plt.show() | |
#plt.hist(nos) | |
#plt.show() | |
#print 'Average age of survival: ' + str(mean(yesses)) | |
#print 'Average age of non-survival: ' + str(mean(nos)) | |
num_correct = 0.0 | |
for x in range(len(predictions)): | |
if predictions[x] == corrects[x]: | |
num_correct += 1 # same thing as num_correct = num_correct + 1 | |
print 'You got ' + str(num_correct/len(predictions)) + ' right' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment