Verified Commit 6df0ae92 authored by 施乐存's avatar 施乐存
Browse files

添加视频时长分析图表



Signed-off-by: 施乐存's avatarszdytom <szdytom@qq.com>
parent 414b7508
Loading
Loading
Loading
Loading
+122 −0
Original line number Original line Diff line number Diff line
(* ::Package:: *)

(* \:4ece\:6570\:636e\:5e93\:83b7\:53d6\:89c6\:9891\:65f6\:957f\:548c\:5e74\:4efd\:6570\:636e *)
rawData = Normal[ExternalEvaluate[
  DatabaseReference[
   File[FileNameJoin[{NotebookDirectory[], "..", "data.db"}]]],
   "SELECT 
      strftime('%Y', datetime(wp.stime, 'unixepoch')) as year,
      v.duration
    FROM videos v
    JOIN weekly_popular wp ON v.weekly_popular_n = wp.n
    ORDER BY wp.stime;"]];

(* \:5c06\:6570\:636e\:8f6c\:6362\:4e3a\:66f4\:65b9\:4fbf\:5904\:7406\:7684\:683c\:5f0f *)
data = {#["year"], #["duration"]} & /@ rawData;

(* \:6309\:5e74\:4efd\:5206\:7ec4 *)
groupedData = GatherBy[data, First];

(* \:8ba1\:7b97\:6bcf\:4e2a\:5e74\:4efd\:7684\:7edf\:8ba1\:91cf *)
yearStats = Table[
  year = FromDigits[groupedData[[i, 1, 1]]];
  durations = groupedData[[i, All, 2]];
  {
    year,
    Length[durations],                          (* \:6837\:672c\:6570\:91cf *)
    Mean[durations] / 60,                       (* \:5e73\:5747\:65f6\:957f\:ff0c\:8f6c\:6362\:4e3a\:5206\:949f *)
    Median[durations] / 60,                     (* \:4e2d\:4f4d\:6570\:65f6\:957f\:ff0c\:8f6c\:6362\:4e3a\:5206\:949f *)
    Quantile[durations, 0.25] / 60,             (* Q1\:ff0c\:8f6c\:6362\:4e3a\:5206\:949f *)
    Quantile[durations, 0.75] / 60,             (* Q3\:ff0c\:8f6c\:6362\:4e3a\:5206\:949f *)
    Quantile[durations, 0.75] / 60 - Quantile[durations, 0.25] / 60,  (* IQR\:ff0c\:8f6c\:6362\:4e3a\:5206\:949f *)
    (Quantile[durations, 0.75] - Quantile[durations, 0.25]) /          (* QCD *)
        (Quantile[durations, 0.75] + Quantile[durations, 0.25])
  },
  {i, Length[groupedData]}
];

(* \:63d0\:53d6\:6570\:636e\:7528\:4e8e\:7ed8\:56fe *)
years = yearStats[[All, 1]];
counts = yearStats[[All, 2]];
means = yearStats[[All, 3]];
medians = yearStats[[All, 4]];
q1s = yearStats[[All, 5]];
q3s = yearStats[[All, 6]];
iqrs = yearStats[[All, 7]];
qcds = yearStats[[All, 8]];

(* \:521b\:5efa\:7edf\:8ba1\:91cf\:5173\:8054\:6570\:7ec4 *)
stats = {
  Association[Thread[years -> means]],     (* \:5747\:503c *)
  Association[Thread[years -> medians]],   (* \:4e2d\:4f4d\:6570 *)
  Association[Thread[years -> q1s]],       (* Q1 *)
  Association[Thread[years -> q3s]],        (* Q3 *)
  Association[Thread[years -> iqrs]]
};

(* \:7ed8\:5236\:56db\:6761\:66f2\:7ebf\:7684\:6298\:7ebf\:56fe\:ff1a\:5747\:503c\:3001\:4e2d\:4f4d\:6570\:3001Q1\:3001Q3 *)
plot1 = ListLinePlot[
  stats,
  PlotStyle -> "Pastel",
  PlotMarkers -> {Automatic, 10},
  PlotLegends -> Placed[{"\:5747\:503c", "\:4e2d\:4f4d\:6570", "Q1", "Q3", "IQR"}, {0.85, 0.85}],
  Frame -> True,
  FrameLabel -> {"\:5e74\:4efd", "\:89c6\:9891\:65f6\:957f\:ff08\:5206\:949f\:ff09"},
  FrameStyle -> Directive[Black, 14],
  GridLines -> Automatic,
  GridLinesStyle -> Directive[Gray, Dotted],
  ImageSize -> 800,
  PlotLabel -> Style["\:89c6\:9891\:65f6\:957f\:5206\:5e03\:7edf\:8ba1\:ff082019-2025\:ff09", 16, Bold],
  LabelStyle -> Directive[Black, 12],
  PlotRange -> All
];

Export[FileNameJoin[{NotebookDirectory[], "video_duration_stats_plot.png"}], plot1]

(* \:7ed8\:5236QCD\:6298\:7ebf\:56fe *)
plot2 = ListLinePlot[
  Association[Thread[years -> qcds]],
  PlotStyle -> "Pastel",
  PlotMarkers -> {Automatic, 10},
  Frame -> True,
  FrameLabel -> {"\:5e74\:4efd", "\:56db\:5206\:4f4d\:6570\:79bb\:6563\:7cfb\:6570 (QCD)"},
  FrameStyle -> Directive[Black, 14],
  GridLines -> Automatic,
  GridLinesStyle -> Directive[Gray, Dotted],
  ImageSize -> 800,
  PlotLabel -> Style["\:89c6\:9891\:65f6\:957f\:79bb\:6563\:7a0b\:5ea6\:53d8\:5316\:8d8b\:52bf\:ff082019-2025\:ff09", 16, Bold],
  LabelStyle -> Directive[Black, 12],
  PlotRange -> All
];

Export[FileNameJoin[{NotebookDirectory[], "video_duration_qcd_plot.png"}], plot2]

(* \:521b\:5efa\:6570\:636e\:8868\:683c *)
tableData = N[yearStats]

(* \:7f8e\:5316\:8868\:683c *)
table = Grid[
  Prepend[
    tableData,
    {"\:5e74\:4efd", "\:6837\:672c\:6570", "\:5747\:503c(\:5206\:949f)", "\:4e2d\:4f4d\:6570(\:5206\:949f)", "Q1(\:5206\:949f)", "Q3(\:5206\:949f)", "IQR(\:5206\:949f)", "QCD"}
  ],
  Frame -> All,
  Background -> {
    None,
    {LightBlue, {LightYellow, LightYellow, LightYellow, LightYellow, LightYellow, LightYellow, LightYellow, LightYellow}}
  },
  Alignment -> {Center, Center},
  ItemSize -> {All, 1.5},
  Spacings -> {1.5, 1}
];

(* \:5728Notebook\:4e2d\:663e\:793a\:8868\:683c *)
Print["\:89c6\:9891\:65f6\:957f\:7edf\:8ba1\:8868\:683c:"];
Print[table];

plot1

plot2


+95.2 KiB
Loading image diff...
+144 KiB
Loading image diff...