搜索
首页后端开发Python教程电影数据集探索和可视化

简介

熟能生巧

这与数据科学家有很多共同点。理论只是等式的一个方面;最关键的是将理论付诸实践。我将努力记录今天开发我的顶点项目的整个过程,其中将涉及研究电影数据集。

这些是目标:
目标:

  1. 从 Kaggle 下载电影数据集或使用 TMDb API 检索它。
  2. 探索电影类型、收视率、导演受欢迎程度和发行年份趋势等各个方面。
  3. 创建仪表板来可视化这些趋势,并有选择地根据用户偏好推荐电影。

1。资料收集
我决定使用 Kaggle 来查找我的数据集。记住您正在使用的数据集所需的关键变量至关重要。重要的是,我的数据集应该包括以下内容:发行年份的趋势、导演的受欢迎程度、收视率和电影类型。因此,我必须确保我选择的数据集至少具有以下内容。
我的数据集位于 Kaggle 上,我将提供下面的链接。您可以通过下载数据集、解压并提取来获取该文件的 CSV 版本。您可以查看它以了解您已经拥有的内容,并真正了解您希望从将要检查的数据中获得什么样的见解。

2。描述数据

首先,我们必须导入所需的库并加载必要的数据。我在我的项目中使用 Python 编程语言和 Jupyter Notebooks,以便我可以更高效地编写和查看代码。
您将导入我们将使用的库并加载数据,如下所示。

Movie Dataset Exploration and Visualization

然后我们将运行以下命令来获取有关我们的数据集的更多详细信息。

data.head() # dispalys the first rows of the dataset.
data.tail() # displays the last rows of the dataset.
data.shape # Shows the total number of rows and columns.
len(data.columns)  # Shows the total number of columns.
data.columns # Describes different column names.
data.dtypes # Describes different data types.


我们现在知道数据集包含什么以及在获得我们需要的所有描述后我们希望提取的见解。示例:使用我的数据集,我希望调查导演受欢迎程度、收视率分布和电影类型的模式。我还想根据用户选择的偏好推荐电影,例如喜欢的导演和类型。

3。数据清理

此阶段涉及查找任何空值并将其删除。为了继续进行数据可视化,我们还将检查数据集是否有重复项,并删除我们发现的任何内容。为此,我们将运行以下代码:

1. data['show_id'].value_counts().sum() # Checks for the total number of rows in my dataset
2. data.isna().sum() # Checks for null values(I found null values in director, cast and country columns)
3. data[['director', 'cast', 'country']] = data[['director', 'cast', 'country']].replace(np.nan, "Unknown ") # Fill null values with unknown.

然后我们将删除具有未知值的行并确认我们已删除所有这些行。我们还将检查已清理数据的剩余行数。

Movie Dataset Exploration and Visualization

下面的代码查找独特的特征和重复项。尽管我的数据集中没有重复项,但您可能仍然需要使用它,以防将来的数据集出现重复项。

data.duplicated().sum() # Checks for duplicates
data.nunique() # Checks for unique features
data.info # Confirms if nan values are present and also shows datatypes.

我的日期/时间数据类型是一个对象,我希望它采用正确的日期/时间格式,所以我使用了
data['date_added']=data['date_added'].astype('datetime64[ms]')将其转换为正确的格式。

4。数据可视化

  • 我的数据集有两种类型的变量,即类型中的电视节目和电影,我使用条形图来呈现分类数据及其代表的值。
    Movie Dataset Exploration and Visualization

  • 我也用饼图来表示,和上面一样。使用的代码如下,预期结果如下所示。

## Pie chart display
plt.figure(figsize=(8, 8))  
data['type'].value_counts().plot(
    kind='pie', 
    autopct='%1.1f%%',  
    colors=['skyblue', 'lightgreen'], 
    startangle=90, 
    explode=(0.05, 0) 
)
plt.title('Distribution of Content Types (Movies vs. TV Shows)')
plt.ylabel('')
plt.show()

Movie Dataset Exploration and Visualization

  • 然后,我使用 pd.crosstab(data.type, data.country) 进行了表格比较,以根据发布日期、国家/地区和其他因素创建类型的表格比较(您可以尝试更改代码中的列)独立)。以下是要使用的代码和预期的比较。我还检查了电视节目制作方面领先的前 20 个国家,并将它们以条形图可视化。您可以复制图像中的代码,确保结果与我的几乎相似。

Movie Dataset Exploration and Visualization

Movie Dataset Exploration and Visualization

  • I then checked for the top 10 movie genre as shown below. You can also use the code to check for TV shows. Just substitute with proper variable names.

