Skip to content

Instantly share code, notes, and snippets.

@benjamincohen1
Created April 14, 2017 03:03
Show Gist options
  • Save benjamincohen1/e39e1b35194ce8609dfd7c9042351acc to your computer and use it in GitHub Desktop.
Save benjamincohen1/e39e1b35194ce8609dfd7c9042351acc to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 13 21:51:23 2017
@author: ben
"""
import csv
import numpy
import matplotlib.pyplot as plt
myfile = open('train.csv')
myreader = csv.DictReader(myfile)
avg_age = 29.5
def get_age(line):
if line['Age'] == '':
age = avg_age
else:
age = float(line['Age'])
return age
def predict(datapoint):
# return true or false; a prediction of whether the pass survives
age = get_age(datapoint)
if datapoint['Sex'] == 'female':
if age > 65:
return False
else:
return True
else:
if age <= 15:
return True
else:
return False
predictions = []
true_values = []
#survivor_ages = []
#other_ages = []
males = []
females = []
for line in myreader:
mypred = predict(line)
predictions.append(mypred)
if line['Survived'] == '1':
survived = True
# survivor_ages.append(get_age(line))
true_values.append(True)
else:
survived = False
# other_ages.append(get_age(line))
true_values.append(False)
if line['Sex'] == 'male':
males.append(survived)
else:
females.append(survived)
#plt.hist(males, bins=2)
#plt.show()
#plt.hist(females, bins=2)
#plt.show()
#print numpy.mean(survivor_ages), numpy.std(survivor_ages)
#print numpy.mean(other_ages), numpy.std(other_ages)
#
#plt.hist(survivor_ages)
#plt.show()
#plt.hist(other_ages)
#plt.show()
correct = 0.0
for i in range(len(predictions)):
if predictions[i] == true_values[i]:
correct += 1
print 'Accuracy: ', (correct/len(predictions)) * 100
#Enter numbers separated by a comma: 1,9,5,11,-2
#Biggest: 11
#Smallest: -2
#Mean: 4.8
#Sorted: [-2,1,5,9,11]
def mymean(numbers):
return sum(numbers)/float(len(numbers))
#def mymath(a, b, c):
# returnn a * b - c
def print_info(a):
max_value = max(a)
print 'Biggest: ', max_value
print 'Smallest: ', min(a)
print 'Mean: ', mymean(a)
print 'Sorted: ', sorted(a)
grades = {
'ben': [80, 98, 99, 100],
'bob': [60, 70, 80],
'mary': [89, 75, 100]
}
for student in grades.keys():
print student
print_info(grades[student])
print ''
#nums_to_sort = input('Enter your numbers: ')
#nums_to_sort = [10, 30, 40, 5]
#print_info(nums_to_sort)
#mydict = {
# 'car': 'a 4 wheeled object for getting you places',
# 'truck': 'a 4 wheeled things for trasporting lots of stuff',
# 'house': 'a place to live',
# 'inner_dict': {
# 'cat': 'another furry friend'
# }
#}
#
#mydict['dog'] = 'a furry 4 legged friend'
#mydict['car'] = 'something with 4 wheels'
#
#print 'dog' in mydict
#print 'turtle' in mydict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment