<jupyter_start><jupyter_text>---title: "K-means"author: "<NAME>"date: 2020-09-07description: "-"type: technical_notedraft: false---<jupyter_code># importing required libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score

# read the train and test dataset
train_data = pd.read_csv('train1-data.csv')
test_data = pd.read_csv('test1-data.csv')

# shape of the dataset
print('Shape of training data :',train_data.shape)
print('Shape of testing data :',test_data.shape)

model = KMeans() 

# fit the model with the training data
model.fit(train_data,train_data)
# Number of Clusters
print('\nDefault number of Clusters : ',model.n_clusters)

# predict the clusters on the train dataset
predict_train = model.predict(train_data)
print('\nCLusters on train data',predict_train) 
# predict the target on the test dataset
predict_test = model.predict(test_data)
print('Clusters on test data',predict_test) 

# Now, we will train a model with n_cluster = 3
model_n3 = KMeans(n_clusters=3)

# fit the model with the training data
model_n3.fit(train_data)
# Number of Clusters
print('\nNumber of Clusters : ',model_n3.n_clusters)

# predict the clusters on the train dataset
predict_train_3 = model_n3.predict(train_data)
print('\nCLusters on train data',predict_train_3) 
# predict the target on the test dataset
predict_test_3 = model_n3.predict(test_data)
print('Clusters on test data',predict_test_3)<jupyter_output>Clusters on test data [2 2 2 1 2 2 1 2 2 2 2 2 2 1 1 2 2 2 2 0 1 1 2 2 2 2 1 2 2 1 0 2 2 2 2 1 0
 0 2 1 1 2 2 1 2 2 2 0 2 2 2 2 2 0 2 2 0 2 2 2 2 2 2 0 0 2 0 2 2 2 0 2 1 0
 2 1 2 1 2 0 2 2 2 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 0 1]
{'fraction_non_alphanumeric': 0.08587896253602305, 'fraction_numerical': 0.06974063400576369, 'mean_word_length': 4.459119496855346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '251378', 'license': 'MIT', 'n_tokens_mistral': 705, 'n_tokens_neox': 595, 'n_words': 249, 'path': 'content/python/ml_algorithms/.ipynb_checkpoints/K-means-checkpoint.ipynb', 'repo_name': 'Palaniappan12345/mlnotes', 'size': '1742'}