Kaggle competition: VSU ML 1 Regression
TickCountDict = {}
TickCountDict = full_data['Ticket'].value_counts()
TickCountDict.head()<categorify>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 sample = pd.read_csv('.. /input/test.csv' )
full_data['TickGroup'] = full_data['Ticket'].map(TickCountDict)
 full_data['TickGroup'].head()<categorify>
x_train = train[['LotArea','LotFrontage']].copy()
y_train = train['SalePrice'].copy()
AgePre = full_data[['Age','Parch','Pclass','SibSp','Title','familySize','TickGroup']]
AgePre = pd.get_dummies(AgePre)
 ParAge = pd.get_dummies(AgePre['Parch'],prefix='Parch')
 SibAge = pd.get_dummies(AgePre['SibSp'],prefix='SibSp')
 PclAge = pd.get_dummies(AgePre['Pclass'],prefix='Pclass')
 AgeCorrDf = pd.DataFrame()
AgeCorrDf = AgePre.corr()
AgeCorrDf['Age'].sort_values()<concatenate>
x_test = test[['LotArea','LotFrontage']].copy()
AgePre = pd.concat([AgePre,ParAge,SibAge,PclAge],axis=1)
 AgePre.head()<prepare_x_and_y>
x_train.isnull().sum()
Age_train = AgePre[AgePre['Age'].notnull() ]
Age_test = AgePre[AgePre['Age'].isnull() ]
AgeKnown_X = Age_train.drop(['Age'],axis=1)
 AgeKnown_Y = Age_train['Age']<drop_column>
x_train.fillna(0,inplace=True)
 x_test.fillna(0,inplace=True )
Age_X_test = Age_test.drop(['Age'],axis=1 )<train_model>
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
rfr=RandomForestRegressor(random_state=None,n_estimators=500,n_jobs=-1)
 rfr.fit(AgeKnown_X,AgeKnown_Y)
 print(rfr.score(AgeKnown_X,AgeKnown_Y))<predict_on_test>
x_scaler = StandardScaler()
y_scaler = StandardScaler()
AgeUnKnown_Y = rfr.predict(Age_X_test)
 full_data.loc[full_data['Age'].isnull() ,['Age']] = AgeUnKnown_Y
full_data.info()<categorify>
x_train_scaler = x_scaler.fit_transform(x_train)
 x_test_scaler = x_scaler.transform(x_test )
fullSel = fullSel.drop(['Family','SibSp','TickGroup','Parch'],axis=1)
 fullSel = pd.get_dummies(fullSel)
 PclassDf = pd.get_dummies(full_data['Pclass'],prefix='Pclass')
 TickGroupDf = pd.get_dummies(full_data['TickGroup'],prefix='TickGroup')
 familySizeDf = pd.get_dummies(full_data['familySize'],prefix='familySize')
 fullSel=pd.concat([fullSel,PclassDf,TickGroupDf,familySizeDf],axis=1 )<prepare_x_and_y>
lr = LinearRegression()
kfold = StratifiedKFold(n_splits=10)
 experData = fullSel[fullSel['Survived'].notnull() ]
preData = fullSel[fullSel['Survived'].isnull() ]
experData_X = experData.drop('Survived',axis=1)
 experData_y = experData['Survived']
preData_X = preData.drop('Survived',axis=1)
 modelLR = LogisticRegression()
LR_param_grid = {'C' : [1,2,3],
'penalty':['l1','l2']}
modelgsLR = GridSearchCV(modelLR,param_grid = LR_param_grid, cv=kfold,
scoring="accuracy", n_jobs= -1, verbose = 1)
 modelgsLR.fit(experData_X,experData_y )<compute_test_metric>
lr.fit(x_train_scaler,y_train )
print("Accuracy: ", modelgsLR.best_score_ )<choose_model_class>
pred = lr.predict(x_test_scaler )
GBC = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [100,200,300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
modelgsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, cv=kfold,
scoring="accuracy", n_jobs= -1, verbose = 1)
 modelgsGBC.fit(experData_X,experData_y )<save_to_csv>
sub = pd.DataFrame(data = {'Id' : test.Id, 'SalePrice' :pred} )
GBCpreData_y=modelgsGBC.predict(preData_X)
 GBCpreData_y=GBCpreData_y.astype(int)
 GBCpreResultDf=pd.DataFrame()
GBCpreResultDf['PassengerId']=full_data['PassengerId'][full_data['Survived'].isnull() ]
GBCpreResultDf['Survived']=GBCpreData_y
GBCpreResultDf
GBCpreResultDf.to_csv('submission.csv',index=False )<load_from_csv>
sub.to_csv('submission.csv',index = False )