Question

In: Computer Science

Using Python, Establish random centroids according to the k , and create your own algorithm to...

Using Python,

Establish random centroids according to the k , and create your own algorithm to make clusters: create a functions kmeans() that accepts a dataframe and k as parameters, and returns the clusters created.

Solutions

Expert Solution

Here is the code of kmean clustering using python programming language please go through it very carefully try to understand each and every line of code.

Before we start we need to import some modules like pandas and all libraries..

Import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

from sklearn.cluster import KMeans

Now here we'll import the data set what type of data we are having.

data=pd.read_csv("Data set name will be here.csv")

data.head() # it will print the 10 or 20 line of top most data from dataset.

## Now standarlising the data

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

data_scaled = scaler.fit_transform(data)

## Here the statitistics of scaled data

pd.DataFrame(data_scaled).describe()

### Here defining the k mean function

kmeans = KMeans(n_clusters=2, init='k-means++')

## Fitting the k mean clusturing on scaled data

kmeans.fit(data_scaled)

## Inartia on fitted data

kmeans.inertia_

## Fitting multiple k-means algorithms and storing the values in an empty list.

SSE = []
for cluster in range(1,20):

kmeans = KMeans(n_jobs = -1, n_clusters = cluster, init='k-means++)

kmeans.fit(data_scaled)
     
     SSE.append(kmeans.inertia_)

## Converting the results into a dataframe and plotting them

frame = pd.DataFrame({'Cluster':range(1,20), 'SSE':SSE})

plt.figure(figsize=(12,6))

plt.plot(frame['Cluster'], frame['SSE'], marker='o')

plt.xlabel('Number of clusters')

plt.ylabel('Inertia')

End of the code .....   



Related Solutions

Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
1. Create a flow chart from the algorithm then create python code. Algorithm: Tell the user...
1. Create a flow chart from the algorithm then create python code. Algorithm: Tell the user to enter their name, how many cars the user has, and the average number of cars per family in the users state. Create a variable to show the difference between the users number of cars and the average number of cars per family. Print the values of name, number of cars, average number of cars, and the difference in 2 decimal places.
Create the appropriate variables and algorithm to calculate the following. Choose your own values for the...
Create the appropriate variables and algorithm to calculate the following. Choose your own values for the variables. Do not enter the code on this paper. Create a program that calculates and displays the calculation. Approximate pi (π) as 3.14159. You do not need to create a new program for each question. If certain math equations use the same variable name, then you only need to declare it once in your main function. Separate each problem using comments, like seen in...
Create a Python program file and . Your program will draw random circles with filled and...
Create a Python program file and . Your program will draw random circles with filled and non-filled color (random colors) and rectangles, fill andnon-fill color (also random colors) on the canvas. At least 10 for each category
Using Python: Normalize a 5x5 random matrix hints: use numpy create a random matrix X apply...
Using Python: Normalize a 5x5 random matrix hints: use numpy create a random matrix X apply Normalization: (X - Mean) / Deviation
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the...
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the following requirements: Reads in a series of positive integers,  one number at a time;  and Calculate the product (multiplication) of all the integers less than 25,  and Calculate the sum (addition) of all the integers greater than or equal to 25. Use 0 as a sentinel value, which stops the input loop. [ If the input is 0 that means the end of the input list. ]...
You will utilize a large dataset to create a predictive analytics algorithm in Python. For this...
You will utilize a large dataset to create a predictive analytics algorithm in Python. For this assignment, complete the following: Utilize one of the following Web sites to identify a dataset to use, preferably over 500K from Google databases, kaggle, or the .gov data website Utilize a machine learning algorithm to create a prediction. K-nearest neighbors is recommended as an introductory algorithm. Your algorithm should read in the dataset, segmenting the data with 70% used for training and 30% used...
Using Python : Create a 4×5 4 × 5 array ? X with random value. Print...
Using Python : Create a 4×5 4 × 5 array ? X with random value. Print out ? X , as well as the largest value of each column.
1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
The prompt is using Python:  Write a 3 rail transposition encryption algorithm, and a corresponding decryption algorithm....
The prompt is using Python:  Write a 3 rail transposition encryption algorithm, and a corresponding decryption algorithm. Implement these two algorithms in their own function. Now write a testing function that demonstrates your algorithms work for all interesting cases!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT