Skip to content

Instantly share code, notes, and snippets.

@jenncross
Last active April 1, 2020 20:40
Show Gist options
  • Save jenncross/963c0dc8cb0937671d43e6cd765eb29f to your computer and use it in GitHub Desktop.
Save jenncross/963c0dc8cb0937671d43e6cd765eb29f to your computer and use it in GitHub Desktop.
starter code for project 2
import numpy as np
import matplotlib.pyplot as plt
import random
def openckdfile():
glucose, hemoglobin, classification = np.loadtxt('ckd.csv', delimiter=',', skiprows=1, unpack=True)
return glucose, hemoglobin, classification
def select(K):
return np.random.random((K, 2))
def assign(centroids, hemoglobin, glucose):
K = centroids.shape[0]
distances = np.zeros((K, len(hemoglobin)))
for i in range(K):
g = centroids[i,1]
h = centroids[i,0]
distances[i] = np.sqrt((hemoglobin-h)**2+(glucose-g)**2)
assignments = np.argmin(distances, axis = 0)
print(assignments)
return None
glucose, hemoglobin, classification = openckdfile()
plt.figure()
plt.plot(hemoglobin[classification==1],glucose[classification==1], "k.", label = "Class 1")
plt.plot(hemoglobin[classification==0],glucose[classification==0], "r.", label = "Class 0")
plt.xlabel("Hemoglobin")
plt.ylabel("Glucose")
plt.legend()
plt.show()
centroids = select(10)
assignments = assign(centroids, hemoglobin, glucose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment