<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 Exercise - Solutions
We'll be working with some California Census Data, we'll be trying to use various features of an individual to predict what class of income they belogn in (>50k or <=50k). 
Here is some information about the data
Step1:  TensorFlow won't be able to understand strings as labels, you'll need to use pandas .apply() method to apply a custom function that converts them to 0s and 1s. This might be hard if you aren't very familiar with pandas, so feel free to take a peek at the solutions for this part.
 Convert the Label column to 0s and 1s instead of strings.
Step2:  Perform a Train Test Split on the Data
Step3:  Create the Feature Columns for tf.esitmator
 Take note of categorical vs continuous values!
Step4:  Import Tensorflow
Step5:  Create the tf.feature_columns for the categorical values. Use vocabulary lists or just use hash buckets.
Step6:  Create the continuous feature_columns for the continuous values using numeric_column
Step7:  Put all these variables into a single list with the variable name feat_cols
Step8:  Create Input Function
 Batch_size is up to you. But do make sure to shuffle!
Step9:  Create your model with tf.estimator
Create a LinearClassifier.(If you want to use a DNNClassifier, keep in mind you'll need to create embedded columns out of the cateogrical feature that use strings, check out the previous lecture on this for more info.)
Step10:  Train your model on the data, for at least 5000 steps.
Step11:  Evaluation
 Create a prediction input function. Remember to only supprt X_test data and keep shuffle=False.
Step12:  Use model.predict() and pass in your input function. This will produce a generator of predictions, which you can then transform into a list, with list()
Step13:  Each item in your list will look like this
Step14:  Create a list of only the class_ids key values from the prediction list of dictionaries, these are the predictions you will use to compare against the real y_test values.
Step15:  Import classification_report from sklearn.metrics and then see if you can figure out how to use it to easily get a full report of your model's performance on the test data.
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
census = pd.read_csv("./data/census_data.csv")
census.head()
Explanation: Classification Exercise - Solutions
We'll be working with some California Census Data, we'll be trying to use various features of an individual to predict what class of income they belogn in (>50k or <=50k). 
Here is some information about the data:
<table>
<thead>
<tr>
<th>Column Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>age</td>
<td>Continuous</td>
<td>The age of the individual</td>
</tr>
<tr>
<td>workclass</td>
<td>Categorical</td>
<td>The type of employer the  individual has (government,  military, private, etc.).</td>
</tr>
<tr>
<td>fnlwgt</td>
<td>Continuous</td>
<td>The number of people the census  takers believe that observation  represents (sample weight). This  variable will not be used.</td>
</tr>
<tr>
<td>education</td>
<td>Categorical</td>
<td>The highest level of education  achieved for that individual.</td>
</tr>
<tr>
<td>education_num</td>
<td>Continuous</td>
<td>The highest level of education in  numerical form.</td>
</tr>
<tr>
<td>marital_status</td>
<td>Categorical</td>
<td>Marital status of the individual.</td>
</tr>
<tr>
<td>occupation</td>
<td>Categorical</td>
<td>The occupation of the individual.</td>
</tr>
<tr>
<td>relationship</td>
<td>Categorical</td>
<td>Wife, Own-child, Husband,  Not-in-family, Other-relative,  Unmarried.</td>
</tr>
<tr>
<td>race</td>
<td>Categorical</td>
<td>White, Asian-Pac-Islander,  Amer-Indian-Eskimo, Other, Black.</td>
</tr>
<tr>
<td>gender</td>
<td>Categorical</td>
<td>Female, Male.</td>
</tr>
<tr>
<td>capital_gain</td>
<td>Continuous</td>
<td>Capital gains recorded.</td>
</tr>
<tr>
<td>capital_loss</td>
<td>Continuous</td>
<td>Capital Losses recorded.</td>
</tr>
<tr>
<td>hours_per_week</td>
<td>Continuous</td>
<td>Hours worked per week.</td>
</tr>
<tr>
<td>native_country</td>
<td>Categorical</td>
<td>Country of origin of the  individual.</td>
</tr>
<tr>
<td>income</td>
<td>Categorical</td>
<td>"&gt;50K" or "&lt;=50K", meaning  whether the person makes more  than \$50,000 annually.</td>
</tr>
</tbody>
</table>
Follow the Directions in Bold. If you get stuck, check out the solutions lecture.
THE DATA
 Read in the census_data.csv data with pandas
End of explanation
census['income_bracket'].unique()
def label_fix(label):
    if label==' <=50K':
        return 0
    else:
        return 1
# Applying function to every row of the DataFrame
census['income_bracket'] = census['income_bracket'].apply(label_fix)
# Alternative
# lambda label:int(label==' <=50k')
# census['income_bracket'].apply(lambda label: int(label==' <=50K'))
Explanation: TensorFlow won't be able to understand strings as labels, you'll need to use pandas .apply() method to apply a custom function that converts them to 0s and 1s. This might be hard if you aren't very familiar with pandas, so feel free to take a peek at the solutions for this part.
 Convert the Label column to 0s and 1s instead of strings.
End of explanation
from sklearn.model_selection import train_test_split
x_data = census.drop('income_bracket', axis = 1)
y_labels = census['income_bracket']
X_train, X_test, y_train, y_test = train_test_split(x_data, y_labels, test_size = 0.3,random_state = 101)
x_data.head()
y_labels.head()
Explanation: Perform a Train Test Split on the Data
End of explanation
census.columns
Explanation: Create the Feature Columns for tf.esitmator
 Take note of categorical vs continuous values!
End of explanation
import tensorflow as tf
Explanation: Import Tensorflow
End of explanation
gender = tf.feature_column.categorical_column_with_vocabulary_list("gender", ["Female", "Male"])
occupation = tf.feature_column.categorical_column_with_hash_bucket("occupation", hash_bucket_size=1000)
marital_status = tf.feature_column.categorical_column_with_hash_bucket("marital_status", hash_bucket_size=1000)
relationship = tf.feature_column.categorical_column_with_hash_bucket("relationship", hash_bucket_size=1000)
education = tf.feature_column.categorical_column_with_hash_bucket("education", hash_bucket_size=1000)
workclass = tf.feature_column.categorical_column_with_hash_bucket("workclass", hash_bucket_size=1000)
native_country = tf.feature_column.categorical_column_with_hash_bucket("native_country", hash_bucket_size=1000)
Explanation: Create the tf.feature_columns for the categorical values. Use vocabulary lists or just use hash buckets.
End of explanation
age = tf.feature_column.numeric_column("age")
education_num = tf.feature_column.numeric_column("education_num")
capital_gain = tf.feature_column.numeric_column("capital_gain")
capital_loss = tf.feature_column.numeric_column("capital_loss")
hours_per_week = tf.feature_column.numeric_column("hours_per_week")
Explanation: Create the continuous feature_columns for the continuous values using numeric_column
End of explanation
feat_cols = [gender, occupation, marital_status, relationship, education, workclass, native_country,
            age, education_num, capital_gain, capital_loss, hours_per_week]
Explanation: Put all these variables into a single list with the variable name feat_cols
End of explanation
input_func = tf.estimator.inputs.pandas_input_fn(x = X_train,
                                                 y = y_train,
                                                 batch_size = 100,
                                                 num_epochs = None,
                                                 shuffle = True)
Explanation: Create Input Function
 Batch_size is up to you. But do make sure to shuffle!
End of explanation
model = tf.estimator.LinearClassifier(feature_columns = feat_cols)
Explanation: Create your model with tf.estimator
Create a LinearClassifier.(If you want to use a DNNClassifier, keep in mind you'll need to create embedded columns out of the cateogrical feature that use strings, check out the previous lecture on this for more info.)
End of explanation
model.train(input_fn = input_func,
            steps = 5000)
Explanation: Train your model on the data, for at least 5000 steps.
End of explanation
pred_fn = tf.estimator.inputs.pandas_input_fn(x = X_test,
                                              batch_size = len(X_test),
                                              shuffle = False)
Explanation: Evaluation
 Create a prediction input function. Remember to only supprt X_test data and keep shuffle=False.
End of explanation
predictions = list(model.predict(input_fn = pred_fn))
Explanation: Use model.predict() and pass in your input function. This will produce a generator of predictions, which you can then transform into a list, with list()
End of explanation
predictions[0]
Explanation: Each item in your list will look like this:
End of explanation
final_preds = []
for pred in predictions:
    final_preds.append(pred['class_ids'][0])
final_preds[:10]
Explanation: Create a list of only the class_ids key values from the prediction list of dictionaries, these are the predictions you will use to compare against the real y_test values.
End of explanation
from sklearn.metrics import classification_report
print(classification_report(y_test, final_preds))
Explanation: Import classification_report from sklearn.metrics and then see if you can figure out how to use it to easily get a full report of your model's performance on the test data.
End of explanation
<END_TASK>