<jupyter_start><jupyter_text>Classification with Python<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import svm
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.metrics import jaccard_score
from sklearn.metrics import f1_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss<jupyter_output><empty_output><jupyter_text>Load Dataset<jupyter_code>df = pd.read_csv('/Users/Asus/Documents/Atmel Studio/cbb.csv')
df.head().T<jupyter_output><empty_output><jupyter_text>**Next we'll add a column that will contain "true" if the wins above bubble are over 7 and "false" if not. We'll call this column Win Index or "windex" for short**<jupyter_code>df["windex"] = np.where(df.WAB > 7, "True", "False")<jupyter_output><empty_output><jupyter_text>Data visualization and pre-processing<jupyter_code>df1 = df.loc[df["POSTSEASON"].str.contains('F4|S16|E8', na = False)]
df1.head()
df1['POSTSEASON'].value_counts()
bins = np.linspace(df1.BARTHAG.min(), df1.BARTHAG.max(), 10)
g = sns.FacetGrid(df1, col="windex", hue="POSTSEASON", palette="Set1", col_wrap=6)
g.map(plt.hist, 'BARTHAG', bins=bins, ec="k")

g.axes[-1].legend()
plt.show()
bins = np.linspace(df1.ADJOE.min(), df1.ADJOE.max(), 10)
g = sns.FacetGrid(df1, col="windex", hue="POSTSEASON", palette="Set1", col_wrap=2)
g.map(plt.hist, 'ADJOE', bins=bins, ec="k")

g.axes[-1].legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Pre-processing: Feature selection/extraction<jupyter_code>bins = np.linspace(df1.ADJDE.min(), df1.ADJDE.max(), 10)
g = sns.FacetGrid(df1, col="windex", hue="POSTSEASON", palette="Set1", col_wrap=2)
g.map(plt.hist, 'ADJDE', bins=bins, ec="k")
g.axes[-1].legend()
plt.show()<jupyter_output><empty_output><jupyter_text>**Convert Categorical features to numerical values**<jupyter_code>df1.groupby(['windex'])['POSTSEASON'].value_counts(normalize=True)
df1['windex'].replace(to_replace=['False','True'], value=[0,1],inplace=True)
df1.head()<jupyter_output>D:\New folder\anaconda\lib\site-packages\pandas\core\generic.py:6746: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._update_inplace(new_data)<jupyter_text>Feature Selection<jupyter_code>X = df1[['G', 'W', 'ADJOE', 'ADJDE', 'BARTHAG', 'EFG_O', 'EFG_D',
       'TOR', 'TORD', 'ORB', 'DRB', 'FTR', 'FTRD', '2P_O', '2P_D', '3P_O',
       '3P_D', 'ADJ_T', 'WAB', 'SEED', 'windex']]
X[0:5]
y = df1['POSTSEASON'].values
y[0:5]<jupyter_output><empty_output><jupyter_text>Normalize Data<jupyter_code>X = StandardScaler().fit(X).transform(X)
X[0:5]<jupyter_output><empty_output><jupyter_text>Training and Validation<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 4)<jupyter_output><empty_output><jupyter_text>Classification<jupyter_code>K Nearest Neighbor(KNN)
Decision Tree
Support Vector Machine
Logistic Regression<jupyter_output><empty_output><jupyter_text>**K Nearest Neighbor(KNN)**<jupyter_code>k_array = []
for n in range(1, 15):
    knn = KNeighborsClassifier(n_neighbors = n)
    knn.fit(x_train, y_train)
    yhat = knn.predict(x_test)
    accuracy = accuracy_score(y_test, yhat)
    k_array.append(accuracy)

np.array(k_array)<jupyter_output><empty_output><jupyter_text>**Decision Tree**<jupyter_code>Tree_array = []
for x in range(1, 10):
    Tree = DecisionTreeClassifier(criterion = "entropy", max_depth = x)
    Tree.fit(x_train, y_train)
    yhat = Tree.predict(x_test)
    accuracy = accuracy_score(y_test, yhat)
    Tree_array.append(accuracy)
