<jupyter_start><jupyter_text>IMPORTING LIBRARIES<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline


df = pd.read_csv('Mall_Customers.csv')
#used to read csv files and perform operations on it

df.head()<jupyter_output><empty_output><jupyter_text>HANDLING CATEGORICAL DATA<jupyter_code>x = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
import pandas as pd 
from sklearn.preprocessing import LabelEncoder
x = df.iloc[:, :-1].values 
y = df.iloc[:, -1].values 
labelencoder_X1 = LabelEncoder()
x[:,0] = labelencoder_X1.fit_transform(x[:,0])
x[:,1] = labelencoder_X1.fit_transform(x[:,1])
print(x)
#Here we are using LabelEncoder<jupyter_output>[[0 1 19 15]
 [1 1 21 15]
 [2 0 20 16]
 [3 0 23 16]
 [4 0 31 17]
 [5 0 22 17]
 [6 0 35 18]
 [7 0 23 18]
 [8 1 64 19]
 [9 0 30 19]
 [10 1 67 19]
 [11 0 35 19]
 [12 0 58 20]
 [13 0 24 20]
 [14 1 37 20]
 [15 1 22 20]
 [16 0 35 21]
 [17 1 20 21]
 [18 1 52 23]
 [19 0 35 23]
 [20 1 35 24]
 [21 1 25 24]
 [22 0 46 25]
 [23 1 31 25]
 [24 0 54 28]
 [25 1 29 28]
 [26 0 45 28]
 [27 1 35 28]
 [28 0 40 29]
 [29 0 23 29]
 [30 1 60 30]
 [31 0 21 30]
 [32 1 53 33]
 [33 1 18 33]
 [34 0 49 33]
 [35 0 21 33]
 [36 0 42 34]
 [37 0 30 34]
 [38 0 36 37]
 [39 0 20 37]
 [40 0 65 38]
 [41 1 24 38]
 [42 1 48 39]
 [43 0 31 39]
 [44 0 49 39]
 [45 0 24 39]
 [46 0 50 40]
 [47 0 27 40]
 [48 0 29 40]
 [49 0 31 40]
 [50 0 49 42]
 [51 1 33 42]
 [52 0 31 43]
 [53 1 59 43]
 [54 0 50 43]
 [55 1 47 43]
 [56 0 51 44]
 [57 1 69 44]
 [58 0 27 46]
 [59 1 53 46]
 [60 1 70 46]
 [61 1 19 46]
 [62 0 67 47]
 [63 0 54 47]
 [64 1 63 48]
 [65 1 18 48]
 [66 0 43 48]
 [67 0 68 48]
 [68 1 19 48]
 [69 0 32 48]
 [70 1 70 49]
 [71 0 47 49]
 [[...]<jupyter_text>SPLITTING OF DATA INTO TRAINING AND TEST<jupyter_code>from sklearn.model_selection import train_test_split

#import model selection train test split for splitting the data into test and train for model validation.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=101)<jupyter_output><empty_output><jupyter_text>NORMALIZING DATA<jupyter_code># data normalization with sklearn
from sklearn.preprocessing import MinMaxScaler

# fit scaler on training data
norm = MinMaxScaler().fit(x_train)

# transform training data
X_train_norm = norm.transform(x_train)

# transform testing data
X_test_norm = norm.transform(x_test)

print(X_train_norm)
print(X_test_norm)<jupyter_output>[[0.75757576 1.         0.48076923 0.51639344]
 [0.8030303  0.         0.23076923 0.51639344]
 [0.91919192 1.         0.53846154 0.68032787]
 [0.93434343 1.         0.23076923 0.68852459]
 [0.97474747 0.         0.38461538 0.80327869]
 [0.13636364 1.         0.32692308 0.10655738]
 [0.96969697 1.         0.28846154 0.80327869]
 [0.84343434 0.         0.28846154 0.58196721]
 [0.52525253 1.         0.59615385 0.3852459 ]
 [0.99494949 1.         0.26923077 0.90983607]
 [0.45959596 1.         0.         0.36065574]
 [0.10606061 1.         0.13461538 0.07377049]
 [0.68181818 0.         0.21153846 0.47540984]
 [0.14646465 0.         0.09615385 0.1147541 ]
 [0.48484848 0.         0.55769231 0.36885246]
 [0.60606061 1.         0.17307692 0.42622951]
 [0.23232323 0.         0.61538462 0.20491803]
 [0.38888889 1.         0.42307692 0.31967213]
 [0.28787879 1.         0.98076923 0.23770492]
 [0.41414141 1.         0.94230769 0.31967213]
 [0.33838384 0.         0.96153846 0.2704918 ]
 [0.49494949 [...]<jupyter_text>GRAPHS<jupyter_code>import matplotlib.pyplot as plt 

plt.plot(x, y) 
 

plt.show()<jupyter_output><empty_output><jupyter_text>ALGORITHM<jupyter_code>from sklearn.cluster import KMeans
num_clusters = 5
km = KMeans(n_clusters=num_clusters)<jupyter_output><empty_output><jupyter_text>FITTING OF TRAINING DATA<jupyter_code>km.fit(x_train,y_train)
#training or fitting the train data into the model<jupyter_output><empty_output><jupyter_text>PREDICTIONS FOR OUR PROBLEM STATEMENT WITH THE RELATED GRAPHS<jupyter_code>predictions = km.predict(x_test)
plt.scatter(y_test,predictions)<jupyter_output><empty_output><jupyter_text>EVALUATION<jupyter_code>from sklearn import metrics
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
print(metrics.accuracy_score(y_test, predictions))<jupyter_output>0.0125<jupyter_text>SAVING THE MODEL USING PICKLE LIBRARY<jupyter_code>import pickle 

# Save the trained model as a pickle string. 
saved_model = pickle.dumps(km) 

# Load the pickled model 
lm_from_pickle = pickle.loads(saved_model) 

# Use the loaded pickled model to make predictions 
lm_from_pickle.predict(x_test)<jupyter_output><empty_output><jupyter_text>ACCURACY w.r.t TRAINED DATA Confusion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix
y_train=y_train[0:80]
results =confusion_matrix(y_train, predictions)
print(results)<jupyter_output>[[0 0 0 ... 0 0 0]
 [0 0 1 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [1 0 0 ... 0 0 0]
 [1 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support

precision_recall_fscore_support(y_train, predictions, average='macro')<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))<jupyter_text>ACCURACY w.r.t TEST DATA Confusion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix

results =confusion_matrix(y_test, predictions)
print(results)<jupyter_output>[[0 0 0 ... 0 0 0]
 [0 1 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 1 0 ... 0 0 0]
 [0 1 0 ... 0 0 0]
 [0 1 0 ... 0 0 0]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support

precision_recall_fscore_support(y_test, predictions, average='macro')<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
{'fraction_non_alphanumeric': 0.1143947963800905, 'fraction_numerical': 0.17081447963800905, 'mean_word_length': 3.9844961240310077, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '563894', 'license': 'MIT', 'n_tokens_mistral': 3639, 'n_tokens_neox': 2767, 'n_words': 811, 'path': 'K-means/ML-ASGN-KM-05/code/mall_cust_km.ipynb', 'repo_name': 'RASHI3004/Project-Internship---ML-DL', 'size': '7072'}