<jupyter_start><jupyter_text>Description* 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 Evaluation* Submissions are evaluated one the Root Mean Squared Logarithmic Error (RMSLE).<jupyter_code>import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import warnings
warnings.filterwarnings(action='ignore') 

# 노트북 안에 그래프를 그리기 위해
%matplotlib inline

# 그래프에서 격자로 숫자 범위가 눈에 잘 띄도록 ggplot 스타일을 사용
plt.style.use('ggplot')
# 폰트
plt.rcParams["font.family"] = 'NanumGothic'
# 그래프에서 마이너스 폰트 깨지는 문제에 대한 대처

mpl.rcParams['axes.unicode_minus'] = False
train = pd.read_csv("dataset/train.csv", parse_dates=["datetime"])
train.shape
test = pd.read_csv("dataset/test.csv", parse_dates=["datetime"])
test.shape
# train.columns
# train.dtypes
train.info()
train.head()
train.temp.describe()
train.isnull().sum()
import missingno as msno

msno.matrix(train, figsize=(12,5))
train["year"] = train["datetime"].dt.year
train["month"] = train["datetime"].dt.month
train["day"] = train["datetime"].dt.day
train["hour"] = train["datetime"].dt.hour
train["minute"] = train["datetime"].dt.minute
train["second"] = train["datetime"].dt.second
train.shape
train.head()
figure, ((ax1,ax2,ax3), (ax4,ax5,ax6)) = plt.subplots(nrows=2, ncols=3)
figure.set_size_inches(18,8)

sns.barplot(data=train, x="year", y="count", ax=ax1)
sns.barplot(data=train, x="month", y="count", ax=ax2)
sns.barplot(data=train, x="day", y="count", ax=ax3)
sns.barplot(data=train, x="hour", y="count", ax=ax4)
sns.barplot(data=train, x="minute", y="count", ax=ax5)
sns.barplot(data=train, x="second", y="count", ax=ax6)

ax1.set(ylabel='Count',title="연도별 대여량")
ax2.set(xlabel='month',title="월별 대여량")
ax3.set(xlabel='day', title="일별 대여량")
ax4.set(xlabel='hour', title="시간별 대여량")
fig, axes = plt.subplots(nrows=2,ncols=2)
fig.set_size_inches(12, 10)
sns.boxplot(data=train,y="count",orient="v",ax=axes[0][0])
sns.boxplot(data=train,y="count",x="season",orient="v",ax=axes[0][1])
sns.boxplot(data=train,y="count",x="hour",orient="v",ax=axes[1][0])
sns.boxplot(data=train,y="count",x="workingday",orient="v",ax=axes[1][1])

axes[0][0].set(ylabel='Count',title="대여량")
axes[0][1].set(xlabel='Season', ylabel='Count',title="계절별 대여량")
axes[1][0].set(xlabel='Hour Of The Day', ylabel='Count',title="시간별 대여량")
axes[1][1].set(xlabel='Working Day', ylabel='Count',title="근무일 여부에 따른 대여량")
train["dayofweek"] = train["datetime"].dt.dayofweek
train.shape
train["dayofweek"].value_counts()
fig,(ax1,ax2,ax3,ax4,ax5)= plt.subplots(nrows=5)
fig.set_size_inches(18,25)

sns.pointplot(data=train, x="hour", y="count", ax=ax1)

sns.pointplot(data=train, x="hour", y="count", hue="workingday", ax=ax2)

sns.pointplot(data=train, x="hour", y="count", hue="dayofweek", ax=ax3)

sns.pointplot(data=train, x="hour", y="count", hue="weather", ax=ax4)

sns.pointplot(data=train, x="hour", y="count", hue="season", ax=ax5)
corrMatt = train[["temp", "atemp", "casual", "registered", "humidity", "windspeed", "count"]]
#상관관계 함수
corrMatt = corrMatt.corr()
corrMatt.head()

