<jupyter_start><jupyter_text>Lab 3 (Data Classification)<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import io
import matplotlib.pyplot as plt
from sklearn import preprocessing
from scipy import stats
from sklearn.decomposition import PCA
from sklearn.feature_selection import SelectKBest,chi2
from sklearn.preprocessing import MinMaxScaler
from sklearn import metrics
from matplotlib import cm as cm

from google.colab import files
uploaded = files.upload()
names = ['fLength','fWidth','fSize','fConc','fConc1','fAsym','fM3Long','fM3Trans','fAlpha','fDist','class']
df = pd.read_csv(io.StringIO(uploaded['data.csv'].decode('utf-8')) ,names = names)
df['class'].value_counts()<jupyter_output><empty_output><jupyter_text>Data Balancing<jupyter_code>df_majority = df.loc[df['class'] == 'g']
df_minority = df.loc[df['class'] == 'h']
count_class_g , count_class_h = df['class'].value_counts()

df_majority_downsampled = df_majority.sample(count_class_h)
df_downsampled = pd.concat([df_majority_downsampled, df_minority])
df_downsampled['class'].value_counts()<jupyter_output><empty_output><jupyter_text>Data Visualisation After Balancing<jupyter_code>for attr1 in names[:10]:
    for attr2 in names[:10] :
        if (attr1 != attr2):
          facet = sns.lmplot(data=df_downsampled, x=attr1, y=attr2, hue='class', 
                  markers=["o", "x"])
          plt.show()
#Box plot
for ind in range(len(names[:10])):
    boxplot = df_downsampled.boxplot(column=names[ind], by=['class'])
    plt.show()  
# majority histogram
df_majority.hist()
# minority hisogram
df_minority.hist()
df_downsampled.plot.line()
#correlation matrix
coef_correlation = df_downsampled.corr(method="pearson")
x=[i for i in range(0,10)]
fig = plt.figure()
ax1 = fig.add_subplot(111)
cmap = cm.get_cmap('jet', 30)
cax = ax1.imshow(df.corr(), interpolation="nearest", cmap=cmap)

plt.title('Feature Coefficient Correlation')
plt.xticks(x,coef_correlation.columns.values,fontsize=15,rotation="vertical")
plt.yticks(x,coef_correlation.columns.values,fontsize=15)
plt.colorbar(cax)
plt.imshow(coef_correlation)

plt.show()
#covariance matrix
covariance=df_downsampled.cov()
plt.figure()
sns.heatmap(covariance, cmap=sns.diverging_palette(220, 10, as_cmap=True),
            xticklabels=covariance.columns.values,
         yticklabels=covariance.columns.values)<jupyter_output><empty_output><jupyter_text>Data Split<jupyter_code>train, test = train_test_split(df_downsampled, test_size=0.3)
print('train size',len(train))
print('test size',len(test))<jupyter_output>train size 9363
test size 4013<jupyter_text>Preprocessing<jupyter_code>from sklearn.preprocessing import LabelEncoder
train_array = train.iloc[:,:-1].values
test_array = test.iloc[:,:-1].values
train_labels = train.iloc[:,-1].values
test_labels = test.iloc[:,-1].values
orginal_train = train_array
orginal_test = test_array
#min_max normalisation
scaler = MinMaxScaler()
train_array = scaler.fit_transform(train_array)
test_array = scaler.fit_transform(test_array)
train.shape

#feature selection on train
train_new = SelectKBest(chi2, k=5).fit_transform(train_array, train_labels)
train_new
#feture selection on test
test_new = SelectKBest(chi2, k=5).fit_transform(test_array, test_labels)
test_new<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier<jupyter_code>from sklearn import tree
d_tree_clf = tree.DecisionTreeClassifier()
d_tree_clf.fit(orginal_train,train_labels)
prediction=d_tree_clf.predict(orginal_test)
print("Traing in the orginal data :")
print("Accuracy score :" ,metrics.accuracy_score(prediction,test_labels))
print("Precision score:" ,metrics.precision_score(prediction,test_labels,
                                                   average = "macro"))
print("F1_score:", metrics.f1_score(prediction,test_labels,
                                                   average = "macro"))
print("Recall_score:", metrics.recall_score(prediction,test_labels,
                                                     average = "macro"),"\n")
d_tree_clf.fit(train_new,train_labels)
prediction=d_tree_clf.predict(test_new)
print("Traing in the preprocessing data :")
print("Accuracy score :" ,metrics.accuracy_score(prediction,test_labels))
print("Precision score:" ,metrics.precision_score(prediction,test_labels,
                                                   average = "macro"))
print("F1_score:", metrics.f1_score(prediction,test_labels,
                                                   average = "macro"))
print("Recall_score:", metrics.recall_score(prediction,test_labels,
                                                     average = "macro"),"\n")<jupyter_output>Traing in the orginal data :
