搜索
首页后端开发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
什么是Python Switch语句?什么是Python Switch语句?Apr 30, 2025 pm 02:08 PM

本文讨论了Python版本3.10中介绍的新“匹配”语句,该语句与其他语言相同。它增强了代码的可读性,并为传统的if-elif-el提供了性能优势

Python中有什么例外组?Python中有什么例外组?Apr 30, 2025 pm 02:07 PM

Python 3.11中的异常组允许同时处理多个异常,从而改善了并发场景和复杂操作中的错误管理。

Python中的功能注释是什么?Python中的功能注释是什么?Apr 30, 2025 pm 02:06 PM

Python中的功能注释将元数据添加到函数中,以进行类型检查,文档和IDE支持。它们增强了代码的可读性,维护,并且在API开发,数据科学和图书馆创建中至关重要。

Python的单位测试是什么?Python的单位测试是什么?Apr 30, 2025 pm 02:05 PM

本文讨论了Python中的单位测试,其好处以及如何有效编写它们。它突出显示了诸如UNITSEST和PYTEST等工具进行测试。

Python中的访问说明符是什么?Python中的访问说明符是什么?Apr 30, 2025 pm 02:03 PM

文章讨论了Python中的访问说明符,这些说明符使用命名惯例表明班级成员的可见性,而不是严格的执法。

Python中的__Init __()是什么?自我如何在其中发挥作用?Python中的__Init __()是什么?自我如何在其中发挥作用?Apr 30, 2025 pm 02:02 PM

文章讨论了Python的\ _ \ _ Init \ _ \ _()方法和Self在初始化对象属性中的作用。还涵盖了其他类方法和继承对\ _ \ _ Init \ _ \ _()的影响。

python中的@classmethod,@staticmethod和实例方法有什么区别?python中的@classmethod,@staticmethod和实例方法有什么区别?Apr 30, 2025 pm 02:01 PM

本文讨论了python中@classmethod,@staticmethod和实例方法之间的差异,详细介绍了它们的属性,用例和好处。它说明了如何根据所需功能选择正确的方法类型和DA

您如何将元素附加到Python数组?您如何将元素附加到Python数组?Apr 30, 2025 am 12:19 AM

Inpython,YouAppendElementStoAlistusingTheAppend()方法。1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware

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脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具