Kaggle competition: Bike Sharing Demand
def svc_param_selection(X, y, nfolds):
Cs = [1.07]
gammas = [2.075]
param_grid = {'C': Cs, 'gamma' : gammas}
grid_search = GridSearchCV(SVC(kernel='rbf'), param_grid, cv=nfolds, n_jobs=8)
 grid_search.fit(X, y)
 grid_search.best_params_
return grid_search 
<choose_model_class>
%matplotlib inline
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(input_dim,)))
 model.add(Dense(128, activation='relu'))
model.add(Dense(output_dim, input_dim=input_dim, activation='softmax'))
model.summary()<train_model>
train = pd.read_csv(".. /input/bike-sharing-demand/train.csv")
 test = pd.read_csv(".. /input/bike-sharing-demand/test.csv")
 submit = pd.read_csv('.. /input/bike-sharing-demand/sampleSubmission.csv')
 train_weather = pd.read_csv(".. /input/train-weather-last/bike.csv" )
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 nb_epoch = 10
history = model.fit(train_arrays,train_labels, batch_size=batch_size, epochs=nb_epoch, verbose=1 )<predict_on_test>
data = train.append(test, sort=True)
 data.shape
data.reset_index(inplace=True)
 data.drop('index',inplace=True,axis=1)
 data["date"] = data.datetime.apply(lambda x : x.split() [0])
 data["hour"] = data.datetime.apply(lambda x : x.split() [1].split(":")[0] ).astype("int")
 data["year"] = data.datetime.apply(lambda x : x.split() [0].split("-")[0])
 data["weekday"] = data.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).weekday())
 data["month"] = data.date.apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).month)
 data.head()
pred = model.predict(test_arrays )<predict_on_test>
year = pd.get_dummies(data['year'], drop_first=True)
 data = data.merge(year, left_index=True, right_index=True)
 data.rename(columns={'2012':'year_2012'},
inplace=True)
 month = pd.get_dummies(data['month'], drop_first=True)
 data = data.merge(month, left_index=True, right_index=True)
 data.rename(columns={2:'Feb',
3:'Mar',
4:'Apr',
5:'May',
6:'Jun',
7:'Jul',
8:'Aug',
9:'Sep',
10:'Oct',
11:'Nov',
12:'Dec'},
inplace=True)
 hour = pd.get_dummies(data['hour'], drop_first=True)
 data = data.merge(hour, left_index=True, right_index=True)
 print(data.columns )
test_pred = model.predict(X_test)
 submission['target'] = test_pred.round().astype(int)
<define_variables>
count = 2
prob_disaster = 0.9
keyword_list_disaster = list(ag[(ag['Count']>count)&(ag['Disaster Probability']>=prob_disaster)].index)
 keyword_list_disaster<feature_engineering>
dataTrain = data[pd.notnull(data['count'])].sort_values(by=["datetime"])
 dataTest = data[~pd.notnull(data['count'])].sort_values(by=["datetime"])
 datetimecol = dataTest["datetime"]
yLabels = dataTrain["count"]
yLabelsLog = np.log(yLabels )
ids_disaster = test['id'][test.keyword.isin(keyword_list_disaster)].values
submission['target'][submission['id'].isin(ids_disaster)] = 1<save_to_csv>
dropFeatures = ['casual',"count","datetime","date","registered","season","weather","year","weekday","month","hour","temp","holiday"]
dataTrain = dataTrain.drop(dropFeatures,axis=1)
 dataTest = dataTest.drop(dropFeatures,axis=1 )
submission.to_csv("submission.csv", index=False)
 submission.head(10 )<import_modules>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import string<load_from_csv>
lrmodel = LinearRegression()
lrmodel.fit(dataTrain, yLabelsLog)
 r_sq = lrmodel.score(dataTrain, yLabelsLog)
 print('coefficient of determination(r_square)= ', r_sq)
 print('intercept = ', lrmodel.intercept_)
 print('slope = ', lrmodel.coef_)
train = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 test = pd.read_csv(".. /input/nlp-getting-started/test.csv")
 sub_sample = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
<create_dataframe>
dataTrain_ols = sm.add_constant(dataTrain)
 olsmodel = sm.OLS(yLabelsLog, dataTrain_ols ).fit()
print(olsmodel.summary() )
all_data_na =(train.isnull().sum() / len(train)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head() 
<create_dataframe>
transformer = PolynomialFeatures(degree=2, include_bias=False)
 dataTrain_poly = transformer.fit_transform(dataTrain)
 prmodel = LinearRegression().fit(dataTrain_poly, yLabelsLog)
 r_sq = prmodel.score(dataTrain_poly, yLabelsLog)
 print('coefficient of determination(r_square)= ', r_sq)
 print('intercept = ', prmodel.intercept_)
 print('slope = ', prmodel.coef_)
all_data_na =(test.isnull().sum() / len(test)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head() 
<count_duplicates>
predsTest = lrmodel.predict(X= dataTest)
 submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_LR.csv', index=False )
train.duplicated().sum()<count_values>
dataTest_poly = transformer.fit_transform(dataTest)
 predsTest_poly = prmodel.predict(X= dataTest_poly)
 submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest_poly)]
})
 submission.to_csv('bike_predictions_PR.csv', index=False )