<jupyter_start><jupyter_text>Grid Search No. 1<jupyter_code># Import the required libraries.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Read the data.
data = pd.read_csv('heart.csv')
# Read the first lines of the data.
data.head()
# Now let's perform some EDAs (Exploratory Data Analysis).
# Show information about the targets.
data.target.value_counts()
# Calculate the mean of targets grouped by sex.
# We can use this command instead:
# data.groupby('sex')['target'].mean()
data.groupby('sex').target.mean()
# Use seaborn library to plot the count of targets.
sns.countplot(data=data, x='target')
# Use seaborn library to plot the count of 'sex' column.
sns.countplot(data=data, x= 'sex')
# Show the histogram of the 'age' column.
data['age'].hist()
# Devide the input data and target.
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# Input Data.
X
# Target.
y
# Prepare train and test datasets.
# In order to do that, we can use a specific package in Sci-Kit library.
from sklearn.model_selection import train_test_split
# Specify the train and test inputs and targets.
# Note that we specify the randon state.
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=25)
# Use KNN algorithm for classification.
from sklearn.neighbors import KNeighborsClassifier
# Create a new KNN classifier with 5 neighbors.
knn_classifier = KNeighborsClassifier(n_neighbors=5)
# Learn from the data.
knn_classifier.fit(X_train, y_train)
# Now let's evaluate our model with specific metrics.
# So we import specific metrics form Sci-Kit library.
from sklearn import metrics
# Save the prediction of our model.
y_predicted = knn_classifier.predict(X_test)
# Present the classification report.
print(metrics.classification_report(y_test, y_predicted))
# Present the confusion matrix.
print(metrics.confusion_matrix(y_test, y_predicted))
# Create a new KNN classifier with 7 neighbors.
knn_classifier7 = KNeighborsClassifier(n_neighbors=7)
# Learn from the data.
knn_classifier7.fit(X_train, y_train)
# Save the prediction of our model.
y_predicted7 = knn_classifier7.predict(X_test)
# Present the classification report.
print(metrics.classification_report(y_test, y_predicted7))
# Present the confusion matrix.
print(metrics.confusion_matrix(y_test, y_predicted7))
# Create a new KNN classifier with 3 neighbors.
knn_classifier3 = KNeighborsClassifier(n_neighbors=3)
# Learn from the data.
knn_classifier3.fit(X_train, y_train)
# Save the prediction of our model.
y_predicted3 = knn_classifier3.predict(X_test)
# Present the classification report.
print(metrics.classification_report(y_test, y_predicted3))
# Present the confusion matrix.
print(metrics.confusion_matrix(y_test, y_predicted3))
# Now Use SVM algorithm for classification.
from sklearn.svm import SVC
# Create a new and specific SVM classifier.
svm_classifier = SVC(C=15, kernel='linear', random_state=25)
# Learn from the data.
svm_classifier.fit(X_train, y_train)
# Save the prediction of our model.
y_predicted_svm = svm_classifier.predict(X_test)
# Present the classification report.
print(metrics.classification_report(y_test, y_predicted_svm))
# Present the confusion matrix.
# It seems that, in this case, SVM algorithm is better than KNN algorithm.
print(metrics.confusion_matrix(y_test, y_predicted_svm))
# Introduce grid search.
# Use grid search from Sci-Kit Library.
from sklearn.model_selection import GridSearchCV
# Specify the parameters for grid search.
parameters = {
    'C': [0, 1, 10],
    'kernel': ['linear'],
    'gamma': ['auto', 0.1]
}
# Create a new grid search.
grid_search = GridSearchCV(SVC(), param_grid=parameters, scoring='accuracy', cv=2, n_jobs=-1, verbose=10)
# Learn from the data.
# Start to grid search for all data.
grid_search.fit(X, y)
# Determine the best estimator.
grid_search.best_estimator_
# Determine the best score.
grid_search.best_score_
# Determine the best parameters.
grid_search.best_params_
# Now let's create this specific SVM classifier.
best_svm_classifier = SVC(C= 1, gamma= 'auto', kernel= 'linear')
# Learn from the data.
best_svm_classifier.fit(X_train, y_train)
# Save the prediction of our model.
y_predicted_best_svm = best_svm_classifier.predict(X_test)
# Present the classification report.
print(metrics.classification_report(y_test, y_predicted_best_svm))
# Present the confusion matrix.
print(metrics.confusion_matrix(y_test, y_predicted_best_svm))
# Now Let's do the grid search only for train data. 
# Create a new grid search fot train data.
grid_search_train = GridSearchCV(SVC(), param_grid=parameters, scoring='accuracy', cv=2, n_jobs=-1, verbose=10)
# Learn from the data.
# Start to grid search for train data.
grid_search_train.fit(X_train, y_train)
# Determine the best estimator.
grid_search_train.best_estimator_
# Determine the best score.
grid_search_train.best_score_
# Determine the best parameters.
# The result is same.
grid_search.best_params_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09143890675241158, 'fraction_numerical': 0.008440514469453377, 'mean_word_length': 5.725675675675675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '101001', 'license': 'MIT', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1594, 'n_words': 524, 'path': 'Grid_Search1.ipynb', 'repo_name': 'farazattar/Python', 'size': '4976'}