Created
December 9, 2021 01:23
-
-
Save Jeremiah-England/0cb56a8d2701efe896fd70d44e41e47a to your computer and use it in GitHub Desktop.
Generate some graphs and projections of the population of Greenville
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Graph the population of Greenville County over time. | |
Data from wikipedia. | |
https://en.wikipedia.org/wiki/Greenville_County,_South_Carolina | |
Need to `pip install sklearn` and `pip install matplotlib` first. | |
""" | |
import matplotlib.pyplot as plt | |
import math | |
from sklearn import linear_model | |
population_by_year = { | |
1790: 6503, | |
1800: 11504, | |
1810: 13133, | |
1820: 14530, | |
1830: 16476, | |
1840: 17839, | |
1850: 20156, | |
1860: 21892, | |
1870: 22262, | |
1880: 37496, | |
1890: 44310, | |
1900: 53490, | |
1910: 68377, | |
1920: 88498, | |
1930: 117009, | |
1940: 136580, | |
1950: 168152, | |
1960: 209776, | |
1970: 240546, | |
1980: 287913, | |
1990: 320167, | |
2000: 379616, | |
2010: 451225, | |
2020: 532486, | |
} | |
years = list(population_by_year.keys()) | |
populations = population_by_year.values() | |
log_populations = [math.log(population) for population in populations] | |
start_year, end_year = 1789, 2051 | |
regression = linear_model.LinearRegression() | |
regression.fit([[year] for year in years], log_populations) | |
slope = regression.coef_[0] | |
intercept = regression.intercept_ | |
predictions_by_year = { | |
year: regression.predict([[year]])[0] for year in range(start_year, end_year) | |
} | |
# Print Model | |
print("Linear least squares fit of the log of the population.") | |
print(f"Intercept: {intercept:.5}") | |
print(f"Slope: {slope:.5}") | |
print(f"Years to Double: {math.log(2) / slope:.3}") | |
# e^{slope * x} = e^{slope * 0} * 2 # doubles when x years pass | |
# e^{slope * x} = 1 * 2 | |
# e^{slope * x} = 2 | |
# slope * x = log(2) | |
# x = log(2) / slope | |
# Make graphs | |
plt.rcParams.update({"text.usetex": True}) | |
plt.plot(years, log_populations, label="$\\log(population)$") | |
plt.plot( | |
predictions_by_year.keys(), | |
predictions_by_year.values(), | |
label=f"$y = {slope:.3}x {intercept:.3}$".replace("-", "- "), | |
) | |
plt.legend() | |
plt.title("Greenville Population (log)") | |
plt.savefig("greenville-population-log.png") | |
plt.close() | |
plt.plot(years, populations, label="$population$") | |
plt.plot( | |
predictions_by_year.keys(), | |
[math.exp(prediction) for prediction in predictions_by_year.values()], | |
label=f"$population = ({math.exp(intercept):.4E}) e^{{{slope:.3}year}}$".replace( | |
"E-", "$E-$" | |
), | |
) | |
plt.legend() | |
plt.title("Greenville Population") | |
plt.savefig("greenville-population.png") | |
plt.close() | |
# Print population table. | |
print("| Year | Projected Population | Population |") | |
print("|------|----------------------|------------|") | |
for year, prediction in predictions_by_year.items(): | |
population = ( | |
f"{population_by_year[year]:10,d}" if year in population_by_year else " " * 10 | |
) | |
print(f"| {year} | {int(math.exp(prediction)):20,d} | {population} |") |
Author
Jeremiah-England
commented
Dec 9, 2021
Year | Projected Population | Population |
---|---|---|
1789 | 7,120 | |
1790 | 7,257 | 6,503 |
1791 | 7,396 | |
1792 | 7,537 | |
1793 | 7,682 | |
1794 | 7,829 | |
1795 | 7,979 | |
1796 | 8,132 | |
1797 | 8,287 | |
1798 | 8,446 | |
1799 | 8,608 | |
1800 | 8,773 | 11,504 |
1801 | 8,941 | |
1802 | 9,112 | |
1803 | 9,287 | |
1804 | 9,465 | |
1805 | 9,646 | |
1806 | 9,831 | |
1807 | 10,019 | |
1808 | 10,211 | |
1809 | 10,407 | |
1810 | 10,606 | 13,133 |
1811 | 10,809 | |
1812 | 11,016 | |
1813 | 11,227 | |
1814 | 11,442 | |
1815 | 11,662 | |
1816 | 11,885 | |
1817 | 12,113 | |
1818 | 12,345 | |
1819 | 12,581 | |
1820 | 12,822 | 14,530 |
1821 | 13,068 | |
1822 | 13,318 | |
1823 | 13,573 | |
1824 | 13,833 | |
1825 | 14,098 | |
1826 | 14,368 | |
1827 | 14,644 | |
1828 | 14,924 | |
1829 | 15,210 | |
1830 | 15,501 | 16,476 |
1831 | 15,798 | |
1832 | 16,101 | |
1833 | 16,410 | |
1834 | 16,724 | |
1835 | 17,044 | |
1836 | 17,371 | |
1837 | 17,704 | |
1838 | 18,043 | |
1839 | 18,388 | |
1840 | 18,741 | 17,839 |
1841 | 19,100 | |
1842 | 19,465 | |
1843 | 19,838 | |
1844 | 20,218 | |
1845 | 20,606 | |
1846 | 21,000 | |
1847 | 21,403 | |
1848 | 21,813 | |
1849 | 22,231 | |
1850 | 22,656 | 20,156 |
1851 | 23,091 | |
1852 | 23,533 | |
1853 | 23,984 | |
1854 | 24,443 | |
1855 | 24,911 | |
1856 | 25,389 | |
1857 | 25,875 | |
1858 | 26,371 | |
1859 | 26,876 | |
1860 | 27,391 | 21,892 |
1861 | 27,915 | |
1862 | 28,450 | |
1863 | 28,995 | |
1864 | 29,551 | |
1865 | 30,117 | |
1866 | 30,694 | |
1867 | 31,282 | |
1868 | 31,881 | |
1869 | 32,491 | |
1870 | 33,114 | 22,262 |
1871 | 33,748 | |
1872 | 34,395 | |
1873 | 35,054 | |
1874 | 35,725 | |
1875 | 36,409 | |
1876 | 37,107 | |
1877 | 37,818 | |
1878 | 38,542 | |
1879 | 39,281 | |
1880 | 40,033 | 37,496 |
1881 | 40,800 | |
1882 | 41,582 | |
1883 | 42,378 | |
1884 | 43,190 | |
1885 | 44,017 | |
1886 | 44,860 | |
1887 | 45,720 | |
1888 | 46,596 | |
1889 | 47,488 | |
1890 | 48,398 | 44,310 |
1891 | 49,325 | |
1892 | 50,270 | |
1893 | 51,233 | |
1894 | 52,214 | |
1895 | 53,215 | |
1896 | 54,234 | |
1897 | 55,273 | |
1898 | 56,332 | |
1899 | 57,411 | |
1900 | 58,511 | 53,490 |
1901 | 59,632 | |
1902 | 60,774 | |
1903 | 61,938 | |
1904 | 63,125 | |
1905 | 64,334 | |
1906 | 65,566 | |
1907 | 66,822 | |
1908 | 68,102 | |
1909 | 69,407 | |
1910 | 70,736 | 68,377 |
1911 | 72,091 | |
1912 | 73,472 | |
1913 | 74,880 | |
1914 | 76,314 | |
1915 | 77,776 | |
1916 | 79,266 | |
1917 | 80,785 | |
1918 | 82,332 | |
1919 | 83,909 | |
1920 | 85,517 | 88,498 |
1921 | 87,155 | |
1922 | 88,824 | |
1923 | 90,526 | |
1924 | 92,260 | |
1925 | 94,027 | |
1926 | 95,829 | |
1927 | 97,664 | |
1928 | 99,535 | |
1929 | 101,442 | |
1930 | 103,385 | 117,009 |
1931 | 105,366 | |
1932 | 107,384 | |
1933 | 109,441 | |
1934 | 111,538 | |
1935 | 113,674 | |
1936 | 115,852 | |
1937 | 118,071 | |
1938 | 120,333 | |
1939 | 122,638 | |
1940 | 124,987 | 136,580 |
1941 | 127,382 | |
1942 | 129,822 | |
1943 | 132,309 | |
1944 | 134,843 | |
1945 | 137,426 | |
1946 | 140,059 | |
1947 | 142,742 | |
1948 | 145,476 | |
1949 | 148,263 | |
1950 | 151,103 | 168,152 |
1951 | 153,998 | |
1952 | 156,948 | |
1953 | 159,954 | |
1954 | 163,018 | |
1955 | 166,141 | |
1956 | 169,324 | |
1957 | 172,568 | |
1958 | 175,873 | |
1959 | 179,242 | |
1960 | 182,676 | 209,776 |
1961 | 186,175 | |
1962 | 189,742 | |
1963 | 193,376 | |
1964 | 197,081 | |
1965 | 200,856 | |
1966 | 204,704 | |
1967 | 208,625 | |
1968 | 212,622 | |
1969 | 216,695 | |
1970 | 220,846 | 240,546 |
1971 | 225,076 | |
1972 | 229,388 | |
1973 | 233,782 | |
1974 | 238,260 | |
1975 | 242,825 | |
1976 | 247,476 | |
1977 | 252,217 | |
1978 | 257,048 | |
1979 | 261,972 | |
1980 | 266,991 | 287,913 |
1981 | 272,105 | |
1982 | 277,318 | |
1983 | 282,630 | |
1984 | 288,044 | |
1985 | 293,562 | |
1986 | 299,186 | |
1987 | 304,917 | |
1988 | 310,758 | |
1989 | 316,711 | |
1990 | 322,778 | 320,167 |
1991 | 328,961 | |
1992 | 335,263 | |
1993 | 341,685 | |
1994 | 348,230 | |
1995 | 354,901 | |
1996 | 361,700 | |
1997 | 368,629 | |
1998 | 375,690 | |
1999 | 382,887 | |
2000 | 390,222 | 379,616 |
2001 | 397,697 | |
2002 | 405,315 | |
2003 | 413,079 | |
2004 | 420,992 | |
2005 | 429,057 | |
2006 | 437,276 | |
2007 | 445,653 | |
2008 | 454,190 | |
2009 | 462,890 | |
2010 | 471,757 | 451,225 |
2011 | 480,794 | |
2012 | 490,005 | |
2013 | 499,391 | |
2014 | 508,958 | |
2015 | 518,707 | |
2016 | 528,644 | |
2017 | 538,770 | |
2018 | 549,091 | |
2019 | 559,610 | |
2020 | 570,330 | 532,486 |
2021 | 581,255 | |
2022 | 592,390 | |
2023 | 603,738 | |
2024 | 615,303 | |
2025 | 627,090 | |
2026 | 639,102 | |
2027 | 651,345 | |
2028 | 663,822 | |
2029 | 676,539 | |
2030 | 689,499 | |
2031 | 702,707 | |
2032 | 716,168 | |
2033 | 729,887 | |
2034 | 743,869 | |
2035 | 758,118 | |
2036 | 772,641 | |
2037 | 787,442 | |
2038 | 802,526 | |
2039 | 817,900 | |
2040 | 833,567 | |
2041 | 849,535 | |
2042 | 865,809 | |
2043 | 882,395 | |
2044 | 899,298 | |
2045 | 916,525 | |
2046 | 934,082 | |
2047 | 951,976 | |
2048 | 970,212 | |
2049 | 988,797 | |
2050 | 1,007,739 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment