├── .gitignore ├── README.md ├── code └── main.py ├── data └── Final.csv └── img ├── 2020年个国家地区互联网用户占比和移动互联网订阅量散点图及线性回归拟合.png ├── 2020年各个国家地区的互联网用户占比柱状图.png ├── 2020年各个国家地区的互联网用户占比饼图.png ├── 2020年各国家地区互联网用户占比分布直方图.png ├── 中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例.png ├── 中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例的增长率.png ├── 全球用户每年的各项数据的分析与可视化.png ├── 到2030年中国互联网总用户数的预测.png ├── 对1980到2020年中国互联网总用户数的拟合.png └── 每年互联网用户的比例最大的国家地区名词云.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
武汉理工大学
6 | 7 | 8 | 9 |

Python数据分析与可视化

10 | 11 |

大 作 业

12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |

题 目: 对全球和中国互联网用户的数据分析与可视化

22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 |
44 | 45 | 46 | 47 | ## 一、项目概述 48 | 49 | 50 | 51 | ### 1.1. 项目背景: 52 | 53 | 互联网是当今时代最重要和最有影响力的技术之一,它已经深刻地改变了人们的生活、工作、学习等方面。互联网用户数据是反映互联网发展水平和潜力的重要指标,它可以帮助我们了解不同国家地区在互联网领域的优势和劣势,以及存在的差异和不平衡。全球互联网用户数量在过去十年中增长迅速,其中中国是全球最大的互联网市场。然而,全球和中国的互联网用户数据也存在着一些问题和挑战。通过对全球和中国的互联网用户数据进行分析和可视化,我们可以更好地把握互联网领域的变化趋势和分布情况,以及预测未来的发展方向和挑战。 54 | 55 | ### 1.2 数据来源: 56 | 57 | 数据来源于kaggle上名为Global Internet users的数据集,数据集链接:[Global Internet users | Kaggle](https://www.kaggle.com/datasets/ashishraut64/internet-users) ,该数据集包含了1980-2020年间关于全球互联网用户的信息。其中包括国家或地区名称、国家代码、年份、每100人的移动端互联网订阅数、互联网用户占总人口的比例、互联网用户数量以及每100人的宽带订阅数等信息。 58 | 59 | ### 1.3. 程序功能: 60 | 61 | 分析和可视化全球用户每年的各项数据,如互联网用户总数、移动端互联网订阅数、互联网使用人数比例、宽带订阅数等。 62 | 63 | - 绘制 2020 年各个国家地区的用户占比饼图和柱状图,展示全球互联网用户占比的分布情况和差异。 64 | - 绘制 2020 年各国家地区互联网用户占比分布直方图,展示全球互联网用户占比的分布特征和偏态。 65 | - 绘制 2020 年个国家地区互联网用户占比和移动互联网订阅量的散点图,并利用线性回归模型分析两者之间的相关性。 66 | - 绘制每一年互联网用户的比例最大的三个国家地区名的词云,展示全球互联网领域的优势和影响力。 67 | - 对中国互联网用户数据进行分析和可视化,展示中国在互联网领域的发展水平和潜力,并利用多元线性回归模型预测中国互联网到 2050 年的总用户数。 68 | 69 | ### 1.4. 所使用第三方库介绍: 70 | 71 | - numpy 库:用于进行数值计算,如数组、矩阵、向量等的创建、操作和运算。 72 | 73 | - pandas 库:用于处理数据,如数据的读取、清洗、分组、聚合、合并等。 74 | - matplotlib 库:用于绘制图形,如折线图、柱状图、饼图等,以及设置图形的样式、标题、标签等。 75 | - seaborn 库:用于绘制图形,如直方图、散点图等,以及设置图形的主题、颜色等。 76 | - wordcloud 库:用于配置和生成词云,如设置词云的形状、大小、字体等。 77 | - sklearn 库:用于进行线性回归分析,如创建线性回归模型、拟合数据、预测数据、评估模型等。 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | ## 二、 功能实现 86 | 87 | 88 | 89 | ### 2.1. 数据读取和工具函数的实现 90 | 91 | 使用 `read_csv` 函数读取 `Final.csv` 文件,获取全球互联网用户信息,定义两个工具函数`set_seaborn_properties`、`get_2020_entities_dataframe`。 92 | 93 | ```python 94 | # 从数据集中读取出所有的数据 95 | global_users = pd.read_csv('../data/Final.csv', delimiter=',', usecols=range(1, 8)) 96 | 97 | # 此函数用于配置每个seaborn图的主题,其中提供了一些默认配置,需要修改的配置在参数中提供,根据图的美观性和实用性需要改变参数 98 | def set_seaborn_properties(context='talk', font_scale=0.8) 99 | 100 | # 此函数用于获取所有Entity的2020年数据组成的DataFrame 101 | def get_2020_entities_dataframe() 102 | ``` 103 | 104 | ### 2.2. 全球用户每年的各项数据的分析与可视化 105 | 106 | 定义一个名为 `global_internet_users_analysis` 的函数,用于分析和可视化全球用户每年的各项数据。在这个函数中,使用 matplotlib 和 seaborn 库绘制了全球每年的互联网用户总数、全球每年每100人移动端互联网订阅数、互联网使用人数比例、每100人宽带订阅数的平均值等图形。 107 | 108 | ``````python 109 | plt.subplot(2, 2, 1) # 绘制子图 110 | sns.lineplot(data=internet_users_sum_data, x='Year', y='sum') # 折线图 111 | plt.bar(column_mean.index, column_mean.values, color='cornflowerblue', width=0.6) # 配合折线图使构图饱满 112 | 113 | for column in ['Cellular Subscription', 'Internet Users(%)', 'Broadband Subscription']: 114 | plt.subplot(2, 2, i) 115 | i += 1 116 | # 最大值 117 | sns.lineplot(data=max_data, x='Year', y='max', label=column + ' max', lw=2, linestyle=(0, (5, 1))) 118 | # 平均值 119 | sns.lineplot(data=mean_data, x='Year', y='mean', label=column + ' mean', lw=3, linestyle=(0, (1, 1))) 120 | `````` 121 | 122 | ![全球用户每年的各项数据的分析与可视化](img/全球用户每年的各项数据的分析与可视化.png) 123 | 124 | 从图中可以看出: 125 | 126 | - 全球互联网用户总数呈现出一个快速增长的趋势,尤其是在 2000 年之后,增长速度更加明显。这说明互联网技术的发展和普及,以及人们对互联网的需求和依赖都在不断增加。 127 | 128 | - 全球每100人移动端互联网订阅数也呈现出一个快速增长的趋势,尤其是在 2005 年之后,增长速度更加明显。这说明移动设备的普及和便捷,以及人们对移动互联网的需求和偏好都在不断增加。 129 | 130 | - 全球互联网使用人数比例也呈现出一个快速增长的趋势。这说明互联网已经成为人们生活、工作、学习等方面不可或缺的一部分,以及互联网的覆盖范围和接入方式都在不断扩大和改善。 131 | 132 | - 全球每100人宽带订阅数呈现出一个缓慢增长的趋势,但在 2010 年之后,增长速度有所放缓。这说明宽带网络的发展和普及还有一定的空间和潜力,以及宽带网络的竞争力和吸引力可能受到了移动网络的影响。 133 | 134 | 135 | 136 | ### 2.3. 2020年各个国家地区的用户占比饼图和柱状图绘制 137 | 138 | 定义了一个名为 `entities_2020_internet_users_percentage_pie_bar` 的函数,用于绘制 2020 年各个国家地区的用户占比饼图和柱状图。在这个函数中,首先获取 2020 年国家地区用户数量最多的10组数据,其他数据用`other`代替的数据封装成的 DataFrame,使用 matplotlib 和 seaborn 库绘制饼图和柱状图。 139 | 140 | ```python 141 | # 只筛选用户数量最多的10组数据,其他数据用`other`代替 142 | entity_2020_df.sort_values(by='No. of Internet Users', axis=0, ascending=False, inplace=True) 143 | processed_data = pd.concat([entity_2020_df.head(10), other_df], axis=0, join='outer') 144 | 145 | # 绘制饼图 146 | plt.pie(processed_data['No. of Internet Users'], labels=processed_data.index, explode=explode_arr, 147 | labeldistance=1.1, autopct='%2.1f%%', pctdistance=0.9, shadow=True) 148 | 149 | # 绘制柱状图 150 | sns.barplot(data=data, x='Entity', y='Percent') 151 | ``` 152 | 153 | 154 | 155 | ![2020年各个国家地区的互联网用户占比饼图](img/2020年各个国家地区的互联网用户占比饼图.png) 156 | 157 | ![2020年各个国家地区的互联网用户占比柱状图](img/2020年各个国家地区的互联网用户占比柱状图.png) 158 | 159 | 从图中可以看出: 160 | 161 | - 在 2020 年,全球互联网用户占比最高的国家地区是中国,占比达到了 20.3%,远高于其他国家地区。这说明中国在互联网领域有着巨大的市场规模和潜力,以及中国在互联网技术、应用、服务等方面有着较强的竞争力和影响力。 162 | 163 | - 在 2020 年,全球互联网用户占比第二高的国家地区是印度,占比为 12.1%,但与中国相比还有较大的差距。这说明印度在互联网领域也有着较大的市场规模和潜力,但与中国相比还有较大的发展空间和挑战。 164 | 165 | - 在 2020 年,全球互联网用户占比第三高的国家地区是南美,占比为 9.7%,与印度相比较为接近。这说明美国在互联网领域也有着较大的市场规模和潜力,但与中国相比也有较大的差距。 166 | 167 | - 在 2020 年,全球互联网用户占比排名前十的国家地区还包括巴西、印度尼西亚、俄罗斯、日本、墨西哥和埃及。这些国家地区的占比都在 1.5% 到 3.6% 之间,相对较低。这说明这些国家地区在互联网领域还有较大的发展空间和潜力,但也面临着较大的挑战和竞争。 168 | 169 | - 在 2020 年,全球互联网用户占比排名第十一以后的国家地区的占比加起来只有 约37%,远低于中国的占比。这说明这些国家地区在互联网领域还有较大的不平衡和差距,需要加强互联网技术的普及和提升。 170 | 171 | 172 | 173 | ### 2.4. 2020年各国家地区互联网用户占比分布直方图 174 | 175 | 定义了一个名为 `entities_2020_internet_users_percentage_distribution_histogram` 的函数,用于绘制 2020 年各国家地区互联网用户占比分布直方图。在这个函数中,首先获取 2020 年国家地区数据封装成的 DataFrame,然后使用 seaborn 库绘制了直方图。 176 | 177 | ```python 178 | # 获取数据 179 | data = pd.DataFrame({'Entity': internet_users_percentage_sr.index, 'Percent':internet_users_percentage_sr.values}) 180 | 181 | # 绘制直方图 182 | sns.histplot(data, x='Percent') 183 | ``` 184 | 185 | 186 | 187 | ![2020年各国家地区互联网用户占比分布直方图](img/2020年各国家地区互联网用户占比分布直方图.png) 188 | 189 | 从图中可以看出: 190 | 191 | - 在 2020 年,各国家地区互联网用户占比的分布呈现出一个右偏态的分布,大部分国家地区的互联网用户占比都集中在较低的范围内,而少数国家地区的互联网用户占比则达到了较高的水平。 192 | 193 | - 在 2020 年,各国家地区互联网用户占比的最高值为 100%,最低值为 0%,平均值为 47.9%,中位数为 53.9%,标准差为 36.1%。这说明各国家地区互联网用户占比存在着较大的差异和不平衡,以及部分国家地区的互联网发展水平还有较大的提升空间。 194 | 195 | 196 | 197 | ### 2.5. 2020年个国家地区互联网用户占比和移动互联网订阅量的散点图 198 | 199 | 定义了一个名为 `entities_2020_internet_users_percentage_distribution_scatter` 的函数,用于绘制 2020 年个国家地区互联网用户占比和移动互联网订阅量的散点图。在这个函数中,首先获取 2020 年国家地区数据封装成的 DataFrame,然后使用 seaborn 库绘制了散点图,并利用 sklearn 库中的线性回归模型分析两者之间的关系。 200 | 201 | ```python 202 | # 绘制散点图 203 | sns.scatterplot(data=entity_2020_df, x='Internet Users(%)', y='Cellular Subscription', 204 | palette='husl', hue='Entity', legend=None) # 根据地区设置hue参数,使颜色丰富 205 | 206 | 207 | # 一元线性回归分析两者关系 208 | x = entity_2020_df[['Internet Users(%)']] 209 | model_1 = linear_model.LinearRegression() 210 | model_1.fit(x, entity_2020_df[['Cellular Subscription']]) 211 | data = pd.DataFrame({'x': x['Internet Users(%)'], 'pred_y': [x[0] for x in model_1.predict(x)]}) 212 | sns.lineplot(data=data, x='x', y='pred_y') 213 | ``` 214 | 215 | 216 | 217 | ![2020年个国家地区互联网用户占比和移动互联网订阅量散点图及线性回归拟合](img/2020年个国家地区互联网用户占比和移动互联网订阅量散点图及线性回归拟合.png) 218 | 219 | 从图中可以看出: 220 | 221 | - 在 2020 年,各国家地区互联网用户占比和移动互联网订阅量呈现出一个正相关的关系,即互联网用户占比越高的国家地区,移动互联网订阅量也越高,反之亦然。这说明互联网用户占比和移动互联网订阅量是两个相互影响和促进的指标,反映了一个国家地区的互联网发展水平和便捷程度。 222 | 223 | 224 | 225 | ### 2.6. 用每一年互联网用户的比例最大的三个国家地区名生成词云 226 | 227 | 定义了一个名为 `draw_internet_users_percentage_annual_top_3_wordcloud` 的函数,用于绘制每一年互联网用户的比例最大的三个国家地区名生成词云。在这个函数中,首先获取每一年互联网用户的比例最大的三个国家地区名,然后使用 wordcloud 库绘制词云。 228 | 229 | ```python 230 | text = '' 231 | year_groups = global_users.groupby('Year') 232 | # 获取每一年互联网用户的比例最大的三个国家地区名数据 233 | for year, year_df in year_groups: 234 | year_df.sort_values(by='Internet Users(%)', ascending=False, inplace=True) 235 | top_3 = year_df.head(3) 236 | entities = top_3['Entity'] 237 | # 数据处理 238 | for entity in entities: 239 | if len(entity.split()) > 1: 240 | text += entity.replace(' ', '_') + ' ' 241 | # 将名字中含有空格的国家地区名中的空格替换成下划线_,避免一个名字被拆分成多个单词 242 | else: 243 | text += entity + ' ' 244 | 245 | # 绘制词云 246 | wc = WordCloud(max_words=100, width=800, height=400, background_color='White', 247 | max_font_size=150, stopwords=STOPWORDS, margin=5, scale=1.5) 248 | wc.generate(text) 249 | plt.imshow(wc) 250 | plt.axis("off") 251 | plt.show() 252 | ``` 253 | 254 | 255 | 256 | ![每年互联网用户的比例最大的国家地区名词云](img/每年互联网用户的比例最大的国家地区名词云.png) 257 | 258 | 从图中可以看出: 259 | 260 | - 在 1980-2020 年间,出现频率最高的国家地区名是 Iceland、Norway和Sweden。这说明这些国家地区在互联网领域有着长期的较高的发展水平和优势,以及较高的人口普及率和接入率。 261 | 262 | - 在 1980-2020 年间,出现频率较高的国家地区名还有 Bermuda、Denmark、Finland、Moracco、Afghanistan和 United_States等。这说明这些国家地区在互联网领域也有着长期的较高的发展水平和优势,以及较高的人口普及率和接入率。 263 | 264 | - 在 1980-2020 年间,出现频率较低或没有出现的国家地区名有 China、India、Brazil、Indonesia 等。结合2.4中的结果,这些国家地区有些是后起之秀,如中国和印度,有些则在互联网领域还有较大的发展空间和潜力,如巴西和印度尼西亚。 265 | 266 | 267 | 268 | ### 2.7. 对中国互联网用户数据的分析与可视化 269 | 270 | 最后,我们定义了一个名为 `chinese_users_analysis` 的函数,用于对中国互联网用户数据进行分析和可视化。首先通过切片获取中国互联网用户信息。然后使用 matplotlib 和 seaborn 库绘制了各项指标的数值图和增长率图,并利用 sklearn 库中的多元线性回归模型预测中国互联网到 2050 年的总用户数。 271 | 272 | ```python 273 | # 基本信息的折线图 274 | sns.lineplot(data=chinese_users, x='Year', y='No. of Internet Users', label='数量(单位:千万人)', lw=3) 275 | sns.lineplot(data=chinese_users, x='Year', y='Internet Users(%)', label='占人口的比例', lw=3) 276 | sns.lineplot(data=chinese_users, x='Year', y='Cellular Subscription', label='移动互联网订阅每一百人比例', lw=3) 277 | sns.lineplot(data=chinese_users, x='Year', y='Broadband Subscription', label='宽带每一百人订阅比例', lw=3) 278 | ``` 279 | 280 | 281 | 282 | ![中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例](img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例.png) 283 | 284 | 从图中可以看出: 285 | 286 | - 中国互联网用户数量呈现出一个快速增长的趋势,尤其是在 2000 年之后,增长速度更加明显。这说明中国在互联网领域有着巨大的市场规模和潜力,以及中国在互联网技术、应用、服务等方面有着较强的竞争力和影响力。 287 | 288 | - 中国互联网用户占人口比例也呈现出一个快速增长的趋势,尤其是在 2005 年之后,增长速度更加明显。这说明中国在互联网领域有着较高的普及率和接入率,以及中国在互联网领域的需求和依赖都在不断增加。 289 | 290 | - 中国移动互联网订阅每一百人比例也呈现出一个快速增长的趋势,尤其是在 2005 年之后,增长速度更加明显。这说明中国在移动设备领域有着较高的普及率和便捷程度,以及中国在移动互联网领域的需求和偏好都在不断增加。 291 | 292 | - 中国宽带每一百人订阅比例呈现出一个缓慢增长的趋势,但在 2017 年之后,增长速度有所放缓。这说明中国在宽带网络领域还有一定的空间和潜力,以及中国在宽带网络领域的竞争力和吸引力可能受到了移动网络的影响。 293 | 294 | ```python 295 | # 计算各项增长率 296 | rows = len(chinese_users.index) 297 | for i in range(rows - 1): 298 | chinese_users.loc[:, 'increase of No. of Internet Users'].iloc[i + 1] = 0 if chinese_users.iloc[i]['No. of Internet Users'] == 0 else (chinese_users.iloc[i + 1].loc['No. of Internet Users'] - chinese_users.iloc[i]['No. of Internet Users']) / chinese_users.iloc[i]['No. of Internet Users'] 299 | ······ 300 | 301 | # 绘制图形 302 | sns.lineplot(data=chinese_users, x='Year', y='increase of No. of Internet Users', lw=4, 303 | label='数量(单位:千万人)增长率') 304 | ······ 305 | ``` 306 | 307 | 308 | 309 | ![中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例的增长率](img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例的增长率.png) 310 | 311 | 从图中可以看出: 312 | 313 | - 中国互联网用户数量增长率和占人口比例增长率基本为同比增长,在1993-2005年之间增长较快之后趋于平稳,这说明中国在互联网领域已经达到了一个较高的发展水平,中国在互联网正在稳步发展。 314 | 315 | - 中国移动互联网订阅每一百人比例增长率呈现在1987-2005年之间增长较快之后趋于平稳,尤其是在 2010 年之后,这说明中国在移动设备领域已经达到了一个较高的普及率和便捷程度,以及中国在移动互联网领域已形成良好发展趋势。 316 | 317 | - 中国宽带每一百人订阅比例增长率在2000-2002年飞速增长之后又下降并趋于平稳。这说明中国在宽带网络领域有一定的空间和潜力,以及中国在宽带网络领域有着较强的竞争力和吸引力。 318 | 319 | ```python 320 | # 散点图 321 | sns.scatterplot(data=chinese_users, x='Year', y='No. of Internet Users') 322 | 323 | # 三元线性回归拟合 324 | poly_reg = PolynomialFeatures(degree=3) 325 | ······ 326 | model_2.fit(x_m, chinese_users[['No. of Internet Users']]) 327 | data = pd.DataFrame({'x': x['Year'], 'pred_y': [x[0] for x in model_2.predict(x_m)]}) 328 | 329 | # 绘制折线图 330 | sns.lineplot(data=data, x='x', y='pred_y') 331 | ``` 332 | 333 | 334 | 335 | ![对1980到2020年中国互联网总用户数的拟合](img/对1980到2020年中国互联网总用户数的拟合.png) 336 | 337 | 从图中可以看出: 338 | 339 | - 我们使用 sklearn 库中的多元线性回归模型对 1980 到 2020 年中国互联网总用户数进行了拟合,得到了一个拟合曲线。这个曲线可以用来描述中国互联网总用户数随时间的变化规律,以及评估拟合效果和意义。 340 | 341 | ```python 342 | # 预测 343 | pred_x = pd.DataFrame(np.arange(1980, 2031), columns=['Year']) 344 | pred_x_m = poly_reg.fit_transform(pred_x) 345 | 346 | # 绘图 347 | plt.plot(pred_x, model_2.predict(pred_x_m)) 348 | ``` 349 | 350 | 351 | 352 | ![到2030年中国互联网总用户数的预测](img/到2030年中国互联网总用户数的预测.png) 353 | 354 | 从图中可以看出: 355 | 356 | - 到2030年中国互联网总用户数将达到21.1亿,此预测具有一定的合理性,因为中国互联网用户的各项指标都在飞速增长,中国互联网具有很大的发展潜力与发展活力。 357 | - 但是此预测也具有一定的局限性,这个预测仅仅只用了一个数据集,没有考虑中国具体国情,仅仅依托数量的线性增长来分析是不合理的,需要更加高级的模型,并且要兼顾中国人口老龄化问题,结合中国人口增长速度来进一步分析,这样的预测效果会更好。 358 | 359 | 360 | 361 | -------------------------------------------------------------------------------- /code/main.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | from matplotlib import pyplot as plt 4 | import seaborn as sns 5 | from wordcloud import STOPWORDS, WordCloud 6 | from sklearn import linear_model 7 | from sklearn.preprocessing import PolynomialFeatures 8 | 9 | 10 | # 环境和参数的配置 11 | def set_seaborn_properties(context='talk', font_scale=0.8): 12 | sns.set_theme(context=context, font='STXIHEI', font_scale=font_scale, 13 | rc={'axes.unicode_minus': False, 14 | 'figure.figsize': (12, 8), 15 | 'figure.dpi': 150}) 16 | 17 | 18 | # 获取2020年国家地区数据封装成的DataFrame 19 | def get_2020_entities_dataframe(): 20 | entity_group = global_users.groupby('Entity') 21 | entity_2020_df = pd.DataFrame() 22 | for entity, entity_df in entity_group: 23 | if entity == 'World': 24 | continue 25 | entity_2020 = entity_df[entity_df['Year'] == 2020] 26 | entity_2020_df = pd.concat([entity_2020_df, entity_2020], join='outer', axis=0) 27 | return entity_2020_df.set_index('Entity') 28 | 29 | 30 | # 全球用户每年的各项数据的分析与可视化 31 | def global_internet_users_analysis(): 32 | set_seaborn_properties() 33 | # 全球每年的互联网用户总数分析与可视化: 34 | plt.subplots_adjust(hspace=1, wspace=0.7) 35 | year_groups = global_users.groupby('Year') 36 | internet_users_groups = year_groups['No. of Internet Users'] 37 | column_mean = internet_users_groups.sum() 38 | internet_users_sum_data = pd.DataFrame({'Year': column_mean.index, 39 | 'sum': column_mean.values}) 40 | plt.subplot(2, 2, 1) 41 | plt.title('全球每年互联网用户总数') 42 | plt.xlabel('年份') 43 | plt.ylabel('全球每年互联网用户总数') 44 | sns.lineplot(data=internet_users_sum_data, x='Year', y='sum') 45 | plt.bar(column_mean.index, column_mean.values, color='cornflowerblue', width=0.6) 46 | 47 | # 全球每年每100人移动端互联网订阅数、互联网使用人数比例、每100人宽带订阅数的平均值分析与可视化: 48 | year_groups = global_users.groupby('Year') 49 | title_mapper = {'Cellular Subscription': '全球每年每100人移动互联网订阅数', 50 | 'Internet Users(%)': '全球每年互联网使用人数比例', 51 | 'Broadband Subscription': '全球每年每100人宽带订阅数平均值'} 52 | i = 2 53 | for column in ['Cellular Subscription', 'Internet Users(%)', 'Broadband Subscription']: 54 | plt.subplot(2, 2, i) 55 | i += 1 56 | internet_users_groups = year_groups[column] 57 | column_mean = internet_users_groups.mean() 58 | column_max = internet_users_groups.max() 59 | max_data = pd.DataFrame({'Year': column_max.index, 'max': column_max.values}) 60 | mean_data = pd.DataFrame({'Year': column_mean.index, 'mean': column_mean.values}) 61 | plt.title(title_mapper[column]) 62 | plt.xlabel('年份') 63 | plt.ylabel(title_mapper[column]) 64 | sns.lineplot(data=max_data, x='Year', y='max', label=column + ' max', lw=2, linestyle=(0, (5, 1))) 65 | sns.lineplot(data=mean_data, x='Year', y='mean', label=column + ' mean', lw=3, linestyle=(0, (1, 1))) 66 | plt.legend(loc='upper left', prop={'size': 8.5}) 67 | plt.savefig('../img/全球用户每年的各项数据的分析与可视化.png') 68 | plt.show() 69 | 70 | 71 | # 2020年各个国家地区的用户占比饼图和柱状图绘制 72 | def entities_2020_internet_users_percentage_pie_bar(): 73 | entity_2020_df = get_2020_entities_dataframe() 74 | entity_2020_df['No. of Internet Users'] /= entity_2020_df['No. of Internet Users'].sum() 75 | 76 | # 只筛选用户数量最多的10组数据,其他数据用`other`代替 77 | entity_2020_df.sort_values(by='No. of Internet Users', axis=0, ascending=False, inplace=True) 78 | other = entity_2020_df.iloc[10:].loc[:, 'No. of Internet Users'].sum() 79 | other_df = pd.DataFrame(data={'': {'Entity': 'Other', 'No. of Internet Users': other}}).T 80 | other_df.set_index('Entity', inplace=True) 81 | processed_data = pd.concat([entity_2020_df.head(10), other_df], axis=0, join='outer') 82 | 83 | # 绘制饼图 84 | set_seaborn_properties(context='notebook', font_scale=0.8) 85 | explode_arr = np.zeros(shape=(11)) 86 | explode_arr[0] = 0.07 87 | plt.axes(aspect=1) 88 | plt.title('2020年各个国家地区的互联网用户占比') 89 | plt.pie(processed_data['No. of Internet Users'], labels=processed_data.index, explode=explode_arr, 90 | labeldistance=1.1, autopct='%2.1f%%', pctdistance=0.9, shadow=True) 91 | plt.legend(loc='lower right', bbox_to_anchor=(0.5, 0., 0.95, 0.5), ncols=2) 92 | plt.savefig('../img/2020年各个国家地区的互联网用户占比饼图.png') 93 | plt.show() 94 | 95 | # 绘制柱状图 96 | set_seaborn_properties(font_scale=0.56) 97 | plt.rcParams['figure.dpi'] = 300 98 | data = pd.DataFrame({'Entity': processed_data.index, 'Percent': processed_data['No. of Internet Users']}) 99 | plt.title('2020年各个国家地区的互联网用户占比') 100 | sns.barplot(data=data, x='Entity', y='Percent') 101 | plt.savefig('../img/2020年各个国家地区的互联网用户占比柱状图.png') 102 | plt.show() 103 | 104 | 105 | # 2020年各国家地区互联网用户占比分布直方图 106 | def entities_2020_internet_users_percentage_distribution_histogram(): 107 | set_seaborn_properties(font_scale=0.8) 108 | entity_2020_df = get_2020_entities_dataframe() 109 | internet_users_percentage_sr = entity_2020_df['Internet Users(%)'] 110 | plt.title('2020年各国家地区互联网用户占比分布直方图') 111 | plt.xlabel('互联网用户占比占比') 112 | plt.ylabel('国家地区数量') 113 | data = pd.DataFrame({'Entity': internet_users_percentage_sr.index, 'Percent': internet_users_percentage_sr.values}) 114 | sns.histplot(data, x='Percent') 115 | plt.savefig('../img/2020年各国家地区互联网用户占比分布直方图.png') 116 | plt.show() 117 | 118 | 119 | # 2020年个国家地区互联网用户占比和移动互联网订阅量的散点图 120 | def entities_2020_internet_users_percentage_distribution_scatter(): 121 | set_seaborn_properties() 122 | entity_2020_df = get_2020_entities_dataframe() 123 | plt.title('2020年个国家地区互联网用户占比和移动互联网订阅量散点图') 124 | plt.xlabel('互联网用户占比占比') 125 | plt.ylabel('移动互联网订阅量') 126 | sns.scatterplot(data=entity_2020_df, x='Internet Users(%)', y='Cellular Subscription', 127 | palette='husl', hue='Entity', legend=None) 128 | 129 | # 利用线性回归分析两者关系 130 | x = entity_2020_df[['Internet Users(%)']] 131 | model_1 = linear_model.LinearRegression() 132 | model_1.fit(x, entity_2020_df[['Cellular Subscription']]) 133 | data = pd.DataFrame({'x': x['Internet Users(%)'], 'pred_y': [x[0] for x in model_1.predict(x)]}) 134 | sns.lineplot(data=data, x='x', y='pred_y') 135 | plt.savefig('../img/2020年个国家地区互联网用户占比和移动互联网订阅量散点图及线性回归拟合.png') 136 | plt.show() 137 | 138 | 139 | # 用每一年互联网用户的比例最大的三个国家地区名生成词云 140 | def draw_internet_users_percentage_annual_top_3_wordcloud(): 141 | text = '' 142 | year_groups = global_users.groupby('Year') 143 | # 获取每一年互联网用户的比例最大的三个国家地区名数据 144 | for year, year_df in year_groups: 145 | year_df.sort_values(by='Internet Users(%)', ascending=False, inplace=True) 146 | top_3 = year_df.head(3) 147 | entities = top_3['Entity'] 148 | for entity in entities: 149 | if len(entity.split()) > 1: 150 | text += entity.replace(' ', '_') + ' ' 151 | # 将名字中含有空格的国家地区名中的空格替换成下划线_,避免一个名字被拆分成多个单词 152 | else: 153 | text += entity + ' ' 154 | wc = WordCloud(max_words=100, width=800, height=400, background_color='White', 155 | max_font_size=150, stopwords=STOPWORDS, margin=5, scale=1.5) 156 | wc.generate(text) 157 | plt.title('每年互联网用户的比例最大的国家地区名词云') 158 | plt.imshow(wc) 159 | plt.axis("off") 160 | wc.to_file('../img/每年互联网用户的比例最大的国家地区名词云.png') 161 | plt.show() 162 | 163 | 164 | # 对中国互联网用户数据的分析与可视化 165 | def chinese_users_analysis(): 166 | # 绘制各项指标的数值图 167 | set_seaborn_properties() 168 | pd.options.mode.chained_assignment = None 169 | plt.title('中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例') 170 | plt.xlabel('年份') 171 | plt.ylabel('数值') 172 | chinese_users.loc[:, 'No. of Internet Users'] /= 10000000 173 | sns.lineplot(data=chinese_users, x='Year', y='No. of Internet Users', label='数量(单位:千万人)', lw=3) 174 | sns.lineplot(data=chinese_users, x='Year', y='Internet Users(%)', label='占人口的比例', lw=3) 175 | sns.lineplot(data=chinese_users, x='Year', y='Cellular Subscription', label='移动互联网订阅每一百人比例', lw=3) 176 | sns.lineplot(data=chinese_users, x='Year', y='Broadband Subscription', label='宽带每一百人订阅比例', lw=3) 177 | plt.legend(loc='upper left') 178 | plt.savefig('../img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例.png') 179 | plt.show() 180 | 181 | # 绘制各项指标的增长率图 182 | set_seaborn_properties() 183 | chinese_users.loc[:, 'increase of No. of Internet Users'] = 0 184 | chinese_users.loc[:, 'increase of Internet Users(%)'] = 0 185 | chinese_users.loc[:, 'increase of Cellular Subscription'] = 0 186 | chinese_users.loc[:, 'increase of Broadband Subscription'] = 0 187 | rows = len(chinese_users.index) 188 | for i in range(rows - 1): 189 | chinese_users.loc[:, 'increase of No. of Internet Users'].iloc[i + 1] = 0 if chinese_users.iloc[i]['No. of Internet Users'] == 0 else (chinese_users.iloc[i + 1].loc['No. of Internet Users'] - chinese_users.iloc[i]['No. of Internet Users']) / chinese_users.iloc[i]['No. of Internet Users'] 190 | chinese_users.loc[:, 'increase of Internet Users(%)'].iloc[i + 1] = 0 if chinese_users.iloc[i]['Internet Users(%)'] == 0 else (chinese_users.iloc[i + 1]['Internet Users(%)'] - chinese_users.iloc[i]['Internet Users(%)']) / chinese_users.iloc[i]['Internet Users(%)'] 191 | chinese_users.loc[:, 'increase of Cellular Subscription'].iloc[i + 1] = 0 if chinese_users.iloc[i]['Cellular Subscription'] == 0 else (chinese_users.iloc[i + 1]['Cellular Subscription'] - chinese_users.iloc[i]['Cellular Subscription']) / chinese_users.iloc[i]['Cellular Subscription'] 192 | chinese_users.loc[:, 'increase of Broadband Subscription'].iloc[i + 1] = 0 if chinese_users.iloc[i]['Broadband Subscription'] == 0 else (chinese_users.iloc[i + 1]['Broadband Subscription'] - chinese_users.iloc[i]['Broadband Subscription']) / chinese_users.iloc[i]['Broadband Subscription'] 193 | plt.title('中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例的增长率') 194 | plt.xlabel('年份') 195 | plt.ylabel('数值') 196 | sns.lineplot(data=chinese_users, x='Year', y='increase of No. of Internet Users', lw=4, 197 | label='数量(单位:千万人)增长率') 198 | sns.lineplot(data=chinese_users, x='Year', y='increase of Internet Users(%)', lw=4, 199 | label='占人口的比例') 200 | sns.lineplot(data=chinese_users, x='Year', y='increase of Cellular Subscription', lw=4, 201 | label='移动互联网订阅每一百人比例增长率') 202 | sns.lineplot(data=chinese_users, x='Year', y='increase of Broadband Subscription', lw=4, 203 | label='宽带每一百人订阅比例增长率') 204 | plt.legend(loc='upper left') 205 | plt.savefig('../img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例的增长率.png') 206 | plt.show() 207 | 208 | # 利用多元线性回归预测中国互联网到2050年的总用户数 209 | # 拟合: 210 | set_seaborn_properties() 211 | plt.title('对1980到2020年中国互联网总用户数的拟合') 212 | sns.scatterplot(data=chinese_users, x='Year', y='No. of Internet Users') 213 | poly_reg = PolynomialFeatures(degree=3) 214 | x = chinese_users[['Year']] 215 | x_m = poly_reg.fit_transform(x) 216 | 217 | model_2 = linear_model.LinearRegression() 218 | model_2.fit(x_m, chinese_users[['No. of Internet Users']]) 219 | data = pd.DataFrame({'x': x['Year'], 'pred_y': [x[0] for x in model_2.predict(x_m)]}) 220 | plt.xlabel('年份') 221 | plt.ylabel('人数(单位:千万人)') 222 | sns.lineplot(data=data, x='x', y='pred_y') 223 | plt.savefig('../img/对1980到2020年中国互联网总用户数的拟合') 224 | plt.show() 225 | 226 | # 预测: 227 | set_seaborn_properties() 228 | plt.title('到2030年中国互联网总用户数的预测') 229 | plt.xlabel('年份') 230 | plt.ylabel('人数(单位:千万人)') 231 | pred_x = pd.DataFrame(np.arange(1980, 2031), columns=['Year']) 232 | pred_x_m = poly_reg.fit_transform(pred_x) 233 | plt.plot(pred_x, model_2.predict(pred_x_m)) 234 | plt.savefig('../img/到2030年中国互联网总用户数的预测') 235 | plt.show() 236 | 237 | 238 | if __name__ == '__main__': 239 | # 读取文件,获取全球互联网用户信息 240 | global_users = pd.read_csv('../data/Final.csv', delimiter=',', usecols=range(1, 8)) # 由于第一列的列名未知,所以不使用第一列 241 | # 对全球用户进行分析: 242 | global_internet_users_analysis() 243 | entities_2020_internet_users_percentage_pie_bar() 244 | entities_2020_internet_users_percentage_distribution_histogram() 245 | entities_2020_internet_users_percentage_distribution_scatter() 246 | draw_internet_users_percentage_annual_top_3_wordcloud() 247 | 248 | # 通过切片获取中国互联网用户信息 249 | chinese_users = global_users.loc[global_users['Entity'] == 'China'] 250 | chinese_users_analysis() 251 | -------------------------------------------------------------------------------- /img/2020年个国家地区互联网用户占比和移动互联网订阅量散点图及线性回归拟合.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/2020年个国家地区互联网用户占比和移动互联网订阅量散点图及线性回归拟合.png -------------------------------------------------------------------------------- /img/2020年各个国家地区的互联网用户占比柱状图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/2020年各个国家地区的互联网用户占比柱状图.png -------------------------------------------------------------------------------- /img/2020年各个国家地区的互联网用户占比饼图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/2020年各个国家地区的互联网用户占比饼图.png -------------------------------------------------------------------------------- /img/2020年各国家地区互联网用户占比分布直方图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/2020年各国家地区互联网用户占比分布直方图.png -------------------------------------------------------------------------------- /img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例.png -------------------------------------------------------------------------------- /img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例的增长率.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/中国互联网用户的数量(单位:千万人)、占人口的比例、移动互联网订阅每一百人比例、宽带每一百人订阅比例的增长率.png -------------------------------------------------------------------------------- /img/全球用户每年的各项数据的分析与可视化.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/全球用户每年的各项数据的分析与可视化.png -------------------------------------------------------------------------------- /img/到2030年中国互联网总用户数的预测.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/到2030年中国互联网总用户数的预测.png -------------------------------------------------------------------------------- /img/对1980到2020年中国互联网总用户数的拟合.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/对1980到2020年中国互联网总用户数的拟合.png -------------------------------------------------------------------------------- /img/每年互联网用户的比例最大的国家地区名词云.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Racgeon/Whut-Python/440c11aa6c34f97faa6d4856336de924bbe7bb84/img/每年互联网用户的比例最大的国家地区名词云.png --------------------------------------------------------------------------------