<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cat say meaw.</title>
    <description>The latest articles on DEV Community by Cat say meaw. (@jumptothehell).</description>
    <link>https://dev.to/jumptothehell</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1062939%2F95947c9e-2d25-4384-97c9-832926352852.jpeg</url>
      <title>DEV Community: Cat say meaw.</title>
      <link>https://dev.to/jumptothehell</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jumptothehell"/>
    <language>en</language>
    <item>
      <title>บางทีคุณอาจจะชอบฟังเพลงของศิลปินคนนี้ก็ได้นะ ?</title>
      <dc:creator>Cat say meaw.</dc:creator>
      <pubDate>Tue, 11 Apr 2023 16:11:43 +0000</pubDate>
      <link>https://dev.to/jumptothehell/baangthiikhunaacchcchachbfangephlngkhngsilpinkhnniikaidna--1bk9</link>
      <guid>https://dev.to/jumptothehell/baangthiikhunaacchcchachbfangephlngkhngsilpinkhnniikaidna--1bk9</guid>
      <description>&lt;p&gt;เบื่อหรือเปล่ากับการหาฟังเพลงที่ถูกใจไม่ได้? ดังนั้นเราจึงได้ใช้ A recommendation algorithm โดยอ้างอิงจากพฤติกรรมการฟังของบุคคลในการเเนะนำศิลปินที่คุณน่าจะชื่นชอบ ในกรณีนี้เนื่องจาก dataset ต้องทำการรออาจจะถึง 30 วัน จึงใช้ dataset ที่มีไปก่อน จึงทำให้ข้อมูลที่ได้ค่อนข้างคลาดเคลื่อนอย่างมาก เนื่องจากมีข้อมูลที่ไม่เพียงพอ ;-;&lt;/p&gt;

&lt;p&gt;บทความนี้ เราจะใช้ Google Colab ในการ Run Code โดย dataset ที่ใช้มาเป็นตัวอย่างก็จะเป็นประวิติการฟังเพลงจาก Spotify โดยในข้อมูลประกอบไปด้วยประวัติการฟังเพลงตั้งเเต่วันที่ 11 เมษายน 2565 จนถึง 19 มีนาคม 2566 เป็นจำนวน 10000 เพลง ข้อมูลประกอบไปด้วย 4 columns&lt;/p&gt;

&lt;p&gt;[1] endTime: วันที่และเวลาที่สตรีมสิ้นสุดในเขตเวลาเชิงสากล&lt;br&gt;
[2] artistName : ชื่อของ "ผู้สร้าง" สำหรับเเต่ละสตรีม (เช่น ชื่อศิลปิน หากเป็นแทร็กเพลง)&lt;br&gt;
[3] trackName : ชื่อรายการที่ฟัง (เช่น ชื่อแทร็กเพลง หรือชื่อวิดีโอ พอสแคสต์)&lt;br&gt;
[4] msPlayed : จำนวนมิลลิวินาทีที่ใช้ฟังในเเต่ละเเทร็ก&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ขั้นตอนที่ 1: นำข้อมูลเข้า df&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd
from google.colab import files
import io

# Load the dataset into a pandas dataframe
uploaded = files.upload()
df = pd.read_json(io.BytesIO(uploaded['StreamingHistory0.json']))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;เมื่อทำกด Run จะมี Choose Files Botton ขึ้นมา ให้เราทำการเลือก File ในเครื่องเรา โดยชื่อของไฟล์ต้องตรงกับ &lt;br&gt;
df = pd.read_json(io.BytesIO(uploaded['StreamingHistory0.json'])) &lt;/p&gt;

&lt;p&gt;ตัวอย่างที่ได้จากโค้ด&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(df)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L3Vkh_s---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfc278p8rtpcsqyftbfb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L3Vkh_s---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfc278p8rtpcsqyftbfb.png" alt="Image description" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ขั้นตอนที่ 2: ทำการวิเคราะห์ข้อมูล เพื่อหาศิลปินที่คุณชื่นชอบมากที่สุด&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ในโค้ดนี้เราจะการจัดกลุ่มชื่อศิลปิน เเละผลรวมมิลลิวินาทีที่ใช้ฟังเเต่ละเทร็กเข้าด้วยกัน เเล้วทำการจัดข้อมูล เพื่อค้นหาว่าศิลปินคนไหนที่เราใช้เวลาในการฟังมากที่สุด นั้นเเหละศิลปินคนโปรดของคุณ&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Group the data by artistName and sum the miliseconds played
artist_plays = df.groupby('artistName')['msPlayed'].sum().reset_index()

# Sort the data by the miliseconds played in descending order
sorted_artists = artist_plays.sort_values('msPlayed', ascending=False)

# Get the name of the artist with the most miliseconds played
favorite_artist = sorted_artists.iloc[0]['artistName']

# Print the name of the favorite artist
print("Your favorite artist is:", favorite_artist)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ตัวอย่างที่ได้จากโค้ด&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kAYx8tYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ek9g9p8fffox4oc2vhof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kAYx8tYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ek9g9p8fffox4oc2vhof.png" alt="Image description" width="800" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- BONUS -&lt;/strong&gt;&lt;br&gt;
ในกรณีที่คุณสงสัยว่าคุณฟังศิลปินคนโปรดไปทั้งหมดกี่มิลวินาที คุณสามารถที่จะ...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Top 5 favorite artists:")
print(sorted_artists.head(5))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ตัวอย่างที่ได้จากโค้ด&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MSMyaiJi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pvhhk9s57mg4ukh28n9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MSMyaiJi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pvhhk9s57mg4ukh28n9.png" alt="Image description" width="800" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ขั้นตอนที่ 3: Top 5 ศิลปินแนะนำ&lt;/strong&gt;&lt;br&gt;
ทำการสร้าง Pivot_table เพื่อรวมจำนวนมิลลิวินาทีที่เราใช้ในการฟังเพลง เเละทำการคำนวณความถี่สัมพัทธ์เพื่อวัดความถี่ที่เราเลือกฟังเพลงศิลปิน ซึ่งสามารถตีความว่าเราน่าเลือกฟังศิลปินคนนี้มากกว่า จากนั้นใช้ cosine_similarity เพื่อคำนวณความคล้ายคลึงจากพฤติกรรมการฟัง จากนั้นทำการ Print Top 5 ศิลปินแนะนำ&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

# Create a pivot table to get the number of times each track was played by user
pivot_table = pd.pivot_table(df, values='msPlayed', index=['artistName'], aggfunc=np.sum)

# Normalize the pivot table by column to get user listening behavior
user_behavior = pivot_table.apply(lambda x: x/x.sum(), axis=1)

# Calculate similarity matrix between users based on their listening behavior
user_similarity = cosine_similarity(user_behavior)

# Get the top 5 artists similar to the user's favorite artist
fav_artist = favorite_artist

artist_index = pivot_table.index.get_loc(favorite_artist)
similar_artists = user_similarity[artist_index].argsort()[:-6:-1]

# Print the recommended artists
print("Your recommended artists:")
for i in similar_artists[1:]:
    print(pivot_table.index[i])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ตัวอย่างที่ได้จากโค้ด&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RbViDdMQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cf2qpqqv3jmp152ywk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RbViDdMQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cf2qpqqv3jmp152ywk6.png" alt="Image description" width="800" height="102"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
