python3中樸素貝葉斯_貝葉斯統計:Python中從零開始的都會都市

python3中樸素貝葉斯

你在這里 (You are here)

If you’re reading this, odds are: (1) you’re interested in bayesian statistics but (2) you have no idea how Markov Chain Monte Carlo (MCMC) sampling methods work, and (3) you realize that all but the simplest, toy problems require MCMC sampling so you’re a bit unsure of how to move forward.

如果您正在閱讀此書,則可能是:(1)您對貝葉斯統計感興趣,但(2)您不知道馬爾可夫鏈蒙特卡洛(MCMC)采樣方法的工作原理,以及(3)您意識到,除了最簡單的玩具問題需要MCMC采樣,因此您不確定如何前進。

Not to worry, we’ll explore this tricky concept using a 1-dimensional normal distribution, using nothing more than python, user-defined functions, and the random module (specifically, the uniform distribution.) We’ll discuss all this nonsense in terms of bars, beers, and a night out with your pals.

不用擔心,我們將使用一維正態分布探索這個棘手的概念,僅使用python,用戶定義函數和隨機模塊(特別是統一分布)。我們將在下面討論所有這些廢話。酒吧,啤酒和與朋友共度夜晚的條件

在酒吧想象自己 (Picture yourself at a bar)

It’s Friday night, you and your gang head out for burgers, beer and to catch a televised baseball game. Let’s say you arbitrarily pick a first bar (we’ll call it Larry’s Sports Bar), you sit down, grab a menu and consider your options. If the food and drinks are affordable, that’s a good reason to stay; but if there’s standing room only, you’ve got a reason to leave. If the bar’s filled with big screen TVs all dialed into the right game, another reason to stay. If the food or drinks aren’t appealing, you’ve got another reason to leave. Etc etc, you get the point.

這是星期五晚上,您和幫派前往漢堡,啤酒和觀看電視轉播的棒球比賽。 假設您隨意選擇第一個酒吧(我們稱其為Larry's Sports Bar ),您坐下來,拿起一個菜單并考慮您的選擇。 如果食物和飲料負擔得起,那就是留下的充分理由。 但是如果只有客廳,那您就有理由離開。 如果酒吧的大屏幕電視都播放了正確的游戲,這是留下的另一個原因。 如果食物或飲料不受歡迎,您還有其他理由離開。 等等,您明白了。

Now, let’s say one (or several) of your friends have gripes with the current establishment — the food is cold, the beers are overpriced, whatever the reason. So he proposes the gang leave Larry’s Sports Bar in favor of Tony’s Pizzeria and Beer Garden because the food there is better, fresher, etc. So the gang deliberates, asking the questions (A) how favorable is Larry’s? (B) How favorable is Tony’s? And (C) how favorable is Tony’s relative to Larry’s?

現在,比方說,您的一個(或幾個)朋友對當前的餐館感到不快-不管是什么原因,食物都很冷,啤酒價格過高。 因此,他建議該團伙離開拉里的體育館 ,轉而使用托尼的比薩店和啤酒花園,因為那里的食物更好,更新鮮,等等。于是,該團伙在考慮以下問題:(A)拉里一家有多有利? (B)托尼的優惠程度如何? (C) 托尼相對于拉里有多有利?

This relative comparison is really the most important detail. If Tony’s is only marginally better than Larry’s (or far worse) there’s a good chance that it’s not worth the effort to relocate. But if Tony’s is unambiguously the better of the two, there’s only a slim chance that you might stay. This is the real juice that makes Metropolis-Hastings “work.”

相對比較確實是最重要的細節。 如果Tony的房屋僅比Larry的房屋略好(或更差),則很有可能不值得重新安置。 但是,如果托尼(Tony's)無疑是兩者中的佼佼者,那么您留下的可能性很小。 這是使Metropolis-Hastings“工作”的真正汁液

算法 (The algorithm)

Metropolis-Hastings algorithm does:

Metropolis-Hastings算法可以:

  1. Start with a random sample

    隨機樣本開始

  2. Determine the probability density associated with the sample

    確定與樣本相關的概率密度

  3. Propose a new, arbitrary sample (and determine its probability density)

    提出一個新的任意樣本(并確定其概率密度)

  4. Compare densities (via division), quantifying the desire to move

    比較密度(通過除法),量化移動的欲望

  5. Generate a random number, compare with desire to move, and decide: move or stay

    生成一個隨機數,與移動的欲望進行比較,并決定:移動還是停留

  6. Repeat

    重復

The real key is (as we’ve discussed) quantifying how desirable the move is as an action/inaction criteria, then (new stuff alert!) observe a random event, compare to said threshold, and make a decision.

真正的關鍵是(正如我們已經討論過的那樣)量化移動作為行動/不作為標準的期望程度,然后(新事物警報!) 觀察隨機事件 ,與所述閾值進行比較,然后做出決定。

隨機事件 (The random event)

For our purposes, our threshold is the ratio of the proposed sample’s probability density to the current sample’s probability density. If this threshold is near (or above) 1, it means that the previous location was highly undesirable (a number close to 0, very improbable) and the proposed location is highly desirable (as close to 1 as is possible, near distribution’s expectation). Now, we need to generate a number in the range [0,1] from the uniform distribution. If the number produced is less than or equal to the threshold, we move. Otherwise, we stay. That’s it!

出于我們的目的,我們的閾值是建議樣本的概率密度與當前樣本的概率密度之比。 如果此閾值接近(或高于)1,則意味著先前的位置非常不理想(數字接近0,非常不可能),建議的位置也非常理想(盡可能接近1,接近分布的期望值) 。 現在,我們需要根據均勻分布生成一個在[0,1]范圍內的數字。 如果產生的數字小于或等于閾值,則移動。 否則,我們留下。 而已!

那硬東西呢? (What about the hard stuff?)

This is starting to sound a little too good to be true, right? We haven’t discussed Markov Chains or Monte Carlo simulations yet but fret not. Both are huge topics in their own right and we only need the most basic familiarity with each to make use of MCMC magic.

這聽起來似乎太好了,難以置信,對吧? 我們尚未討論馬爾可夫鏈或蒙特卡洛模擬,但不用擔心。 兩者本身都是很重要的話題,我們只需要對它們有最基本的了解就可以使用MCMC魔術。

A Markov Chain is is a chain of discrete events where the probability of the next event is conditioned only upon the current event. (ex: I just finished studying, do I go to sleep or go to the bar? These are my only choices of a finite set.) In this system of discrete choices, there exists a transition matrix, which quantifies the probability of transitioning from any given state to any given state. A Monte Carlo method is really just a fancy name for a simulation/experiment that relies on usage of random numbers.

馬爾可夫鏈是離散事件的鏈,其中下一個事件的概率僅取決于當前事件。 (例如:我剛剛完成學習,我要去睡覺還是去酒吧? 這是我對有限集合的唯一選擇。 )在這個離散選擇的系統中,存在一個轉換矩陣,該矩陣量化從任何給定狀態到任何給定狀態。 蒙特卡洛方法實際上只是依賴于隨機數使用的模擬/實驗的奇特名稱。

As discussed, we’ll be sampling from the normal distribution — a continuous, not discrete, distribution. So how can there be a transition matrix? Surprise!— there’s no transition matrix at all. (It’s actually called a Markov kernel, and it’s just the comparison of probability densities, at least for our purposes.)

正如討論的那樣,我們將從正態分布中進行采樣-連續而不是離散的分布。 那么如何有一個過渡矩陣呢? 驚喜! —根本沒有過渡矩陣。 (實際上,它被稱為Markov核 ,它只是概率密度的比較,至少出于我們的目的。)

代碼 (The code)

below, we define three functions: (1) Normal, which evaluates the probability density of any observation given the parameters mu and sigma. (2) Random_coin, which references a fellow TDS writer’s post (lined below). And (3) Gaussian_mcmc, which samples executes the algorithm as described above.

下面,我們定義三個函數:(1)正態,在給定參數mu和sigma的情況下評估任何觀測值的概率密度。 (2)Random_coin,它引用了TDS同行作家的帖子(如下所示)。 以及(3)采樣的高斯_mcmc執行上述算法。

As promised, we’re not calling any Gaussian or normal function from numpy, scipy, etc. In the third function, we initialize a current sample as an instance of the uniform distribution (where the lower and upper boundaries are +/- 5 standard deviations from the mean.) Likewise, movement is defined in the same way. Lastly, we move (or stay) based on the random event’s observed value in relation to acceptance, which is the probability density comparison discussed at length elsewhere.

如所承諾的,我們不會從numpy,scipy等中調用任何高斯函數或正態函數。在第三個函數中,我們將當前樣本初始化為均勻分布的實例(上下邊界為+/- 5標準同樣,以相同的方式定義運動。 最后,我們根據與接受相關的隨機事件的觀察值來移動(或停留),這是在其他地方詳細討論的概率密度比較。

Github Gist吉斯圖·吉斯特(Github Gist)
Image for post
Histogram vs actual distribution
直方圖與實際分布

Lastly, one must always give credit where credit is due: Rahul Agarwal’s post defining a Beta distribution MH sampler was instrumental to my development of the above Gaussian distribution MH sampler. I encourage you to read his post as well for a more detailed exploration of the foundational concepts, namely Markov Chains and Monte Carlo simulations.

最后,必須始終在應歸功的地方給予信譽: Rahul Agarwal的職位定義了Beta分布MH采樣器,這對我開發上述高斯分布MH采樣器至關重要。 我鼓勵您也閱讀他的文章,以更詳細地探索基本概念,即馬爾可夫鏈和蒙特卡洛模擬。

Thank you for reading — If you think my content is alright, please subscribe! :)

感謝您的閱讀-如果您認為我的內容還可以,請訂閱! :)

翻譯自: https://towardsdatascience.com/bayesian-statistics-metropolis-hastings-from-scratch-in-python-c3b10cc4382d

python3中樸素貝葉斯

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388801.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388801.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388801.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java映射的概念_Java 反射 概念理解

文章來源:http://hollischuang.gitee.io/tobetopjavaer/#/basics/java-basic/reflection反射反射機制指的是程序在運行時能夠獲取自身的信息。在java中,只要給定類的名字,那么就可以通過反射機制來獲得類的所有屬性和方法。反射有什么作用在運行時判斷任…

【轉載】移動端布局概念總結

布局準備工作及布局思想及概念: 一個顯示器(pc端顯示器 及 手機屏顯示器),既有物理像素,又有獨立像素(獨立像素也叫作css像素,用于前端人員使用); -->重要 首先確定設計稿的尺寸…

深入淺出:HTTP/2

上篇文章深入淺出:5G和HTTP里給自己挖了一根深坑,說是要寫一篇關于HTTP/2的文章,今天來還賬了。 本文分為以下幾個部分: HTTP/2的背景HTTP/2的特點HTTP/2的協議分析HTTP/2的支持 HTTP/2簡介 HTTP/2主要是為了解決現HTTP 1.1性能不…

畫了個Android

畫了個Android 今晚瞎折騰,閑著沒事畫了個機器人——android,浪費了一個晚上的時間。畫這丫還真不容易,為那些坐標,差點砸了鍵盤,好在最后畫出個有模有樣的,心稍安。 下面來看看畫這么個機器人需要些什么東…

數據治理 主數據 元數據_我們對數據治理的誤解

數據治理 主數據 元數據Data governance is top of mind for many of my customers, particularly in light of GDPR, CCPA, COVID-19, and any number of other acronyms that speak to the increasing importance of data management when it comes to protecting user data.…

mysql 選擇前4個_mysql從4個表中選擇

不要認為GROUP BY是必需的 . 雖然如果一個孩子有2個父記錄,你可能想用它來將2個父母分組到一行 - 但不確定這是否是你的要求 . 因為如果一個孩子有2個父母,那么將為該孩子返回的父母是未定義的 .假設所有孩子都有父母,所有父母都會有姓&#…

提高機器學習質量的想法_如何提高機器學習的數據質量?

提高機器學習質量的想法The ultimate goal of every data scientist or Machine Learning evangelist is to create a better model with higher predictive accuracy. However, in the pursuit of fine-tuning hyperparameters or improving modeling algorithms, data might …

mysql 集群實踐_MySQL Cluster集群探索與實踐

MySQL集群是一種在無共享架構(SNA,Share Nothing Architecture)系統里應用內存數據庫集群的技術。這種無共享的架構可以使得系統使用低廉的硬件獲取高的可擴展性。MySQL集群是一種分布式設計,目標是要達到沒有任何單點故障點。因此,任何組成部…

Python基礎:搭建開發環境(1)

1.Python語言簡介 2.Python環境 Python環境產品存在多個。 2.1 CPython CPython是Python官方提供的。一般情況下提到的Python就是指CPython,CPython是基于C語言編寫的。 CPython實現的解釋器將源代碼編譯為字節碼(ByteCode),再由虛…

python數據結構之隊列(一)

隊列概念隊列(queue)是只允許在一端進行插入操作,而在另一端進行刪除操作的線性表。隊列是一種先進先出的(First In First Out)的線性表,簡稱FIFO。允許插入的一端為隊尾,允許刪除的一端為隊頭。…

Android實現圖片放大縮小

Android實現圖片放大縮小 package com.min.Test_Gallery; import Android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.Matrix; import android.os.Bun…

matlab散點圖折線圖_什么是散點圖以及何時使用

matlab散點圖折線圖When you were learning algebra back in high school, you might not have realized that one day you would need to create a scatter plot to demonstrate real-world results.當您在高中學習代數時,您可能沒有意識到有一天需要創建一個散點圖…

java判斷題_【Java判斷題】請大神們進來看下、這些判斷題你都知道多少~

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓、判斷改錯題(每題2分,共20分)(正確的打√,錯誤的打并說明原因)1、 Java系統包提供了很多預定義類,我們可以直接引用它們而不必從頭開始編寫程序。 ( )2、 程序可以用字符‘*’替代一個TextField中的每個字…

PoPo數據可視化第8期

PoPo數據可視化 聚焦于Web數據可視化與可視化交互領域,發現可視化領域有意思的內容。不想錯過可視化領域的精彩內容, 就快快關注我們吧 :) 微信訂閱號:popodv_com谷歌決定關閉云可視化服務 Fusion Tables谷歌宣布即將關閉其 Fusion Tables 云服務&#x…

