Created
December 15, 2016 04:00
-
-
Save benjamincohen1/0d98076f015835959b9ea3aa369adc70 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 Wed Dec 14 22:35:34 2016 | |
@author: ben | |
""" | |
#grades = { | |
# 'Ben': [90,85, 76], | |
# 'Bob': [98, 77, 75] | |
#} | |
class Student(object): | |
def __init__(self, name, grades, year_in_school, address): | |
self.name = name | |
self.grades = grades | |
self.year_in_school = year_in_school | |
self.address = address | |
def print_average(self): | |
print sum(self.grades)/float(len(self.grades)) | |
class Animal(object): | |
def __init__(self, name): | |
self.name = 'A pet named: ' + name | |
def eat(self): | |
print 'I ate a lot of food.' | |
class Cat(Animal): | |
def speak(self): | |
print 'Meow' | |
class Dog(Animal): | |
def speak(self): | |
print 'bark bark' | |
class Beagle(Dog): | |
def speak(self): | |
print 'Woof woof I am a beagle' | |
cat = Cat('Jimmmy') | |
dog = Beagle('Ruff') | |
print dog.name | |
dog.eat() | |
dog.speak() | |
#ben = Student('Ben', [90,85, 76], 8, 'Cambridge, MA') | |
#student2 = Student('Bob', [98,77, 100], 9, 'Boston, MA') | |
# | |
#student2.print_average() |
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 Dec 8 21:45:56 2016 | |
@author: ben | |
""" | |
import csv | |
import numpy | |
import matplotlib.pyplot as plt | |
myfile = open('/Users/Ben/Desktop/train.csv').readlines() | |
predictions = [] | |
corrects = [] | |
count = 0 | |
datapoints = [] | |
for line in csv.DictReader(myfile): | |
prediction = predict(line) | |
true_val = line['Survived'] | |
age = line['Age'] | |
sex = line['Sex'] | |
pclass = int(line['Pclass']) | |
if sex == 'male': | |
sex = 1 | |
else: | |
sex = 0 | |
if age == '': | |
age = 29.5 | |
else: | |
age = float(age) | |
datapoint = [age, sex, pclass] | |
datapoints.append(datapoint) | |
if true_val == '1': | |
corrects.append(True) | |
else: | |
corrects.append(False) | |
count += 1 | |
#from sklearn.linear_model import LogisticRegression | |
from sklearn.neighbors import KNeighborsClassifier | |
from sklearn.metrics import accuracy_score | |
model = KNeighborsClassifier(n_neighbors=3, weights='distance') | |
model.fit(datapoints, corrects) | |
predictions = model.predict(datapoints) | |
print accuracy_score(predictions, corrects) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment