<jupyter_start><jupyter_text>**Dataset**


*   datetime : hourly date + timestamp
*   season : 1 = spring, 2 = summer, 3 = fall, 4 = winter
*   holiday : whether the day is considered a holiday
*   workingday : whether the day is neither a weekend nor holiday
*   weather
  *   1: Clear, Few clouds, Partly cloudy, Partly cloudy
  *   2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist
  *   3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds
  *   4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog
*   temp : temperature in Celsius
*   atemp : "feels like" temperature in Celsius
*   humidity : relative humidity
*   windspeed : wind speed
*   casual : number of non-registered user rentals initiated
*   registered : number of registered user rentals initiated
*   count : number of total rentals<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>**Data**<jupyter_code>from google.colab import files
myfile = files.upload()
import io

test_df = pd.read_csv(io.BytesIO(myfile['test.csv']), parse_dates=["datetime"])
train_df = pd.read_csv(io.BytesIO(myfile['train.csv']), parse_dates=["datetime"])
all_df = pd.concat((train_df, test_df), axis=0).reset_index() #handle duplicate index values through reset index
all_df.head()
all_df.tail()
train_index = list(range(len(train_df)))
test_index = list(range(len(train_df),len(all_df)))
all_df.isnull().sum()<jupyter_output><empty_output><jupyter_text>**Evaluation metrics**

$\sqrt{\frac{1}{n}\Sigma^n_{i=1}(log(p_i+1)-log(a_i+1))^2}$

*   n is the number of hours in the test set
*   pi is your predicted count
*   ai is the actual count
*   log(x) is the natural logarithm<jupyter_code>x = np.array([np.inf, -np.inf, np.nan, -128, 128])
np.nan_to_num(x)
def rmsle(y, y_):
    log1 = np.nan_to_num(np.log(y + 1))
    log2 = np.nan_to_num(np.log(y_ + 1))
    calc = (log1 - log2) ** 2
    return np.sqrt(np.mean(calc))
myfile2 = files.upload()
submission_df = pd.read_csv(io.BytesIO(myfile2['sampleSubmission.csv']), parse_dates=["datetime"])
submission_df.head()
rmsle(submission_df["count"].values, 
      np.random.randint(0, 100, size=len(submission_df)))
del all_df["casual"]
del all_df["registered"]
del all_df["index"]
pre_df = all_df.merge(pd.get_dummies(all_df["season"], prefix="season"), left_index=True, right_index=True)
pre_df.head()
pre_df = pre_df.merge(pd.get_dummies(all_df["weather"], prefix="weather"), left_index=True, right_index=True)
pre_df.head()
pre_df["datetime"].unique()
pre_df["year"] = pre_df["datetime"].dt.year
pre_df["month"] = pre_df["datetime"].dt.month
pre_df["day"] = pre_df["datetime"].dt.day
pre_df["hour"] = pre_df["datetime"].dt.hour
pre_df["weekday"] = pre_df["datetime"].dt.weekday
pre_df= pre_df.merge(pd.get_dummies(pre_df["weekday"], prefix="weekday"), left_index=True, right_index=True)
pre_df.head()
pre_df.dtypes
category_variable_list = ["season","weather","workingday","season_1","season_2","season_3","season_4","weather_1","weather_2","weather_3","weather_4","year","month","day","hour","weekday","weekday_0","weekday_1","weekday_2","weekday_3","weekday_4","weekday_5","weekday_6"]
for var_name in category_variable_list:
    pre_df[var_name] = pre_df[var_name].astype("category")
pre_df.dtypes
train_df = pre_df.iloc[train_index]
fig, axes = plt.subplots(nrows=3,ncols=3)
fig.set_size_inches(12, 5)
axes[0][0].bar(train_df["year"], train_df["count"])
axes[0][1].bar(train_df["weather"], train_df["count"])
axes[0][2].bar(train_df["workingday"], train_df["count"])
axes[1][0].bar(train_df["holiday"], train_df["count"])
axes[1][1].bar(train_df["weekday"], train_df["count"])
axes[1][2].bar(train_df["month"], train_df["count"])
axes[2][0].bar(train_df["day"], train_df["count"])
axes[2][1].bar(train_df["hour"], train_df["count"])
plt.show()
serires_data = train_df.groupby(["month"])["count"].mean()
serires_data.index.tolist()[:5]
fig, ax = plt.subplots()
ax.bar(range(len(serires_data)), serires_data)
fig.set_size_inches(12,5)
plt.show()
import seaborn as sn

fig,(ax1,ax2,ax3) = plt.subplots(ncols=3)
fig.set_size_inches(12, 5)
sn.regplot(x="temp", y="count", data=train_df,ax=ax1)
sn.regplot(x="windspeed", y="count", data=train_df,ax=ax2)
sn.regplot(x="humidity", y="count", data=train_df,ax=ax3)
plt.show()
category_variable_list
corrMatt = train_df[["temp","atemp","humidity","windspeed","count"]].corr()
mask = np.array(corrMatt)
mask[np.tril_indices_from(mask)] = False
fig,ax= plt.subplots()
fig.set_size_inches(20,10)
sn.heatmap(corrMatt, mask=mask,vmax=.8, square=True,annot=True)
plt.show()
category_variable_list[:5]
continuous_variable_list = ["temp","humidity","windspeed","atemp"]
season_list = ['season_1', 'season_2', 'season_3', 'season_4']
weather_list = ['weather_1', 'weather_2', 'weather_3', 'weather_4']
weekday_list = ['weekday_0','weekday_1','weekday_2','weekday_3','weekday_4','weekday_5','weekday_6']
category_varialbe_list = ["season","holiday","workingday","weather","weekday","month","year","hour"]
all_variable_list = continuous_variable_list + category_varialbe_list
all_variable_list.append(season_list)
all_variable_list.append(weather_list)
all_variable_list.append(weekday_list)
all_variable_list
number_of_variables = len(all_variable_list)
number_of_variables
varaible_combinations = []
import itertools
for L in range(8, number_of_variables+1):
    for subset in itertools.combinations(all_variable_list, L):
        temp = []
        for variable in subset:
            if isinstance(variable, list):
                for value in variable:
                    temp.append(value)
            else:
                temp.append(variable)
        varaible_combinations.append(temp)
len(varaible_combinations)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16573272202411277, 'fraction_numerical': 0.017999660383766346, 'mean_word_length': 6.13939393939394, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '451071', 'license': 'Apache-2.0', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2171, 'n_words': 375, 'path': 'Lec3_8_Kaggle_with_bike_example.ipynb', 'repo_name': 'YeonKang/Python-for-Machine-Learning', 'size': '5889'}