Skip to content

Instantly share code, notes, and snippets.

View aihubprojects's full-sized avatar
🏠
Working from home

AI HUB aihubprojects

🏠
Working from home
View GitHub Profile
k_means = K_Means(K)
k_means.fit(data)
# Plotting starts here
colors = 10*["r", "g", "c", "b", "k"]
for centroid in k_means.centroids:
plt.scatter(k_means.centroids[centroid][0], k_means.centroids[centroid][1], s = 130, marker = "x")
#generate dummy cluster datasets
# Set three centers, the model should predict similar results
center_1 = np.array([1,1])
center_2 = np.array([5,5])
center_3 = np.array([8,1])
# Generate random data and center it to the three centers
cluster_1 = np.random.randn(100, 2) + center_1
cluster_2 = np.random.randn(100,2) + center_2
cluster_3 = np.random.randn(100,2) + center_3
previous = dict(self.centroids)
for cluster_index in self.classes:
self.centroids[cluster_index] = np.average(self.classes[cluster_index], axis = 0)
isOptimal = True
for centroid in self.centroids:
original_centroid = previous[centroid]
curr = self.centroids[centroid]
if np.sum((curr - original_centroid)/original_centroid * 100.0) > self.tolerance:
for i in range(self.max_iterations):
self.classes = {}
for j in range(self.k):
self.classes[j] = []
for point in data:
distances = []
for index in self.centroids:
def fit(self, data):
self.centroids = {}
for i in range(self.k):
self.centroids[i] = data[i]
def euclidean_distance(self, point1, point2):
#return math.sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2 + (point1[2]-point2[2])**2) #sqrt((x1-x2)^2 + (y1-y2)^2)
return np.linalg.norm(point1-point2, axis=0)
import pandas as pd
import numpy as np
import random as rd
import matplotlib.pyplot as plt
import math
class K_Means:
def __init__(self, k=2, tolerance = 0.001, max_iter = 500):
self.k = k
x_fut = np.arange(30).reshape(-1,1)
xf = x_fut+x1[-1:]
y_fut = (model.predict(polyfet.transform(xf))).astype(int)
plt.figure(figsize=(16, 10))
plt.plot(x1,yp,"--b")
plt.plot(x1,yact,"-g")
plt.plot(xf,y_fut,"--r")
plt.legend(['predicted', 'actual',"future_pred"])
plt.xticks()
polyfet = PolynomialFeatures(degree=4) #you can change degree
xa = polyfet.fit_transform(x1)
model = linear_model.LinearRegression()
model.fit(xa,y)
yp = model.predict(xa)
yact = np.array(df['total_cases'])#.reshape(-1,1)
plt.figure(figsize=(8, 6))
plt.plot(yp,"--b")
plt.plot(yact,"-g")
from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
print('--'*15,end ='');print('polynomial model training',end ='');print('--'*10)
for i in range(1,6):
polyfet = PolynomialFeatures(degree=i)
xa = polyfet.fit_transform(x1)
model = linear_model.LinearRegression()