# 做完特徵工程前的所有準備
import pandas as pd
import numpy as np
import datetime
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
import warnings
warnings.filterwarnings('ignore')


data_path ='E:\\python\\ML-day100\\data\\'
df = pd.read_csv(data_path + 'taxi_data1.csv')

train_Y = df['fare_amount']
df = df.drop(['fare_amount'] , axis=1)
df.head()



# 時間特徵分解方式:使用datetime
df['pickup_datetime'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S UTC'))
df['pickup_year'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%Y')).astype('int64')
df['pickup_month'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%m')).astype('int64')
df['pickup_day'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%d')).astype('int64')
df['pickup_hour'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%H')).astype('int64')
df['pickup_minute'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%M')).astype('int64')
df['pickup_second'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%S')).astype('int64')
df.head()



# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
scaler = MinMaxScaler()
train_X = scaler.fit_transform(df_temp)
Linear = LinearRegression()
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
GDBT = GradientBoostingRegressor()
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')




# 加入星期幾與第幾周兩個特徵

import time
df['pickup_weekday'] = df['pickup_datetime'].apply(lambda x: datetime.date.weekday(x)).astype('int64')
df['pickup_weeks'] = df['pickup_datetime'].apply(lambda x: x.strftime("%W")).astype('int64')
df.head()



# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
train_X = scaler.fit_transform(df_temp)
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')




# 加上"日週期"特徵 (參考講義"週期循環特徵")
import math
df['day_cycle'] = df['pickup_hour']/12 + df['pickup_minute']/720 + df['pickup_second']/43200
df['day_cycle'] = df['day_cycle'].map(lambda x:math.sin(x*math.pi))
df.head()




# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
train_X = scaler.fit_transform(df_temp)
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')









# 加上"年週期"與"周週期"特徵


df['year_cycle'] = df['pickup_month']/6 + df['pickup_day']/180 
df['year_cycle'] = df['year_cycle'].map(lambda x:math.cos(x*math.pi))
df['week_cycle'] = (df['pickup_weekday'] + 1)/3.5 + df['pickup_hour']/84
df['week_cycle'] = df['week_cycle'].map(lambda x:math.sin(x*math.pi))
df.head()



# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
train_X = scaler.fit_transform(df_temp)
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')
898470c466515d6f94d2cf57a6d8556d15d9ae5f
<mask token>

<mask token>
warnings.filterwarnings('ignore')
<mask token>
df.head()
<mask token>
df.head()
<mask token>
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
<mask token>
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
<mask token>
df.head()
<mask token>
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
<mask token>
df.head()
<mask token>
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
<mask token>
df.head()
<mask token>
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )

<mask token>
warnings.filterwarnings('ignore')
data_path = 'E:\\python\\ML-day100\\data\\'
df = pd.read_csv(data_path + 'taxi_data1.csv')
train_Y = df['fare_amount']
df = df.drop(['fare_amount'], axis=1)
df.head()
df['pickup_datetime'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strptime(x, '%Y-%m-%d %H:%M:%S UTC'))
df['pickup_year'] = df['pickup_datetime'].apply(lambda x: datetime.datetime
    .strftime(x, '%Y')).astype('int64')
df['pickup_month'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strftime(x, '%m')).astype('int64')
df['pickup_day'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.
    strftime(x, '%d')).astype('int64')
df['pickup_hour'] = df['pickup_datetime'].apply(lambda x: datetime.datetime
    .strftime(x, '%H')).astype('int64')
df['pickup_minute'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strftime(x, '%M')).astype('int64')
df['pickup_second'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strftime(x, '%S')).astype('int64')
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
scaler = MinMaxScaler()
train_X = scaler.fit_transform(df_temp)
Linear = LinearRegression()
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
GDBT = GradientBoostingRegressor()
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
<mask token>
df['pickup_weekday'] = df['pickup_datetime'].apply(lambda x: datetime.date.
    weekday(x)).astype('int64')
df['pickup_weeks'] = df['pickup_datetime'].apply(lambda x: x.strftime('%W')
    ).astype('int64')
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
train_X = scaler.fit_transform(df_temp)
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
<mask token>
df['day_cycle'] = df['pickup_hour'] / 12 + df['pickup_minute'] / 720 + df[
    'pickup_second'] / 43200
df['day_cycle'] = df['day_cycle'].map(lambda x: math.sin(x * math.pi))
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
train_X = scaler.fit_transform(df_temp)
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
df['year_cycle'] = df['pickup_month'] / 6 + df['pickup_day'] / 180
df['year_cycle'] = df['year_cycle'].map(lambda x: math.cos(x * math.pi))
df['week_cycle'] = (df['pickup_weekday'] + 1) / 3.5 + df['pickup_hour'] / 84
df['week_cycle'] = df['week_cycle'].map(lambda x: math.sin(x * math.pi))
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
train_X = scaler.fit_transform(df_temp)
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )

import pandas as pd
import numpy as np
import datetime
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
import warnings
warnings.filterwarnings('ignore')
data_path = 'E:\\python\\ML-day100\\data\\'
df = pd.read_csv(data_path + 'taxi_data1.csv')
train_Y = df['fare_amount']
df = df.drop(['fare_amount'], axis=1)
df.head()
df['pickup_datetime'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strptime(x, '%Y-%m-%d %H:%M:%S UTC'))
df['pickup_year'] = df['pickup_datetime'].apply(lambda x: datetime.datetime
    .strftime(x, '%Y')).astype('int64')
df['pickup_month'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strftime(x, '%m')).astype('int64')
df['pickup_day'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.
    strftime(x, '%d')).astype('int64')
df['pickup_hour'] = df['pickup_datetime'].apply(lambda x: datetime.datetime
    .strftime(x, '%H')).astype('int64')
df['pickup_minute'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strftime(x, '%M')).astype('int64')
df['pickup_second'] = df['pickup_datetime'].apply(lambda x: datetime.
    datetime.strftime(x, '%S')).astype('int64')
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
scaler = MinMaxScaler()
train_X = scaler.fit_transform(df_temp)
Linear = LinearRegression()
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
GDBT = GradientBoostingRegressor()
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
import time
df['pickup_weekday'] = df['pickup_datetime'].apply(lambda x: datetime.date.
    weekday(x)).astype('int64')
df['pickup_weeks'] = df['pickup_datetime'].apply(lambda x: x.strftime('%W')
    ).astype('int64')
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
train_X = scaler.fit_transform(df_temp)
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
import math
df['day_cycle'] = df['pickup_hour'] / 12 + df['pickup_minute'] / 720 + df[
    'pickup_second'] / 43200
df['day_cycle'] = df['day_cycle'].map(lambda x: math.sin(x * math.pi))
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
train_X = scaler.fit_transform(df_temp)
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )
df['year_cycle'] = df['pickup_month'] / 6 + df['pickup_day'] / 180
df['year_cycle'] = df['year_cycle'].map(lambda x: math.cos(x * math.pi))
df['week_cycle'] = (df['pickup_weekday'] + 1) / 3.5 + df['pickup_hour'] / 84
df['week_cycle'] = df['week_cycle'].map(lambda x: math.sin(x * math.pi))
df.head()
df_temp = df.drop(['pickup_datetime'], axis=1)
train_X = scaler.fit_transform(df_temp)
print(
    f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}'
    )
print(
    f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}'
    )

# 做完特徵工程前的所有準備
import pandas as pd
import numpy as np
import datetime
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
import warnings
warnings.filterwarnings('ignore')


data_path ='E:\\python\\ML-day100\\data\\'
df = pd.read_csv(data_path + 'taxi_data1.csv')

train_Y = df['fare_amount']
df = df.drop(['fare_amount'] , axis=1)
df.head()



# 時間特徵分解方式:使用datetime
df['pickup_datetime'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S UTC'))
df['pickup_year'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%Y')).astype('int64')
df['pickup_month'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%m')).astype('int64')
df['pickup_day'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%d')).astype('int64')
df['pickup_hour'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%H')).astype('int64')
df['pickup_minute'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%M')).astype('int64')
df['pickup_second'] = df['pickup_datetime'].apply(lambda x: datetime.datetime.strftime(x, '%S')).astype('int64')
df.head()



# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
scaler = MinMaxScaler()
train_X = scaler.fit_transform(df_temp)
Linear = LinearRegression()
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
GDBT = GradientBoostingRegressor()
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')




# 加入星期幾與第幾周兩個特徵

import time
df['pickup_weekday'] = df['pickup_datetime'].apply(lambda x: datetime.date.weekday(x)).astype('int64')
df['pickup_weeks'] = df['pickup_datetime'].apply(lambda x: x.strftime("%W")).astype('int64')
df.head()



# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
train_X = scaler.fit_transform(df_temp)
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')




# 加上"日週期"特徵 (參考講義"週期循環特徵")
import math
df['day_cycle'] = df['pickup_hour']/12 + df['pickup_minute']/720 + df['pickup_second']/43200
df['day_cycle'] = df['day_cycle'].map(lambda x:math.sin(x*math.pi))
df.head()




# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
train_X = scaler.fit_transform(df_temp)
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')









# 加上"年週期"與"周週期"特徵


df['year_cycle'] = df['pickup_month']/6 + df['pickup_day']/180 
df['year_cycle'] = df['year_cycle'].map(lambda x:math.cos(x*math.pi))
df['week_cycle'] = (df['pickup_weekday'] + 1)/3.5 + df['pickup_hour']/84
df['week_cycle'] = df['week_cycle'].map(lambda x:math.sin(x*math.pi))
df.head()



# 將結果使用線性迴歸 / 梯度提升樹分別看結果
df_temp = df.drop(['pickup_datetime'] , axis=1)
train_X = scaler.fit_transform(df_temp)
print(f'Linear Reg Score : {cross_val_score(Linear, train_X, train_Y, cv=5).mean()}')
print(f'Gradient Boosting Reg Score : {cross_val_score(GDBT, train_X, train_Y, cv=5).mean()}')