<jupyter_start><jupyter_text>Model Selection<jupyter_code>from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
pred = model.predict(X_test)
print("Train accuracy: ", model.score(X_train,y_train))
print("Test accuracy: ", model.score(X_test,y_test))
print(classification_report(y_test,pred))
# The Decision tree Classifier
from sklearn.tree import DecisionTreeClassifier 
# Create Decision Tree classifer object
dtc = DecisionTreeClassifier()
# Train Decision Tree Classifer
dtc.fit(X_train, y_train)
#Predict the response for test dataset
y_pred = dtc.predict(X_test)
# model Evaluation
acc_dtc = accuracy_score(y_test, y_pred)
acc_dtc
print(classification_report(y_test, y_pred))
from sklearn.metrics import recall_score
tpr_dtc = recall_score(y_test, y_pred)
tpr_dtc
from sklearn.tree import DecisionTreeRegressor
# build model
dtr = DecisionTreeRegressor()
# fit classifiers
dtr.fit(X_train, y_train)
# Prediction
y_pred = dtr.predict(X_test)

# model Evaluation
acc_dtr = accuracy_score(y_test, y_pred)
acc_dtr
print(classification_report(y_test, y_pred))
tpr_dtr = recall_score(y_test, y_pred)
tpr_dtr
# The KNN Classifier
from sklearn.neighbors import KNeighborsClassifier
# build model
knn_model = KNeighborsClassifier()
# fit classifiers
knn_model.fit(X_train, y_train)
# Prediction
y_pred = knn_model.predict(X_test)

# model Evaluation
acc_knn = accuracy_score(y_test, y_pred)
acc_knn
tpr_knn = recall_score(y_test, y_pred)
tpr_knn
print(classification_report(y_test,y_pred))
# the SVM Classifier
from sklearn import svm
# build model
svm_model = svm.SVC()
# fit classifiers
svm_model.fit(X_train, y_train)
# Prediction
y_pred = svm_model.predict(X_test)
# model Evaluation
acc_svm = accuracy_score(y_test, y_pred)
acc_svm
tpr_svm = recall_score(y_test, y_pred)
tpr_svm
print(classification_report(y_test,y_pred))
from sklearn.neural_network import MLPClassifier
mlp_model = MLPClassifier()
# fit classifiers
mlp_model.fit(X_train, y_train)
# Prediction
y_pred = mlp_model.predict(X_test)
# model Evaluation
acc_mlp = accuracy_score(y_test, y_pred)
acc_mlp
tpr_mlp = recall_score(y_test, y_pred)
tpr_mlp
print(classification_report(y_test,y_pred))
# Linear Discriminant Analysis
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
# build model
lda = LinearDiscriminantAnalysis()
# fit classifiers
lda.fit(X_train, y_train)
# Prediction
y_pred = lda.predict(X_test)
# model Evaluation
acc_lda = accuracy_score(y_test, y_pred)
acc_lda
tpr_lda = recall_score(y_test, y_pred)
tpr_lda
print(classification_report(y_test,y_pred))
# The Logistic Regression Classifier
from sklearn.linear_model import LogisticRegression
# build model
log_model = LogisticRegression()
# fit classifiers
log_model.fit(X_train, y_train)
# Prediction
y_pred = log_model.predict(X_test)

