Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<drop_column>
%matplotlib inline
mpl.rcParams['axes.unicode_minus']=False
warnings.filterwarnings('ignore' )
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 remove_URL(example )<feature_engineering>
train=pd.read_csv(".. /input/bike-sharing-demand/train.csv", parse_dates=["datetime"])
 train.shape
df['text']=df['text'].apply(lambda x : remove_URL(x))<drop_column>
test=pd.read_csv(".. /input/bike-sharing-demand/test.csv", parse_dates=["datetime"])
 test.shape
def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 print(remove_html(example))<feature_engineering>
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["dayofweek"]=train["datetime"].dt.dayofweek
train.shape
df['text']=df['text'].apply(lambda x : remove_html(x))<drop_column>
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
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 remove_emoji("Omg another Earthquake 😔😔" )<feature_engineering>
df['text']=df['text'].apply(lambda x: remove_emoji(x))<categorify>
def predict_windspeed(data):
dataWind0 = data.loc[data['windspeed'] == 0]
dataWindNot0 = data.loc[data['windspeed'] != 0]
wCol = ["season", "weather", "humidity", "month", "temp", "year", "atemp"]
dataWindNot0["windspeed"] = dataWindNot0["windspeed"].astype("str")
 rfModel_wind = RandomForestClassifier()
rfModel_wind.fit(dataWindNot0[wCol], dataWindNot0["windspeed"])
 wind0Values = rfModel_wind.predict(X = dataWind0[wCol])
 predictWind0 = dataWind0
predictWindNot0 = dataWindNot0
predictWind0["windspeed"] = wind0Values
data = predictWindNot0.append(predictWind0)
 data["windspeed"] = data["windspeed"].astype("float")
 data.reset_index(inplace=True)
 data.drop('index', inplace=True, axis=1)
 return data
def remove_punct(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 example="I am a
print(remove_punct(example))<feature_engineering>
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" )
df['text']=df['text'].apply(lambda x : remove_punct(x))<split>
feature_names = ["season", "weather", "temp", "atemp", "humidity", "windspeed",
"year", "hour", "dayofweek", "holiday", "workingday"]
feature_names
def cv(data):
count_vectorizer = CountVectorizer()
emb = count_vectorizer.fit_transform(data)
 return emb, count_vectorizer
list_corpus = df["text"].tolist()
list_labels = df["target"].tolist()
X_train, X_test, y_train, y_test = train_test_split(list_corpus, list_labels, test_size=0.2, random_state=random_state_split)
 X_train_counts, count_vectorizer = cv(X_train)
 X_test_counts = count_vectorizer.transform(X_test )<feature_engineering>
def rmsle(predicted_values, actual_values):
predicted_values = np.array(predicted_values)
 actual_values = np.array(actual_values)
 log_predict = np.log(predicted_values + 1)
 log_actual = np.log(actual_values + 1)
 difference = log_predict - log_actual
difference = np.square(difference)
 mean_difference = difference.mean()
score = np.sqrt(mean_difference)
 return score
rmsle_scorer = make_scorer(rmsle)
 rmsle_scorer
def tfidf(data):
tfidf_vectorizer = TfidfVectorizer()
train = tfidf_vectorizer.fit_transform(data)
 return train, tfidf_vectorizer
X_train_tfidf, tfidf_vectorizer = tfidf(X_train)
 X_test_tfidf = tfidf_vectorizer.transform(X_test )<import_modules>
k_fold = KFold(n_splits=10, shuffle=True, random_state=0 )
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.metrics import classification_report, accuracy_score,make_scorer<split>
max_depth_list = []
model = RandomForestRegressor(n_estimators=100,
n_jobs=-1,
random_state=0)
 model
df_train = df[:7612]
df_test = df[7613:10875]<prepare_x_and_y>
%time score = cross_val_score(model, X_train, y_train, cv=k_fold, scoring=rmsle_scorer)
 score = score.mean()
print("Score= {0:.5f}".format(score))
X = df_train['text']
y = df_train['target']<split>
model.fit(X_train, y_train )
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1,random_state=42 )<train_model>
predictions = model.predict(X_test)
 print(predictions.shape)
 predictions[0:10]
vectorizer=TfidfVectorizer(ngram_range=(1,3),min_df=3,strip_accents='unicode', use_idf=1,smooth_idf=1, sublinear_tf=1,max_features=None)
 vectorizer.fit(list(df['text']))
print('vocab length',len(vectorizer.vocabulary_))<categorify>
submission = pd.read_csv(".. /input/bike-sharing-demand/sampleSubmission.csv")
 submission
submission["count"] = predictions
print(submission.shape)
 submission.head()
X_train_onehot = vectorizer.transform(X_train ).todense()
X_val_onehot = vectorizer.transform(X_val ).todense()<train_model>
submission.to_csv("Score_{0:.5f}_sampleSubmission.csv".format(score), index=False )