Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<import_modules>
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.model_selection import cross_val_score<prepare_x_and_y>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )
X = df_final.drop('Survived',axis=1)
 y = df_final['Survived']<choose_model_class>
sum(train.isnull().sum() )
lr = LogisticRegression(solver='liblinear',max_iter=200)
 knn = KNeighborsClassifier(n_neighbors=10)
 forest = RandomForestClassifier(n_estimators=200)
 svc = SVC()<compute_train_metric>
sum(test.isnull().sum() )
models = [lr, knn, forest, svc]
scores = []
for model in models:
scores.append(cross_val_score(model,X,y,cv=10))
for score in scores:
print("Accuracy: %0.2f(+/- %0.2f)" %(score.mean() , score.std() * 2))<choose_model_class>
house_data = pd.concat([train.iloc[:,:-1], test],axis=0)
 house_data = house_data.drop(columns=['Id'], axis=1)
 sep = len(train )
vote = VotingClassifier(estimators=[('knn',knn),('forest',forest),('svc',svc),('lr',lr)] )<compute_train_metric>
n_columns = [name for name in house_data.columns if house_data[name].dtype in ['int64', 'float64']]
w_columns = [name for name in house_data.columns if house_data[name].dtype == "object"]
values = {}
for a in w_columns:
values[a] = 'UNKNOWN'
for a in n_columns:
values[a] = house_data[a].median()
house_data.fillna(value=values,inplace=True)
 house_data.head()
score = cross_val_score(vote,X,y,cv=10 )<compute_test_metric>
house_data = house_data.drop(['PoolQC'], axis=1 )
print("Accuracy: %0.2f(+/- %0.2f)" %(score.mean() , score.std() * 2))<load_from_csv>
house_data = pd.get_dummies(house_data )
test_df = pd.read_csv('.. /input/titanic/test.csv' )<drop_column>
y_train = train.iloc[:,-1]
X_train = house_data.iloc[:sep, :]
X_test = house_data.iloc[sep:, :]
p_id = test_df['PassengerId']
test_df = test_df.drop('PassengerId',axis=1 )<feature_engineering>
search_rf = RandomForestRegressor(n_estimators = 100, random_state=0)
 search_rf.fit(X_train, y_train)
 y_test_rf = search_rf.predict(X_test)
 print('Random forest accuracy:', search_rf.score(X_train, y_train))
test_df['title']= test_df['Name'].apply(splitter)
 test_df['deck'] = test_df['Cabin'].apply(lambda x: str(x ).split(' ')[0] ).apply(remove_digits)
 test_df['title'] = test_df['title'].apply(lambda x: 'Other' if x not in ['Mr','Master','Miss','Mrs'] else x)
 test_df['alone'] =(test_df['SibSp'] + test_df['Parch'] ).apply(lambda x: 1 if x==0 else 0)
 test_df['age_imp'] = test_df[['Age','title']].apply(imp,axis=1)
 test_df['fare_bins'] = pd.qcut(x=test_df['Fare'],q=10,labels=[1,2,3,4,5,6,7,8,9,10],duplicates='drop')
 test_df['age_bins'] = pd.qcut(x=test_df['age_imp'],q=10,labels=[1,2,3,4,5,6,7,8,9],duplicates='drop' )<categorify>
output1 = pd.DataFrame({'Id': test.Id.values, 'SalePrice': y_test_rf})
 output1.to_csv('output1.csv', index=False )
test_df['deck'] = test_df['deck'].map({'A':'ABC','B':'ABC','C':'ABC','D':'DE','E':'DE','F':'FG','G':'FG','n':'U'} )<categorify>
search_cv = RidgeCV(alphas =(0.001, 0.01, 0.05, 0.1, 0.3, 0.5, 1, 3, 5, 10))
search_cv.fit(X_train, y_train)
 y_test_cv = search_cv.predict(X_test)
 print('RidgeCV accuracy:', search_cv.score(X_train, y_train))
test_dummies = pd.get_dummies(test_df[['Embarked','Sex','title','deck']],drop_first=True)
 test_df_final = pd.concat([test_df,test_dummies],axis=1 ).drop(
 ['Age','SibSp','Parch','Fare','Name','Sex','Ticket','Cabin','Embarked','title','age_imp','deck'],axis=1 )<filter>
output2 = pd.DataFrame({'Id': test.Id.values, 'SalePrice': y_test_cv})
 output2.to_csv('output2.csv', index=False )
<feature_engineering><EOS> 
prediction =(y_test_rf + y_test_cv)/2
output = pd.DataFrame({'Id': test.Id.values, 'SalePrice': prediction})
 output.to_csv('submission.csv', index=False )