左手python右手R

写在前面

最近在学习python,结合一个实际案例,写一下python和R在做数据分析上的差异。
本人还不是特别熟练python,所以python的代码来自于kagle的一个高vote回帖。
我这里只是转写一下R的版本,转写python代码之后感觉python做数据分析和可视化实在不如R给力。代码丢这了,有机会说说如何用tidyverse分析数据吧。
这里写了多数代码,剩下流程差不多的就放弃写了。还有机器学习的部分回头有心情了用tidymodels写一下基本的框架吧。


Netflix is an application that keeps growing bigger and faster with its popularity, shows and content. This is an EDA
or a story telling through its data along with a content-based recommendation system and a wide range of different
graphs and visuals.

image.png

The python source code is from here

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)import seaborn as sns
import matplotlib.pyplot as plt
import seaborn as sns
library(tidyverse)
library(skimr)
# Loading the dataset
data <- tidytuesdayR::tt_load('2021-04-20')
netfix_dta <- data$netflix_titles
# install a module if your python don't have
# reticulate::py_install('seaborn',pip = TRUE) 

Pass the data to Python from R in rstudio


netflix_overall=r.netfix_dta
netflix_overall.head()

Also, you can do the same thing using R

head(netfix_dta)

Or

glimpse(netfix_dta)

Therefore, it is clear that the dataset contains 12 columns for exploratory analysis.

netflix_overall.count()

Also, in R you can do it better.

skim(netfix_dta)

netflix_shows=netflix_overall[netflix_overall['type']=='TV Show']
netflix_shows.head()

In R, you can use pipe to repeat, which makes your script easy to read.

netflix_shows <- netfix_dta %>%
  filter(type == "TV Show")

head(netflix_shows)

netflix_movies=netflix_overall[netflix_overall['type']=='Movie']
netflix_movies <- netfix_dta %>%
  filter(type == "Movie")

Analysis of Movies vs TV Shows.


sns.set(style="darkgrid") 
ax = sns.countplot(x="type", data=netflix_overall, palette="Set2")
plt.show()

In R

netfix_dta %>% 
  ggplot(aes(x = fct_rev(type), fill = type)) + 
  geom_bar() + 
  theme_bw()

It is evident that there are more Movies on Netflix than TV shows.

```{python} md

# If a producer wants to release some content, which month must he do so?( Month when least amount of content is added)

```{python}
netflix_date = netflix_shows[['date_added']].dropna()
netflix_date['year'] = netflix_date['date_added'].apply(lambda x : x.split(', ')[-1])
netflix_date['month'] = netflix_date['date_added'].apply(lambda x : x.lstrip().split(' ')[0])

month_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][::-1]
df = netflix_date.groupby('year')['month'].value_counts().unstack().fillna(0)[month_order].T
plt.figure(figsize=(10, 7), dpi=200)
plt.pcolor(df, cmap='afmhot_r', edgecolors='white', linewidths=2) # heatmap
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns, fontsize=7, fontfamily='serif')
plt.yticks(np.arange(0.5, len(df.index), 1), df.index, fontsize=7, fontfamily='serif')

plt.title('Netflix Contents Update', fontsize=12, fontfamily='calibri', fontweight='bold', position=(0.20, 1.0+0.02))
cbar = plt.colorbar()

cbar.ax.tick_params(labelsize=8) 
cbar.ax.minorticks_on()
plt.show()
library(lubridate)
library(viridis)

netfix_dta %>% 
  select(date_added) %>% 
  mutate(date_added = mdy(date_added),
         month = month(date_added, label = TRUE, abbr = FALSE),
         year = year(date_added)) %>% 
  group_by(year, month) %>% 
  filter(!is.na(month)) %>% 
  summarise(contents = n()) %>% 
  ggplot(aes(x = year, y = fct_rev(month), fill = contents)) + 
  geom_tile() + 
  viridis::scale_fill_viridis(option = "A") + 
  labs(title = 'Netflix Contents Update',
       x = '',
       y = '')

Movie ratings analysis

