<jupyter_start><jupyter_text>Using K-Means Clustering to "Guess" at Classifying Warranty Claims<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
from scipy import sparse
from sklearn.cluster import KMeans
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, LabelBinarizer
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
pd.set_option('max_colwidth', 100)
df = pd.read_excel(r'D:\temp\turn_signal_switch.xlsx')
df.head()
X = df[['DAYS_TO_FAIL_MINZERO']]<jupyter_output><empty_output><jupyter_text>Initialize Encoders<jupyter_code>count_vect = CountVectorizer()
tfidf_transformer = TfidfTransformer()<jupyter_output><empty_output><jupyter_text>Fit and transform ("encode") the data<jupyter_code>X_complaint_counts = count_vect.fit_transform(df.ORIG_CUST_CNTN_TXT)
X_complaint_tfidf = tfidf_transformer.fit_transform(X_complaint_counts)
X_final = sparse.hstack((X, X_complaint_tfidf), format='csr')
X_final
kmeans = KMeans(n_clusters=2)
kmeans.fit(X_final)
y_kmeans = kmeans.predict(X_final)
kmeans = KMeans(n_clusters=3)
kmeans.fit(X_complaint_tfidf)
y_kmeans = kmeans.predict(X_complaint_tfidf)
y_kmeans
labelled_data = df.assign(LABEL=y_kmeans)
labelled_data
labelled_data.query("LABEL == 0")
labelled_data.query("LABEL == 1")
labelled_data.query("LABEL == 2")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10092395167022032, 'fraction_numerical': 0.005685856432125089, 'mean_word_length': 10.0, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '211650', 'license': 'MIT', 'n_tokens_mistral': 570, 'n_tokens_neox': 535, 'n_words': 89, 'path': 'jupyter_notebooks/machine_learning/unsupervised/KMeans_Clustering_Warranty_Claims.ipynb', 'repo_name': 'manual123/Nacho-Jupyter-Notebooks', 'size': '1407'}