Movie Dataset Exploration and Visualization

  • I extracted months and years separately from the dates provided so that I could visualize some histogram plots over the years.

Movie Dataset Exploration and Visualization

Movie Dataset Exploration and Visualization

Movie Dataset Exploration and Visualization

  • Checked for the top 10 directors with the most movies and compared them using a bar graph.

Movie Dataset Exploration and Visualization

  • Checked for the cast with the highest rating and visualized them.

Movie Dataset Exploration and Visualization

5. Recommendation System

I then built a recommendation system that takes in genre or director's name as input and produces a list of movies as per the user's preference. If the input cannot be matched by the algorithm then the user is notified.

Movie Dataset Exploration and Visualization

The code for the above is as follows:

def recommend_movies(genre=None, director=None):
    recommendations = data
    if genre:
        recommendations = recommendations[recommendations['listed_in'].str.contains(genre, case=False, na=False)]
    if director:
        recommendations = recommendations[recommendations['director'].str.contains(director, case=False, na=False)]
    if not recommendations.empty:
        return recommendations[['title', 'director', 'listed_in', 'release_year', 'rating']].head(10)
    else:
        return "No movies found matching your preferences."
print("Welcome to the Movie Recommendation System!")
print("You can filter movies by Genre or Director (or both).")
user_genre = input("Enter your preferred genre (or press Enter to skip): ")
user_director = input("Enter your preferred director (or press Enter to skip): ")
recommendations = recommend_movies(genre=user_genre, director=user_director)
print("\nRecommended Movies:")
print(recommendations)

Conclusion

My goals were achieved, and I had a great time taking on this challenge since it helped me realize that, even though learning is a process, there are days when I succeed and fail. This was definitely a success. Here, we celebrate victories as well as defeats since, in the end, each teach us something. Do let me know if you attempt this.
Till next time!

Note!!
The code is in my GitHub:
https://github.com/MichelleNjeri-scientist/Movie-Dataset-Exploration-and-Visualization

The Kaggle dataset is:
https://www.kaggle.com/datasets/shivamb/netflix-shows

以上是电影数据集探索和可视化的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何解决Linux终端中查看Python版本时遇到的权限问题?如何解决Linux终端中查看Python版本时遇到的权限问题?Apr 01, 2025 pm 05:09 PM

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

我如何使用美丽的汤来解析HTML?我如何使用美丽的汤来解析HTML?Mar 10, 2025 pm 06:54 PM

本文解释了如何使用美丽的汤库来解析html。 它详细介绍了常见方法,例如find(),find_all(),select()和get_text(),以用于数据提取,处理不同的HTML结构和错误以及替代方案(SEL)

Python中的数学模块:统计Python中的数学模块:统计Mar 09, 2025 am 11:40 AM

Python的statistics模块提供强大的数据统计分析功能,帮助我们快速理解数据整体特征,例如生物统计学和商业分析等领域。无需逐个查看数据点,只需查看均值或方差等统计量,即可发现原始数据中可能被忽略的趋势和特征,并更轻松、有效地比较大型数据集。 本教程将介绍如何计算平均值和衡量数据集的离散程度。除非另有说明,本模块中的所有函数都支持使用mean()函数计算平均值,而非简单的求和平均。 也可使用浮点数。 import random import statistics from fracti

如何使用TensorFlow或Pytorch进行深度学习?如何使用TensorFlow或Pytorch进行深度学习?Mar 10, 2025 pm 06:52 PM

本文比较了Tensorflow和Pytorch的深度学习。 它详细介绍了所涉及的步骤:数据准备,模型构建,培训,评估和部署。 框架之间的关键差异,特别是关于计算刻度的

哪些流行的Python库及其用途?哪些流行的Python库及其用途?Mar 21, 2025 pm 06:46 PM

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

如何使用Python创建命令行接口(CLI)?如何使用Python创建命令行接口(CLI)?Mar 10, 2025 pm 06:48 PM

本文指导Python开发人员构建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等库详细介绍,强调输入/输出处理,并促进用户友好的设计模式,以提高CLI可用性。

在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中?在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中?Apr 01, 2025 pm 11:15 PM

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

解释Python中虚拟环境的目的。解释Python中虚拟环境的目的。Mar 19, 2025 pm 02:27 PM

文章讨论了虚拟环境在Python中的作用,重点是管理项目依赖性并避免冲突。它详细介绍了他们在改善项目管理和减少依赖问题方面的创建,激活和利益。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)