Node JS Online
Online Python Interpreter
import numpy as np import pandas as pd import random import copy def dynamic_arr(k_means): return [ for i in range(k_means)] def dynamic_arr0(k_means): return [ for i in range(k_means)] fileuploaded = files.upload() # reading the file (Course Evaluation) course_Evaluation_File = pd.read_excel ('Course Evaluation .xlsx') # taking the values of quizzes into a variable (quizzes) quizzes = course_Evaluation_File[["Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13","Q14","Q15","Q16","Q17","Q18","Q19","Q20"]].to_numpy() #taking the Students IDs into a varible(Students_IDs) student_IDs = course_Evaluation_File[["Student ID"]].to_numpy() # taking an input from user to know what is the number of clusters clusters_number = int(input("Enter Number of Clusters: ")) #array of clusters clusters =  #taking the samples for i in range (0, clusters_number): clusters.append(course_Evaluation_File.iloc[:, 1:].sample(1)) # initializing two arrays to enter the while loop (instead of recursion) cluster_class = dynamic_arr(clusters_number) temp_class=dynamic_arr0(clusters_number) # stop conditions when the two arrays is equal which means the pervious cetroid is the new one while(not pd.DataFrame(cluster_class).equals(pd.DataFrame(temp_class))): # initializing two arrays to store the clusters values temp_class=dynamic_arr(clusters_number) # just make a copy from cluster to the temp temp_class=copy.deepcopy(cluster_class) cluster_class = dynamic_arr(clusters_number) #print(temp_class) #print(cluster_class) #print(clusters) #classification for i in range (0, len(quizzes)): # take the current process currentProcess = quizzes[i] #initialize the array of distance tempDistance =  # calculating and storing the distances for j in range(0, len(clusters)): distance = np.linalg.norm(np.array(currentProcess) - np.array(clusters[j])) # print (distance) tempDistance.append(distance) #print(tempDistance) #take the minimum distance of the process minimum = np.argmin(np.array(tempDistance)) # assign the minimum distance to own process cluster_class[minimum].append(currentProcess) #print(currentProcess) #print(minimum) #just printing the values of clusters for i in range(clusters_number): print (len(cluster_class[i])) # calculating new centroid # we redeclare the clusters clusters= #calculating the means of all columns(Quiz) and store it in clusters to calculate new centroid for i in range (len(cluster_class)): if cluster_class[i]==: continue else: clusters.append(np.array(cluster_class[i]).mean(axis=0)) #print("*") print (clusters) print(temp_class) print(cluster_class)
Buy Samsung Galaxy Buds+ From Amazon 27% Off
Share this code with others
Create a free account. No Credit card info required.
I agree with the Codepy
Term of Service