np.array(Tree_array)
best_depth = Tree_array.index(max(Tree_array))
print("Best Depth index = " + str(best_depth))
print("Best Depth Accuracy = " + str(Tree_array[best_depth]))<jupyter_output>Best Depth index = 0
Best Depth Accuracy = 0.6666666666666666<jupyter_text>**Support Vector Machine**<jupyter_code>f1score = []
kernel = ["linear", "poly", "rbf", "sigmoid"]
for k in kernel:
    clf = svm.SVC(kernel = k)
    clf.fit(x_train, y_train)
    yhat = clf.predict(x_test)
    accuracy = f1_score(y_test, yhat, average = "weighted")
    f1score.append(accuracy)
svm_accuracy = {"linear": f1score[0],
                "poly": f1score[1],
                "rbf": f1score[2],
                "sigmoid": f1score[3]}
svm_accuracy
print("Best SVM = " + str(max(svm_accuracy.items(), key = lambda x: x[1])))<jupyter_output>Best SVM = ('sigmoid', 0.5372549019607843)<jupyter_text>**Logistic Regression**<jupyter_code>logistic_solvers = ["liblinear", "lbfgs", "newton-cg", "sag", "saga"]
average = ["micro", "macro", "weighted"]
Accuracy = []
for y in average:
    for x in logistic_solvers:
        LR = LogisticRegression(C = 0.01, solver = x)
        LR.fit(x_train, y_train)
        yhat = LR.predict(x_test)
        jaccard = jaccard_score(y_test, yhat, average = y)
        Accuracy.append(jaccard)
print(Accuracy)<jupyter_output>[0.4117647058823529, 0.4117647058823529, 0.4117647058823529, 0.4117647058823529, 0.4117647058823529, 0.5370370370370371, 0.19444444444444445, 0.19444444444444445, 0.19444444444444445, 0.19444444444444445, 0.49074074074074076, 0.3888888888888889, 0.3888888888888889, 0.3888888888888889, 0.3888888888888889]<jupyter_text>Model Evaluation using Test set  Import Data<jupyter_code>test_df = pd.read_csv("basketball_train.csv")
test_df
test_df["windex"] = np.where(test_df.WAB > 7, "True", "False")
test_df1 = test_df[test_df["POSTSEASON"].str.contains("F4|S18|E8", na = False)]
test_feature = test_df1[['G', 'W', 'ADJOE', 'ADJDE', 'BARTHAG', 'EFG_O', 'EFG_D',
       'TOR', 'TORD', 'ORB', 'DRB', 'FTR', 'FTRD', '2P_O', '2P_D', '3P_O',
       '3P_D', 'ADJ_T', 'WAB', 'SEED', 'windex']]
