<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
Step1:  TPOT uses a genetic algorithm (implemented with DEAP library) to pick an optimal pipeline for a regression task.
What is a pipeline?
Pipeline is composed of preprocessors
Step2:  Sklearn model
Step3:  TPOT Classifier
Step4:  Export Pseudo Pipeline Code
<ASSISTANT_TASK:>
Python Code:
!sudo pip install deap update_checker tqdm xgboost tpot
import pandas as pd 
import numpy as np
import psycopg2 
import os
import json
from tpot import TPOTClassifier
from sklearn.metrics import classification_report
conn = psycopg2.connect(
    user = os.environ['REDSHIFT_USER']
    ,password = os.environ['REDSHIFT_PASS']    
    ,port = os.environ['REDSHIFT_PORT']
    ,host = os.environ['REDSHIFT_HOST']
    ,database = 'tradesy'
)
query = 
    select 
        purchase_dummy
        ,shipping_price_ratio
        ,asking_price
        ,price_level
        ,brand_score
        ,brand_size
        ,a_over_b
        ,favorite_count
        ,has_blurb
        ,has_image
        ,seasonal_component
        ,description_length
        ,product_category_accessories
        ,product_category_shoes
        ,product_category_bags
        ,product_category_tops
        ,product_category_dresses
        ,product_category_weddings
        ,product_category_bottoms
        ,product_category_outerwear
        ,product_category_jeans
        ,product_category_activewear
        ,product_category_suiting
        ,product_category_swim
        
    from saleability_model_v2
     
    limit 50000
    
df = pd.read_sql(query, conn)
target = 'purchase_dummy'
domain = filter(lambda x: x != target, df.columns.values)
df = df.astype(float)
y_all = df[target].values
X_all = df[domain].values
idx_all = np.random.RandomState(1).permutation(len(y_all))
idx_train = idx_all[:int(.8 * len(y_all))]
idx_test = idx_all[int(.8 *  len(y_all)):]
# TRAIN AND TEST DATA
X_train = X_all[idx_train]
y_train = y_all[idx_train]
X_test = X_all[idx_test]
y_test = y_all[idx_test]
Explanation: TPOT uses a genetic algorithm (implemented with DEAP library) to pick an optimal pipeline for a regression task.
What is a pipeline?
Pipeline is composed of preprocessors:
* take polynomial transformations of features
* 
TPOTBase is key class
parameters:
population_size: int (default: 100)
            The number of pipelines in the genetic algorithm population. Must
            be > 0.The more pipelines in the population, the slower TPOT will
            run, but it's also more likely to find better pipelines.
* generations: int (default: 100)
    The number of generations to run pipeline optimization for. Must
    be > 0. The more generations you give TPOT to run, the longer it
    takes, but it's also more likely to find better pipelines.
* mutation_rate: float (default: 0.9)
    The mutation rate for the genetic programming algorithm in the range
    [0.0, 1.0]. This tells the genetic programming algorithm how many
    pipelines to apply random changes to every generation. We don't
    recommend that you tweak this parameter unless you know what you're
    doing.
* crossover_rate: float (default: 0.05)
    The crossover rate for the genetic programming algorithm in the
    range [0.0, 1.0]. This tells the genetic programming algorithm how
    many pipelines to "breed" every generation. We don't recommend that
    you tweak this parameter unless you know what you're doing.
* scoring: function or str
    Function used to evaluate the quality of a given pipeline for the
    problem. By default, balanced class accuracy is used for
    classification problems, mean squared error for regression problems.
    TPOT assumes that this scoring function should be maximized, i.e.,
    higher is better.
    Offers the same options as sklearn.cross_validation.cross_val_score:
    ['accuracy', 'adjusted_rand_score', 'average_precision', 'f1',
    'f1_macro', 'f1_micro', 'f1_samples', 'f1_weighted',
    'precision', 'precision_macro', 'precision_micro', 'precision_samples',
    'precision_weighted', 'r2', 'recall', 'recall_macro', 'recall_micro',
    'recall_samples', 'recall_weighted', 'roc_auc']
* num_cv_folds: int (default: 3)
    The number of folds to evaluate each pipeline over in k-fold
    cross-validation during the TPOT pipeline optimization process
* max_time_mins: int (default: None)
    How many minutes TPOT has to optimize the pipeline. If not None,
    this setting will override the generations parameter.
TPOTClassifier and TPOTRegressor inherit parent class TPOTBase, with modifications of the scoring function.
End of explanation
from sklearn.ensemble import RandomForestClassifier
sklearn_model = RandomForestClassifier()
sklearn_model.fit(X_train, y_train)
sklearn_predictions = sklearn_model.predict(X_test)
print classification_report(y_test, sklearn_predictions)
Explanation: Sklearn model:
End of explanation
tpot_model = TPOTClassifier(generations=3, population_size=10, verbosity=2, max_time_mins=10)
tpot_model.fit(X_train, y_train)
tpot_predictions = tpot_model.predict(X_test)
print classification_report(y_test, tpot_predictions)
Explanation: TPOT Classifier
End of explanation
tpot_model.export('optimal-saleability-model.py')
!cat optimal-saleability-model.py
Explanation: Export Pseudo Pipeline Code
End of explanation
<END_TASK>