AC自動機題單

AC自動機題目 真的超級感謝xzy 真的幫到我很多 題單 [X] [luogu3808]【模板】AC自動機(簡單版) https://www.luogu.org/problemnew/show/P3808[X] [luogu3796]【模板】AC自動機(加強版)https://www.luogu.org/problemnew/show/P37…

java list用法_Java List 用法詳解及實例分析

Java List 用法詳解及實例分析Java中可變數組的原理就是不斷的創建新的數組,將原數組加到新的數組中,下文對Java List用法做了詳解。List:元素是有序的(怎么存的就怎么取出來,順序不會亂),元素可以重復(角標1上有個3,角標2上也可以…

python字符串和List:索引值以 0 為開始值,-1 為從末尾的開始位置;值和位置的區別哦...

String(字符串)Python中的字符串用單引號 或雙引號 " 括起來,同時使用反斜杠 \ 轉義特殊字符。 字符串的截取的語法格式如下: 變量[頭下標:尾下標]索引值以 0 為開始值,-1 為從末尾的開始位置。[一個是值&#x…

邏輯回歸 python_深入研究Python的邏輯回歸

邏輯回歸 pythonClassification techniques are an essential part of machine learning and data science applications. Approximately 70% of problems in machine learning are classification problems. There are lots of classification problems that are available, b…

spring定時任務(@Scheduled注解)

(一)在xml里加入task的命名空間 xmlns:task"http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd(二)啟用注…

net user 用戶名 密碼 /add 建立用戶

net user 用戶名 密碼 /add 建立用戶 other: net use \\ip\ipc$ " " /user:" " 建立IPC空鏈接 net use \\ip\ipc$ "密碼" /user:"用戶名" 建立IPC非空鏈接 net use h: \\ip\c$ "密碼" /user:"用戶名" 直接登陸后映…