Kaggle competition: New York City Taxi Fare Prediction
mean_error = 0
all_predict = 0
cv = KFold(n_splits=splits, random_state=seed, shuffle=False)
 for train_index, test_index in cv.split(train):
x_train = train.loc[train_index, ::]
x_test = train.loc[test_index, ::]
y_train, y_test = x_train.target, x_test.target
x_train = x_train.drop(['id', 'target'], axis = 1)
 x_test = x_test.drop(['id', 'target'], axis = 1)
 model = model_lgb().fit(x_train, y_train)
 error = mean_squared_error(y_test, model.predict(x_test),squared=False)
 print(error, "
")
 mean_error += error
all_predict += model.predict(test.drop(['id'], axis=1))
print('mean error', mean_error/splits)
 predict = all_predict/splits<save_to_csv>
def rmse(y_true, y_pred):
return np.sqrt(np.sum(( y_true - y_pred)** 2)/ y_true.size )
sample_submission['target'] = predict
sample_submission.to_csv('submission.csv', index=False )<import_modules>
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor
%matplotlib inline 
<load_from_csv>
np.random.seed(seed=1234)
 skiprows = np.random.rand(55 * 10 ** 7)> 0.02
skiprows[0] = False
df_ = pd.read_csv("/kaggle/input/new-york-city-taxi-fare-prediction/train.csv", skiprows=lambda x: skiprows[x])
 df_.head()
train_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv')
 test_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv' )<train_model>
lon_min, lon_max = -75, -72
lat_min, lat_max = 40, 43
y = train_df['target']
X_lr = train_df.drop(['id', 'target'], axis=1)
 scaler = StandardScaler()
scaler.fit(X_lr)
 X_lr = scaler.transform(X_lr)
 X_lr_test = scaler.transform(test_df.drop('id', axis=1 ).values)
 X_train_lr, X_val_lr, y_train_lr, y_val_lr = train_test_split(X_lr, y, test_size=0.3, random_state=17, shuffle=False )<split>
df = df_.copy()
df = df.drop(columns=["key"])
 df["date"] = df["pickup_datetime"].apply(lambda x: x.split() [0])
 df["time"] = df["pickup_datetime"].apply(lambda x: x.split() [1])
 df = df.drop("pickup_datetime", axis=1)
 df["year"] = df["date"].apply(lambda x: int(x.split("-")[0]))
df["month"] = df["date"].apply(lambda x: int(x.split("-")[1]))
df["day"] = df["date"].apply(lambda x: int(x.split("-")[2]))
df = df.drop("date", axis=1)
 df["time"] = df["time"].apply(lambda x: int(x[:2])* 60 + int(x[3:5]))
df["befor_shock"] =(( df["year"] <= 2011)|(( df["year"] <= 2012)&(df["month"] <= 8)) ).apply(int)
 df = df[ 
(~df["dropoff_longitude"].isnull())& 
(~df["dropoff_latitude"].isnull())
 ]
df = df[ 
(lon_min < df["pickup_longitude"])& 
(df["pickup_longitude"] < lon_max)& 
(lat_min < df["pickup_latitude"])& 
(df["pickup_latitude"] < lat_max)& 
(lon_min < df["dropoff_longitude"])& 
(df["dropoff_longitude"] < lon_max)& 
(lat_min < df["dropoff_latitude"])& 
(df["dropoff_latitude"] < lat_max)
 ]
df.head()
y = train_df['target']
X = train_df.drop(['target'], axis=1)
 X_train_df, X_val_df, y_train_df, y_val_df = train_test_split(X, y, test_size =0.3, shuffle=False)
 del train_df<choose_model_class>
X = np.array(df.drop(
 columns=[
"fare_amount",
] 
))
y = np.array(df["fare_amount"] )
m1 = Lasso(alpha=0.001, random_state=123 )<train_model>
np.random.seed(seed=1234)
 train_rows = np.random.rand(y.size)> 0.2
X_train, y_train = X[train_rows, :], y[train_rows]
X_valid, y_valid = X[~train_rows, :], y[~train_rows]
m1_fit = m1.fit(X_train_lr, y_train_lr)
 print('Score reached: {} '.format(m1.score(X_train_lr, y_train_lr)))
<predict_on_test>
model = RandomForestRegressor(max_depth=30, n_estimators=100, n_jobs=-1)
 model.fit(X_train, y_train)
 y_valid_pred = model.predict(X_valid)
 rmse(y_valid, y_valid_pred )
X_test_lr = test_df.drop(['id'], axis=1)
 y_test_lasso = m1.predict(X_test_lr )<choose_model_class>
test = pd.read_csv("/kaggle/input/new-york-city-taxi-fare-prediction/test.csv")
 test.head()
m2 = Ridge(alpha=0.1 )<train_model>
test["date"] = test["pickup_datetime"].apply(lambda x: x.split() [0])
 test["time"] = test["pickup_datetime"].apply(lambda x: x.split() [1])
 test = test.drop("pickup_datetime", axis=1)
 test["year"] = test["date"].apply(lambda x: int(x.split("-")[0]))
test["month"] = test["date"].apply(lambda x: int(x.split("-")[1]))
test["day"] = test["date"].apply(lambda x: int(x.split("-")[2]))
test = test.drop("date", axis=1)
 test["time"] = test["time"].apply(lambda x: int(x[:2])* 60 + int(x[3:5]))
test["before_shock"] =(( test["year"] <= 2011)|(( test["year"] <= 2012)&(test["month"] <= 8)) ).apply(int)
 test.head()
<predict_on_test><EOS> 
X_test = np.array(test.drop(columns=[
"key",
]))
y_pred = model.predict(X_test)
 test["fare_amount"] = y_pred
submission= test[["key", "fare_amount"]]
submission.to_csv("./submission.csv", index=False )