<jupyter_start><jupyter_text>KNN CLASSIFIER<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=30)
knn.fit(X_train,y_train)

pred = knn.predict(X_train)

print("Training set score:{:.2f}".format(knn.score(X_train,y_train)*100))
print("Testing set score:{:.2f}".format(knn.score(X_test,y_test)*100))
import matplotlib.pyplot as plt
training_accuracy = []
testing_accuracy  = []

neighbors_settings = range(1,40)


for n_neighbors in neighbors_settings:
    knn = KNeighborsClassifier(n_neighbors = n_neighbors)
    knn.fit(X_train,y_train)
    training_accuracy.append(knn.score(X_train,y_train))
    testing_accuracy.append(knn.score(X_test,y_test))
    
#print("training accuracy of the model".format(training_accuracy))

#print("Tesing accuracy of the model:\n{}".format(testing_accuracy))
    

plt.plot(neighbors_settings,training_accuracy,label = "training accuracy")
plt.plot(neighbors_settings,testing_accuracy,label = "testing accuracy")

plt.ylabel("accuracy")
plt.xlabel("n_neighbors")
plt.legend()
#FEATURE IMPORTANCES

from sklearn.inspection import permutation_importance

knn.fit(X,y)

results = permutation_importance(knn,X,y,scoring="accuracy") # performs permutation importances
imp = results.importances_mean  # get importances

for i,v in enumerate(imp):
    print("Feature: %0d, score: %.3f"%(i,v))
    
plt.bar([X for X in range(len(imp))],imp)
plt.show()<jupyter_output>Feature: 0, score: 0.000
Feature: 1, score: -0.001
Feature: 2, score: 0.024
Feature: 3, score: -0.000
Feature: 4, score: -0.000
Feature: 5, score: -0.000
Feature: 6, score: 0.000
Feature: 7, score: -0.001
Feature: 8, score: 0.000<jupyter_text>RANDOM FOREST<jupyter_code>#Random forest

from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier(n_estimators=10,random_state=42,max_depth=3,n_jobs=-1)
rfc.fit(X_train,y_train)
pred = rfc.predict(X_train)

print("Training set score:{:.2f}".format(rfc.score(X_train,y_train)*100))
print("Testing set score:{:.2f}".format(rfc.score(X_test,y_test)*100))

training_accuracy = []
testing_accuracy = []

estimators_settings = range(1,50)

for n_estimators in estimators_settings:
    rfc = RandomForestClassifier(n_estimators)
    rfc.fit(X_train,y_train)
    training_accuracy.append(rfc.score(X_train,y_train)*100)
    testing_accuracy.append(rfc.score(X_test,y_test)*100)
    
plt.plot(estimators_settings,training_accuracy,label="training accuracy")
plt.plot(estimators_settings,testing_accuracy,label="testing accuracy")

plt.ylabel("accuracy")
plt.xlabel("n_estimators")
plt.legend()
    
rfc.fit(X,y)

imp = rfc.feature_importances_

for i,v in enumerate(imp):
    print("Feature:%0d , score: %.2f"%(i,v))

plt.bar([X for X in range(len(imp))],imp)
plt.show()<jupyter_output>Feature:0 , score: 0.12
Feature:1 , score: 0.12
Feature:2 , score: 0.12
Feature:3 , score: 0.12
Feature:4 , score: 0.13
Feature:5 , score: 0.10
Feature:6 , score: 0.10
Feature:7 , score: 0.10
Feature:8 , score: 0.10<jupyter_text>GRADIENT BOOSTER REGRESSION<jupyter_code># Gradient Booster Regression
from sklearn.ensemble import GradientBoostingClassifier

gbrt = GradientBoostingClassifier(random_state=42)

gbrt.fit(X_train,y_train)

print("Training set score:{:.2f}".format(gbrt.score(X_train,y_train)*100))
print("Testing set score:{:.2f}".format(gbrt.score(X_test,y_test)*100))<jupyter_output>Training set score:72.89
Testing set score:60.32<jupyter_text>LOGISTIC REGRESSION<jupyter_code># logistic regression

# implementation with all the features

from sklearn.linear_model import LogisticRegression

lreg = LogisticRegression().fit(X_train,y_train)

pred = lreg.predict(X_train)

print("Training set score:{:.2f}".format(lreg.score(X_train,y_train)*100))
print("Testing set score:{:.2f}".format(lreg.score(X_test,y_test)*100))

lreg10 = LogisticRegression(C=10).fit(X_train,y_train)

print("\nTraining set score with C=10:\n{:.2f}".format(lreg10.score(X_train,y_train)*100))
print("Testing set score with C=10:\n{:.2f}".format(lreg10.score(X_test,y_test)*100))


lreg100 = LogisticRegression(C=100).fit(X_train,y_train)

print("\nTraining set score with C=100:\n{:.2f}".format(lreg100.score(X_train,y_train)*100))
print("Testing set score with C=100:\n{:.2f}".format(lreg100.score(X_test,y_test)*100))



lreg001 = LogisticRegression(C=0.01).fit(X_train,y_train)



print("\nTraining set score with C=0.01:\n{:.2f}".format(lreg001.score(X_train,y_train)*100))
print("Testing set score with C=0.01:\n{:.2f}".format(lreg001.score(X_test,y_test)*100))

lreg.fit(X,y)
importance = lreg.coef_[0]
for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
plt.bar([x for x in range(len(importance))], importance)
plt.show()

# since feature 6 i.e, organic_carbon is triggering a huge loss we will eliminate that feature

X1 = X.drop('Organic_carbon',axis=1)
X1

X1_train,X1_test,y_train,y_test = train_test_split(X1,y,random_state=0)

lreg0 = LogisticRegression().fit(X1_train,y_train)

print("training score after removing feature 6:\n{:.2f}".format(lreg0.score(X1_train,y_train)*100))

print("testing score after removing feature 6:\n{:.2f}".format(lreg0.score(X1_test,y_test)*100))
lreg.fit(X1,y)
importance = lreg.coef_[0]
for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
#plot feature importance
plt.bar([x for x in range(len(importance))], importance)
plt.show()
X2 = X1.drop('Hardness',axis=1)
X2
X2_train,X2_test,y_train,y_test = train_test_split(X2,y,random_state=0)

lreg1 = LogisticRegression().fit(X2_train,y_train)

print("training score after removing feature 6 & 1:\n{:.2f}".format(lreg1.score(X2_train,y_train)*100))

print("testing score after removing feature 6 & 1:\n{:.2f}".format(lreg1.score(X2_test,y_test)*100))<jupyter_output>training score after removing feature 6 & 1:
60.97
testing score after removing feature 6 & 1:
61.29
{'fraction_non_alphanumeric': 0.150067521944632, 'fraction_numerical': 0.048615800135043886, 'mean_word_length': 7.039348710990502, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '494599', 'license': 'MIT', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2307, 'n_words': 392, 'path': 'Water Potability/water potability calssification.ipynb', 'repo_name': 'sandilya761/Machine-Learning', 'size': '5924'}