diff --git a/README.md b/README.md index 6f00465..4b9e141 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,11 @@ ci = fci.random_forest_error( ## Examples -The examples (gallery below) demonstrates the package functionality with random forest classifiers and regression models. -The regression examples use scikit-learn's bundled diabetes dataset, while the -classifier example simulates how to add measurements of uncertainty to tasks -like predicting spam emails. Keeping the regression data bundled makes +The examples (gallery below) demonstrate the package functionality with random forest classifiers and regression models. +The regression examples use a bundled copy of the +[UCI Auto MPG dataset](https://doi.org/10.24432/C5859H), while the classifier +example simulates how to add measurements of uncertainty to tasks like +predicting spam emails. Keeping the regression data in the repository makes documentation builds reproducible without relying on an external data service. [Examples gallery](http://contrib.scikit-learn.org/forest-confidence-interval/auto_examples/index.html) diff --git a/examples/README.txt b/examples/README.txt index 4324632..5428a35 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -3,10 +3,13 @@ Examples ========= -The examples use data from standard machine learning libraries to demonstrate -how `forestci` can be used to calculate error bars on +The examples use standard machine learning datasets to demonstrate how +`forestci` can be used to calculate error bars on :class:`RandomForestRegressor` and :class:`RandomForestClassifier` objects. The -regression example uses a data-set from the `UC Irvine Machine Learning Repository `_ with features of -different cars and their MPG. The classification example generates synthetic -data to simulate a task like that of a spam filter: classifying items into one -of two categories (e.g., spam/non-spam) based on a number of features. +regression examples use a bundled copy of the `Auto MPG dataset +`_ from the UC Irvine Machine Learning +Repository, with features of different cars and their MPG. The classification +example generates synthetic data to simulate a task like that of a spam +filter: classifying items into one of two categories (e.g., spam/non-spam) +based on a number of features. The dataset attribution and license are +documented in ``data/README.md``. diff --git a/examples/data/README.md b/examples/data/README.md new file mode 100644 index 0000000..1b962e9 --- /dev/null +++ b/examples/data/README.md @@ -0,0 +1,21 @@ +# Auto MPG dataset + +`auto_mpg.csv` is a copy of the Auto MPG dataset created by R. Quinlan +(1993), obtained from [OpenML dataset 196](https://www.openml.org/d/196). +The canonical UCI Machine Learning Repository record is: + +> Quinlan, R. (1993). *Auto MPG* [Dataset]. UCI Machine Learning Repository. +> https://doi.org/10.24432/C5859H + +## Copyright and license + +The dataset is licensed under the +[Creative Commons Attribution 4.0 International license (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/). +The SPDX license identifier is `CC-BY-4.0`. The UCI record does not provide a +separate copyright notice; copyright remains with the original rights +holder(s). + +The OpenML representation omits the original car-name identifier. For inclusion +here, the ARFF data were converted to CSV, and the `model` and `class` column +names were changed to `model_year` and `mpg`, respectively. Missing horsepower +values are represented by empty CSV fields. diff --git a/examples/data/auto_mpg.csv b/examples/data/auto_mpg.csv new file mode 100644 index 0000000..6e2afb3 --- /dev/null +++ b/examples/data/auto_mpg.csv @@ -0,0 +1,399 @@ +cylinders,displacement,horsepower,weight,acceleration,model_year,origin,mpg +8,307,130,3504,12,70,1,18 +8,350,165,3693,11.5,70,1,15 +8,318,150,3436,11,70,1,18 +8,304,150,3433,12,70,1,16 +8,302,140,3449,10.5,70,1,17 +8,429,198,4341,10,70,1,15 +8,454,220,4354,9,70,1,14 +8,440,215,4312,8.5,70,1,14 +8,455,225,4425,10,70,1,14 +8,390,190,3850,8.5,70,1,15 +8,383,170,3563,10,70,1,15 +8,340,160,3609,8,70,1,14 +8,400,150,3761,9.5,70,1,15 +8,455,225,3086,10,70,1,14 +4,113,95,2372,15,70,3,24 +6,198,95,2833,15.5,70,1,22 +6,199,97,2774,15.5,70,1,18 +6,200,85,2587,16,70,1,21 +4,97,88,2130,14.5,70,3,27 +4,97,46,1835,20.5,70,2,26 +4,110,87,2672,17.5,70,2,25 +4,107,90,2430,14.5,70,2,24 +4,104,95,2375,17.5,70,2,25 +4,121,113,2234,12.5,70,2,26 +6,199,90,2648,15,70,1,21 +8,360,215,4615,14,70,1,10 +8,307,200,4376,15,70,1,10 +8,318,210,4382,13.5,70,1,11 +8,304,193,4732,18.5,70,1,9 +4,97,88,2130,14.5,71,3,27 +4,140,90,2264,15.5,71,1,28 +4,113,95,2228,14,71,3,25 +4,98,,2046,19,71,1,25 +6,232,100,2634,13,71,1,19 +6,225,105,3439,15.5,71,1,16 +6,250,100,3329,15.5,71,1,17 +6,250,88,3302,15.5,71,1,19 +6,232,100,3288,15.5,71,1,18 +8,350,165,4209,12,71,1,14 +8,400,175,4464,11.5,71,1,14 +8,351,153,4154,13.5,71,1,14 +8,318,150,4096,13,71,1,14 +8,383,180,4955,11.5,71,1,12 +8,400,170,4746,12,71,1,13 +8,400,175,5140,12,71,1,13 +6,258,110,2962,13.5,71,1,18 +4,140,72,2408,19,71,1,22 +6,250,100,3282,15,71,1,19 +6,250,88,3139,14.5,71,1,18 +4,122,86,2220,14,71,1,23 +4,116,90,2123,14,71,2,28 +4,79,70,2074,19.5,71,2,30 +4,88,76,2065,14.5,71,2,30 +4,71,65,1773,19,71,3,31 +4,72,69,1613,18,71,3,35 +4,97,60,1834,19,71,2,27 +4,91,70,1955,20.5,71,1,26 +4,113,95,2278,15.5,72,3,24 +4,97.5,80,2126,17,72,1,25 +4,97,54,2254,23.5,72,2,23 +4,140,90,2408,19.5,72,1,20 +4,122,86,2226,16.5,72,1,21 +8,350,165,4274,12,72,1,13 +8,400,175,4385,12,72,1,14 +8,318,150,4135,13.5,72,1,15 +8,351,153,4129,13,72,1,14 +8,304,150,3672,11.5,72,1,17 +8,429,208,4633,11,72,1,11 +8,350,155,4502,13.5,72,1,13 +8,350,160,4456,13.5,72,1,12 +8,400,190,4422,12.5,72,1,13 +3,70,97,2330,13.5,72,3,19 +8,304,150,3892,12.5,72,1,15 +8,307,130,4098,14,72,1,13 +8,302,140,4294,16,72,1,13 +8,318,150,4077,14,72,1,14 +4,121,112,2933,14.5,72,2,18 +4,121,76,2511,18,72,2,22 +4,120,87,2979,19.5,72,2,21 +4,96,69,2189,18,72,2,26 +4,122,86,2395,16,72,1,22 +4,97,92,2288,17,72,3,28 +4,120,97,2506,14.5,72,3,23 +4,98,80,2164,15,72,1,28 +4,97,88,2100,16.5,72,3,27 +8,350,175,4100,13,73,1,13 +8,304,150,3672,11.5,73,1,14 +8,350,145,3988,13,73,1,13 +8,302,137,4042,14.5,73,1,14 +8,318,150,3777,12.5,73,1,15 +8,429,198,4952,11.5,73,1,12 +8,400,150,4464,12,73,1,13 +8,351,158,4363,13,73,1,13 +8,318,150,4237,14.5,73,1,14 +8,440,215,4735,11,73,1,13 +8,455,225,4951,11,73,1,12 +8,360,175,3821,11,73,1,13 +6,225,105,3121,16.5,73,1,18 +6,250,100,3278,18,73,1,16 +6,232,100,2945,16,73,1,18 +6,250,88,3021,16.5,73,1,18 +6,198,95,2904,16,73,1,23 +4,97,46,1950,21,73,2,26 +8,400,150,4997,14,73,1,11 +8,400,167,4906,12.5,73,1,12 +8,360,170,4654,13,73,1,13 +8,350,180,4499,12.5,73,1,12 +6,232,100,2789,15,73,1,18 +4,97,88,2279,19,73,3,20 +4,140,72,2401,19.5,73,1,21 +4,108,94,2379,16.5,73,3,22 +3,70,90,2124,13.5,73,3,18 +4,122,85,2310,18.5,73,1,19 +6,155,107,2472,14,73,1,21 +4,98,90,2265,15.5,73,2,26 +8,350,145,4082,13,73,1,15 +8,400,230,4278,9.5,73,1,16 +4,68,49,1867,19.5,73,2,29 +4,116,75,2158,15.5,73,2,24 +4,114,91,2582,14,73,2,20 +4,121,112,2868,15.5,73,2,19 +8,318,150,3399,11,73,1,15 +4,121,110,2660,14,73,2,24 +6,156,122,2807,13.5,73,3,20 +8,350,180,3664,11,73,1,11 +6,198,95,3102,16.5,74,1,20 +6,200,,2875,17,74,1,21 +6,232,100,2901,16,74,1,19 +6,250,100,3336,17,74,1,15 +4,79,67,1950,19,74,3,31 +4,122,80,2451,16.5,74,1,26 +4,71,65,1836,21,74,3,32 +4,140,75,2542,17,74,1,25 +6,250,100,3781,17,74,1,16 +6,258,110,3632,18,74,1,16 +6,225,105,3613,16.5,74,1,18 +8,302,140,4141,14,74,1,16 +8,350,150,4699,14.5,74,1,13 +8,318,150,4457,13.5,74,1,14 +8,302,140,4638,16,74,1,14 +8,304,150,4257,15.5,74,1,14 +4,98,83,2219,16.5,74,2,29 +4,79,67,1963,15.5,74,2,26 +4,97,78,2300,14.5,74,2,26 +4,76,52,1649,16.5,74,3,31 +4,83,61,2003,19,74,3,32 +4,90,75,2125,14.5,74,1,28 +4,90,75,2108,15.5,74,2,24 +4,116,75,2246,14,74,2,26 +4,120,97,2489,15,74,3,24 +4,108,93,2391,15.5,74,3,26 +4,79,67,2000,16,74,2,31 +6,225,95,3264,16,75,1,19 +6,250,105,3459,16,75,1,18 +6,250,72,3432,21,75,1,15 +6,250,72,3158,19.5,75,1,15 +8,400,170,4668,11.5,75,1,16 +8,350,145,4440,14,75,1,15 +8,318,150,4498,14.5,75,1,16 +8,351,148,4657,13.5,75,1,14 +6,231,110,3907,21,75,1,17 +6,250,105,3897,18.5,75,1,16 +6,258,110,3730,19,75,1,15 +6,225,95,3785,19,75,1,18 +6,231,110,3039,15,75,1,21 +8,262,110,3221,13.5,75,1,20 +8,302,129,3169,12,75,1,13 +4,97,75,2171,16,75,3,29 +4,140,83,2639,17,75,1,23 +6,232,100,2914,16,75,1,20 +4,140,78,2592,18.5,75,1,23 +4,134,96,2702,13.5,75,3,24 +4,90,71,2223,16.5,75,2,25 +4,119,97,2545,17,75,3,24 +6,171,97,2984,14.5,75,1,18 +4,90,70,1937,14,75,2,29 +6,232,90,3211,17,75,1,19 +4,115,95,2694,15,75,2,23 +4,120,88,2957,17,75,2,23 +4,121,98,2945,14.5,75,2,22 +4,121,115,2671,13.5,75,2,25 +4,91,53,1795,17.5,75,3,33 +4,107,86,2464,15.5,76,2,28 +4,116,81,2220,16.9,76,2,25 +4,140,92,2572,14.9,76,1,25 +4,98,79,2255,17.7,76,1,26 +4,101,83,2202,15.3,76,2,27 +8,305,140,4215,13,76,1,17.5 +8,318,150,4190,13,76,1,16 +8,304,120,3962,13.9,76,1,15.5 +8,351,152,4215,12.8,76,1,14.5 +6,225,100,3233,15.4,76,1,22 +6,250,105,3353,14.5,76,1,22 +6,200,81,3012,17.6,76,1,24 +6,232,90,3085,17.6,76,1,22.5 +4,85,52,2035,22.2,76,1,29 +4,98,60,2164,22.1,76,1,24.5 +4,90,70,1937,14.2,76,2,29 +4,91,53,1795,17.4,76,3,33 +6,225,100,3651,17.7,76,1,20 +6,250,78,3574,21,76,1,18 +6,250,110,3645,16.2,76,1,18.5 +6,258,95,3193,17.8,76,1,17.5 +4,97,71,1825,12.2,76,2,29.5 +4,85,70,1990,17,76,3,32 +4,97,75,2155,16.4,76,3,28 +4,140,72,2565,13.6,76,1,26.5 +4,130,102,3150,15.7,76,2,20 +8,318,150,3940,13.2,76,1,13 +4,120,88,3270,21.9,76,2,19 +6,156,108,2930,15.5,76,3,19 +6,168,120,3820,16.7,76,2,16.5 +8,350,180,4380,12.1,76,1,16.5 +8,350,145,4055,12,76,1,13 +8,302,130,3870,15,76,1,13 +8,318,150,3755,14,76,1,13 +4,98,68,2045,18.5,77,3,31.5 +4,111,80,2155,14.8,77,1,30 +4,79,58,1825,18.6,77,2,36 +4,122,96,2300,15.5,77,1,25.5 +4,85,70,1945,16.8,77,3,33.5 +8,305,145,3880,12.5,77,1,17.5 +8,260,110,4060,19,77,1,17 +8,318,145,4140,13.7,77,1,15.5 +8,302,130,4295,14.9,77,1,15 +6,250,110,3520,16.4,77,1,17.5 +6,231,105,3425,16.9,77,1,20.5 +6,225,100,3630,17.7,77,1,19 +6,250,98,3525,19,77,1,18.5 +8,400,180,4220,11.1,77,1,16 +8,350,170,4165,11.4,77,1,15.5 +8,400,190,4325,12.2,77,1,15.5 +8,351,149,4335,14.5,77,1,16 +4,97,78,1940,14.5,77,2,29 +4,151,88,2740,16,77,1,24.5 +4,97,75,2265,18.2,77,3,26 +4,140,89,2755,15.8,77,1,25.5 +4,98,63,2051,17,77,1,30.5 +4,98,83,2075,15.9,77,1,33.5 +4,97,67,1985,16.4,77,3,30 +4,97,78,2190,14.1,77,2,30.5 +6,146,97,2815,14.5,77,3,22 +4,121,110,2600,12.8,77,2,21.5 +3,80,110,2720,13.5,77,3,21.5 +4,90,48,1985,21.5,78,2,43.1 +4,98,66,1800,14.4,78,1,36.1 +4,78,52,1985,19.4,78,3,32.8 +4,85,70,2070,18.6,78,3,39.4 +4,91,60,1800,16.4,78,3,36.1 +8,260,110,3365,15.5,78,1,19.9 +8,318,140,3735,13.2,78,1,19.4 +8,302,139,3570,12.8,78,1,20.2 +6,231,105,3535,19.2,78,1,19.2 +6,200,95,3155,18.2,78,1,20.5 +6,200,85,2965,15.8,78,1,20.2 +4,140,88,2720,15.4,78,1,25.1 +6,225,100,3430,17.2,78,1,20.5 +6,232,90,3210,17.2,78,1,19.4 +6,231,105,3380,15.8,78,1,20.6 +6,200,85,3070,16.7,78,1,20.8 +6,225,110,3620,18.7,78,1,18.6 +6,258,120,3410,15.1,78,1,18.1 +8,305,145,3425,13.2,78,1,19.2 +6,231,165,3445,13.4,78,1,17.7 +8,302,139,3205,11.2,78,1,18.1 +8,318,140,4080,13.7,78,1,17.5 +4,98,68,2155,16.5,78,1,30 +4,134,95,2560,14.2,78,3,27.5 +4,119,97,2300,14.7,78,3,27.2 +4,105,75,2230,14.5,78,1,30.9 +4,134,95,2515,14.8,78,3,21.1 +4,156,105,2745,16.7,78,1,23.2 +4,151,85,2855,17.6,78,1,23.8 +4,119,97,2405,14.9,78,3,23.9 +5,131,103,2830,15.9,78,2,20.3 +6,163,125,3140,13.6,78,2,17 +4,121,115,2795,15.7,78,2,21.6 +6,163,133,3410,15.8,78,2,16.2 +4,89,71,1990,14.9,78,2,31.5 +4,98,68,2135,16.6,78,3,29.5 +6,231,115,3245,15.4,79,1,21.5 +6,200,85,2990,18.2,79,1,19.8 +4,140,88,2890,17.3,79,1,22.3 +6,232,90,3265,18.2,79,1,20.2 +6,225,110,3360,16.6,79,1,20.6 +8,305,130,3840,15.4,79,1,17 +8,302,129,3725,13.4,79,1,17.6 +8,351,138,3955,13.2,79,1,16.5 +8,318,135,3830,15.2,79,1,18.2 +8,350,155,4360,14.9,79,1,16.9 +8,351,142,4054,14.3,79,1,15.5 +8,267,125,3605,15,79,1,19.2 +8,360,150,3940,13,79,1,18.5 +4,89,71,1925,14,79,2,31.9 +4,86,65,1975,15.2,79,3,34.1 +4,98,80,1915,14.4,79,1,35.7 +4,121,80,2670,15,79,1,27.4 +5,183,77,3530,20.1,79,2,25.4 +8,350,125,3900,17.4,79,1,23 +4,141,71,3190,24.8,79,2,27.2 +8,260,90,3420,22.2,79,1,23.9 +4,105,70,2200,13.2,79,1,34.2 +4,105,70,2150,14.9,79,1,34.5 +4,85,65,2020,19.2,79,3,31.8 +4,91,69,2130,14.7,79,2,37.3 +4,151,90,2670,16,79,1,28.4 +6,173,115,2595,11.3,79,1,28.8 +6,173,115,2700,12.9,79,1,26.8 +4,151,90,2556,13.2,79,1,33.5 +4,98,76,2144,14.7,80,2,41.5 +4,89,60,1968,18.8,80,3,38.1 +4,98,70,2120,15.5,80,1,32.1 +4,86,65,2019,16.4,80,3,37.2 +4,151,90,2678,16.5,80,1,28 +4,140,88,2870,18.1,80,1,26.4 +4,151,90,3003,20.1,80,1,24.3 +6,225,90,3381,18.7,80,1,19.1 +4,97,78,2188,15.8,80,2,34.3 +4,134,90,2711,15.5,80,3,29.8 +4,120,75,2542,17.5,80,3,31.3 +4,119,92,2434,15,80,3,37 +4,108,75,2265,15.2,80,3,32.2 +4,86,65,2110,17.9,80,3,46.6 +4,156,105,2800,14.4,80,1,27.9 +4,85,65,2110,19.2,80,3,40.8 +4,90,48,2085,21.7,80,2,44.3 +4,90,48,2335,23.7,80,2,43.4 +5,121,67,2950,19.9,80,2,36.4 +4,146,67,3250,21.8,80,2,30 +4,91,67,1850,13.8,80,3,44.6 +4,85,,1835,17.3,80,2,40.9 +4,97,67,2145,18,80,3,33.8 +4,89,62,1845,15.3,80,2,29.8 +6,168,132,2910,11.4,80,3,32.7 +3,70,100,2420,12.5,80,3,23.7 +4,122,88,2500,15.1,80,2,35 +4,140,,2905,14.3,80,1,23.6 +4,107,72,2290,17,80,3,32.4 +4,135,84,2490,15.7,81,1,27.2 +4,151,84,2635,16.4,81,1,26.6 +4,156,92,2620,14.4,81,1,25.8 +6,173,110,2725,12.6,81,1,23.5 +4,135,84,2385,12.9,81,1,30 +4,79,58,1755,16.9,81,3,39.1 +4,86,64,1875,16.4,81,1,39 +4,81,60,1760,16.1,81,3,35.1 +4,97,67,2065,17.8,81,3,32.3 +4,85,65,1975,19.4,81,3,37 +4,89,62,2050,17.3,81,3,37.7 +4,91,68,1985,16,81,3,34.1 +4,105,63,2215,14.9,81,1,34.7 +4,98,65,2045,16.2,81,1,34.4 +4,98,65,2380,20.7,81,1,29.9 +4,105,74,2190,14.2,81,2,33 +4,100,,2320,15.8,81,2,34.5 +4,107,75,2210,14.4,81,3,33.7 +4,108,75,2350,16.8,81,3,32.4 +4,119,100,2615,14.8,81,3,32.9 +4,120,74,2635,18.3,81,3,31.6 +4,141,80,3230,20.4,81,2,28.1 +6,145,76,3160,19.6,81,2,30.7 +6,168,116,2900,12.6,81,3,25.4 +6,146,120,2930,13.8,81,3,24.2 +6,231,110,3415,15.8,81,1,22.4 +8,350,105,3725,19,81,1,26.6 +6,200,88,3060,17.1,81,1,20.2 +6,225,85,3465,16.6,81,1,17.6 +4,112,88,2605,19.6,82,1,28 +4,112,88,2640,18.6,82,1,27 +4,112,88,2395,18,82,1,34 +4,112,85,2575,16.2,82,1,31 +4,135,84,2525,16,82,1,29 +4,151,90,2735,18,82,1,27 +4,140,92,2865,16.4,82,1,24 +4,151,,3035,20.5,82,1,23 +4,105,74,1980,15.3,82,2,36 +4,91,68,2025,18.2,82,3,37 +4,91,68,1970,17.6,82,3,31 +4,105,63,2125,14.7,82,1,38 +4,98,70,2125,17.3,82,1,36 +4,120,88,2160,14.5,82,3,36 +4,107,75,2205,14.5,82,3,36 +4,108,70,2245,16.9,82,3,34 +4,91,67,1965,15,82,3,38 +4,91,67,1965,15.7,82,3,32 +4,91,67,1995,16.2,82,3,38 +6,181,110,2945,16.4,82,1,25 +6,262,85,3015,17,82,1,38 +4,156,92,2585,14.5,82,1,26 +6,232,112,2835,14.7,82,1,22 +4,144,96,2665,13.9,82,3,32 +4,135,84,2370,13,82,1,36 +4,151,90,2950,17.3,82,1,27 +4,140,86,2790,15.6,82,1,27 +4,97,52,2130,24.6,82,2,44 +4,135,84,2295,11.6,82,1,32 +4,120,79,2625,18.6,82,1,28 +4,119,82,2720,19.4,82,1,31 diff --git a/examples/plot_mpg.py b/examples/plot_mpg.py index e070f3d..0e0a741 100644 --- a/examples/plot_mpg.py +++ b/examples/plot_mpg.py @@ -6,25 +6,40 @@ This example demonstrates using `forestci` to calculate the error bars of the predictions of a :class:`sklearn.ensemble.RandomForestRegressor` object. -The data used here are scikit-learn's bundled diabetes regression dataset, -which avoids requiring a network connection when building the documentation. +The data used here are the Auto MPG dataset by R. Quinlan, bundled under the +Creative Commons Attribution 4.0 International license. See ``data/README.md`` +for attribution and licensing details. """ # Regression Forest Example +from pathlib import Path + import numpy as np from matplotlib import pyplot as plt -from sklearn.datasets import load_diabetes from sklearn.ensemble import RandomForestRegressor import sklearn.model_selection as xval import forestci as fci -# Load a regression dataset bundled with scikit-learn -diabetes_X, diabetes_y = load_diabetes(return_X_y=True) +# Load the bundled Auto MPG data +data_path = Path.cwd() / "data" / "auto_mpg.csv" +if not data_path.exists(): + # Also support running ``python examples/plot_mpg.py`` from the repo root. + data_path = Path.cwd() / "examples" / "data" / "auto_mpg.csv" +mpg_data = np.genfromtxt( + data_path, + delimiter=",", + skip_header=1, +) + +# Separate the predictors and target, removing rows with missing values +mpg_data = mpg_data[~np.isnan(mpg_data).any(axis=1)] +mpg_X = mpg_data[:, :-1] +mpg_y = mpg_data[:, -1] # Split the data into training and test sets X_train, X_test, y_train, y_test = xval.train_test_split( - diabetes_X, - diabetes_y, + mpg_X, + mpg_y, test_size=0.25, random_state=42, ) @@ -34,13 +49,13 @@ forest = RandomForestRegressor(n_estimators=n_trees, random_state=42) forest.fit(X_train, y_train) y_pred = forest.predict(X_test) -target_range = [diabetes_y.min(), diabetes_y.max()] +target_range = [mpg_y.min(), mpg_y.max()] # Plot predictions without error bars plt.scatter(y_test, y_pred) plt.plot(target_range, target_range, 'k--') -plt.xlabel('Observed disease progression') -plt.ylabel('Predicted disease progression') +plt.xlabel('Reported MPG') +plt.ylabel('Predicted MPG') plt.show() # Calculate the variance @@ -49,6 +64,6 @@ # Plot error bars for predictions using unbiased variance plt.errorbar(y_test, y_pred, yerr=np.sqrt(variance), fmt='o') plt.plot(target_range, target_range, 'k--') -plt.xlabel('Observed disease progression') -plt.ylabel('Predicted disease progression') +plt.xlabel('Reported MPG') +plt.ylabel('Predicted MPG') plt.show() diff --git a/examples/plot_mpg_svr.py b/examples/plot_mpg_svr.py index 5938cf0..b81ca54 100644 --- a/examples/plot_mpg_svr.py +++ b/examples/plot_mpg_svr.py @@ -6,25 +6,40 @@ This example demonstrates using `forestci` to calculate the error bars of the predictions of a :class:`sklearn.ensemble.BaggingRegressor` object. -The data used here are scikit-learn's bundled diabetes regression dataset, -which avoids requiring a network connection when building the documentation. +The data used here are the Auto MPG dataset by R. Quinlan, bundled under the +Creative Commons Attribution 4.0 International license. See ``data/README.md`` +for attribution and licensing details. """ # Regression Forest Example +from pathlib import Path + import numpy as np from matplotlib import pyplot as plt -from sklearn.datasets import load_diabetes from sklearn.ensemble import BaggingRegressor from sklearn.svm import SVR import sklearn.model_selection as xval import forestci as fci -# Load a regression dataset bundled with scikit-learn -diabetes_X, diabetes_y = load_diabetes(return_X_y=True) +# Load the bundled Auto MPG data +data_path = Path.cwd() / "data" / "auto_mpg.csv" +if not data_path.exists(): + # Also support running ``python examples/plot_mpg_svr.py`` from the repo root. + data_path = Path.cwd() / "examples" / "data" / "auto_mpg.csv" +mpg_data = np.genfromtxt( + data_path, + delimiter=",", + skip_header=1, +) + +# Separate the predictors and target, removing rows with missing values +mpg_data = mpg_data[~np.isnan(mpg_data).any(axis=1)] +mpg_X = mpg_data[:, :-1] +mpg_y = mpg_data[:, -1] # Split the data into training and test sets X_train, X_test, y_train, y_test = xval.train_test_split( - diabetes_X, diabetes_y, test_size=0.25, random_state=42 + mpg_X, mpg_y, test_size=0.25, random_state=42 ) # Create a bagged SVR model @@ -34,13 +49,13 @@ ) bagger.fit(X_train, y_train) y_pred = bagger.predict(X_test) -target_range = [diabetes_y.min(), diabetes_y.max()] +target_range = [mpg_y.min(), mpg_y.max()] # Plot predictions without error bars plt.scatter(y_test, y_pred) plt.plot(target_range, target_range, "k--") -plt.xlabel("Observed disease progression") -plt.ylabel("Predicted disease progression") +plt.xlabel("Reported MPG") +plt.ylabel("Predicted MPG") plt.show() # Calculate the variance @@ -49,6 +64,6 @@ # Plot error bars for predictions using unbiased variance plt.errorbar(y_test, y_pred, yerr=np.sqrt(variance), fmt="o") plt.plot(target_range, target_range, "k--") -plt.xlabel("Observed disease progression") -plt.ylabel("Predicted disease progression") +plt.xlabel("Reported MPG") +plt.ylabel("Predicted MPG") plt.show()