<jupyter_start><jupyter_text>simple greedy solution:<jupyter_code>##forward selection by definition from the book
greedy_selected_columns_index=list()

selected_column_indice=df_features_forward_greedy['feature_idx'].as_matrix()[11]
greedy_selected_columns_index=list(selected_column_indice)
greedy_selected_colname_list = df_before_match_out_training.columns[greedy_selected_columns_index].tolist()
print "Selected columns by simple greedy sequential forward selection are: \n",greedy_selected_colname_list<jupyter_output>Selected columns by simple greedy sequential forward selection are: 
['buildUpPlayPassing_home', 'defenceTeamWidth_away', 'B365H', 'buildUpPlayDribbling_home', 'buildUpPlaySpeed_home', 'chanceCreationShooting_away', 'IWA', 'chanceCreationCrossing_home', 'buildUpPlaySpeed_away', 'buildUpPlayPassing_away', 'defenceAggression_away', 'defencePressure_home']<jupyter_text>Apply selected features in the linear regression model<jupyter_code>scaler = MinMaxScaler()

df_before_match_selected_training=df_before_match_out_training[greedy_selected_colname_list]
df_before_match_selected_test=df_before_match_out_test[greedy_selected_colname_list]

##Normalize the data
df_before_match_selected_training[greedy_selected_colname_list] = scaler.fit_transform(df_before_match_selected_training[greedy_selected_colname_list])
df_before_match_selected_test[greedy_selected_colname_list] = scaler.fit_transform(df_before_match_selected_test[greedy_selected_colname_list])

lr_greedy = linear_model.LinearRegression()
lr_greedy.fit(df_before_match_selected_training.as_matrix(), df_before_match_out_trainingtarget.as_matrix())
before_match_greedy_pred = lr_greedy.predict(df_before_match_selected_test.as_matrix())

print "Mean squared error is ", mean_squared_error(df_before_match_out_testtarget.tolist(), before_match_greedy_pred.tolist())<jupyter_output>C:\WorkingGround\Anaconda\lib\site-packages\ipykernel\__main__.py:7: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
C:\WorkingGround\Anaconda\lib\site-packages\ipykernel\__main__.py:8: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy<jupyter_text>Feature importance evaluation in the linear regression estimator<jupyter_code>labels = [ l[:15] + "..."+l[-4:] if len(l) > 20 else l for l in greedy_selected_colname_list  ]
x = np.arange(len(lr_greedy.coef_))
plt.barh(x, lr_greedy.coef_, tick_label=labels)
plt.xlabel('Value of coefficient')
plt.savefig('lr_coef.png',bbox_inches='tight')
fig_two, axe = plt.subplots(1)
fig_two.set_size_inches(18.5, 10.5, forward=True)
width = 0.35  
length=len(df_before_match_out_testtarget.tolist())
x = np.arange(0, length, 1) #number of matches. 
#re_test_list=np.subtract(np.array(df_before_match_out_testtarget.tolist()),np.array(before_match_greedy_pred.tolist()))
axe.scatter(x,df_before_match_out_testtarget.tolist(),alpha=0.5,label='Test')
axe.scatter(x,before_match_greedy_pred.tolist(),alpha=0.5,label='Predict')
axe.set_title("Linear Regression of home team possession")
legend = axe.legend(loc='upper right', shadow=True)
plt.ylabel('Possesion')
plt.xlabel('Match No')

plt.savefig('lr_residual_model.png', bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>Residual Plot<jupyter_code>fig = plt.figure() 
fig.set_size_inches(18.5, 10.5, forward=True)
fig.subplots_adjust(left=0, right=1, bottom=0, top=3, hspace=0.9, wspace=0.15) 
lr = linear_model.LinearRegression()
i=0
re_erro_dict=dict()
for column in df_before_match_out_training.columns.tolist():
    lr.fit(df_before_match_out_training[column].as_matrix().reshape(df_before_match_out_training.shape[0],1), df_before_match_out_trainingtarget.as_matrix().reshape(df_before_match_out_trainingtarget.shape[0],1))
    y_pred = lr.predict(df_before_match_out_test[column].as_matrix().reshape(df_before_match_out_test.shape[0],1)).reshape(1,df_before_match_out_test.shape[0])
    re_erro = np.subtract(df_before_match_out_testtarget.as_matrix(),y_pred[0])
    re_erro_dict[column]=re_erro
    ax = fig.add_subplot(9, 5, i+1)
    ax.scatter(df_before_match_out_test[column].as_matrix(), re_erro, s=1)
    ax.grid(True)
    ax.set_title(column)
    i=i+1

plt.savefig('lr_residual_all.png',bbox_inches='tight')
plt.show() 

########this section is only for report!!!!!!!!!!!!!!!!!!!!!!!
f, (ax1,ax2) = plt.subplots(1, 2, sharey=True)
f.suptitle('Typcial features for Residual Plot', fontsize=16)
f.set_size_inches(18.5, 8.5, forward=True)
ax1.scatter(df_before_match_out_test['chanceCreationPassing_home'].as_matrix(), re_erro_dict['chanceCreationPassing_home'], s=1)
ax1.grid(True)
ax1.set_title("chanceCreationPassing_home")
ax2.scatter(df_before_match_out_test['B365A'].as_matrix(), re_erro_dict['B365A'], s=1)
ax2.grid(True)
ax2.set_title("B365A")
plt.ylabel('Residual Error');
plt.savefig('lr_residual_typical.png',bbox_inches='tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1224952741020794, 'fraction_numerical': 0.01625708884688091, 'mean_word_length': 9.79795918367347, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '526971', 'license': 'Apache-2.0', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1934, 'n_words': 266, 'path': 'Project2/Project 2 Regression - Residual.ipynb', 'repo_name': 'ShawnHouCHN/Introduction-to-Machine-Learning-and-Data-Mining-E17', 'size': '5290'}