<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Introduction
In this course, you will learn a practical approach to feature engineering. You'll be able to apply what you learn to Kaggle competitions and other machine learning applications. 
Load the data
We'll work with data from Kickstarter projects. The first few rows of the data looks like this
Step1:  The state column shows the outcome of the project.
Step2:  Using this data, how can we use features such as project category, currency, funding goal, and country to predict if a Kickstarter project will succeed? 
Prepare the target column
First we'll convert the state column into a target we can use in a model.  Data cleaning isn't the current focus, so we'll simplify this example by
Step3:  Convert timestamps
Next, we convert the launched feature into categorical features we can use in a model. Since we loaded the columns as timestamp data, we access date and time values through the .dt attribute on the timestamp column.
Note
Step4:  Prep categorical variables
Now for the categorical variables -- category, currency, and country -- we'll need to convert them into integers so our model can use the data. For this we'll use scikit-learn's LabelEncoder. This assigns an integer to each value of the categorical feature.
Step5:  We collect all of these features in a new dataframe that we can use to train a model.
Step6:  Create training, validation, and test splits
We need to create data sets for training, validation, and testing. We'll use a fairly simple approach and split the data using slices. We'll use 10% of the data as a validation set, 10% for testing, and the other 80% for training.
Step7:  Train a model
For this course we'll be using a LightGBM model. This is a tree-based model that typically provides the best performance, even compared to XGBoost. It's also relatively fast to train. 
We won't do hyperparameter optimization because that isn't the goal of this course. So, our models won't be the absolute best performance you can get. But you'll still see model performance improve as we do feature engineering.
Step8:  Make predictions & evaluate the model
Finally, let's make predictions on the test set with the model and see how well it performs. An important thing to remember is that you can overfit to the validation data. This is why we need a test set that the model never sees until the final evaluation.
<ASSISTANT_TASK:>
Python Code:
#$HIDE_INPUT$
import pandas as pd
ks = pd.read_csv('../input/kickstarter-projects/ks-projects-201801.csv',
                 parse_dates=['deadline', 'launched'])
ks.head(6)
Explanation: Introduction
In this course, you will learn a practical approach to feature engineering. You'll be able to apply what you learn to Kaggle competitions and other machine learning applications. 
Load the data
We'll work with data from Kickstarter projects. The first few rows of the data looks like this:
End of explanation
print('Unique values in `state` column:', list(ks.state.unique()))
Explanation: The state column shows the outcome of the project.
End of explanation
# Drop live projects
ks = ks.query('state != "live"')
# Add outcome column, "successful" == 1, others are 0
ks = ks.assign(outcome=(ks['state'] == 'successful').astype(int))
Explanation: Using this data, how can we use features such as project category, currency, funding goal, and country to predict if a Kickstarter project will succeed? 
Prepare the target column
First we'll convert the state column into a target we can use in a model.  Data cleaning isn't the current focus, so we'll simplify this example by:
Dropping projects that are "live"
Counting "successful" states as outcome = 1
Combining every other state as outcome = 0
End of explanation
ks = ks.assign(hour=ks.launched.dt.hour,
               day=ks.launched.dt.day,
               month=ks.launched.dt.month,
               year=ks.launched.dt.year)
Explanation: Convert timestamps
Next, we convert the launched feature into categorical features we can use in a model. Since we loaded the columns as timestamp data, we access date and time values through the .dt attribute on the timestamp column.
Note: If you're not familiar with categorical features and label encoding, please check out this lesson from the Intermediate Machine Learning course.
End of explanation
from sklearn.preprocessing import LabelEncoder
cat_features = ['category', 'currency', 'country']
encoder = LabelEncoder()
# Apply the label encoder to each column
encoded = ks[cat_features].apply(encoder.fit_transform)
Explanation: Prep categorical variables
Now for the categorical variables -- category, currency, and country -- we'll need to convert them into integers so our model can use the data. For this we'll use scikit-learn's LabelEncoder. This assigns an integer to each value of the categorical feature.
End of explanation
# Since ks and encoded have the same index and I can easily join them
data = ks[['goal', 'hour', 'day', 'month', 'year', 'outcome']].join(encoded)
data.head()
Explanation: We collect all of these features in a new dataframe that we can use to train a model.
End of explanation
valid_fraction = 0.1
valid_size = int(len(data) * valid_fraction)
train = data[:-2 * valid_size]
valid = data[-2 * valid_size:-valid_size]
test = data[-valid_size:]
Explanation: Create training, validation, and test splits
We need to create data sets for training, validation, and testing. We'll use a fairly simple approach and split the data using slices. We'll use 10% of the data as a validation set, 10% for testing, and the other 80% for training.
End of explanation
import lightgbm as lgb
feature_cols = train.columns.drop('outcome')
dtrain = lgb.Dataset(train[feature_cols], label=train['outcome'])
dvalid = lgb.Dataset(valid[feature_cols], label=valid['outcome'])
param = {'num_leaves': 64, 'objective': 'binary'}
param['metric'] = 'auc'
num_round = 1000
bst = lgb.train(param, dtrain, num_round, valid_sets=[dvalid], early_stopping_rounds=10, verbose_eval=False)
Explanation: Train a model
For this course we'll be using a LightGBM model. This is a tree-based model that typically provides the best performance, even compared to XGBoost. It's also relatively fast to train. 
We won't do hyperparameter optimization because that isn't the goal of this course. So, our models won't be the absolute best performance you can get. But you'll still see model performance improve as we do feature engineering.
End of explanation
from sklearn import metrics
ypred = bst.predict(test[feature_cols])
score = metrics.roc_auc_score(test['outcome'], ypred)
print(f"Test AUC score: {score}")
Explanation: Make predictions & evaluate the model
Finally, let's make predictions on the test set with the model and see how well it performs. An important thing to remember is that you can overfit to the validation data. This is why we need a test set that the model never sees until the final evaluation.
End of explanation
<END_TASK>