<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('driver-data.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>[[1487.     71.24]
 [2764.     52.53]
 [3276.     64.54]
 ...
 [2473.    170.91]
 [3182.    176.14]
 [1085.    168.03]]<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.98749687 0.214725  ]
 [0.23030758 0.09547695]
 [0.69667417 0.18929646]
 ...
 [0.00950238 0.12893095]
 [0.5743936  0.24434073]
 [0.48387097 0.14956165]]
[[0.95648912 0.07781219]
 [0.73968492 0.06115061]
 [0.51312828 0.15514459]
 ...
 [0.6364091  0.17743272]
 [0.26056514 0.23138657]
 [0.40185046 0.17171893]]<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.054375<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:1600]

results =confusion_matrix(y_train, predictions)
print(results)<jupyter_output>[[ 3  0  0 ...  0  0  0]
 [ 8  5  6 ...  0  0  0]
 [13 16 18 ...  0  0  0]
 ...
 [ 1  0  0 ...  0  0  0]
 [ 0  0  1 ...  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))<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>[[ 2  5  2 ...  0  0  0]
 [13 12 11 ...  0  0  0]
 [11 10 14 ...  0  0  0]
 ...
 [ 0  0  1 ...  0  0  0]
 [ 1  0  0 ...  0  0  0]
 [ 1  0  1 ...  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))
StarcoderdataJupyterStructuredCleanDedup