mask = np.array(corrMatt)
mask[np.tril_indices_from(mask)] = False
mask
fig, ax = plt.subplots()
fig.set_size_inches(20,10)
sns.heatmap(corrMatt, mask=mask,vmax=.8, square=True,annot=True)
fig,(ax1,ax2,ax3) = plt.subplots(ncols=3)
fig.set_size_inches(12, 5)
sns.regplot(x="temp", y="count", data=train,ax=ax1)
sns.regplot(x="windspeed", y="count", data=train,ax=ax2)
sns.regplot(x="humidity", y="count", data=train,ax=ax3)
def concatenate_year_month(datetime):
    return "{0}-{1}".format(datetime.year, datetime.month)

train["year_month"] = train["datetime"].apply(concatenate_year_month)

print(train.shape)
train[["datetime", "year_month"]].head()
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
fig.set_size_inches(18, 4)

sns.barplot(data=train, x="year", y="count", ax=ax1)
sns.barplot(data=train, x="month", y="count", ax=ax2)

fig, ax3 = plt.subplots(nrows=1, ncols=1)
fig.set_size_inches(18, 4)

sns.barplot(data=train, x="year_month", y="count", ax=ax3)
# trainWithoutOutliers 표준편차보다 큰차이가 나는 것 들을 제거
train_ = train[np.abs(train["count"] - train["count"].mean()) <= (3*train["count"].std())]
print(train.shape)
print(train_.shape)
#train_.drop('year_month', axis=1, inplace=True)
# count값의 데이터 분포도를 비교파악

figure, axes = plt.subplots(ncols=2, nrows=2)
figure.set_size_inches(12, 10)

sns.distplot(train["count"], ax=axes[0][0])
stats.probplot(train["count"], dist='norm', fit=True, plot=axes[0][1])
sns.distplot(np.log(train_["count"]), ax=axes[1][0])
stats.probplot(np.log1p(train_["count"]), dist='norm', fit=True, plot=axes[1][1])
# widspeed 풍속에 0 값이 가장 많다. => 잘못 기록된 데이터를 고쳐 줄 필요가 있음
fig, axes = plt.subplots(nrows=2)
fig.set_size_inches(18,10)

plt.sca(axes[0])
plt.xticks(rotation=30, ha='right')
axes[0].set(ylabel='Count',title="train windspeed")
sns.countplot(data=train, x="windspeed", ax=axes[0])

plt.sca(axes[1])
plt.xticks(rotation=30, ha='right')
axes[1].set(ylabel='Count',title="test windspeed")
sns.countplot(data=test, x="windspeed", ax=axes[1])
test_ = test
test_["year"] = test["datetime"].dt.year
test_["month"] = test["datetime"].dt.month
test_["day"] = test["datetime"].dt.day
test_["hour"] = test["datetime"].dt.hour
test_["minute"] = test["datetime"].dt.minute
test_["second"] = test["datetime"].dt.second
test_["dayofweek"] = test["datetime"].dt.dayofweek
test_.shape
# 풍속이 0인것과 아닌 것의 세트를 나누어 준다.
trainWind0 = train_.loc[train['windspeed'] == 0]
trainWindNot0 = train_.loc[train['windspeed'] != 0]
print(trainWind0.shape)
print(trainWindNot0.shape)
# 그래서 머신러닝으로 예측을 해서 풍속을 넣어주도록 한다.
from sklearn.ensemble import RandomForestClassifier