Accuracy score : 0.7889359581360578
Precision score: 0.7889432520320356
F1_score: 0.7889359057113485
Recall_score: 0.7889451180406489 

Traing in the preprocessing data :
Accuracy score : 0.6698230750062297
Precision score: 0.6700179979592709
F1_score: 0.6682309132435904
Recall_score: 0.6734799159585064<jupyter_text>AdaBoost<jupyter_code>from sklearn.ensemble import AdaBoostClassifier
n_estimators_vals = [20,40,60,80,100,200,300]
for i in n_estimators_vals :
  # Create and fit an AdaBoosted decision tree
  bdt = AdaBoostClassifier(n_estimators=i)
  bdt.fit(orginal_train,train_labels)
  prediction=bdt.predict(orginal_test)
  print("\t\tn_estimators : ", i )
  print("Traing in the orginal data :")
  
  bdt.fit(train_new,train_labels)
  prediction=bdt.predict(test_new)
  print("Traing in the preprocessing data :")
  print("Accuracy score :" ,metrics.accuracy_score(prediction,test_labels))
  print("Precision score:" ,metrics.precision_score(prediction,test_labels,
                                                     average = "macro"))
  print("F1_score:", metrics.f1_score(prediction,test_labels,
                                                     average = "macro"))
  print("Recall_score:", metrics.recall_score(prediction,test_labels,
                                                     average = "macro"),"\n")<jupyter_output><empty_output><jupyter_text>K-Nearest Neighbor (K-NN)<jupyter_code>from sklearn.neighbors import KNeighborsClassifier

k_vals = [1,2,3,4,5,6,7,8,9,10,20,100]  
for i in k_vals: 
        knn_fit = KNeighborsClassifier(n_neighbors=i) 
        knn_fit.fit(train_new,train_labels) 
        prediction=knn_fit.predict(test_new)
        print("\t\tn_neighbors : ", i)
        print("Accuracy score :" ,metrics.accuracy_score(prediction,test_labels))
        print("Precision score:" ,metrics.precision_score(prediction,test_labels,
                                                           average = "macro"))
        print("F1_score:", metrics.f1_score(prediction,test_labels,
                                                           average = "macro"))
        print("Recall_score:", metrics.recall_score(prediction,test_labels,
                                                     average = "macro"),"\n")<jupyter_output><empty_output><jupyter_text>Random Forests<jupyter_code>from sklearn.ensemble import RandomForestClassifier
n_estimators_vals = [10,20,40,60,80,100,200,300]
for i in n_estimators_vals :
  R_Forest_clf = RandomForestClassifier(n_estimators=i)
  R_Forest_clf.fit(train_new,train_labels)
  prediction=R_Forest_clf.predict(test_new)
  print("\t\tn_estimators : ", i )
  print("Accuracy score :" ,metrics.accuracy_score(prediction,test_labels))
  print("Precision score:" ,metrics.precision_score(prediction,test_labels,
                                                     average = "macro"))
  print("F1_score:", metrics.f1_score(prediction,test_labels,
                                                     average = "macro"))
  print("Recall_score:", metrics.recall_score(prediction,test_labels,
                                                     average = "macro"),"\n")<jupyter_output><empty_output><jupyter_text>SVM Classifier<jupyter_code>from sklearn import svm
C_range = [1e-2, 1, 1e2]
for i in C_range :
  svm_clf = svm.SVC(C=i, gamma='auto')
  svm_clf.fit(train_new,train_labels)
  print("\t\tC : ", i )
  print("Accuracy score :" ,metrics.accuracy_score(prediction,test_labels))
  print("Precision score:" ,metrics.precision_score(prediction,test_labels,
                                                     average = "macro"))
  print("F1_score:", metrics.f1_score(prediction,test_labels,
                                                     average = "macro"))
  print("Recall_score:", metrics.recall_score(prediction,test_labels,
                                                     average = "macro"),"\n")<jupyter_output><empty_output><jupyter_text>Naïve Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(train_new, train_labels)
prediction=clf.predict(test_new)
print("Accuracy score :" ,metrics.accuracy_score(prediction,test_labels))
print("Precision score:" ,metrics.precision_score(prediction,test_labels,
                                                   average = "macro"))
print("F1_score:", metrics.f1_score(prediction,test_labels,
                                                   average = "macro"))
print("Recall_score:", metrics.recall_score(prediction,test_labels,
                                                     average = "macro"),"\n")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11596530677802763, 'fraction_numerical': 0.028268551236749116, 'mean_word_length': 3.5829244357212953, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '528127', 'license': 'MIT', 'n_tokens_mistral': 3187, 'n_tokens_neox': 2954, 'n_words': 466, 'path': 'lab_3/Lab_3.ipynb', 'repo_name': 'MohamedBakrAli/Data-mining', 'size': '9339'}