Question

kmeans = KMeans(n_clusters=5).fit(df) print(kmeans.cluster_centers_.shape) fig = plt.figure(figsize=(20,10)) for c in range(1,16): fig.add_subplot(4,4,c).plot(kmeans.cluster_centers_[c-1,:]) plt.show...

kmeans = KMeans(n_clusters=5).fit(df)
print(kmeans.cluster_centers_.shape)


fig = plt.figure(figsize=(20,10))
for c in range(1,16):
fig.add_subplot(4,4,c).plot(kmeans.cluster_centers_[c-1,:])
plt.show()

I have an error which is 'index 5 is out of bounds for axis 0 with size 5'.

I think there is an problem on this numbers (n_clusters and figsize)

kmeans = KMeans(n_clusters=5).fit(df)

fig = plt.figure(figsize=(20,10))

How can i fix this?or change numbers?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here in the given code error is in

for c in range(1,16):
fig.add_subplot(4,4,c).plot(kmeans.cluster_centers_[c-1,:])

Here

kmeans = KMeans(n_clusters=5).fit(df) will fit the input data in 5 number of clusters. So there will be 5 number cluster centers...

Now kmeans.cluster_centers_.shape[0] = 5... That means 5 centers with given data dimensions are created.

And the given snippet of code tries to access kmeans.cluster_centers_[5] , kmeans.cluster_centers_[6] ....kmeans.cluster_centers_[15] ... So it is giving error because only 5 centers are available...

for c in range(1,16):
fig.add_subplot(4,4,c).plot(kmeans.cluster_centers_[c-1,:])

Solution :-

Either change :-

kmeans = KMeans(n_clusters=5).fit(df) to

kmeans = KMeans(n_clusters=16).fit(df)

or

for c in range(1,16):
fig.add_subplot(4,4,c).plot(kmeans.cluster_centers_[c-1,:])

to

for c in range(1,5):
fig.add_subplot(3,2,c).plot(kmeans.cluster_centers_[c-1,:])

Add a comment
Know the answer?
Add Answer to:
kmeans = KMeans(n_clusters=5).fit(df) print(kmeans.cluster_centers_.shape) fig = plt.figure(figsize=(20,10)) for c in range(1,16): fig.add_subplot(4,4,c).plot(kmeans.cluster_centers_[c-1,:]) plt.show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node;...

    IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • This is my code: import numpy as np import pandas as pd import sys from keras.models...

    This is my code: import numpy as np import pandas as pd import sys from keras.models import Sequential from keras.layers import Dense from sklearn.preprocessing import StandardScaler from keras.layers.normalization import BatchNormalization from keras.layers import Dropout file_full=pd.read_csv("/Users/anwer/Desktop/copy/FULL.csv") file_bottom=pd.read_csv("/Users/anwer/Desktop/copy/bottom.csv") train=[] train_targets=[] test=[] test_targets=[] p=[] q=[]    # We will generate train data using 50% of full data and 50% of bottom data. #is train target for labeling ? yes for train data train_df = file_full[:len(file_full)//2] labels=[ 0 for i in range(len(file_full)//2)] train_df=train_df.append(file_bottom[:len(file_bottom)//2]) for...

  • please complete the missing function only to figure out how many numbers fall within the range...

    please complete the missing function only to figure out how many numbers fall within the range of 90 through 99 total of 29 values in C 6 finclude "lab5.h" 8 const char *FILENAME() - / array of the data file names * ("lab5a.dat", "lab5b.dat", NULL); 12 int main(void) 13 int file count = 0; keeps track of which file we are on/ int check overflow - 0; / counter to prevent array overflow int real filesize = 0; /actual count...

  • Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector...

    Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector with random numbers. Use merge sort to reorder the vector Prompt user to enter a number to search in the vector. If there are more than one number in the vector equal to the search value, display the indices remove the repeated numbers. If found just one matching number, display the index. If no matching number, prompt user for 2 options: add to the...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • Problem 1 The following table summarizes the results for all values of d. for a converging...

    Problem 1 The following table summarizes the results for all values of d. for a converging lens Case Type Direction Magnification S< do < 26 Real Inverted Enlarged d -2/ Real Inverted Same size de > 27 Real Inverted Reduced d. Virtual Upright Enlarged Sketch and find the image for each case shown in the table by changing the objects position Problem 2 0.25 p 0.23 P AI (b) Graded index (GRIN) rod lens is a glass rod whose refractive...

  • C++. Need some help getting started. We will also have the following two functions: 1. A...

    C++. Need some help getting started. We will also have the following two functions: 1. A mutate function that randomly modifies a chromosome. 2. A crossover function that takes two chromosomes and splits each one at the same spot, then combines them together. Our genetic algorithm works by iterating over generations of chromosomes via the following process: 1. Generate random population. 2. Until we get an answer that is good enough, do the next steps in a loop: (a) Do...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT