<jupyter_start><jupyter_text>Exercise Solution<jupyter_code>import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
df = pd.read_csv("HR_comma_sep.csv")
df.head()<jupyter_output><empty_output><jupyter_text>1. Data Exploration & Visualization<jupyter_code>df.shape       # 8535 rows, 10 columns
df.count().isna()   # No null values
df[df['left']==1].shape # 2000 employees left w.r.t dataset
df[df['left']==0].shape  # 6538 employees retain w.r.t dataset
df.groupby('left').mean()  # After grouping average of all columns for left and retain<jupyter_output><empty_output><jupyter_text>From above table we can draw following conclusions,1. **Satisfaction Level**: Satisfaction level seems to be relatively low (0.43) in employees leaving the firm vs the retained ones (0.66)2. **Average Monthly Hours**: Average monthly hours are higher in employees leaving the firm (199 vs 207)3. **Promotion Last 5 Years**: Employees who are given promotion are likely to be retained at firm  >> Feature extraction of text values<jupyter_code>pd.crosstab(df.salary,df.left) # pandas crosstab: first parameter indicate what we want in x-axis
df=df[df.salary != 'm']            # removing 'm' outliers
pd.crosstab(df.salary,df.left)
pd.crosstab(df.salary,df.left).plot(kind='bar')    #  impact of salary<jupyter_output><empty_output><jupyter_text>Above bar chart shows employees with high salaries are likely to not leave the company<jupyter_code>pd.crosstab(df.Department,df.left).plot(kind='bar') # Impact of department on retention<jupyter_output><empty_output><jupyter_text>From above chart there seem to be some impact of department on employee retention but it is not major hence we will ignore department in our analysis  From the data analysis so far we can conclude that we will use following variables as independant variables in our model1. **Satisfaction Level**2. **Average Monthly Hours**3. **Promotion Last 5 Years**4. **Salary**<jupyter_code>new_df=df[['satisfaction_level','average_montly_hours','promotion_last_5years','salary','left']]
new_df.head()<jupyter_output><empty_output><jupyter_text>Since, salary is categorycal variable here, we should use pandas dummy variable before buiding model<jupyter_code>salary_dummies = pd.get_dummies(new_df.salary, prefix="salary")
salary_dummies
final_df=pd.concat([new_df,salary_dummies],axis='columns') # Let's concate this two dataframe
final_df.head()<jupyter_output><empty_output><jupyter_text>Now, we can drop salary column<jupyter_code>final_df.drop('salary',axis='columns',inplace=True)
final_df.head()<jupyter_output><empty_output><jupyter_text>2. Train Test Split &  Building Model<jupyter_code>X=final_df.drop('left',axis=1) # axis 1 mean column
X
y=final_df['left']
y
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3)
X_test.shape
# Logistic regression for binary classification
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)           # Training the model<jupyter_output><empty_output><jupyter_text>Results<jupyter_code>model.predict(X_test)
model.predict_proba(X_test) 
model.score(X_test, y_test)   # 78.6% accuracy<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10623272950568008, 'fraction_numerical': 0.014430457476205097, 'mean_word_length': 7.044444444444444, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '258953', 'license': 'MIT', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1054, 'n_words': 284, 'path': 'Machine Learning/7. Logistic Regression(Binary Classification) 2.ipynb', 'repo_name': 'Shaon2221/Learning-and-Experimenting_Data-Science', 'size': '3257'}