predictors = subset[variables]
targets = subset['High BreastCancer']

"""
Explanation: Build LASSO Regression
End of explanation
"""


#Split into training and testing sets
training_data, test_data, training_target, test_target  = train_test_split(predictors, targets, test_size=.3)

"""
Explanation: (a) Split into training and testing sets
End of explanation
"""


model=LassoLarsCV(cv=10, precompute=False).fit(training_data, training_target)

# Show the regression coeff of the predictors
dict(zip(predictors.columns,model.coef_))

"""
Explanation: (b) Building the LASSO Regression Model
End of explanation
"""


feature_name = list(predictors.columns.values)
feature_coefficient = list(model.coef_)
features = pd.DataFrame({'Variable':feature_name, 'Regression Coefficients':feature_coefficient}).sort_values(by='Regression Coefficients', ascending=False)
print(features.head(len(feature_name)))

"""
Explanation: Note: In the above output, the variables with coefecient value 0.0 are insignificant in the model and thus have been removed from the final model. So the results show that 9 variables 5 were selected by the final model. 
Also, as we scaled the data, we can use the size of the regression coeffecients to determine which predictors are the strongest predictors of breastcancerper100th. 
Here, incomeperperson has the highest regression coeff followed by urbanrate, femaleemployrate, co2emission and polityscore. These five predictor variables are positively associated to breast cancer.  
Evaluating the LASSO Model
(a) print variable names and regression coefficients
End of explanation
"""


m_log_alphas = -np.log10(model.alphas_)
ax = plt.gca() # set the plot axis
plt.plot(m_log_alphas, model.coef_path_.T) # applying the -log10 transformation to the alpha values simply to make the values easier to read.
plt.axvline(-np.log10(model.alpha_), linestyle='--', color='k', label='alpha CV') # A black vertical line will be plotted at the negative log10 transformed alpha value for the selected model
plt.ylabel('Regression Coefficients')
plt.xlabel('-log(alpha)')
plt.title('Regression Coefficients Progression for Lasso Paths')

"""
Explanation: (b) Plot coefficient progression
End of explanation
"""


m_log_alphascv = -np.log10(model.cv_alphas_)
plt.figure()
plt.plot(m_log_alphascv, model.cv_mse_path_, ':')
plt.plot(m_log_alphascv, model.cv_mse_path_.mean(axis=-1), 'k', label='Average across the folds', linewidth=2)
plt.axvline(-np.log10(model.alpha_), linestyle='--', color='k', label='alpha CV')
plt.legend()
plt.xlabel('-log(alpha)')
plt.ylabel('Mean squared error')
plt.title('Mean squared error on each fold')

"""
Explanation: (c) Plot mean square error for each fold
End of explanation
"""


from sklearn.metrics import mean_squared_error
train_error = mean_squared_error(training_target, model.predict(training_data))
test_error = mean_squared_error(test_target, model.predict(test_data))
print ('training data MSE')
print(train_error)
print ('test data MSE')
print(test_error)

"""
Explanation: MSE from training and test data
End of explanation
"""


rsquared_train=model.score(training_data, training_target)
rsquared_test=model.score(test_data, test_target)
print ('training data R-square')
print(rsquared_train)
print ('test data R-square')
print(rsquared_test)

"""
Explanation: The selected model has predicted better in the test data as compared to the train data. 
Also the test Mean Square Error (MSE) is close to the training MSE this suggests that prediction accuracy scores is pretty stable across the two datasets.
R-square from training and test data
End of explanation
"""


