<jupyter_start><jupyter_text><jupyter_code>#Hello and welcome to my attempt at solving this project! 

# The first thing I do is import all the needed info
#I usually just import everything I think of so I don't have to add anything in later
import pandas as pd
from pandas import DataFrame, Series
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn import tree
from sklearn.linear_model import ElasticNet
#unzip your data and label it
!wget https://raw.githubusercontent.com/zacharski/ml-class/master/data/bach.zip
!unzip bach.zip
bach=pd.read_csv('bach.csv')
#print your data to get a look at what it has
bach


#Lets look around at the data we have
print(bach.columns)
print(bach.info)
#by using describe we see that the event number is being used. We don't really need that, so let's drop that column
bach.drop(['choral_ID','event_number'], axis = 1) 
bach.describe()

#I know we for sure need to onehot encode the bass column, so im going to do that
one_hot=pd.get_dummies(bach['bass'])
#joing the one hot encoded dataframe to the origional
test=pd.concat([bach,one_hot],axis=1)
#now we drop the bass column from the origional dataframe
bach_hotshotted=test.drop(['bass','choral_ID','event_number'], axis=1)
bach_hotshotted.columns

#I don't know enough about music, but I feel like we should also hotshot the meter column?
#I'm going to do so because it can't hurt, right?

#we need to onehot encode the meter column
one_hot=pd.get_dummies(bach_hotshotted['meter'])
#joing the one hot encoded dataframe to the otigional
test2=pd.concat([bach_hotshotted,one_hot],axis=1)
#now we drop the bass column from the origional dataframe
finalbach=test2.drop('meter', axis=1)

finalbach
#the meter columns just have numbers, so im going to rename them for my own sanity
finalbach.rename(columns={1:'One Meter', 2:'Two Meter',3:'Three Meter',4:'Four Meter',5:'Five Meter'}, inplace=True)

#It was at this point I realized I would have to onehot the whole thing
#because when I tried to predict it told me I couldnt convert floats like "yes" and "no"
#So now I will try to onehot a bunch of rows at once to save time

df=pd.get_dummies(finalbach, columns=['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B','A', 'A#', 'Ab', 'B', 'Bb', 'C', 'C#', 'D', 'D#', 'Db',
       'E', 'Eb', 'F', 'F#', 'G', 'G#'],drop_first=True)
#and it worked. Oh. My. Gosh. 
df.columns
#Now we train the classifier
from sklearn import tree
clf = tree.DecisionTreeClassifier(criterion='entropy')

#set up train test split
bachtrain, bachtest = train_test_split(df, test_size = 0.2, random_state=42)

bach_train_features = bachtrain[['One Meter', 'Two Meter', 'Three Meter', 'Four Meter',
       'Five Meter', 'C_YES', 'C_1', 'C#_YES', 'C#_1', 'D_YES', 'D_1',
       'D#_YES', 'D#_1', 'E_YES', 'E_1', 'F_YES', 'F_1', 'F#_YES', 'F#_1',
       'G_YES', 'G_1', 'G#_YES', 'G#_1', 'A_YES', 'A_1', 'A#_YES', 'A#_1',
       'B_YES', 'B_1', 'A_YES', 'A_1', 'A#_YES', 'A#_1', 'Ab_1', 'B_YES',
       'B_1', 'Bb_1', 'C_YES', 'C_1', 'C#_YES', 'C#_1', 'D_YES', 'D_1',
       'D#_YES', 'D#_1', 'Db_1', 'E_YES', 'E_1', 'Eb_1', 'F_YES', 'F_1',
       'F#_YES', 'F#_1', 'G_YES', 'G_1', 'G#_YES', 'G#_1']]
bach_train_labels = bachtrain['chord_label']

bach_test_features = bachtest[['One Meter', 'Two Meter', 'Three Meter', 'Four Meter',
       'Five Meter', 'C_YES', 'C_1', 'C#_YES', 'C#_1', 'D_YES', 'D_1',
       'D#_YES', 'D#_1', 'E_YES', 'E_1', 'F_YES', 'F_1', 'F#_YES', 'F#_1',
       'G_YES', 'G_1', 'G#_YES', 'G#_1', 'A_YES', 'A_1', 'A#_YES', 'A#_1',
       'B_YES', 'B_1', 'A_YES', 'A_1', 'A#_YES', 'A#_1', 'Ab_1', 'B_YES',
       'B_1', 'Bb_1', 'C_YES', 'C_1', 'C#_YES', 'C#_1', 'D_YES', 'D_1',
       'D#_YES', 'D#_1', 'Db_1', 'E_YES', 'E_1', 'Eb_1', 'F_YES', 'F_1',
       'F#_YES', 'F#_1', 'G_YES', 'G_1', 'G#_YES', 'G#_1']]
bach_test_labels = bachtest['chord_label']

#fit the classifier
clf.fit(bach_train_features, bach_train_labels)

#make predictions
predictions=clf.predict(bach_test_features)
#determine accuracy
accuracy_score(bach_test_labels, predictions)
#I want to try and imporve my score, so lets see if building a bagging classifier helps?

from sklearn.ensemble import BaggingClassifier
clf = tree.DecisionTreeClassifier(criterion='entropy')

bagging_clf = BaggingClassifier(clf, n_estimators=500, max_samples=500, 
                                bootstrap=True, n_jobs=5)
bagging_clf.fit(bach_train_features, bach_train_labels)
predictions = bagging_clf.predict(bach_test_features)
accuracy_score(bach_test_labels, predictions)
#maybe try it without replacement?
from sklearn.ensemble import BaggingClassifier
clf = tree.DecisionTreeClassifier(criterion='entropy')

bagging_clf = BaggingClassifier(clf, n_estimators=500, max_samples=500, 
                                bootstrap=False, n_jobs=5)
bagging_clf.fit(bach_train_features, bach_train_labels)
predictions = bagging_clf.predict(bach_test_features)
accuracy_score(bach_test_labels, predictions)
#Now im going to try random subspaces, because honest why not

subspace_clf = BaggingClassifier(clf, n_estimators=100, max_features=100, 
                                bootstrap=True, n_jobs=-3)
subspace_clf.fit(bach_train_features, bach_train_labels)
predictions = subspace_clf.predict(bach_test_features)
accuracy_score(bach_test_labels, predictions)
#Lastly lets try random patches because I've already done all the other ones and it doesn't look like im going to get above 80% accuracy
subspace_clf = BaggingClassifier(clf, n_estimators=40, max_features=20, 
                                 max_samples=500, bootstrap=True, n_jobs=4)
subspace_clf.fit(bach_train_features, bach_train_labels)
predictions = subspace_clf.predict(bach_test_features)
accuracy_score(bach_test_labels, predictions)<jupyter_output><empty_output><jupyter_text>In the end, after messing around with literally every number/classifier, my accuracy for everything averaged around 72, which could be worse!<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1388572358782354, 'fraction_numerical': 0.017092625752889466, 'mean_word_length': 4.720670391061453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '611962', 'license': 'Apache-2.0', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2298, 'n_words': 633, 'path': 'bach.ipynb', 'repo_name': 'AbigailBuchholz/test', 'size': '6143'}