<jupyter_start><jupyter_text>A sample ML classification project<jupyter_code>pip install nbconvert

# Define classification<jupyter_output><empty_output><jupyter_text>Assumes data vizualization and pre-possing is already achieved  Step 1: Load the Data<jupyter_code>from sklearn import datasets
iris_data = datasets.load_iris()
# Define target value, feature value
iris_data.target
iris_data.data[:5]
iris_data.feature_names
iris_data.target_names<jupyter_output><empty_output><jupyter_text>Step 2: Create train, test, validate<jupyter_code># Why we need train test?
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(ir.data, ir.target, test_size=0.3)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2)
# Explain train-test ratio, random_seed<jupyter_output><empty_output><jupyter_text>Step 3: Initialise the classification model<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5)
# Explain basic working of knn
knn.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Step 4-5: Evaluation & Prediction<jupyter_code># Perform model evaluation on validate/test set, explain confusion matrix, accuracy
# Explain prediction step
from sklearn import metrics
y_val_pred = knn.predict(X_val)
print("Accuracy:",metrics.accuracy_score(y_val, y_val_pred))
y_test_pred = knn.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_test_pred))<jupyter_output>Accuracy: 0.9333333333333333<jupyter_text>Step 6: HyperParameter Tuining<jupyter_code># Define learned parameter, hyperparameter, curse of dim, over/under fitting
# Explain k-fold-cross-validation, GridSearch
from sklearn.model_selection import GridSearchCV
parameters = {'n_neighbors':[1,2,3,4], 'p':[1,3]}
# Explain n_neighbour, and P for knn
model = GridSearchCV(knn, param_grid=parameters,cv=4)
model.fit(X_train,y_train)<jupyter_output>c:\users\cs\appdata\local\programs\python\python37-32\lib\site-packages\sklearn\model_selection\_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.
  DeprecationWarning)<jupyter_text>Step 7: Finding the best model<jupyter_code>model.best_estimator_
model.best_score_
knn_best=model.best_estimator_
knn_best.score(X_val,y_val)
knn_best.score(X_test,y_test)
y_test_pred = knn_best.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_test_pred))
# Try a different classifier
from sklearn.tree import DecisionTreeClassifier
df=DecisionTreeClassifier()
df.fit(X_train,y_train)
y_test_pred = df.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_test_pred))
# Step 8: Save, load, serve model(H.W)
# Mention flask API, pickel, model_persistance<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09612083762444215, 'fraction_numerical': 0.017507723995880537, 'mean_word_length': 8.713333333333333, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '198451', 'license': 'MIT', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1028, 'n_words': 216, 'path': '3_2_Sample_project_knn.ipynb', 'repo_name': 'BRAMHA12/Py_Machine-Learning', 'size': '2913'}