<jupyter_start><jupyter_text>Homework 06  Brand Value Monitoring, Fairness & Interpretability      MTAT.03.319 - Business Data Analytics  University of Tartu - Spring 2021  Homework Instructions  Homework instructions- Please provide the names and student IDs of the team-members (Maximum 2 person) in the field "Team mates" below. If you are not working in a team please insert only your name and student ID. - The accepted submission formats are Colab links or .ipynb files. If you are submitting Colab links please make sure that the privacy settings for the file is public so we can access your code. - The submission will automatically close on **16 May at 23:59**, so please make sure to submit before the deadline. - ONLY one of the teammates should submit the homework. We will grade the homework and the marks and feedback is applied for both the team members. So please communicate with your team member about marks and feedback if you are submit the homework.- If a question is not clear, please ask us in Moodle ONLY. - After you have finished solving the Homework, please restart the Kernel and run all the cells to check if there is any persisting issues. - Plagiarism is **PROHIBITED**. Any form of plagiarism will be dealt according to the university policy (https://www.ut.ee/en/current-students/academic-fraud). **Team mates:**Name: &emsp;   Student ID: Name: &emsp;   Student ID:   The homework is divided into four sections and the points are distributed as below:- Brand Value Monitoring        -> 6 points- Fairness & Interpretability   -> 4 points__________________________________________Total                           -> 10 points  1. Brand Value Monitoring (6 points)You are going to use two annotated datasets containing tweets about Apple stock (AAPL) and the sentiment it represents (postive, negative, neutral) **1.1 There are two datasets with 1000 rows each. Please concatenate them together so that you have 2000 rows. (0.2 Points)**<jupyter_code>import pandas as pd
import numpy as np


a1 = pd.read_csv('Apple1.csv', encoding='latin-1') #DO NOT change the encoding method, may give you error
a2 = pd.read_csv('Apple2.csv', encoding='latin-1') #DO NOT change the encoding method, may give you error

#Concatenate<jupyter_output><empty_output><jupyter_text>**1.2 Plot a distribution of ```sentiment``` unique values. (0.2 Points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**1.3 Drop the unecessary column/s. Perform the following preprocessings to the ```text``` column. (1.6 Points)**- change all characters to lowercase- remove URLs- remove words starting with ‘@’- remove words starting with ‘$’ - remove punctuation- remove stopwords- remove numbers- remove whitespacesDon’t forget to inspect<jupyter_code><jupyter_output><empty_output><jupyter_text>**1.4 Create wordclouds for each sentiment group. Find out the most frequent word for each group. You should have three plot for three sentiment groups. (1.5 Points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**1.5 Apply TF-IDF technique on the textual data and split the dataset between train and test (80/20 ratio) (1.0 Points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**1.6 Train a random forest model with the prepared data and show the classification report on the test data (0.5 Points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**1.7 Train an SVM model with the prepared data and show the classification report on the test data (0.5 Points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**1.8 Which model performed better? Consider the f1 metrics and the time it took to train the model. Which model would you use in a real-life scenario and why? (0.5 Points)** **Answer:**  2. Fairness & Interpretability ( 4 points) In this section you are going to use the standard German Credit dataset used to bechmark many model interpretability techniques. The daset contains 1000 records of loan applications associatid with a risk score: Good or Bad. **2.1 The dataset has empty values. Choose an imputation method of your choice for each column. (0.5 points)**<jupyter_code>#!pip install sklearn_pandas
from sklearn_pandas import CategoricalImputer
import pandas as pd
german_data = pd.read_csv('german_credit_data.csv', sep=',')<jupyter_output><empty_output><jupyter_text>**2.2 Plot the values of the column ```Age``` against the ```Risk``` column. Do you think there exist  bias in this dataset? Choose the appropriate plot to address the problem. (0.5 points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**Answer:**  **2.3 Perform label encoding. Split the dataset in train/test (80/20 ratio) set, keep the random state 99. Train the XGBoost classifier below and predict the results in test set. Plot the classifcation report. (0.5 points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**2.4 Plot 3 types of feature importance (the parameter importance_type='weight', 'gain', 'cover') given by XGBoost and interpret the results. (0.5 points)**<jupyter_code>#Example: xgb.plot_importance(..., importance_type='weight', ...)
#Example: xgb.plot_importance(..., importance_type='gain', ...)
#Example: xgb.plot_importance(..., importance_type='cover', ...)<jupyter_output><empty_output><jupyter_text>**Answer:** **2.5 Use eli5 to analyze the most important features of a sample where the credit score is Bad and another sample where the credit score is Good. Explain briefly the results (0.5 points)**<jupyter_code>#!pip install eli5
import eli5<jupyter_output><empty_output><jupyter_text>**Answer:**  **2.6 Use SHAP to explain the prediction of the model for a sample where the credit score is Bad and another sample where the credit score is Good. Explain briefly the results.(0.5 points)**<jupyter_code>#!pip install shap
import shap<jupyter_output>In /home/hasan/.local/lib/python3.6/site-packages/matplotlib/mpl-data/stylelib/_classic_test.mplstyle: 
The text.latex.unicode rcparam was deprecated in Matplotlib 3.0 and will be removed in 3.2.
In /home/hasan/.local/lib/python3.6/site-packages/matplotlib/mpl-data/stylelib/_classic_test.mplstyle: 
The savefig.frameon rcparam was deprecated in Matplotlib 3.1 and will be removed in 3.3.
In /home/hasan/.local/lib/python3.6/site-packages/matplotlib/mpl-data/stylelib/_classic_test.mplstyle: 
The pgf.debug rcparam was deprecated in Matplotlib 3.0 and will be removed in 3.2.
In /home/hasan/.local/lib/python3.6/site-packages/matplotlib/mpl-data/stylelib/_classic_test.mplstyle: 
The verbose.level rcparam was deprecated in Matplotlib 3.1 and will be removed in 3.3.
In /home/hasan/.local/lib/python3.6/site-packages/matplotlib/mpl-data/stylelib/_classic_test.mplstyle: 
The verbose.fileo rcparam was deprecated in Matplotlib 3.1 and will be removed in 3.3.<jupyter_text>**Answer:** **2.7 Use SHAP to explain the prediction of the model for  the first 250 samples. Based on the similar patterns explain briefly the results(0.5 points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**Answer:**  **2.8 Plot as a bar chart of the feature importances received from SHAP. Finally compare these results  with the result from 2.6 and 2.7. (0.5 points)**<jupyter_code><jupyter_output><empty_output><jupyter_text>**Answer:**   How long did it take you to solve the homework?* Please answer as precisely as you can. It does not affect your points or grade in any way. It is okay, if it took 0.5 hours or 24 hours. The collected information will be used to improve future homeworks. **Answer:**  What is the level of difficulty for this homework?you can put only number between $0:10$ ($0:$ easy, $10:$ difficult) **Answer:**<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09095620627105468, 'fraction_numerical': 0.022026431718061675, 'mean_word_length': 5.131056393963463, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 67, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '499693', 'license': 'MIT', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2208, 'n_words': 923, 'path': 'Copy_of_Homework_06.ipynb', 'repo_name': 'Folasade21/Analysis-of-the-influence-of-Metallica-concert-on-public-transportation-service', 'size': '7718'}