<jupyter_start><jupyter_text>***Using Spacy to clean the data***<jupyter_code>model = spacy.load('en_core_web_md')
def clean_my_string(string):
    """use spacy to tokenize, lemmatize and remove stop words"""
    clean_string = []
    token_string = model(string)
    for token in token_string:
        if not token.is_stop and not token.is_punct and not token.like_num:
            clean_string.append(token.lemma_)
    return ' '.join(clean_string)
clean_eagles=[]
for item in eagles_lyrics: 
    item=clean_my_string(item)
    clean_eagles.append(item)
clean_scorpions=[]
for item in scorpions_lyrics:   
    item=clean_my_string(item)
    clean_scorpions.append(item)
#checking the difference in length after spacy cleaning
print(len(eagles_lyrics[0]),len(clean_eagles[0]))
print(len(scorpions_lyrics[0]),len(clean_scorpions[0]))
print(len(clean_eagles),len(clean_scorpions))
#saving to pickles

with open('clean_eagles.pickle', 'wb') as file:
    pickle.dump(clean_eagles,file)
with open('clean_scorpions.pickle', 'wb') as file:
    pickle.dump(clean_scorpions,file)    
    

#loading pickled files
with open('clean_eagles.pickle', 'rb') as f:
      clean_eagles = pickle.load(f)
with open('clean_scorpions.pickle', 'rb') as f:
      clean_scorpions = pickle.load(f)

df1=pd.DataFrame({'lyrics':clean_scorpions, 'artist':0})
df2=pd.DataFrame({'lyrics':clean_eagles, 'artist':1})
df=pd.concat([df1,df2])
df
#saving df
pickle.dump(df, open('df.pkl',"wb"))
#reading saved df
df=pd.read_pickle('df.pkl')
df
def clean_text_final(text):
    'remove words containing numbers'
    text = re.sub('\w*\d\w*', '', text)
    return text
df['lyrics'] = df['lyrics'].apply(clean_text_final)

#splitting the data
X,y=df['lyrics'],df['artist']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
X_train.shape,y_train.shape
tf = TfidfVectorizer()
vectorized_lyrics=tf.fit_transform(X_train)  #creating sparse matrix
vectorized_lyrics
pickle.dump(tf,open('tf.pkl','wb'))   # saving for later use in Command-Line Interface
#transforming X_test
X_test_trans=tf.transform(X_test).toarray()
# converting sparse matrix into a dense matrix to view it effectively
tf_vectors=pd.DataFrame(vectorized_lyrics.todense().round(2), columns=tf.get_feature_names())  #index=X
tf_vectors
def print_evaluations(X_test,y_test, model):
    """Model evaluation metrics"""
    y_pred=model.predict(X_test)

    print(f'Accuracy: {round(accuracy_score(y_test, y_pred), 3)}')
    print(f'Precision: {round(precision_score(y_test, y_pred), 3)}')
    print(f'Recall: {round(recall_score(y_test, y_pred), 3)}')
    print(f'F1-score: {round(f1_score(y_test, y_pred), 3)}')
    plot_confusion_matrix(model, X_test, y_test,cmap=plt.cm.Blues,display_labels=['Scorpions', 'Eagles'])<jupyter_output><empty_output><jupyter_text>***Logreg***<jupyter_code>model=LogisticRegression()
model.fit(tf_vectors,y_train)
print("Train score :",model.score(tf_vectors,y_train))
acc_log=round(model.score(X_test_trans,y_test),3) #saving for later model comparison
accuracy=cross_val_score(model,tf_vectors,y_train)
print("Cross-validation score :", accuracy.mean())
#testing one song
test_song=["I'm gettin' out of control oh,oh,oh she's cool water, her momma taught her I got news, she's"]
test=tf.transform(test_song)
print(model.predict(test))
print_evaluations(X_test_trans, y_test, model)<jupyter_output>Accuracy: 0.964
Precision: 0.996
Recall: 0.903
F1-score: 0.948<jupyter_text>***Naive Bayes***<jupyter_code>from sklearn.naive_bayes import MultinomialNB
nb = MultinomialNB()
nb.fit(tf_vectors,y_train)
print("Train score :",nb.score(tf_vectors,y_train))
acc_nb=round(nb.score(X_test_trans,y_test),3) #saving for later model comparison
accuracy=cross_val_score(nb,tf_vectors,y_train)
print("Cross-validation score :", accuracy.mean())
print_evaluations(X_test_trans, y_test, nb)<jupyter_output>Accuracy: 0.953
Precision: 0.989
Recall: 0.88
F1-score: 0.931<jupyter_text>***SVM***<jupyter_code>svm_clf=svm.SVC(kernel='linear')
svm_clf.fit(tf_vectors,y_train)
print("Train score :",svm_clf.score(tf_vectors,y_train))
acc_svm=round(svm_clf.score(X_test_trans,y_test),3) #saving for later model comparison
accuracy=cross_val_score(svm_clf,tf_vectors,y_train)
print("Cross-validation score :", accuracy.mean())
print_evaluations(X_test_trans, y_test, svm_clf)<jupyter_output>Accuracy: 0.984
Precision: 0.958
Recall: 1.0
F1-score: 0.979<jupyter_text>***RandomForest***<jupyter_code>forest = RandomForestClassifier()
from tqdm import tqdm_notebook
from tqdm.notebook import tqdm
#searching for best max_depth
forest_params = {"max_depth": range(50,70)}

forest_grid = GridSearchCV(forest, forest_params, cv=5, verbose=True)

forest_grid.fit(tf_vectors,y_train)

forest_grid.best_params_, forest_grid.best_score_ 
forest = RandomForestClassifier(n_estimators=100, max_depth=58)
forest.fit(tf_vectors,y_train)
print("Train score :",forest.score(tf_vectors,y_train))
pickle.dump(forest, open('rf_clf.pkl', 'wb')) # saving for later use in Command-Line Interface
acc_rf=round(forest.score(X_test_trans,y_test),3)  #saving for later model comparison
accuracy=cross_val_score(forest,tf_vectors,y_train)
print("Cross-validation score :", accuracy.mean())
print_evaluations(X_test_trans, y_test, forest)<jupyter_output>Accuracy: 0.986
Precision: 0.965
Recall: 0.997
F1-score: 0.98<jupyter_text>***Model comparison***<jupyter_code>models = pd.DataFrame({
    'Model': ['Logistic Regression','Naive Bayes', 'Support Vector Machine', 'Random Forest'],
    
    'Score': [acc_log, acc_nb, acc_svm, acc_rf]
    })

models.sort_values(by='Score', ascending=False)
from sklearn import metrics
fig, axes = plt.subplots(1,1, figsize=(8,5))
metrics.plot_roc_curve(model, X_test_trans, y_test, ax=axes)
metrics.plot_roc_curve(nb, X_test_trans, y_test, ax=axes)
metrics.plot_roc_curve(svm_clf, X_test_trans, y_test,ax=axes)
metrics.plot_roc_curve(forest, X_test_trans, y_test, ax=axes)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13909395973154362, 'fraction_numerical': 0.016946308724832215, 'mean_word_length': 7.676855895196507, 'pattern_counts': {'":': 1, '<': 21, '<?xml version=': 0, '>': 21, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '622808', 'license': 'MIT', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2262, 'n_words': 396, 'path': 'final project.ipynb', 'repo_name': 'dashnak90/Lyrics-Classification', 'size': '5960'}