Skip to content

Commit 6f9a79b

Browse files
committed
Pushing the docs for revision for branch: master, commit 25c931a50542cf6ce0d9a31372c91d0e43807773
1 parent 417fd57 commit 6f9a79b

File tree

973 files changed

+4497
-3360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

973 files changed

+4497
-3360
lines changed

dev/_downloads/plot_pca_vs_fa_model_selection.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
by Thomas P. Minka is also compared.
2424
2525
"""
26-
print(__doc__)
2726

2827
# Authors: Alexandre Gramfort
2928
# Denis A. Engemann
@@ -38,6 +37,8 @@
3837
from sklearn.model_selection import cross_val_score
3938
from sklearn.model_selection import GridSearchCV
4039

40+
print(__doc__)
41+
4142
###############################################################################
4243
# Create the data
4344

@@ -61,7 +62,7 @@
6162

6263

6364
def compute_scores(X):
64-
pca = PCA()
65+
pca = PCA(svd_solver='full')
6566
fa = FactorAnalysis()
6667

6768
pca_scores, fa_scores = [], []
@@ -90,7 +91,7 @@ def lw_score(X):
9091
n_components_pca = n_components[np.argmax(pca_scores)]
9192
n_components_fa = n_components[np.argmax(fa_scores)]
9293

93-
pca = PCA(n_components='mle')
94+
pca = PCA(svd_solver='full', n_components='mle')
9495
pca.fit(X)
9596
n_components_pca_mle = pca.n_components_
9697

@@ -105,7 +106,8 @@ def lw_score(X):
105106
plt.axvline(n_components_pca, color='b',
106107
label='PCA CV: %d' % n_components_pca, linestyle='--')
107108
plt.axvline(n_components_fa, color='r',
108-
label='FactorAnalysis CV: %d' % n_components_fa, linestyle='--')
109+
label='FactorAnalysis CV: %d' % n_components_fa,
110+
linestyle='--')
109111
plt.axvline(n_components_pca_mle, color='k',
110112
label='PCA MLE: %d' % n_components_pca_mle, linestyle='--')
111113

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
============================================================
3+
Comparing random forests and the multi-output meta estimator
4+
============================================================
5+
6+
An example to compare multi-output regression with random forest and
7+
the :ref:`multioutput.MultiOutputRegressor <_multiclass>` meta-estimator.
8+
9+
This example illustrates the use of the
10+
:ref:`multioutput.MultiOutputRegressor <_multiclass>` meta-estimator
11+
to perform multi-output regression. A random forest regressor is used,
12+
which supports multi-output regression natively, so the results can be
13+
compared.
14+
15+
The random forest regressor will only ever predict values within the
16+
range of observations or closer to zero for each of the targets. As a
17+
result the predictions are biased towards the centre of the circle.
18+
19+
Using a single underlying feature the model learns both the
20+
x and y coordinate as output.
21+
22+
"""
23+
print(__doc__)
24+
25+
# Author: Tim Head <[email protected]>
26+
#
27+
# License: BSD 3 clause
28+
29+
import numpy as np
30+
import matplotlib.pyplot as plt
31+
from sklearn.ensemble import RandomForestRegressor
32+
from sklearn.model_selection import train_test_split
33+
from sklearn.multioutput import MultiOutputRegressor
34+
35+
36+
# Create a random dataset
37+
rng = np.random.RandomState(1)
38+
X = np.sort(200 * rng.rand(600, 1) - 100, axis=0)
39+
y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T
40+
y += (0.5 - rng.rand(*y.shape))
41+
42+
X_train, X_test, y_train, y_test = train_test_split(X, y,
43+
train_size=400,
44+
random_state=4)
45+
46+
max_depth = 30
47+
regr_multirf = MultiOutputRegressor(RandomForestRegressor(max_depth=max_depth,
48+
random_state=0))
49+
regr_multirf.fit(X_train, y_train)
50+
51+
regr_rf = RandomForestRegressor(max_depth=max_depth, random_state=2)
52+
regr_rf.fit(X_train, y_train)
53+
54+
# Predict on new data
55+
y_multirf = regr_multirf.predict(X_test)
56+
y_rf = regr_rf.predict(X_test)
57+
58+
# Plot the results
59+
plt.figure()
60+
s = 50
61+
a = 0.4
62+
plt.scatter(y_test[:, 0], y_test[:, 1],
63+
c="navy", s=s, marker="s", alpha=a, label="Data")
64+
plt.scatter(y_multirf[:, 0], y_multirf[:, 1],
65+
c="cornflowerblue", s=s, alpha=a,
66+
label="Multi RF score=%.2f" % regr_multirf.score(X_test, y_test))
67+
plt.scatter(y_rf[:, 0], y_rf[:, 1],
68+
c="c", s=s, marker="^", alpha=a,
69+
label="RF score=%.2f" % regr_rf.score(X_test, y_test))
70+
plt.xlim([-6, 6])
71+
plt.ylim([-6, 6])
72+
plt.xlabel("target 1")
73+
plt.ylabel("target 2")
74+
plt.title("Comparing random forests and the multi-output meta estimator")
75+
plt.legend()
76+
plt.show()
47 Bytes
47 Bytes
177 Bytes
177 Bytes
398 Bytes
398 Bytes
86 Bytes
86 Bytes

0 commit comments

Comments
 (0)