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...
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
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: 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
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!
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and Tyler Gary = { "name": "Gary", "homework": [90.0,97.0,75.0,92.0], "quizzes": [88.0,40.0,94.0], "tests": [75.0,90.0] } Alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] } Tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] } Insert one additional (new) dictionary of your choice Create a new list named students that...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and Tyler Gary = { "name": "Gary", "homework": [90.0,97.0,75.0,92.0], "quizzes": [88.0,40.0,94.0], "tests": [75.0,90.0] } Alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] } Tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] } Insert one additional (new) dictionary of your choice Create a new list named students that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT