<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Classification
Timothy Helton
<br>
<font color="red">
    NOTE
Step1:  Exercise 1
This question should be answered using the Weekly data set. This data is similar in nature to the Smarket data from earlier, except that it contains 1,089
weekly returns for 21 years, from the beginning of 1990 to the end of
2010.
Produce some numerical and graphical summaries of the Weekly
data. Do there appear to be any patterns?
Use the full data set to perform a logistic regression with
Direction as the response and the five lag variables plus Volume
as predictors. Use the summary function to print the results. Do
any of the predictors appear to be statistically significant? If so,
which ones?
Compute the confusion matrix and overall fraction of correct
predictions. Explain what the confusion matrix is telling you
about the types of mistakes made by logistic regression.
Now fit the logistic regression model using a training data period
from 1990 to 2008, with Lag2 as the only predictor. Compute the
confusion matrix and the overall fraction of correct predictions
for the held out data (that is, the data from 2009 and 2010).
Repeat (4) using LDA.
Repeat (4) using QDA.
Repeat (4) using KNN with K = 1.
Which of these methods appears to provide the best results on
this data?
Experiment with different combinations of predictors, including
possible transformations and interactions, for each of the
methods. Report the variables, method, and associated confusion
matrix that appears to provide the best results on the held
out data. Note that you should
1. Produce some numerical and graphical summaries of the Weekly
data. Do there appear to be any patterns?
Step2:  FINDINGS
There does not appear to be noticable patterns in dataset.
All field variables except volume appear to follow a Gausian distribution.
2. Use the full data set to perform a logistic regression with
Direction as the response and the five lag variables plus Volume
as predictors. Use the summary function to print the results. Do
any of the predictors appear to be statistically significant? If so,
which ones?
Step3:  FINDINGS
The intercept and lag2 features have P-values below the 0.05 threshold and appear statistically significant.
3. Compute the confusion matrix and overall fraction of correct
Step4:  FINDINGS
The model is not well suited to the data.
The Precision measures the accuracy of the Positive predictions.
$$\frac{T_p}{T_p - F_p}$$
The Recall measures the fraction of the model correctly identified.
$$\frac{T_p}{T_p + F_n}$$
The F1-score is the harmonic mean of the precision and recall.
Harmonic Mean is used when the average of rates is desired.
$$\frac{2 \times Precision \times Recall}{Precision + Recall}$$
The Support is the total number of each class.
The sum each row of the confusion matrix.
4. Now fit the logistic regression model using a training data period
Step5:  FINDINGS
Using 80% of the data as a training set did not improve the models accuracy.
Step6:  FINDINGS
Testing the model on the remaining 20% of the data yield a result worse than just randomly guessing.
5. Repeat (4) using LDA.
Step7:  FINDINGS
This model is extremely acurate.
6. Repeat (4) using QDA.
Step8:  FINDINGS
This model is better than the logistic regression, but not as good as the LDA model.
7. Repeat (4) using KNN with K = 1.
Step9:  FINDINGS
This model is better than the logistic regression, but not as good as the QDA.
8.Which of these methods appears to provide the best results on
this data?
The model acuracy in decending order is the following
Step10:  2. Explore the data graphically in order to investigate the association
between mpg01 and the other features. Which of the other
features seem most likely to be useful in predicting mpg01? Scatterplots
and boxplots may be useful tools to answer this question.
Describe your findings.
Step11:  FINDINGS
The following features appear to have a direct impact on the vehicles gas milage.
Displacement
Cylinders are related to Displacement and will not be included.
Horsepower
Weight
Origin
3. Split the data into a training set and a test set.
Step12:  4. Perform LDA on the training data in order to predict mpg01
using the variables that seemed most associated with mpg01 in
(2). What is the test error of the model obtained?
Step13:  5. Perform QDA on the training data in order to predict mpg01
using the variables that seemed most associated with mpg01 in
(2). What is the test error of the model obtained?
Step14:  6. Perform logistic regression on the training data in order to predict
mpg01 using the variables that seemed most associated with
mpg01 in (2). What is the test error of the model obtained?
Step15:  7. Perform KNN on the training data, with several values of K, in
order to predict mpg01. Use only the variables that seemed most
associated with mpg01 in (2). What test errors do you obtain?
Which value of K seems to perform the best on this data set?
<ASSISTANT_TASK:>
Python Code:
from k2datascience import classification
from k2datascience import plotting
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
%matplotlib inline
Explanation: Classification
Timothy Helton
<br>
<font color="red">
    NOTE:
    <br>
    This notebook uses code found in the
    <a href="https://github.com/TimothyHelton/k2datascience/blob/master/k2datascience/classification.py">
    <strong>k2datascience.classification</strong></a> module.
    To execute all the cells do one of the following items:
    <ul>
        <li>Install the k2datascience package to the active Python interpreter.</li>
        <li>Add k2datascience/k2datascience to the PYTHON_PATH system variable.</li>
        <li>Create a link to the classification.py file in the same directory as this notebook.</li>
</font>
Imports
End of explanation
weekly = classification.Weekly()
weekly.data.info()
weekly.data.describe()
weekly.data.head()
plotting.correlation_heatmap_plot(
    data=weekly.data, title='Weekly Stockmarket')
plotting.correlation_pair_plot(
    weekly.data, title='Weekly Stockmarket')
Explanation: Exercise 1
This question should be answered using the Weekly data set. This data is similar in nature to the Smarket data from earlier, except that it contains 1,089
weekly returns for 21 years, from the beginning of 1990 to the end of
2010.
Produce some numerical and graphical summaries of the Weekly
data. Do there appear to be any patterns?
Use the full data set to perform a logistic regression with
Direction as the response and the five lag variables plus Volume
as predictors. Use the summary function to print the results. Do
any of the predictors appear to be statistically significant? If so,
which ones?
Compute the confusion matrix and overall fraction of correct
predictions. Explain what the confusion matrix is telling you
about the types of mistakes made by logistic regression.
Now fit the logistic regression model using a training data period
from 1990 to 2008, with Lag2 as the only predictor. Compute the
confusion matrix and the overall fraction of correct predictions
for the held out data (that is, the data from 2009 and 2010).
Repeat (4) using LDA.
Repeat (4) using QDA.
Repeat (4) using KNN with K = 1.
Which of these methods appears to provide the best results on
this data?
Experiment with different combinations of predictors, including
possible transformations and interactions, for each of the
methods. Report the variables, method, and associated confusion
matrix that appears to provide the best results on the held
out data. Note that you should
1. Produce some numerical and graphical summaries of the Weekly
data. Do there appear to be any patterns?
End of explanation
weekly.logistic_regression(data=weekly.data)
weekly.logistic_model.summary()
Explanation: FINDINGS
There does not appear to be noticable patterns in dataset.
All field variables except volume appear to follow a Gausian distribution.
2. Use the full data set to perform a logistic regression with
Direction as the response and the five lag variables plus Volume
as predictors. Use the summary function to print the results. Do
any of the predictors appear to be statistically significant? If so,
which ones?
End of explanation
weekly.confusion
print(weekly.classification)
Explanation: FINDINGS
The intercept and lag2 features have P-values below the 0.05 threshold and appear statistically significant.
3. Compute the confusion matrix and overall fraction of correct
End of explanation
weekly.logistic_regression(data=weekly.x_train)
weekly.logistic_model.summary()
weekly.confusion
print(weekly.classification)
Explanation: FINDINGS
The model is not well suited to the data.
The Precision measures the accuracy of the Positive predictions.
$$\frac{T_p}{T_p - F_p}$$
The Recall measures the fraction of the model correctly identified.
$$\frac{T_p}{T_p + F_n}$$
The F1-score is the harmonic mean of the precision and recall.
Harmonic Mean is used when the average of rates is desired.
$$\frac{2 \times Precision \times Recall}{Precision + Recall}$$
The Support is the total number of each class.
The sum each row of the confusion matrix.
4. Now fit the logistic regression model using a training data period
End of explanation
weekly.categorize(weekly.x_test)
weekly.calc_prediction(weekly.y_test, weekly.prediction_nom)
weekly.confusion
print(weekly.classification)
Explanation: FINDINGS
Using 80% of the data as a training set did not improve the models accuracy.
End of explanation
weekly.lda()
weekly.confusion
print(weekly.classification)
Explanation: FINDINGS
Testing the model on the remaining 20% of the data yield a result worse than just randomly guessing.
5. Repeat (4) using LDA.
End of explanation
weekly.qda()
weekly.confusion
print(weekly.classification)
Explanation: FINDINGS
This model is extremely acurate.
6. Repeat (4) using QDA.
End of explanation
weekly.knn()
weekly.confusion
print(weekly.classification)
Explanation: FINDINGS
This model is better than the logistic regression, but not as good as the LDA model.
7. Repeat (4) using KNN with K = 1.
End of explanation
auto = classification.Auto()
auto.data.info()
auto.data.describe()
auto.data.head()
Explanation: FINDINGS
This model is better than the logistic regression, but not as good as the QDA.
8.Which of these methods appears to provide the best results on
this data?
The model acuracy in decending order is the following:
Linear Discriminate Analysis
Quadradic Discriminate Analysis
K-Nearest Neighbors
Logistic Regression
9. Experiment with different combinations of predictors, including
possible transformations and interactions, for each of the
methods. Report the variables, method, and associated confusion
matrix that appears to provide the best results on the held
out data. Note that you should
Exercise 2
In this problem, you will develop a model to predict whether a given
car gets high or low gas mileage based on the Auto data set.
Create a binary variable, mpg01, that contains a 1 if mpg contains
a value above its median, and a 0 if mpg contains a value below
its median. 
Explore the data graphically in order to investigate the association
between mpg01 and the other features. Which of the other
features seem most likely to be useful in predicting mpg01? Scatterplots
and boxplots may be useful tools to answer this question.
Describe your findings.
Split the data into a training set and a test set.
Perform LDA on the training data in order to predict mpg01
using the variables that seemed most associated with mpg01 in
(2). What is the test error of the model obtained?
Perform QDA on the training data in order to predict mpg01
using the variables that seemed most associated with mpg01 in
(2). What is the test error of the model obtained?
Perform logistic regression on the training data in order to predict
mpg01 using the variables that seemed most associated with
mpg01 in (2). What is the test error of the model obtained?
Perform KNN on the training data, with several values of K, in
order to predict mpg01. Use only the variables that seemed most
associated with mpg01 in (2). What test errors do you obtain?
Which value of K seems to perform the best on this data set?
1. Create a binary variable, mpg01, that contains a 1 if mpg contains
a value above its median, and a 0 if mpg contains a value below
its median.
End of explanation
plotting.correlation_heatmap_plot(
    data=auto.data, title='Auto')
plotting.correlation_pair_plot(
    data=auto.data, title='Auto')
auto.box_plots()
Explanation: 2. Explore the data graphically in order to investigate the association
between mpg01 and the other features. Which of the other
features seem most likely to be useful in predicting mpg01? Scatterplots
and boxplots may be useful tools to answer this question.
Describe your findings.
End of explanation
auto.x_train.info()
auto.y_train.head()
auto.x_test.info()
auto.y_test.head()
Explanation: FINDINGS
The following features appear to have a direct impact on the vehicles gas milage.
Displacement
Cylinders are related to Displacement and will not be included.
Horsepower
Weight
Origin
3. Split the data into a training set and a test set.
End of explanation
auto.classify_data(model='LDA')
auto.confusion
print(auto.classification)
Explanation: 4. Perform LDA on the training data in order to predict mpg01
using the variables that seemed most associated with mpg01 in
(2). What is the test error of the model obtained?
End of explanation
auto.classify_data(model='QDA')
auto.confusion
print(auto.classification)
Explanation: 5. Perform QDA on the training data in order to predict mpg01
using the variables that seemed most associated with mpg01 in
(2). What is the test error of the model obtained?
End of explanation
auto.classify_data(model='LR')
auto.confusion
print(auto.classification)
Explanation: 6. Perform logistic regression on the training data in order to predict
mpg01 using the variables that seemed most associated with
mpg01 in (2). What is the test error of the model obtained?
End of explanation
auto.accuracy_vs_k()
auto.classify_data(model='KNN', n=13)
auto.confusion
print(auto.classification)
Explanation: 7. Perform KNN on the training data, with several values of K, in
order to predict mpg01. Use only the variables that seemed most
associated with mpg01 in (2). What test errors do you obtain?
Which value of K seems to perform the best on this data set?
End of explanation
<END_TASK>