<jupyter_start><jupyter_text>Multidimensional regression<jupyter_code># try to predict a single output (the average house price) based on multiple inputs.
x = boston.data
y = boston.target
lr.fit(x, y)
p = lr.predict(x)
plt.scatter(p, y)
plt.xlabel('Predicted price')
plt.ylabel('Actual price')
plt.plot([y.min(), y.max()], [[y.min()], [y.max()]])
plt.show()<jupyter_output><empty_output><jupyter_text>Cross-validation for regression<jupyter_code>from sklearn.model_selection import KFold
kf = KFold(n_splits=5)
p = np.zeros_like(y)
for train_index, test_index in kf.split(x):
    #print(train_index, test_index)
    X_train, X_test = x[train_index], x[test_index]
    y_train, y_test = y[train_index], y[test_index]
    lr.fit(X_train, y_train)
    p[test_index] = lr.predict(X_test)
rmse_cv = np.sqrt(mean_squared_error(p, y))
print('RMSE on 5-fold CV: {:.2}'.format(rmse_cv))<jupyter_output>RMSE on 5-fold CV: 6.1<jupyter_text>Penalized or regularized regression<jupyter_code># In ordinary regression, the returned fit is the best fit on the training data. This can lead to over-fitting.
# Penalizing means that we add a penalty for over-confidence in the parameter values.
# Thus, we accept a slightly worse fit in order to have a simpler model.
# The L1 penalized model is often called the Lasso, while an L2 penalized
# one is known as Ridge Regression. When using both, we call this an ElasticNet model.
# Using Lasso or ElasticNet in scikit-learn
from sklearn.linear_model import ElasticNet, Lasso
en = ElasticNet(alpha=0.5)

for train_index, test_index in kf.split(x):
    #print(train_index, test_index)
    X_train, X_test = x[train_index], x[test_index]
    y_train, y_test = y[train_index], y[test_index]
    en.fit(X_train, y_train)
    p[test_index] = en.predict(X_test)
rmse_cv = np.sqrt(mean_squared_error(p, y))
print('RMSE on 5-fold CV: {:.2}'.format(rmse_cv))

x = boston.data[:,5]
y = boston.target
x = np.transpose(np.atleast_2d(x))
#print(x, y)
en.fit(x, y)
y_predicted = en.predict(x)

mse = mean_squared_error(y, y_predicted)
print("Mean squared error (of training data): {:.3}".format(mse))

rmse = np.sqrt(mse)
print("RMSE (of training data): {:.3}".format(rmse))
# Visualizing the Lasso path
las = Lasso(normalize=1)
alphas = np.logspace(-5, 2, 1000)
alphas, coefs, _= las.path(x, y, alphas=alphas)
fig,ax = plt.subplots()
ax.plot(alphas, coefs.T)
# Set log scale
ax.set_xscale('log')
# Make alpha decrease from left to right
ax.set_xlim(alphas.max(), alphas.min())
#plt.show()<jupyter_output><empty_output><jupyter_text>P-greater-than-N scenarios<jupyter_code># the number of features, P, was greater than the number of examples, N (these letters were the
# conventional statistical shorthand for these concepts). These became known as P greater than N problems.<jupyter_output><empty_output><jupyter_text>An example based on text documents<jupyter_code>from sklearn.datasets import load_svmlight_file

data,target = load_svmlight_file('E2006.train')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12595546693253573, 'fraction_numerical': 0.00897308075772682, 'mean_word_length': 5.703786191536748, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '324405', 'license': 'MIT', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1121, 'n_words': 297, 'path': 'Regression.ipynb', 'repo_name': 'arunbutte/Getting-Started-with-Python-Machine-Learning', 'size': '3009'}