Last active
April 1, 2020 20:40
-
-
Save jenncross/963c0dc8cb0937671d43e6cd765eb29f to your computer and use it in GitHub Desktop.
starter code for project 2
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
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