Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<choose_model_class>
warnings.filterwarnings("ignore")
 np.random.seed(2019)
 %matplotlib inline
print("done" )
estimators = [ 
(KNeighborsClassifier(n_neighbors=3), 'K-Nearest Neighbors', 'yellow'), 
(SVC(C=1, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear', max_iter=-1, probability=False, random_state=None, shrinking=True,tol=0.001, verbose=False), 'Support Vector Machine', 'red'), 
(LogisticRegression(tol=1e-8, penalty='l2', C=0.1), 'Logistic Regression', 'green'), 
(MultinomialNB() , 'Naive Bayes', 'magenta'), 
(RandomForestClassifier(n_estimators=10, criterion='gini'), 'Random Forest', 'gray'), 
(None, 'XGBoost', 'pink')
 ]<init_hyperparams>
def read_and_concat_dataset(training_path, test_path):
train = pd.read_csv(training_path)
 train['train'] = 1
test = pd.read_csv(test_path)
 test['train'] = 0
data = train.append(test, ignore_index=True)
 return train, test, data
train, test, data = read_and_concat_dataset('.. /input/train.csv', '.. /input/test.csv')
 data = data.set_index('Id' )
params = {}
params['objective'] = 'multi:softprob'
params['eta'] = 0.1
params['max_depth'] = 3
params['silent'] = 1
params['num_class'] = 3
params['eval_metric'] = 'mlogloss'
params['min_child_weight'] = 1
params['subsample'] = 0.8
params['colsample_bytree'] = 0.3
params['seed'] = 0<split>
data.columns[data.isnull().sum() >0]
def vectorize() :
test_size = 0.3
train_split, test_split = train_test_split(train, test_size=test_size)
 y_train_split = train_split['author'].map(author_mapping ).values
y_test_split = test_split['author'].map(author_mapping ).values
for vectorizer in vectorizers:
print(vectorizer[0] + '
')
 X = vectorizer[1].fit_transform(train.text.values)
 X_train, X_test = train_test_split(X, test_size=test_size)
 for estimator in estimators:
if estimator[1] == 'XGBoost':
xgtrain = xgb.DMatrix(X_train, y_train_split)
 xgtest = xgb.DMatrix(X_test)
 model = xgb.train(params=list(params.items()), dtrain=xgtrain, num_boost_round=40)
 predictions = model.predict(xgtest, ntree_limit=model.best_ntree_limit ).argmax(axis=1)
 else:
estimator[0].fit(X_train, y_train_split)
 predictions = estimator[0].predict(X_test)
 print(accuracy_score(predictions, y_test_split), estimator[1] )<feature_engineering>
def filling_missing_values(data,variable, new_value):
data[variable] = data[variable].fillna(new_value )
train['num_words'] = train.text.apply(lambda x: len(str(x ).split()))
test['num_words'] = test.text.apply(lambda x: len(str(x ).split()))
train['num_unique_words'] = train.text.apply(lambda x: len(set(str(x ).split())))
 test['num_unique_words'] = test.text.apply(lambda x: len(set(str(x ).split())))
 train['num_chars'] = train.text.apply(lambda x: len(str(x)))
 test['num_chars'] = test.text.apply(lambda x: len(str(x)))
 train['num_stopwords'] = train.text.apply(lambda x: len([w for w in str(x ).lower().split() if w in stop]))
test['num_stopwords'] = test.text.apply(lambda x: len([w for w in str(x ).lower().split() if w in stop]))
train['mean_word_len'] = train.apply(lambda x: np.mean([len(w)for w in str(x ).split() ]))
test['mean_word_len'] = test.apply(lambda x: np.mean([len(w)for w in str(x ).split() ]))<string_transform>
filling_missing_values(data,'GarageCond','None')
 filling_missing_values(data,'GarageQual','None')
 filling_missing_values(data,'FireplaceQu','None')
 filling_missing_values(data,'BsmtCond','None')
 filling_missing_values(data,'BsmtQual','None')
 filling_missing_values(data,'PoolQC','None')
 filling_missing_values(data,'MiscFeature','None' )
train_text = [' '.join([lemmatizer.lemmatize(word.lower())for word in alpha_tokenizer.tokenize(sent)if word.lower() not in stop])for sent in train.text.values]
test_text = [' '.join([lemmatizer.lemmatize(word.lower())for word in alpha_tokenizer.tokenize(sent)if word.lower() not in stop])for sent in test.text.values]<find_best_model_class>
data['MSSubClass'][data['MSSubClass'] == 20] = '1-STORY 1946 & NEWER ALL STYLES'
data['MSSubClass'][data['MSSubClass'] == 30] = '1-STORY 1945 & OLDER'
data['MSSubClass'][data['MSSubClass'] == 40] = '1-STORY W/FINISHED ATTIC ALL AGES'
data['MSSubClass'][data['MSSubClass'] == 45] = '1-1/2 STORY - UNFINISHED ALL AGES'
data['MSSubClass'][data['MSSubClass'] == 50] = '1-1/2 STORY FINISHED ALL AGES'
data['MSSubClass'][data['MSSubClass'] == 60] = '2-STORY 1946 & NEWER'
data['MSSubClass'][data['MSSubClass'] == 70] = '2-STORY 1945 & OLDER'
data['MSSubClass'][data['MSSubClass'] == 75] = '2-1/2 STORY ALL AGES'
data['MSSubClass'][data['MSSubClass'] == 80] = 'SPLIT OR MULTI-LEVEL'
data['MSSubClass'][data['MSSubClass'] == 85] = 'SPLIT FOYER'
data['MSSubClass'][data['MSSubClass'] == 90] = 'DUPLEX - ALL STYLES AND AGES'
data['MSSubClass'][data['MSSubClass'] == 120] = '1-STORY PUD(Planned Unit Development)- 1946 & NEWER'
data['MSSubClass'][data['MSSubClass'] == 150] = '1-1/2 STORY PUD - ALL AGES'
data['MSSubClass'][data['MSSubClass'] == 160] = '2-STORY PUD - 1946 & NEWER'
data['MSSubClass'][data['MSSubClass'] == 180] = 'PUD - MULTILEVEL - INCL SPLIT LEV/FOYER'
data['MSSubClass'][data['MSSubClass'] == 190] = '2 FAMILY CONVERSION - ALL STYLES AND AGES'
vectorizer = CountVectorizer(ngram_range=(1,7), analyzer='char')
 full = vectorizer.fit_transform(train_text + test_text)
 X_train = vectorizer.transform(train_text)
 X_test = vectorizer.transform(test_text)
 pred_full_test = 0
pred_train = np.zeros([train.shape[0], 3])
 for dev_index, val_index in KFold(n_splits=5, shuffle=True, random_state=42 ).split(train.drop(['id', 'author'], axis=1)) :
dev_X, val_X = X_train[dev_index], X_train[val_index]
dev_y, val_y = y_train[dev_index], y_train[val_index]
model = MultinomialNB()
model.fit(dev_X, dev_y)
 pred_full_test = pred_full_test + model.predict_proba(X_test)
 pred_train[val_index,:] = model.predict_proba(val_X)
 pred_full_test = pred_full_test / 5.
 train['CH_EAP'] = pred_train[:,0]
train['CH_HPL'] = pred_train[:,1]
train['CH_MWS'] = pred_train[:,2]
test['CH_EAP'] = pred_full_test[:,0]
test['CH_HPL'] = pred_full_test[:,1]
test['CH_MWS'] = pred_full_test[:,2]<find_best_model_class>
def fixing_ordinal_variables(data, variable):
data[variable][data[variable] == 'Ex'] = 5
data[variable][data[variable] == 'Gd'] = 4
data[variable][data[variable] == 'TA'] = 3
data[variable][data[variable] == 'Fa'] = 2
data[variable][data[variable] == 'Po'] = 1
data[variable][data[variable] == 'None'] = 0
vectorizer = CountVectorizer(stop_words='english', ngram_range=(1,3))
full = vectorizer.fit_transform(train_text + test_text)
 X_train = vectorizer.transform(train_text)
 X_test = vectorizer.transform(test_text)
 pred_full_test = 0
pred_train = np.zeros([train.shape[0], 3])
 for dev_index, val_index in KFold(n_splits=5, shuffle=True, random_state=42 ).split(train.drop(['id', 'author'], axis=1)) :
dev_X, val_X = X_train[dev_index], X_train[val_index]
dev_y, val_y = y_train[dev_index], y_train[val_index]
model = MultinomialNB()
model.fit(dev_X, dev_y)
 pred_full_test = pred_full_test + model.predict_proba(X_test)
 pred_train[val_index,:] = model.predict_proba(val_X)
 pred_full_test = pred_full_test / 5.
 train['C_EAP'] = pred_train[:,0]
train['C_HPL'] = pred_train[:,1]
train['C_MWS'] = pred_train[:,2]
test['C_EAP'] = pred_full_test[:,0]
test['C_HPL'] = pred_full_test[:,1]
test['C_MWS'] = pred_full_test[:,2]<find_best_model_class>
fixing_ordinal_variables(data,'ExterQual')
 fixing_ordinal_variables(data,'ExterCond')
 fixing_ordinal_variables(data,'BsmtCond')
 fixing_ordinal_variables(data,'BsmtQual')
 fixing_ordinal_variables(data,'HeatingQC')
 fixing_ordinal_variables(data,'KitchenQual')
 fixing_ordinal_variables(data,'FireplaceQu')
 fixing_ordinal_variables(data,'GarageQual')
 fixing_ordinal_variables(data,'GarageCond')
 fixing_ordinal_variables(data,'PoolQC' )
vectorizer = TfidfVectorizer(ngram_range=(1,5), analyzer='char')
 full = vectorizer.fit_transform(train_text + test_text)
 X_train = vectorizer.transform(train_text)
 X_test = vectorizer.transform(test_text)
 pred_full_test = 0
pred_train = np.zeros([train.shape[0], 3])
 for dev_index, val_index in KFold(n_splits=5, shuffle=True, random_state=42 ).split(train.drop(['id', 'author'], axis=1)) :
dev_X, val_X = X_train[dev_index], X_train[val_index]
dev_y, val_y = y_train[dev_index], y_train[val_index]
model = MultinomialNB()
model.fit(dev_X, dev_y)
 pred_full_test = pred_full_test + model.predict_proba(X_test)
 pred_train[val_index,:] = model.predict_proba(val_X)
 pred_full_test = pred_full_test / 5.
 train['T_EAP'] = pred_train[:,0]
train['T_HPL'] = pred_train[:,1]
train['T_MWS'] = pred_train[:,2]
test['T_EAP'] = pred_full_test[:,0]
test['T_HPL'] = pred_full_test[:,1]
test['T_MWS'] = pred_full_test[:,2]<concatenate>
data['PavedDrive'][data['PavedDrive'] == 'Y'] = 3
data['PavedDrive'][data['PavedDrive'] == 'P'] = 2
data['PavedDrive'][data['PavedDrive'] == 'N'] = 1
svd = TruncatedSVD(n_components=20, algorithm='arpack')
 svd.fit(full)
 train_svd = pd.DataFrame(svd.transform(X_train))
test_svd = pd.DataFrame(svd.transform(X_test))
train_svd.columns = ['SVD_' + str(i)for i in range(20)]
test_svd.columns = ['SVD_' + str(i)for i in range(20)]
train = pd.concat([train, train_svd], axis=1)
 test = pd.concat([test, test_svd], axis=1 )<drop_column>
colu = data.columns[(data.isnull().sum() <50)&(data.isnull().sum() >0)]
for i in colu:
print(data[colu].isnull().sum() )
train = train.drop(['id', 'text', 'author'], axis=1)
 test = test.drop(['id', 'text'], axis=1 )<prepare_x_and_y>
colu = data.columns[data.isnull().sum() >=50]
for i in colu:
print(data[colu].isnull().sum() )
<compute_test_metric>
filling_missing_values(data, 'GarageArea',0)
 filling_missing_values(data, 'GarageCars',0)
 data['GarageFinish'][(data.GarageFinish.isnull() ==True)&(data.GarageCond==0)] =0
data['GarageType'][(data.GarageType.isnull() ==True)&(data.GarageCond==0)] =0
data['GarageYrBlt'][(data.GarageYrBlt.isnull() ==True)&(data.GarageCond==0)] =0
NUM_FEATURES = 100
model = Word2Vec(train_text + test_text, min_count=2, size=NUM_FEATURES, window=4, sg=1, alpha=1e-4, workers=4 )<find_best_params>
print(data[['MiscFeature','MiscVal']][(data.MiscFeature=='None')&(data.MiscVal>0)])
 data.MiscVal.loc[2550] = 0
print(data[['MiscFeature','MiscVal']][(data.MiscVal==0)&(data.MiscFeature!='None')])
 c=data[['MiscFeature','MiscVal']][(data.MiscVal==0)&(data.MiscFeature!='None')].index
data.MiscFeature.loc[c] = 'None'
len(model.wv.vocab )<feature_engineering>
def inputing(variab):
y = data[variab]
data2 = data.drop([variab],axis=1)
 col = data2.columns[data2.isnull().sum() ==0]
data2 = data2[col]
data2 = pd.get_dummies(data2)
 c_train = y[y.notnull() ==True].index
y_train = y[c_train]
columny = data2.columns
X_train = data2[columny].loc[c_train]
c_test = y[y.notnull() !=True].index
y_test = y[c_test]
X_test = data2[columny].loc[c_test]
model = RandomForestClassifier()
model.fit(X_train, y_train)
 y_pred = model.predict(X_test)
 y_pred = pd.Series(y_pred, index=c_test)
 data[variab].loc[c_test] = y_pred.loc[c_test]
def inputingnum(variab):
y = data[variab]
data2 = data.drop([variab],axis=1)
 col = data2.columns[data2.isnull().sum() ==0]
data2 = data2[col]
data2 = pd.get_dummies(data2)
 c_train = y[y.notnull() ==True].index
y_train = y[c_train]
columny = data2.columns
X_train = data2[columny].loc[c_train]
c_test = y[y.notnull() !=True].index
y_test = y[c_test]
X_test = data2[columny].loc[c_test]
model = RandomForestRegressor()
model.fit(X_train, y_train)
 y_pred = model.predict(X_test)
 y_pred = pd.Series(y_pred, index=c_test)
 data[variab].loc[c_test] = y_pred.loc[c_test]
def get_feature_vec(tokens, num_features, model):
featureVec = np.zeros(shape=(1, num_features), dtype='float32')
 missed = 0
for word in tokens:
try:
featureVec = np.add(featureVec, model[word])
 except KeyError:
missed += 1
pass
if len(tokens)- missed == 0:
return np.zeros(shape=(num_features), dtype='float32')
 return np.divide(featureVec, len(tokens)- missed ).squeeze()<feature_engineering>
inputing(variab='Electrical')
 inputing(variab='Exterior2nd')
 inputing(variab='Exterior1st')
 inputing(variab='MasVnrType')
 inputing(variab='Functional')
 inputing(variab='MSZoning')
 inputing(variab='SaleType')
 inputing(variab='Alley')
 inputing(variab='BsmtExposure')
 inputing(variab='BsmtFinType1')
 inputing(variab='BsmtFinType2')
 inputing(variab='Fence')
 inputingnum(variab='KitchenQual')
 data['KitchenQual'] = data.KitchenQual.astype(int)
 inputingnum(variab='BsmtFullBath')
 data['BsmtFullBath'] = data.BsmtFullBath.astype(int)
 inputingnum(variab='BsmtHalfBath')
 data['BsmtHalfBath'] = data.BsmtHalfBath.astype(int)
 inputingnum(variab='TotalBsmtSF')
 inputingnum(variab='BsmtFinSF1')
 inputingnum(variab='BsmtFinSF2')
 inputingnum(variab='MasVnrArea')
 inputingnum(variab='BsmtUnfSF')
 inputingnum(variab='LotFrontage' )
train_vectors = []
for i in train_text:
train_vectors.append(get_feature_vec([lemmatizer.lemmatize(word.lower())for word in alpha_tokenizer.tokenize(i)if word.lower() not in stop], NUM_FEATURES, model))<feature_engineering>
print(data['Utilities'].value_counts())
 data = data.drop(['Utilities'],axis=1 )
test_vectors = []
for i in test_text:
test_vectors.append(get_feature_vec([lemmatizer.lemmatize(word.lower())for word in alpha_tokenizer.tokenize(i)if word.lower() not in stop], NUM_FEATURES, model))<feature_engineering>
data.columns[data.isnull().sum() >0]
full_vectors = []
for i in train_text + test_text:
full_vectors.append(get_feature_vec([lemmatizer.lemmatize(word.lower())for word in alpha_tokenizer.tokenize(i)if word.lower() not in stop], NUM_FEATURES, model))<concatenate>
def dropping_outliers(data, condition):
condition_to_drop = data[condition].index
data = data.drop(condition_to_drop )
svd = TruncatedSVD(n_components=30, algorithm='arpack')
 svd.fit(full_vectors)
 train_svd = pd.DataFrame(svd.transform(np.array(train_vectors)))
 test_svd = pd.DataFrame(svd.transform(np.array(test_vectors)))
 train_svd.columns = ['W2V_' + str(i)for i in range(30)]
test_svd.columns = ['W2V_' + str(i)for i in range(30)]
train = pd.concat([train, train_svd], axis=1)
 test = pd.concat([test, test_svd], axis=1 )<predict_on_test>
dropping_outliers(data,(data.SalePrice<100000)&(data.train==1)&(data.LotFrontage>150))
dropping_outliers(data,(data.LotFrontage>200)&(data.train==1))
dropping_outliers(data,(data.SalePrice>700000)&(data.train==1))
dropping_outliers(data,(data.SalePrice>700000)&(data.train==1))
dropping_outliers(data,(data.LotArea>60000)&(data.train==1))
dropping_outliers(data,(data.MasVnrArea>1450)&(data.train==1))
dropping_outliers(data,(data.BedroomAbvGr==8)&(data.train==1))
dropping_outliers(data,(data.KitchenAbvGr==3)&(data.train==1))
dropping_outliers(data,(data['3SsnPorch']>400)&(data.train==1))
dropping_outliers(data,(data.LotArea>100000)&(data.train==1))
dropping_outliers(data,(data.MasVnrArea>1300)&(data.train==1))
dropping_outliers(data,(data.BsmtFinSF1>2000)&(data.train==1)&(data.SalePrice<300000))
dropping_outliers(data,(data.BsmtFinSF2>200)&(data.SalePrice>350000)&(data.train==1))
dropping_outliers(data,(data.BedroomAbvGr==8)&(data.train==1))
dropping_outliers(data,(data.KitchenAbvGr==3)&(data.train==1))
dropping_outliers(data,(data.TotRmsAbvGrd==2)&(data.train==1))
pred_full_test = 0
pred_train = np.zeros([train.shape[0], 3])
 for dev_index, val_index in KFold(n_splits=5, shuffle=True, random_state=42 ).split(train):
dev_X, val_X = train.loc[dev_index], train.loc[val_index]
dev_y, val_y = y_train[dev_index], y_train[val_index]
xgtrain = xgb.DMatrix(dev_X, dev_y)
 xgtest = xgb.DMatrix(test)
 model = xgb.train(params=list(params.items()), dtrain=xgtrain, num_boost_round=1000)
 predictions = model.predict(xgtest, ntree_limit=model.best_ntree_limit)
 pred_full_test = pred_full_test + predictions
pred_full_test = pred_full_test / 5 .<save_to_csv>
print(data['CentralAir'].value_counts())
 data['CentralAir'] = pd.Series(np.where(data['CentralAir'].values == 'Y', 1, 0),
data.index )
author = pd.DataFrame(pred_full_test)
 final = pd.DataFrame()
final['id'] = test_id
final['EAP'] = author[0]
final['HPL'] = author[1]
final['MWS'] = author[2]
final.to_csv('submission.csv', sep=',',index=False )<set_options>
data['2ndFloor'] = pd.Series(np.where(data['2ndFlrSF'].values == 0, 0, 1),data.index)
 data['Floors'] = data['1stFlrSF'] + data['2ndFlrSF']
data = data.drop(['1stFlrSF'],axis=1)
 data = data.drop(['2ndFlrSF'],axis=1)
 data['TotBath'] = data['FullBath'] +(0.5 * data['HalfBath'])+ data['BsmtFullBath'] +(0.5 * data['BsmtHalfBath'])
 data['Porch'] = data['OpenPorchSF'] + data['3SsnPorch'] + data['EnclosedPorch'] + data['ScreenPorch']
data['TotalSF'] = data['BsmtFinSF1'] + data['BsmtFinSF2'] + data['Floors']
data['Pool'] = pd.Series(np.where(data['PoolArea'].values == 0, 0, 1),data.index)
 data['Bsmt'] = pd.Series(np.where(data['TotalBsmtSF'].values == 0, 0, 1),data.index)
 data['Garage'] = pd.Series(np.where(data['GarageArea'].values == 0, 0, 1),data.index)
 data['Fireplace'] = pd.Series(np.where(data['Fireplaces'].values == 0, 0, 1),data.index)
 data['Remod'] = pd.Series(np.where(data['YearBuilt'].values == data['YearRemodAdd'].values, 0, 1),data.index)
 data['NewHouse'] = pd.Series(np.where(data['YearBuilt'].values == data['YrSold'].values, 1, 0),data.index)
 data['Age'] = data['YrSold'] - data['YearRemodAdd']
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
<set_options>
c = data[(data['Floors']>4000)&(data.train==1)].index
data = data.drop(c)
 c = data[(data['SalePrice']>500000)&(data['TotalSF']<3500)&(data.train==1)].index
data = data.drop(c )
env = twosigmanews.make_env()
print('Done!' )<prepare_x_and_y>
data = data.drop(['PoolQC'],axis=1)
 data = data.drop(['GrLivArea'],axis=1)
 data = data.drop(['Street'],axis=1)
 data = data.drop(['GarageYrBlt'],axis=1)
 data = data.drop(['PoolArea'],axis=1)
 data = data.drop(['MiscFeature'],axis=1 )
( market_train_df, news_train_df)= env.get_training_data()<create_dataframe>
Results = pd.DataFrame({'Model': [],'RMSLE': []} )
market_train, news_train = market_train_df.copy() , news_train_df.copy()
market_train_df1, news_train_df1 = market_train_df.copy() , news_train_df.copy()<statistical_test>
data = pd.get_dummies(data )
mis_value_graph(market_train_df)
 market_train_df = mis_impute(market_train_df)
 market_train_df.isna().sum().to_frame()<create_dataframe>
trainX, testX, trainY, testY = train_test_split(data[data.SalePrice.isnull() ==False].drop('SalePrice',axis=1),data.SalePrice[data.SalePrice.isnull() ==False],test_size=0.30, random_state=2019)
 trainY = np.log(trainY )
best_asset_volume = market_train_df.groupby("assetCode")["close"].count().to_frame().sort_values(by=['close'],ascending= False)
 best_asset_volume = best_asset_volume.sort_values(by=['close'])
 largest_by_volume = list(best_asset_volume.nlargest(10, ['close'] ).index)
<feature_engineering>
model = XGBRegressor(learning_rate=0.001,n_estimators=4600,
max_depth=7, min_child_weight=0,
gamma=0, subsample=0.7,
colsample_bytree=0.7,
scale_pos_weight=1, seed=27,
reg_alpha=0.00006)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 res = pd.DataFrame({"Model":['XGBoost'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )
for i in largest_by_volume:
asset1_df['high'] = asset1_df['open']
asset1_df['low'] = asset1_df['close']
for ind, row in asset1_df.iterrows() :
if row['close'] > row['open']:
asset1_df.loc[ind, 'high'] = row['close']
asset1_df.loc[ind, 'low'] = row['open']
trace1 = go.Candlestick(
 x = asset1_df['time'].dt.strftime(date_format='%Y-%m-%d' ).values,
open = asset1_df['open'].values,
low = asset1_df['low'].values,
high = asset1_df['high'].values,
close = asset1_df['close'].values,
increasing=dict(line=dict(color= generate_color())) ,
decreasing=dict(line=dict(color= generate_color())))
 layout = dict(title = "Candlestick chart for {}".format(i),
xaxis = dict(
 title = 'Month',
rangeslider = dict(visible = False)
),
yaxis = dict(title = 'Price(USD)')
)
 data = [trace1]
py.iplot(dict(data=data, layout=layout), filename='basic-line' )<statistical_test>
model = DecisionTreeRegressor(max_depth=6)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['Decision Tree'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )
mis_value_graph(news_train_df)
 news_train_df = mis_impute(news_train_df)
 news_train_df.isna().sum().to_frame()<groupby>
model = RandomForestRegressor(n_estimators=1500,
max_depth=6)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['Random Forest'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )
news_sentiment_count = news_train_df.groupby(["urgency","assetName"])[["sentimentNegative","sentimentNeutral","sentimentPositive"]].count()
news_sentiment_count = news_sentiment_count.reset_index()<groupby>
model = Lasso(alpha=0.0005)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['LASSO'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )
news_sentiment_urgency = news_train_df.groupby(["urgency"])[["sentimentNegative","sentimentNeutral","sentimentPositive"]].count()
news_sentiment_urgency = news_sentiment_urgency.reset_index()<define_variables>
X2 = sm.add_constant(trainX)
 o=0
for i in X2.columns:
o+=1
print(o)
 model = sm.OLS(trainY, X2.astype(float))
model = model.fit()
p_values = pd.DataFrame(model.pvalues)
 p_values = p_values.sort_values(by=0, ascending=False)
 if float(p_values.loc[p_values.index[0]])>=0.05:
X2=X2.drop(p_values.index[0],axis=1)
 else:
break
kolumny = X2.columns
testX2 = sm.add_constant(testX)
 testX2 = testX2[kolumny]
y_pred = model.predict(testX2)
 y_pred = np.exp(y_pred)
 res = pd.DataFrame({"Model":['Stepwise Regression'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )
cat_cols = ['assetCode']
num_cols = ['volume', 'close', 'open', 'returnsClosePrevRaw1', 'returnsOpenPrevRaw1', 'returnsClosePrevMktres1',
'returnsOpenPrevMktres1', 'returnsClosePrevRaw10', 'returnsOpenPrevRaw10', 'returnsClosePrevMktres10',
'returnsOpenPrevMktres10']<split>
model = Ridge(alpha=0.0005)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['Ridge'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )
train_indices, val_indices = train_test_split(market_train_df1.index.values,test_size=0.25, random_state=23 )<categorify>
model = Lasso(alpha=0)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['Linear Regression'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )
<normalization><EOS> 
trainX = data[data.SalePrice.isnull() ==False].drop(['SalePrice','train'],axis=1)
 trainY = data.SalePrice[data.SalePrice.isnull() ==False]
testX = data[data.SalePrice.isnull() ==True].drop(['SalePrice','train'],axis=1)
 trainY = np.log(trainY)
 model = Lasso(alpha=0.0005)
 model.fit(trainX, trainY)
 test = data[data.train==0]
test['SalePrice'] = model.predict(testX)
 test['SalePrice'] = np.exp(test['SalePrice'])
 test = test.reset_index()
test[['Id','SalePrice']].to_csv("submissionLASSO.csv",index=False)
 print("done1" )