test_feature["windex"].replace(to_replace = ["False", "True"], value = [0, 1], inplace = True)
test_X = test_feature
test_X = StandardScaler().fit(test_X).transform(test_X)
test_X[0:5]
test_y = test_df1["POSTSEASON"].values
test_y[0:5]<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(test_X, test_y, test_size = 0.2, random_state = 4)<jupyter_output><empty_output><jupyter_text>KNN<jupyter_code>knn = KNeighborsClassifier(n_neighbors = 4)
knn.fit(x_train, y_train)
yhat = knn.predict(x_test)
yhat[0:5]<jupyter_output><empty_output><jupyter_text>**Accuracy**<jupyter_code>accuracy = accuracy_score(y_test, yhat)
print("Accuracy Score of KNN = " + str(accuracy))<jupyter_output>Accuracy Score of KNN = 0.6666666666666666<jupyter_text>**Jaccard**<jupyter_code>jaccard = jaccard_score(y_test, yhat, average = "micro")
print("Jaccard Score of KNN = " + str(jaccard))<jupyter_output>Jaccard Score of KNN = 0.5<jupyter_text>**f1_score**<jupyter_code>f1 = f1_score(y_test, yhat, average = "micro")
print("f1 score of KNN = " + str(f1))<jupyter_output>f1 score of KNN = 0.6666666666666666<jupyter_text>Decision Tree<jupyter_code>Tree = DecisionTreeClassifier(criterion = "entropy", max_depth = 1)
Tree.fit(x_train, y_train)
yhat = Tree.predict(x_test)
yhat[0:5]<jupyter_output><empty_output><jupyter_text>**Accuracy**<jupyter_code>accuracy = accuracy_score(y_test, yhat)
print("Accuracy score of Decision Tree = " + str(accuracy))<jupyter_output>Accuracy score of Decision Tree = 0.5<jupyter_text>**Jaccard**<jupyter_code>jaccard = jaccard_score(y_test, yhat, average = "micro")
print("Jaccard score of Decision Tree = " + str(jaccard))<jupyter_output>Jaccard score of Decision Tree = 0.3333333333333333<jupyter_text>**f1_score**<jupyter_code>f1 = f1_score(y_test, yhat, average = "micro")
print("f1 score of Decision Tree = " + str(f1))<jupyter_output>f1 score of Decision Tree = 0.5<jupyter_text>SVM<jupyter_code>clf = svm.SVC(kernel = "rbf")
clf.fit(x_train, y_train)
yhat = clf.predict(x_test)
yhat[0:5]<jupyter_output><empty_output><jupyter_text>**Accuracy**<jupyter_code>accuracy = accuracy_score(y_test, yhat)
print("Accuracy score of SVM = " + str(accuracy))<jupyter_output>Accuracy score of SVM = 0.6666666666666666<jupyter_text>**Jaccard**<jupyter_code>jaccard = jaccard_score(y_test, yhat, average = "micro")
print("Jaccard score of SVM = " + str(jaccard))<jupyter_output>Jaccard score of SVM = 0.5<jupyter_text>**f1_score**<jupyter_code>f1 = f1_score(y_test, yhat, average = "micro")
print("f1 score of SVM = " + str(f1))<jupyter_output>f1 score of SVM = 0.6666666666666666<jupyter_text>Logistic Regression<jupyter_code>LR = LogisticRegression(C = 0.01, solver = "liblinear")
LR.fit(x_test, y_test)
yhat = LR.predict(x_test)
yhat[0:5]<jupyter_output><empty_output><jupyter_text>**Accuracy**<jupyter_code>accuracy = accuracy_score(y_test, yhat)
print("Accuracy Score = " + str(accuracy))<jupyter_output>Accuracy Score = 0.8333333333333334<jupyter_text>**Jaccard Index**<jupyter_code>jaccard = jaccard_score(y_test, yhat, average = "micro")
print("Jaccard Score = " + str(jaccard))<jupyter_output>Jaccard Score = 0.7142857142857143<jupyter_text>**f1_score**<jupyter_code>f1 = f1_score(y_test, yhat, average = "micro")
print("f1_score = " + str(f1))<jupyter_output>f1_score = 0.8333333333333334<jupyter_text>**log loss**<jupyter_code>yhat_prob = LR.predict_proba(x_test)
log = log_loss(y_test, yhat_prob)
print("log loss = " + str(log))<jupyter_output>log loss = 0.6397720819433546
{'fraction_non_alphanumeric': 0.15145872129112353, 'fraction_numerical': 0.06134905855576247, 'mean_word_length': 7.297854077253219, 'pattern_counts': {'":': 4, '<': 116, '<?xml version=': 0, '>': 118, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 16, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '33', 'dir': 'jupyter-structured-clean-dedup', 'id': '264839', 'license': 'Apache-2.0', 'n_tokens_mistral': 4370, 'n_tokens_neox': 3938, 'n_words': 640, 'path': 'Jupiter Book/Prediction for Basket Ball Team in Semi Final.ipynb', 'repo_name': 'Pandula1234/PythonDeepSource', 'size': '9666'}