# model Evaluation
acc_log = accuracy_score(y_test, y_pred)
acc_log
tpr_log = recall_score(y_test, y_pred)
tpr_log
print(classification_report(y_test,y_pred))
# The Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier 
# build model
rf_model = RandomForestClassifier()
# Fitting the classifier
rf_model.fit(X_train, y_train)
# Prediction
y_pred = rf_model.predict(X_test)
# model Evaluation
acc_rf = accuracy_score(y_test, y_pred)
acc_rf
tpr_rf = recall_score(y_test, y_pred)
tpr_rf
print(classification_report(y_test,y_pred))
# Gradient Boosting Classifier
from sklearn.ensemble import GradientBoostingClassifier
# build model
gbc = GradientBoostingClassifier()
# Fitting the classifier
gbc.fit(X_train, y_train)
# Prediction
y_pred = gbc.predict(X_test)
# model Evaluation
acc_gbc = accuracy_score(y_test, y_pred)
acc_gbc
tpr_gbc = recall_score(y_test, y_pred)
tpr_gbc
print(classification_report(y_test,y_pred))
df = pd.DataFrame(np.array([[acc_dtc, tpr_dtc], [acc_dtr, tpr_dtr], [acc_knn, tpr_knn], [acc_svm, tpr_svm], [acc_mlp, tpr_mlp], [acc_lda, tpr_lda], 
                            [acc_log, tpr_log], [acc_rf, tpr_rf], [acc_gbc, tpr_gbc]]),
                  columns=['accuracy', 'TPR'], 
                  index=['Decision Tree Classifier', 'Decision Tree Regressor', 'KNeighbors Classifier', 'Support Vector Machines Classifier', 'Multi-layer Perceptron Classifier', 
                  'Linear Discriminant Analysis', 'Logistic Regression', 'Random Forest Classifier', 'Gradient Boosting'])
df
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
grid_values = {'n_estimators': [100], 'learning_rate': [0.1]}
gbc = GradientBoostingClassifier()
gbc_cv = GridSearchCV(gbc, param_grid=grid_values, cv=10, n_jobs=-1)
gbc_cv.fit(X_train, y_train)
y_pred = gbc_cv.predict(X_test)
acc_gbc_cv = accuracy_score(y_test, y_pred)
acc_gbc_cv
y_pred = gbc_cv.predict(X_test)
acc_gbc_cv10 = accuracy_score(y_test, y_pred)
acc_gbc_cv10
tpr_gbc_cv = recall_score(y_test, y_pred)
tpr_gbc_cv
df = pd.DataFrame(np.array([[acc_gbc, tpr_gbc], [acc_gbc_cv, tpr_gbc_cv]]),
                  columns=['accuracy', 'TPR'], 
                  index=['Gradient Boosting Classifier', 'Gradient Boosting Classifier with CV'])
df
# Random Forest Classifier with CV
grid_values = {'max_features': ["auto"], 'min_samples_split': [2], 'n_estimators': [100]}
rf = RandomForestClassifier()
rf_cv = GridSearchCV(rf, param_grid=grid_values, cv=5,n_jobs=-1)
rf_cv.fit(X_train, y_train)
y_pred = rf_cv.predict(X_test)
acc_rf_cv = accuracy_score(y_test, y_pred)
acc_rf_cv
tpr_rf_cv = recall_score(y_test, y_pred)
tpr_rf_cv
rf_cv.best_params_
df = pd.DataFrame(np.array([[acc_rf, tpr_rf], [acc_rf_cv, tpr_rf_cv]]),
                  columns=['accuracy', 'TPR'], 
                  index=['Random Forest Classifier', 'Random Forest Classifier with CV'])
df
grid_values = {'criterion': ["squared_error"]}
dtr = DecisionTreeRegressor()
dtr_cv = GridSearchCV(dtr, param_grid=grid_values, cv=5,n_jobs=-1)
dtr_cv.fit(X_train, y_train)
y_pred = dtr_cv.predict(X_test)
acc_dtr_cv = accuracy_score(y_test, y_pred)
acc_dtr_cv
tpr_dtr_cv = recall_score(y_test, y_pred)
tpr_dtr_cv
df = pd.DataFrame(np.array([[acc_dtr, tpr_dtr], [acc_dtr_cv, tpr_dtr_cv]]),
                  columns=['accuracy', 'TPR'], 
                  index=['Decision Tree Regressor', 'Decision Tree Regressor with CV'])
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1028944020356234, 'fraction_numerical': 0.0031806615776081423, 'mean_word_length': 6.4869047619047615, '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': '356664', 'license': 'MIT', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2358, 'n_words': 477, 'path': 'Sales_Analysis_model.ipynb', 'repo_name': 'YingluDeng/Education-Company-Data-ML', 'size': '6288'}