20250124

Text Mining with R: Chapter4

這是給自己的一份學習紀錄,以免日子久了忘記這是甚麼理論XD Tokenizing by n-gram (n-gram 分詞) 將文檔的內容依照 n 個單詞作分類,例如: [1] “The Bank is a place where you put your money” [2] The Bee is an insect that gathers honey" 我們可以利用函式 tokenize_ngrams() 依照 n = 2 分類為 [1] “the bank”、“bank is”、“is a”、“a place”、“place where”、“where you”、“you put”、“put your”、“your money” [2] “the bee”、“bee is”、“is an”、“an insect”、“insect that”、“that gathers”、“gathers honey”

January 26, 2025 · 1 min
20250124

Text Mining with R: Chapter3

這是給自己的一份學習紀錄,以免日子久了忘記這是甚麼理論XD Analyzing word and document frequency: tf-idf Term Frequency(tf): How frequently a word occurs in a document 詞頻: 單詞出現在文檔的頻率 Inverse Document Frequency(idf): use to decrease the weight for commonly used words and increase the weight for words that are not used very much in a collection of documents 逆文檔頻率: 文檔中出現愈多次的單詞,其權重愈低;反之則愈低。即衡量某詞在所有文檔中分佈的稀疏程度,是一種懲罰項 。 並定義為: $$idf(term) = ln\left(\frac{n_{documents}}{n_{documents containing term}}\right)$$ 其中分子$n_{documents}$是文檔總數,分母$n_{documents containing term}$是包含該詞的文檔總數, 若某單詞出現在文檔的次數少,表示分母小,其 IDF 大,重要性高,權重高;反之出現的次數多,表示分母大,其 IDF 小,重要性低,權重低。 取對數則是為了減少極端數值,使 IDF 分佈更加平滑。 tf-idf的目標是用於識別在單一文檔中有價值、但不常見的單詞(詞彙) Zipf’s law ( 齊夫定律) 一種描述自然語言中單詞使用頻率分佈的統計規律,其宣稱單詞的頻率與其在文檔裡的頻率排名成反比,即:$$frequency \propto \frac{1}{rank} $$ 出現頻率第一名的單詞的次數會是出現頻率第二名的單詞的次數的兩倍,會是出現頻率第三名的單詞的次數的三倍…依此類推,會是出現頻率第 N 名的單詞的次數的 N 倍 若將排名 x 與出現頻率 y 取對數,即 logx 、 logy ,若整個文檔的坐標點標出來接近一直線,則該文檔符合 Zipf’s law

January 24, 2025 · 1 min
20250113

Hirarchical bayesian modeling

這是給自己的一份學習紀錄,以免日子久了忘記這是甚麼理論XD Bayesian hierarchical modeling,譯為「貝氏分層建模」 針對多個層級分析的一套統計方法 「Hierarchical modeling is used with information is available on several different levels of observational units」 可以對多個不同層級的觀測單位的資訊進行分析,例如: – 每個國家的每日感染病例的時間概況,單位是國家 – 多個油井產量的遞減曲線分析(油氣產量),單位是油藏區的油井 引自維基百科 公式理論 Bayes’ theorem: the updated probability statements about $\theta_j$, given the occurence of event $y$, using the basic property of conditional probability, the posterior distribution will yield: $$P(\theta \mid y) = \frac{P(\theta, y)}{P(y)} = \frac{P(y \mid \theta)P(\theta)}{P(y)}$$ so, we can say that: $$P(\theta \mid y) \propto P(y \mid \theta)P(\theta)$$ Hierarchical models: Bayesian hierarchical modeling makes use of two important concepts in deriving the posterior distribution namely: (1) Hyperparameters: parameters of prior distribution 先驗分配的參數(超參數/超母數) (2) Hyperdisrtibution: distribution of hyperparameters 超參數的分配 例如:我們需要建模學校的學生測驗成績 ...

January 13, 2025 · 1 min
20250112

Expectation maximization algorithm

這是給自己的一份學習紀錄,以免日子久了忘記這是甚麼理論XD Expectation-maximization algorithm 又翻譯為「最大期望值演算法」 經過兩個步驟交替進行計算: 第一步是計算期望值(E):利用對隱藏變量的現有估計值,計算其最大概似估計值 第二步是最大化(M):最大化在E步上求得的最大概似值來計算參數的值 M步上找到的參數估計值被用於下一個E步計算中,這個過程不斷交替進行 引自維基百科

January 12, 2025 · 1 min
20250110

cocktail webcrawler

這是給我自己的一份教學,以免日子久了忘記怎麼爬蟲,同時也是一篇學習紀錄 擁有一個可以寫code的環境,網路上很多安裝環境的教學 我是用anoconda的vs code寫code的 import requests as req # 向客戶端要求網址 from bs4 import BeautifulSoup as B # 索取網址內容 import pandas as pd # 最後將結果輸出為CSV檔 import time # 避免爬蟲時被抓到是爬蟲,所以移用這個延長每次爬蟲時間,假裝自己是人類 import random # 延遲隨機時間用的 builder_url = 'https://www.theeducatedbarfly.com/cocktail-builder/' # 我是用 **cocktail builder** 這個網站來爬蟲我要的酒單 header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'} # 起初在爬的時候有發現跑不出資料,所以新增header來假裝我是人類,網路上也有很多如何在瀏覽器找header的教學 resp = req.get(builder_url, headers=header) # 建立抓取url的回應變數 cocktail_name_list = [] # 建立空清單,將抓取到的資料先放進來,以便最後做成dataframe if resp.status_code == 200: # 確定你的url是可以連線的 soup = B(resp.text, 'html.parser') # 建立爬蟲的頭頭,後面的html.parser是每次建立都要有,好像是用來解析html的功能 for cocktail_name in soup.find_all('div', class_="wpupg-item-title wpupg-block-text-bold"): # 打開網頁按下F2,按下ctrl+shift+c進入選取網頁元素模式,點選任一調酒,會指引到該調酒的block # 你會發現所有的調酒名稱都在'div', class_="wpupg-item-title wpupg-block-text-bold"這個標籤底下,所以我們利用find_all遍歷該標籤 name = cocktail_name.text.strip() # 調酒名稱都在'div', class_="wpupg-item-title wpupg-block-text-bold"的標籤的text內容中,strip()是去掉text前後多餘的空格 if name: # 確定該標籤有內容才抓,沒有就不抓 cocktail_name_list.append(name) # 抓到修飾後的text並放入剛剛建立的list time.sleep(random.uniform(1,3)) # 每次抓完一個就等待 1~3 秒 cocktail_name_df = pd.DataFrame(cocktail_name_list, columns=['Name']) # 將抓完後的清list建立成一個Dataframe,以Name當作該欄位的名稱 cocktail_data = [] # 這是最後的調酒名稱+該調酒的材料的空清單 for name in cocktail_name_df['Name']: urls = f'https://www.theeducatedbarfly.com/{name.replace(" ", "-").lower()}/' # 建立每個調酒的url,點進去隨便一個調酒,你會發現網址是https://www.theeducatedbarfly.com/xxx-xxx/ # xxx是該調酒的名稱,只是我們剛剛的調酒名稱list有大寫而且中間是空格不是-,所以用replace將空格替換成-,且轉為小寫 resp = req.get(urls, headers=header) if resp.status_code == 200: soup = B(resp.text, 'html.parser') # 查找所有具有指定 class 的 <span> 元素 ingredients = soup.find_all('span', class_='wprm-recipe-ingredient-name') ingredient_name_list = [] for ingredient in ingredients: # 提取<a>標籤的文字內容 ingredient_name = ingredient.find('a') if ingredient_name: # 確保<a>存在 ingredient_name_list.append(ingredient_name.text.strip()) cocktail_data.append({'Cocktail Name': name, 'Ingredients': ", ".join(ingredient_name_list)}) # 最後就是將剛剛抓到的Name跟這個Inredients清單中每個素材加入剛剛建立的加入剛剛建立的cocktail_data清單中 time.sleep(random.uniform(1,3)) cocktail_df = pd.DataFrame(cocktail_data) # 最後的最後將list轉為Dataframe並用pd.to_csv輸出成csv檔,結束這回合 以上是我提醒自己的爬蟲教學 有任何疑問歡迎提出 雖然我還沒有建立留言板就是了 ...

January 10, 2025 · 1 min
20240930

Linear Regression Learning

這是給自己的一份學習紀錄,以免日子久了忘記這是甚麼理論XD Simple linear regression 給定一組已知的(X, y),其中X,y皆為連續變項 線性迴歸就是由X來預測y 我們總是優先繪製散佈圖,觀察兩變項的變動趨勢 是第一種情況:X愈高,y愈高,還是第二種情況:X愈低,y愈低,又或者是第三種情況:y不會因為X的變動而跟著變動? 第一種稱為正相關,第二種稱為負向關,第三種稱為零相關 若X為一維,則稱為簡單迴歸,其迴歸線方程式為: $$ $$ 若X為多維,則稱為複迴歸,其迴歸方程式為 $$ $$

September 30, 2024 · 1 min