Browse files

Changed dataset from france's mileage to knuth's USA mileage dataset

  • Loading branch information...
1 parent 81b75a8 commit 62a348406c7b1c3eff441240462b12a5e272e1b9 @NelleV NelleV committed with GaelVaroquaux Apr 26, 2012
View
35 examples/manifold/plot_mds.py
@@ -1,11 +1,8 @@
"""
===============================================================================
-Multi-dimensional scaling - Reconstructing the map of France
+Multi-dimensional scaling - Reconstructing the map of the USA
===============================================================================
-The dataset consists of kilometers one has to travel to go from one city in
-france to another. The goal is to reconstruct the map of France using these
-distances.
"""
# Author: Nelle Varoquaux <nelle.varoquaux@gmail.com>
@@ -19,20 +16,35 @@
from sklearn import manifold
from sklearn.datasets import load_cities
+from sklearn.metrics import euclidean_distances
cities_dataset = load_cities()
-similarities = cities_dataset.data
+similarities = cities_dataset.data[:10, :10] * 1e-5
+
+old_stress, best_pos = None, None
+for n in range(10):
+ mds = manifold.MDS(out_dim=2, n_init=6, max_iter=3000, n_jobs=2,
+ verbose=1, eps=1e-9)
+ pos = mds.fit(similarities).positions_
+ mds = manifold.MDS(out_dim=2, metric=False, n_init=1, max_iter=3000, n_jobs=2,
+ verbose=1, eps=1e-19)
+ pos = mds.fit(similarities, init=pos).positions_
+ stress = mds.stress_
+
+ if old_stress is None or old_stress > stress:
+ old_stress = stress
+ best_pos = pos.copy()
+
-mds = manifold.MDS(out_dim=2, max_iter=3000, eps=1e-9)
-pos = mds.fit(similarities).positions_
fig = plt.figure(1)
+distances = euclidean_distances(pos)
+plt.scatter(distances, similarities, cmap=plt.cm.hot_r)
+
+fig = plt.figure(2)
ax = plt.axes([0., 0., 1., 1.])
plt.scatter(pos[:, 0], pos[:, 1])
-similarities = 10 * similarities.max() / similarities
-similarities[np.isinf(similarities)] = 0
-
# Plot the edges
start_idx, end_idx = np.where(pos)
#a sequence of (*line0*, *line1*, *line2*), where::
@@ -48,7 +60,6 @@
ax.add_collection(lc)
for index, (label, (x, y)) in enumerate(zip(cities_dataset.header, pos)):
-
dx = x - pos[:, 0]
dx[index] = 1
dy = y - pos[:, 1]
@@ -73,5 +84,5 @@
bbox=dict(facecolor='w',
alpha=.6))
-plt.title("Map of France inferred from travel distances")
+plt.title("Map of the US inferred from travel distances")
plt.show()
View
28 sklearn/datasets/base.py
@@ -335,31 +335,37 @@ def load_diabetes():
def load_cities():
- """Load and return the travelling distances between major
- cities in france.
+ """
+ Mileage data for US cities from the Stanford Graph Base
- There are 17 french cities. The dataset is composed of a symetric matrice,
- containing the distances to travel the fastest from one city to another.
- The distances and the routes where evaluated through Google Maps.
+ Revised mileage data for highways in the United States and Canada, 1949
============== ==================
- Samples total 17 cities
+ Samples total 128 cities
Dimensionality
- Features real, 0 < x < 1300
- Targets 3D positions
+ Features
+ Targets Coordinates
==============
Returns
-------
data: Bunch
Dictionary-like object, the interesting attributes are: 'data', the
- distance matrix to learn and 'header', labels for the 14 cities.
+ distance matrix to learn and 'header', labels for the 128 cities.
+
+ Notes
+ ------
+
+ [1] Donald E. Knuth,
+ "The Stanford GraphBase: A Platform for Combinatorial Computing",
+ ACM Press, New York, 1993.
+ [2] http://www-cs-faculty.stanford.edu/~knuth/sgb.html
"""
base_dir = join(dirname(__file__), 'data/')
# Read data
- data = np.loadtxt(base_dir + 'france_distances.csv',
+ data = np.loadtxt(base_dir + 'knuth_miles.csv',
skiprows=1)
- with open(base_dir + 'france_distances.csv') as f:
+ with open(base_dir + 'knuth_miles.csv') as f:
header = f.readline().split(',')
return Bunch(data=data, header=header)
View
19 sklearn/datasets/data/france_distances.csv
@@ -1,19 +0,0 @@
-Poitiers,Tours,Lille,Dijon,Le Mans,Toulouse,Bordeaux,Paris,Clermont Ferrand,Limoges,Rennes,Nantes,Brest,Le Havre,Caen,Lyon,Marseille,Grenoble
-0.000000000000000000e+00 1.050000000000000000e+02 5.550000000000000000e+02 5.210000000000000000e+02 2.020000000000000000e+02 4.130000000000000000e+02 2.270000000000000000e+02 3.400000000000000000e+02 3.250000000000000000e+02 1.300000000000000000e+02 2.850000000000000000e+02 2.170000000000000000e+02 4.750000000000000000e+02 4.400000000000000000e+02 3.620000000000000000e+02 5.080000000000000000e+02 7.760000000000000000e+02 5.970000000000000000e+02
-1.050000000000000000e+02 0.000000000000000000e+00 4.560000000000000000e+02 4.210000000000000000e+02 1.020000000000000000e+02 5.160000000000000000e+02 3.490000000000000000e+02 2.400000000000000000e+02 3.330000000000000000e+02 2.290000000000000000e+02 2.530000000000000000e+02 2.170000000000000000e+02 5.020000000000000000e+02 3.410000000000000000e+02 2.630000000000000000e+02 5.220000000000000000e+02 7.810000000000000000e+02 6.120000000000000000e+02
-5.550000000000000000e+02 4.560000000000000000e+02 0.000000000000000000e+00 5.020000000000000000e+02 4.250000000000000000e+02 8.960000000000000000e+02 8.000000000000000000e+02 2.210000000000000000e+02 6.400000000000000000e+02 6.100000000000000000e+02 5.690000000000000000e+02 6.020000000000000000e+02 7.560000000000000000e+02 3.170000000000000000e+02 3.880000000000000000e+02 6.820000000000000000e+02 9.910000000000000000e+02 7.920000000000000000e+02
-5.210000000000000000e+02 4.210000000000000000e+02 5.020000000000000000e+02 0.000000000000000000e+00 7.800000000000000000e+02 6.720000000000000000e+02 6.660000000000000000e+02 3.150000000000000000e+02 3.220000000000000000e+02 4.340000000000000000e+02 6.180000000000000000e+02 6.390000000000000000e+02 8.610000000000000000e+02 5.080000000000000000e+02 5.450000000000000000e+02 1.950000000000000000e+02 5.070000000000000000e+02 3.050000000000000000e+02
-2.020000000000000000e+02 1.020000000000000000e+02 4.250000000000000000e+02 7.800000000000000000e+02 0.000000000000000000e+00 6.150000000000000000e+02 4.460000000000000000e+02 2.100000000000000000e+02 4.320000000000000000e+02 3.050000000000000000e+02 1.550000000000000000e+02 1.890000000000000000e+02 3.970000000000000000e+02 2.430000000000000000e+02 1.640000000000000000e+02 6.280000000000000000e+02 8.900000000000000000e+02 7.380000000000000000e+02
-4.130000000000000000e+02 5.160000000000000000e+02 8.960000000000000000e+02 6.720000000000000000e+02 6.150000000000000000e+02 0.000000000000000000e+00 2.460000000000000000e+02 6.790000000000000000e+02 3.750000000000000000e+02 2.910000000000000000e+02 7.000000000000000000e+02 5.860000000000000000e+02 8.820000000000000000e+02 8.480000000000000000e+02 7.720000000000000000e+02 5.390000000000000000e+02 4.050000000000000000e+02 5.310000000000000000e+02
-2.270000000000000000e+02 3.490000000000000000e+02 8.000000000000000000e+02 6.660000000000000000e+02 4.460000000000000000e+02 2.460000000000000000e+02 0.000000000000000000e+00 5.860000000000000000e+02 3.690000000000000000e+02 2.200000000000000000e+02 4.610000000000000000e+02 3.460000000000000000e+02 6.430000000000000000e+02 6.860000000000000000e+02 6.030000000000000000e+02 5.880000000000000000e+02 6.450000000000000000e+02 6.780000000000000000e+02
-3.400000000000000000e+02 2.400000000000000000e+02 2.210000000000000000e+02 3.150000000000000000e+02 2.100000000000000000e+02 6.790000000000000000e+02 5.860000000000000000e+02 0.000000000000000000e+00 4.240000000000000000e+02 3.940000000000000000e+02 3.500000000000000000e+02 3.860000000000000000e+02 5.930000000000000000e+02 2.040000000000000000e+02 2.410000000000000000e+02 4.650000000000000000e+02 7.770000000000000000e+02 5.750000000000000000e+02
-3.250000000000000000e+02 3.330000000000000000e+02 6.400000000000000000e+02 3.220000000000000000e+02 4.320000000000000000e+02 3.750000000000000000e+02 3.690000000000000000e+02 4.240000000000000000e+02 0.000000000000000000e+00 2.220000000000000000e+02 5.840000000000000000e+02 5.360000000000000000e+02 8.220000000000000000e+02 5.930000000000000000e+02 5.890000000000000000e+02 2.060000000000000000e+02 4.780000000000000000e+02 2.970000000000000000e+02
-1.300000000000000000e+02 2.290000000000000000e+02 6.100000000000000000e+02 4.340000000000000000e+02 3.050000000000000000e+02 2.910000000000000000e+02 2.200000000000000000e+02 3.940000000000000000e+02 2.220000000000000000e+02 0.000000000000000000e+00 4.120000000000000000e+02 3.190000000000000000e+02 6.160000000000000000e+02 5.610000000000000000e+02 4.660000000000000000e+02 4.420000000000000000e+02 6.900000000000000000e+02 5.320000000000000000e+02
-2.850000000000000000e+02 2.530000000000000000e+02 5.690000000000000000e+02 6.180000000000000000e+02 1.550000000000000000e+02 7.000000000000000000e+02 4.610000000000000000e+02 3.500000000000000000e+02 5.840000000000000000e+02 4.120000000000000000e+02 0.000000000000000000e+00 1.070000000000000000e+02 2.440000000000000000e+02 2.760000000000000000e+02 1.790000000000000000e+02 7.680000000000000000e+02 1.077000000000000000e+03 8.780000000000000000e+02
-2.170000000000000000e+02 2.170000000000000000e+02 6.020000000000000000e+02 6.390000000000000000e+02 1.890000000000000000e+02 5.860000000000000000e+02 3.460000000000000000e+02 3.860000000000000000e+02 5.360000000000000000e+02 3.190000000000000000e+02 1.070000000000000000e+02 0.000000000000000000e+00 2.960000000000000000e+02 3.840000000000000000e+02 3.840000000000000000e+02 7.250000000000000000e+02 9.850000000000000000e+02 8.140000000000000000e+02
-4.750000000000000000e+02 5.020000000000000000e+02 7.560000000000000000e+02 8.610000000000000000e+02 3.970000000000000000e+02 8.820000000000000000e+02 6.430000000000000000e+02 5.930000000000000000e+02 8.220000000000000000e+02 6.160000000000000000e+02 2.440000000000000000e+02 2.960000000000000000e+02 0.000000000000000000e+00 4.670000000000000000e+02 3.710000000000000000e+02 1.011000000000000000e+03 1.282000000000000000e+03 1.121000000000000000e+03
-4.400000000000000000e+02 3.410000000000000000e+02 3.170000000000000000e+02 5.080000000000000000e+02 2.430000000000000000e+02 8.480000000000000000e+02 6.860000000000000000e+02 2.040000000000000000e+02 5.930000000000000000e+02 5.610000000000000000e+02 2.760000000000000000e+02 3.840000000000000000e+02 4.670000000000000000e+02 0.000000000000000000e+00 9.500000000000000000e+01 6.580000000000000000e+02 9.670000000000000000e+02 7.680000000000000000e+02
-3.620000000000000000e+02 2.630000000000000000e+02 3.880000000000000000e+02 5.450000000000000000e+02 1.640000000000000000e+02 7.720000000000000000e+02 6.030000000000000000e+02 2.410000000000000000e+02 5.890000000000000000e+02 4.660000000000000000e+02 1.790000000000000000e+02 3.840000000000000000e+02 3.710000000000000000e+02 9.500000000000000000e+01 0.000000000000000000e+00 6.950000000000000000e+02 1.005000000000000000e+03 7.680000000000000000e+02
-5.080000000000000000e+02 5.220000000000000000e+02 6.820000000000000000e+02 1.950000000000000000e+02 6.280000000000000000e+02 5.390000000000000000e+02 5.880000000000000000e+02 4.650000000000000000e+02 2.060000000000000000e+02 4.420000000000000000e+02 7.680000000000000000e+02 7.250000000000000000e+02 1.011000000000000000e+03 6.580000000000000000e+02 6.950000000000000000e+02 0.000000000000000000e+00 3.150000000000000000e+02 1.120000000000000000e+02
-7.760000000000000000e+02 7.810000000000000000e+02 9.910000000000000000e+02 5.070000000000000000e+02 8.900000000000000000e+02 4.050000000000000000e+02 6.450000000000000000e+02 7.770000000000000000e+02 4.780000000000000000e+02 6.900000000000000000e+02 1.077000000000000000e+03 9.850000000000000000e+02 1.282000000000000000e+03 9.670000000000000000e+02 1.005000000000000000e+03 3.150000000000000000e+02 0.000000000000000000e+00 2.730000000000000000e+02
-5.970000000000000000e+02 6.120000000000000000e+02 7.920000000000000000e+02 3.050000000000000000e+02 7.380000000000000000e+02 5.310000000000000000e+02 6.780000000000000000e+02 5.750000000000000000e+02 2.970000000000000000e+02 5.320000000000000000e+02 8.780000000000000000e+02 8.140000000000000000e+02 1.121000000000000000e+03 7.680000000000000000e+02 7.680000000000000000e+02 1.120000000000000000e+02 2.730000000000000000e+02 0.000000000000000000e+00
View
129 sklearn/datasets/data/knuth_miles.csv
129 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit 62a3484

Please sign in to comment.