def predict_windspeed(data):
    
    # 풍속이 0인것과 아닌 것을 나누어 준다.
    dataWind0 = data.loc[data['windspeed'] == 0]
    dataWindNot0 = data.loc[data['windspeed'] != 0]
    
    # 풍속을 예측할 피처를 선택한다.
    wCol = ["season", "weather", "humidity", "month", "temp", "year", "atemp"]

    # 풍속이 0이 아닌 데이터들의 타입을 스트링으로 바꿔준다.
    dataWindNot0["windspeed"] = dataWindNot0["windspeed"].astype("str")

    # 랜덤포레스트 분류기를 사용한다.
    rfModel_wind = RandomForestClassifier()

    # wCol에 있는 피처의 값을 바탕으로 풍속을 학습시킨다.
    rfModel_wind.fit(dataWindNot0[wCol], dataWindNot0["windspeed"])

    # 학습한 값을 바탕으로 풍속이 0으로 기록 된 데이터의 풍속을 예측한다.
    predict0Values = rfModel_wind.predict(X = dataWind0[wCol])
    print(predict0Values)

    # 값이 0으로 기록 된 풍속에 대해 예측한 값을 넣어준다.
    dataWind0["windspeed"] = predict0Values

    # dataWindNot0 0이 아닌 풍속이 있는 데이터프레임에 예측한 값이 있는 데이터프레임을 합쳐준다.
    data = dataWindNot0.append(dataWind0)

    # 풍속의 데이터타입을 float으로 지정해 준다.
    data["windspeed"] = data["windspeed"].astype("float")

    data.reset_index(inplace=True)
    data.drop('index', inplace=True, axis=1)
    
    return data
'''
# 0값을 조정한다.
train_ = predict_windspeed(train_)
# test = predict_windspeed(test)

# widspeed 의 0값을 조정한 데이터를 시각화
fig, ax1 = plt.subplots()
fig.set_size_inches(18,6)

plt.sca(ax1)
plt.xticks(rotation=30, ha='right')
ax1.set(ylabel='Count',title="train windspeed")
sns.countplot(data=train_, x="windspeed", ax=ax1)
'''
# 연속형 feature와 범주형 feature 
# 연속형 feature = ["temp","humidity","windspeed","atemp"]
# 범주형 feature의 type을 category로 변경 해 준다.
categorical_feature_names = ["season","holiday","workingday","weather",
                             "dayofweek","month","year","hour"]

for var in categorical_feature_names:
    train_[var] = train[var].astype("category")
    test_[var] = test[var].astype("category")
feature_names = ["season", "weather", "temp", "atemp", "humidity", "windspeed",
                 "year", "hour", "dayofweek", "holiday", "workingday"]

X_train = train_[feature_names]

print(X_train.shape)
X_train.head()
y_train = train_["count"]
print(y_train.shape)
y_train.head()
X_test = test[feature_names]

print(X_test.shape)
X_test.head()
from sklearn.metrics import make_scorer

def rmsle_score(preds, true):
    rmsle_score = (np.sum((np.log1p(preds)-np.log1p(true))**2)/len(true))**0.5
    return rmsle_score
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score

k_fold = KFold(n_splits=10, shuffle=True, random_state=0)
from sklearn.ensemble import RandomForestRegressor

max_depth_list = []

model = RandomForestRegressor(n_estimators=100,
                              n_jobs=-1,
                              random_state=0)
model
%time score = cross_val_score(model, X_train, y_train, cv=k_fold, scoring=rmsle_scorer)
score = score.mean()
# 0에 근접할수록 좋은 데이터
print("Score= {0:.5f}".format(score))
print(score)
model.fit(X_train, y_train)
# 예측
predictions = model.predict(X_test)

print(predictions.shape)
predictions[0:10]
# 예측한 데이터를 시각화 해본다. 
fig,(ax1,ax2)= plt.subplots(ncols=2)
fig.set_size_inches(12,5)
sns.distplot(y_train,ax=ax1,bins=50)
ax1.set(title="train")
sns.distplot(predictions,ax=ax2,bins=50)
ax2.set(title="test")
submission = pd.read_csv("dataset/sampleSubmission.csv")
submission

submission["count"] = predictions

print(submission.shape)
submission.head()
submission.to_csv("./Score_{0:.5f}_submission.csv".format(score), index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17558886509635974, 'fraction_numerical': 0.021413276231263382, 'mean_word_length': 6.005714285714285, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '471593', 'license': 'MIT', 'n_tokens_mistral': 4315, 'n_tokens_neox': 4464, 'n_words': 741, 'path': 'competition/data_analysis/bike_sharing_demand/BikeSharingDemand.ipynb', 'repo_name': 'codeMinst/ML-SimplerAPI', 'size': '9807'}