plt.figure(figsize=(12,10))
sns.set(style="darkgrid")
ax = sns.countplot(x="rating", data=netflix_movies, palette="Set2", order=netflix_movies['rating'].value_counts().index[0:15])
plt.show()

In R

netfix_dta %>% 
  group_by(rating) %>% 
  summarise(n = n()) %>% 
  filter(!is.na(rating)) %>% 
  ggplot(aes(x = fct_reorder(rating,n, .desc = TRUE), y = n, fill = rating)) + 
  geom_bar(stat = "identity", show.legend = F) + 
  scale_y_continuous(expand = expansion(c(0,.1))) + 
  labs(
    x = 'Rating',
    y = 'Count'
  )

Analysing IMDB ratings to get top rated movies on Netflix

imdb_ratings=pd.read_csv('netflix/IMDb ratings.csv',usecols=['weighted_average_vote'])

imdb_titles=pd.read_csv('netflix/IMDb movies.csv', usecols=['title','year','genre'])

ratings = pd.DataFrame({'Title':imdb_titles.title, 'Release Year':imdb_titles.year, 'Rating': imdb_ratings.weighted_average_vote, 'Genre':imdb_titles.genre})
ratings.drop_duplicates(subset=['Title','Release Year','Rating'], inplace=True)
ratings.shape

ratings.head()

In R

imdb_ratings <- read_csv('netflix/IMDb ratings.csv') %>% 
  select(1,2)
imdb_titles <- read_csv('netflix/IMDb movies.csv') %>% 
  select(1, title, year, genre)

ratings <- left_join(imdb_titles, imdb_ratings, by = "imdb_title_id") %>% 
  select(-1) %>% 
  select(1:3,Rating = "weighted_average_vote")
ratings
dim(ratings)
ratings.dropna()
joint_data=ratings.merge(netflix_overall,left_on='Title',right_on='title',how='inner')
joint_data=joint_data.sort_values(by='Rating', ascending=False)

joint_data.head()
joint_data.shape
joint_data <- ratings %>% 
  filter(!is.na(.)) %>% 
  inner_join(., netfix_dta, by = "title") %>% 
  arrange(desc(Rating))

dim(joint_data)
import plotly.express as px
top_rated=joint_data[0:10]
top_rated
fig =px.sunburst(
    top_rated,
    path=['title','country'],
    values='Rating',
    color='Rating')
fig.show()
library(plotly)
top_rated <- joint_data[1:10,]
fig <- plot_ly(
  ids = c(top_rated$title, paste0(top_rated$title,"-",top_rated$country)),
  labels = c(top_rated$title,top_rated$country),
  parents = c(rep('',10), top_rated$title),
  colors = c(top_rated$Rating,top_rated$Rating),
  type = "sunburst",
  branchvalues = 'total'
)

fig
fig =px.sunburst(
    r.top_rated,
    path=['title','country'],
    values='Rating',
    color='Rating')
fig.show()

Countries with highest rated content.

country_count=joint_data['country'].value_counts().sort_values(ascending=False)
country_count=pd.DataFrame(country_count)
topcountries=country_count[0:11]
topcountries
topcountries <- joint_data %>% 
  group_by(country) %>% 
  summarise(n = n()) %>% 
  arrange(desc(n)) %>% 
  filter(!is.na(country))
import plotly.express as px
data = dict(
    number=[1063,619,135,60,44,41,40,40,38,35],
    country=["United States", "India", "United Kingdom", "Canada", "Spain",'Turkey','Philippines','France','South Korea','Australia'])
fig = px.funnel(data, x='number', y='country')
fig.show()
library(reticulate)
data <- py$data %>% 
  as.data.frame() %>% 
  arrange(desc(number))

plot_ly(
  y = data$country,
  x = data$number,
  type = "funnel",
) %>% 
  layout(yaxis = list(categoryarray = data$country))

Year wise analysis

plt.figure(figsize=(12,10))
sns.set(style="darkgrid")
ax = sns.countplot(y="release_year", data=netflix_movies, palette="Set2", order=netflix_movies['release_year'].value_counts().index[0:15])
plt.show()
netflix_movies %>% 
  group_by(release_year) %>% 
  summarise(n = n()) %>% 
  arrange(desc(n)) %>% 
  slice(1:15) %>% 
  mutate(release_year = factor(release_year, levels = release_year)) %>% 
  ggplot(aes(y = fct_rev(release_year), x = n, fill = release_year)) + 
  geom_bar(stat = "identity",show.legend = FALSE) + 
  ggsci::scale_fill_simpsons()

Analysis of duration of movies¶

netflix_movies['duration']=netflix_movies['duration'].str.replace(' min','')
netflix_movies['duration']=netflix_movies['duration'].astype(str).astype(int)
netflix_movies['duration']
plt.figure(figsize=(8,8))
sns.set(style="darkgrid")
sns.kdeplot(data=netflix_movies['duration'], shade=True)
plt.show()
netflix_movies %>% 
  mutate(duration = str_remove(duration, " min") %>% as.double()) %>% 
  ggplot(aes(x = duration)) + 
           geom_density(fill = "blue2",alpha = .4) + 
  ggthemes::theme_solarized()
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
from PIL import Image

from collections import Counter

genres=list(netflix_movies['listed_in'])
gen=[]

for i in genres:
    i=list(i.split(','))
    for j in i:
        gen.append(j.replace(' ',""))
g=Counter(gen)

text = list(set(gen))
plt.rcParams['figure.figsize'] = (13, 13)

wordcloud = WordCloud(max_words=1000000,background_color="white").generate(str(text))

plt.imshow(wordcloud,interpolation="bilinear")
plt.axis("off")
plt.show()
library(wordcloud)
library(tidytext)
set.seed(2021)
netflix_movies %>% 
  unnest_tokens(word, listed_in) %>% 
  count(word, sort = TRUE) %>% 
  with(wordcloud(word, n, max.words = 100))


matplotlib.use('TkAgg')
g={k: v for k, v in sorted(g.items(), key=lambda item: item[1], reverse= True)}
g
fig, ax = plt.subplots()

x=list(g.keys())
y=list(g.values())
ax.vlines(x, ymin=0, ymax=y, color='green')
ax.plot(x,y, "o", color='maroon')
ax.set_xticklabels(x, rotation = 90)
ax.set_ylabel("Count of movies")
# set a title
ax.set_title("Genres")
plt.show()
g <- py$g %>% unlist() %>% data.frame() %>% select(n = ".")

g %>% 
  mutate(name = rownames(g),
         name = fct_reorder(name, n, .desc = TRUE)) %>% 
  ggplot(aes(x = name, y = n)) + 
  geom_segment(aes(x = name, xend = name, y= 0, yend = n)) + 
  geom_point(size = 5, color = 'orange') + 
  theme(
    axis.text.x = element_text(angle = 90, hjust = 1)
  )

Lowest number of seasons.

features=['title','duration']
durations= netflix_shows[features]

durations['no_of_seasons']=durations['duration'].str.replace(' Season','')

#durations['no_of_seasons']=durations['no_of_seasons'].astype(str).astype(int)
durations['no_of_seasons']=durations['no_of_seasons'].str.replace('s','')
durations['no_of_seasons']=durations['no_of_seasons'].astype(str).astype(int)

t=['title','no_of_seasons']
top=durations[t]

top=top.sort_values(by='no_of_seasons', ascending=False)
bottom=top.sort_values(by='no_of_seasons')
bottom=bottom[20:50]

import plotly.graph_objects as go
# Set the width and height of the figure
plt.figure(figsize=(15,15))
fig = go.Figure(data=[go.Table(header=dict(values=['Title', 'No of seasons']), cells=dict(values=[bottom['title'],bottom['no_of_seasons']],fill_color='lavender'))])
fig.show()
library(kableExtra)
netflix_shows %>% 
  select(title, duration) %>% 
  separate(duration, ' ',into = c('duration','season')) %>% 
  mutate(duration = as.numeric(duration)) %>% 
  arrange(desc(duration)) %>% 
  kbl() %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342