Kaggle competition: Bike Sharing Demand
train_data = train_data.drop(['Cabin'], axis=1)
 test_data = test_data.drop(['Cabin'], axis=1 )<feature_engineering>
%matplotlib inline
train_data['Embarked'] = train_data['Embarked'].replace(np.nan, 'Q')
 test_data['Fare'] = test_data['Fare'].replace(np.nan, 36.0 )<count_missing_values>
train_df = pd.read_csv('.. /input/bike-sharing-demand/train.csv')
 test_df = pd.read_csv('.. /input/bike-sharing-demand/test.csv')
train_data.isna().sum()<count_missing_values>
print(train_df.isnull().values.any() ,'
',test_df.isnull().values.any() )
test_data.isna().sum()<count_values>
test = pd.DataFrame(test_df)
 test
train_data['Title'] = train_data.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 train_data.Title.value_counts()<count_values>
train = pd.DataFrame(train_df)
 train
test_data['Title'] = test_data.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 test_data.Title.value_counts()<define_variables>
train['datetime'] = pd.to_datetime(train['datetime'], format = '%Y-%m-%dT%H:%M:%S')
 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
normalized_titles = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royal",
"Don": "Royal",
"Sir" : "Royal",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royal",
"Dona": "Royal",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royal"
}
train_data.Title = train_data.Title.map(normalized_titles)
 test_data.Title = test_data.Title.map(normalized_titles )<count_values>
train = train_df.drop('datetime' , axis=1)
 train
test_data.Title.value_counts()<count_values>
train[["season", "count"]].groupby(['season'], as_index=False ).sum().sort_values(by='count', ascending=False )
train_data.Title.value_counts()<categorify>
train[["holiday", "count"]].groupby(['holiday'], as_index=False ).sum().sort_values(by='count', ascending=False )
le = LabelEncoder()
train_data["Sex"] = le.fit_transform(train_data["Sex"].values)
 test_data["Sex"] = le.fit_transform(test_data["Sex"].values )<drop_column>
train[["workingday", "count"]].groupby(['workingday'], as_index=False ).sum().sort_values(by='count', ascending=False )
train_data = train_data.drop(['Ticket'], axis=1)
 test_data = test_data.drop(['Ticket'], axis=1 )<count_values>
test['datetime'] = pd.to_datetime(test['datetime'], format = '%Y-%m-%dT%H:%M:%S')
 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
train_data["Embarked"].value_counts()<categorify>
train = pd.DataFrame(train)
 train.drop(['casual' , 'registered' ],axis=1,inplace=True)
train_data["Embarked"] = le.fit_transform(train_data["Embarked"].values)
 test_data["Embarked"] = le.fit_transform(test_data["Embarked"].values )<categorify>
c=[]
for i in test['hour']:
if i>= 6 or i<= 1 :
c.append("Day")
 else:
c.append("Night")
 test['DayorNight']=c
test['DayorNight']=pd.factorize(test['DayorNight'])[0].reshape(-1, 1)
 c=[]
test = test.drop('datetime' , axis =1)
 test
train_data["Name"] = le.fit_transform(train_data["Name"].values)
 test_data["Name"] = le.fit_transform(test_data["Name"].values )<import_modules>
c=[]
for i in train['hour']:
if i>= 6 or i<= 1 :
c.append("Day")
 else:
c.append("Night")
 train['DayorNight']=c
train['DayorNight']=pd.factorize(train['DayorNight'])[0].reshape(-1, 1)
 c=[]
train.head(2 )
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from xgboost import XGBClassifier
import lightgbm as gbm
from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedKFold, cross_val_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score
from sklearn.metrics import r2_score<split>
x = train
x =x.drop('count',axis=1)
 x
features = ["Pclass","Sex","Age","SibSp","Fare"]
X = train_data[features]
y = train_data['Survived']
X_train,X_test, y_train,y_test = train_test_split(X,y,test_size=0.15)
 sc_X = MinMaxScaler()
X_train = sc_X.fit_transform(X_train)
 X_test = sc_X.transform(X_test )<train_on_grid>
x_train, x_val, y_train, y_val = train_test_split(x , y, train_size=0.8, test_size=0.2, random_state = 0 )
pipelineone = Pipeline([('RandomForest', RandomForestClassifier())])
 param_grid = {'RandomForest__max_features': ['auto'],
'RandomForest__min_samples_leaf': [1, 2, 4],
'RandomForest__min_samples_split': [2,3,4],
'RandomForest__n_estimators': [100,200,300]}
model = GridSearchCV(pipelineone, param_grid, cv =None)
 model.fit(X_train, y_train)
 y_pred_sub = model.predict(test_data[features])
 y_pred_random = model.predict(X_test)
 forest = accuracy_score(y_test, y_pred_random)
 print(forest )<train_on_grid>
import xgboost as xgb
from sklearn.model_selection import cross_val_score, KFold
from sklearn.metrics import accuracy_score
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_log_error
pipelinetwo = Pipeline([('logisticregression', LogisticRegression(max_iter=100)) ])
 param_grid = {'logisticregression__penalty' : ['l2'],
'logisticregression__C' : [0.001, 0.01, 0.1, 1, 10, 100, 1000],
'logisticregression__solver' : ['liblinear']}
model = GridSearchCV(pipelinetwo, param_grid, cv =None)
 model.fit(X_train, y_train)
 model = LogisticRegression()
model.fit(X_train, y_train)
 y_pred = model.predict(X_test)
 logistic = accuracy_score(y_test, y_pred)
 print(logistic )<compute_train_metric>
xg = XGBClassifier()
xg.fit(X_train, y_train)
 y_pred = xg.predict(X_test)
 XGB = accuracy_score(y_test, y_pred)
 print(XGB )<compute_train_metric>
xgbr = xgb.XGBRegressor(verbosity=0)
 xgbr.fit(x_train, y_train)
 score_xgbr = xgbr.score(x_train, y_train)
 print("Training score: ", score_xgbr)
 scores_xgbr = cross_val_score(xgbr, x_train, y_train,cv=10)
 print("Mean cross-validation score: %.2f" % scores_xgbr.mean())
 y_pred_xgbr = xgbr.predict(x_val)
 print('Validation score' , xgbr.score(x_val , y_val))
rmsle_xgbr = mean_squared_log_error(y_pred_xgbr ,y_val)
 print("RMSLE: %.2f" % rmsle_xgbr)
KN = KNeighborsClassifier(n_neighbors=3)
 KN.fit(X_train, y_train)
 y_pred = KN.predict(X_test)
 knn = accuracy_score(y_test, y_pred)
 print(knn )<compute_train_metric>
<compute_train_metric><EOS> 
pred =np.round(np.expm1(xgbr.predict(test)) ).astype(int)
 pred = pd.DataFrame({"datetime": test_df["datetime"],"count": np.fix(pred)})
 pred.shape