Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<train_on_grid>
warnings.filterwarnings('always')
 warnings.filterwarnings('ignore')
 style.use('fivethirtyeight')
 sns.set(style='whitegrid',color_codes=True)
params={'n_neighbors' : range(1, 20), 'leaf_size' : range(1, 50)}
knn_grid = GridSearchCV(KNeighborsClassifier() , params, scoring='roc_auc')
 knn_grid.fit(X_train, y_train)
 print("Accuracy on train data: ", knn_grid.score(X_train, y_train))
print("Accuracy on test data: ", knn_grid.score(X_test, y_test))<train_on_grid>
train=pd.read_csv(r'.. /input/bike-sharing-demand/train.csv')
 test=pd.read_csv(r'.. /input/bike-sharing-demand/test.csv')
 df=train.copy()
test_df=test.copy()
df.head()
params2 = {'alpha' : [0.00001, 0.0001, 0.001, 0.01, 1, 10, 100, 1000], 'normalize' : [True, False], 'random_state' : [0, 50, 100, 150, 200]}
r_grid = GridSearchCV(RidgeClassifier() , params2, scoring='roc_auc')
 r_grid.fit(X_train, y_train)
 print("Accuracy on train data: ", r_grid.score(X_train, y_train))
print("Accuracy on test data: ", r_grid.score(X_test, y_test))<train_model>
df.season.value_counts()
svm_classifier = svm.SVC(kernel="poly", C=3, degree=6)
 svm_classifier.fit(X_train, y_train)
 print("Accuracy on train data: ", svm_classifier.score(X_train, y_train))
print("Accuracy on test data: ", svm_classifier.score(X_test, y_test))<train_model>
df.holiday.value_counts()
gbm = xgb.XGBClassifier(
 learning_rate = 0.02,
use_label_encoder=False,
n_estimators= 2000,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
nthread= -1,
eval_metric = 'error',
scale_pos_weight=1 ).fit(X_train, y_train)
 print("Accuracy on train data: ", gbm.score(X_train, y_train))
print("Accuracy on test data: ", gbm.score(X_test, y_test))<prepare_x_and_y>
df.workingday.value_counts()
Y_train_full = train['Survived']
X_train_full = train.drop(columns='Survived')
 X_test_full = test.drop(columns='PassengerId')
 X_train_full.shape, X_test_full.shape, Y_train_full.shape<train_model>
df.weather.value_counts()
model = svm.SVC(kernel="poly", C=2)
 model.fit(X_train_full, Y_train_full)
 print("Accuracy on train data: ", model.score(X_train_full, Y_train_full))<predict_on_test>
season=pd.get_dummies(df['season'],prefix='season')
 df=pd.concat([df,season],axis=1)
 df.head()
season=pd.get_dummies(test_df['season'],prefix='season')
 test_df=pd.concat([test_df,season],axis=1)
 test_df.head()
y_test_predicted = model.predict(X_test_full)
 y_test_predicted<save_to_csv>
weather=pd.get_dummies(df['weather'],prefix='weather')
 df=pd.concat([df,weather],axis=1)
 df.head()
weather=pd.get_dummies(test_df['weather'],prefix='weather')
 test_df=pd.concat([test_df,weather],axis=1)
 test_df.head()
sub = pd.DataFrame({
"PassengerId": comb[1]['PassengerId'],
"Survived": y_test_predicted
})
 sub.to_csv('submission.csv', index = False )<load_from_csv>
df.drop(['season','weather'],inplace=True,axis=1)
 df.head()
test_df.drop(['season','weather'],inplace=True,axis=1)
 test_df.head()
df = pd.read_csv("/kaggle/input/titanic/train.csv")
 df<load_from_csv>
df["hour"] = [t.hour for t in pd.DatetimeIndex(df.datetime)]
df["day"] = [t.dayofweek for t in pd.DatetimeIndex(df.datetime)]
df["month"] = [t.month for t in pd.DatetimeIndex(df.datetime)]
df['year'] = [t.year for t in pd.DatetimeIndex(df.datetime)]
df['year'] = df['year'].map({2011:0, 2012:1})
 df.head()
test_df["hour"] = [t.hour for t in pd.DatetimeIndex(test_df.datetime)]
test_df["day"] = [t.dayofweek for t in pd.DatetimeIndex(test_df.datetime)]
test_df["month"] = [t.month for t in pd.DatetimeIndex(test_df.datetime)]
test_df['year'] = [t.year for t in pd.DatetimeIndex(test_df.datetime)]
test_df['year'] = test_df['year'].map({2011:0, 2012:1})
 test_df.head()
test = pd.read_csv(".. /input/titanic/test.csv")
 test<count_unique_values>
df.drop('datetime',axis=1,inplace=True)
 df.head()
for col in df.columns:
print(str(col)+ ":" + str(len(df[col].unique())) )<drop_column>
df.drop(['casual','registered'],axis=1,inplace=True )
df["Age"].dropna()<feature_engineering>
df.drop(['month'],inplace=True,axis=1)
 test_df.drop(['month'],inplace=True,axis=1)
 df['holiday'] = df['holiday']
df['workingday'] = df['workingday']
dmean = df["Age"].dropna().mean()
dmean<data_type_conversions>
df[(df.atemp-df.temp ).abs() >10]
df["Age"] = df["Age"].fillna(dmean )<feature_engineering>
line_fitter = LinearRegression()
line_fitter.fit(df['temp'].values.reshape(-1,1), df['atemp'].values.reshape(-1,1))
test["Age"] = test["Age"].fillna(dmean )<import_modules>
pred_result = line_fitter.predict(df[(df.atemp-df.temp ).abs() >10]['temp'].values.reshape(-1,1))
from sklearn.preprocessing import LabelEncoder<categorify>
df.columns.to_series().groupby(df.dtypes ).groups
x_train,x_test,y_train,y_test=train_test_split(df.drop('count',axis=1),df['count'],test_size=0.25,random_state=42)
 models=[RandomForestRegressor() ]
model_names=['RandomForestRegressor']
rmsle=[]
d={}
for model in range(len(models)) :
clf=models[model]
clf.fit(x_train,y_train)
 test_pred=clf.predict(x_test)
 rmsle.append(np.sqrt(mean_squared_log_error(test_pred,y_test)))
 d={'Modelling Algo':model_names,'RMSLE':rmsle}
d
le=LabelEncoder()
le.fit(df["Sex"])
 df["Sex"] = le.transform(df["Sex"])
 test["Sex"] = le.transform(test["Sex"] )<concatenate>
no_of_test=[500]
params_dict={'n_estimators':no_of_test,'n_jobs':[-1],'max_features':["auto",'sqrt','log2']}
clf_rf=GridSearchCV(estimator=RandomForestRegressor() ,param_grid=params_dict,scoring='neg_mean_squared_log_error')
 clf_rf.fit(x_train,y_train)
 pred=clf_rf.predict(x_test)
 print(( np.sqrt(mean_squared_log_error(pred,y_test))))
dfall = pd.concat([df,test],axis=0)
 dfall<categorify>
clf_rf.best_params_
<concatenate><EOS> 
pred=clf_rf.predict(test_df.drop('datetime',axis=1))
d={'datetime':test['datetime'],'count':pred}
ans=pd.DataFrame(d)
 ans.to_csv('answer.csv',index=False )