缺失值和異常值的識別與處理_識別異常值-第一部分

缺失值和異常值的識別與處理

📈Python金融系列 (📈Python for finance series)

Warning: There is no magical formula or Holy Grail here, though a new world might open the door for you.

警告這里沒有神奇的配方或圣杯,盡管新世界可能為您打開了大門。

📈Python金融系列 (📈Python for finance series)

  1. Identifying Outliers

    識別異常值

  2. Identifying Outliers — Part Two

    識別異常值-第二部分

  3. Identifying Outliers — Part Three

    識別異常值-第三部分

  4. Stylized Facts

    程式化的事實

  5. Feature Engineering & Feature Selection

    特征工程與特征選擇

  6. Data Transformation

    數據轉換

Pandas has quite a few handy methods to clean up messy data, like dropna,drop_duplicates, etc.. However, finding and removing outliers is one of those functions that we would like to have and still not exist yet. Here I would like to share with you how to do it step by step in details:

Pandas有很多方便的方法可以清理混亂的數據,例如dropnadrop_duplicates等。但是,查找和刪除異常值是我們希望擁有的但仍然不存在的功能之一。 在這里,我想與您分享如何逐步進行詳細操作:

The key to defining an outlier lays at the boundary we employed. Here I will give 3 different ways to define the boundary, namely, the Average mean, the Moving Average mean and the Exponential Weighted Moving Average mean.

定義離群值的關鍵在于我們采用的邊界。 在這里,我將給出3種不同的方法來定義邊界,即平均均值,移動平均數和指數加權移動平均數。

1.數據準備 (1. Data preparation)

Here I used Apple’s 10-year stock history price and returns from Yahoo Finance as an example, of course, you can use any data.

在這里,我以蘋果公司10年的股票歷史價格和Yahoo Finance的收益為例,當然,您可以使用任何數據。

import pandas as pd 
import yfinance as yfimport matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.rcParams['figure.dpi'] = 300df = yf.download('AAPL',
start = '2000-01-01',
end= '2010-12-31')

As we only care about the returns, a new DataFrame (d1) is created to hold the adjusted price and returns.

由于我們只關心收益, DataFrame (d1)會創建一個新的DataFrame (d1)來容納調整后的價格和收益。

d1 = pd.DataFrame(df['Adj Close'])
d1.rename(columns={'Adj Close':'adj_close'}, inplace=True)
d1['simple_rtn']=d1.adj_close.pct_change()
d1.head()
Image for post

2.以均值和標準差為邊界。 (2. Using mean and standard deviation as the boundary.)

Calculate the mean and std of the simple_rtn:

計算simple_rtn的均值和std:

d1_mean = d1['simple_rtn'].agg(['mean', 'std'])

If we use mean and one std as the boundary, the results will look like these:

如果我們使用均值和一個std作為邊界,結果將如下所示:

fig, ax = plt.subplots(figsize=(10,6))
d1['simple_rtn'].plot(label='simple_rtn', legend=True, ax = ax)
plt.axhline(y=d1_mean.loc['mean'], c='r', label='mean')
plt.axhline(y=d1_mean.loc['std'], c='c', linestyle='-.',label='std')
plt.axhline(y=-d1_mean.loc['std'], c='c', linestyle='-.',label='std')
plt.legend(loc='lower right')
Image for post

What happens if I use 3 times std instead?

如果我使用3次std會怎樣?

Image for post

Looks good! Now is the time to look for those outliers:

看起來挺好的! 現在是時候尋找那些離群值了:

mu = d1_mean.loc['mean']
sigma = d1_mean.loc['std']def get_outliers(df, mu=mu, sigma=sigma, n_sigmas=3):
'''
df: the DataFrame
mu: mean
sigmas: std
n_sigmas: number of std as boundary
'''
x = df['simple_rtn']
mu = mu
sigma = sigma

if (x > mu+n_sigmas*sigma) | (x<mu-n_sigmas*sigma):
return 1
else:
return 0

After applied the rule get_outliers to the stock price return, a new column is created:

將規則get_outliers應用于股票價格收益后,將創建一個新列:

d1['outlier'] = d1.apply(get_outliers, axis=1)
d1.head()
Image for post

?提示! (?Tip!)

#The above code snippet can be refracted as follow:cond = (d1['simple_rtn'] > mu + sigma * 2) | (d1['simple_rtn'] < mu - sigma * 2)
d1['outliers'] = np.where(cond, 1, 0)

Let’s have a look at the outliers. We can check how many outliers we found by doing a value count.

讓我們看看異常值。 我們可以通過計數來檢查發現了多少離群值。

d1.outlier.value_counts()
Image for post

We found 30 outliers if we set 3 times std as the boundary. We can pick those outliers out and put it into another DataFrame and show it in the graph:

如果我們將std設置為3倍,則發現30個離群值。 我們可以挑選出這些離群值,并將其放入另一個DataFrame ,并在圖中顯示出來:

outliers = d1.loc[d1['outlier'] == 1, ['simple_rtn']]fig, ax = plt.subplots()ax.plot(d1.index, d1.simple_rtn, 
color='blue', label='Normal')
ax.scatter(outliers.index, outliers.simple_rtn,
color='red', label='Anomaly')
ax.set_title("Apple's stock returns")
ax.legend(loc='lower right')plt.tight_layout()
plt.show()
Image for post

In the above plot, we can observe outliers marked with a red dot. In the next post, I will show you how to use Moving Average Mean and Standard deviation as the boundary.

在上圖中,我們可以觀察到標有紅點的離群值。 在下一篇文章中,我將向您展示如何使用移動平均均值和標準差作為邊界。

Happy learning, happy coding!

學習愉快,編碼愉快!

翻譯自: https://medium.com/python-in-plain-english/identifying-outliers-part-one-c0a31d9faefa

缺失值和異常值的識別與處理

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

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

相關文章

SQL Server 常用分頁SQL

今天無聊和朋友討論分頁&#xff0c;發現網上好多都是錯的。網上經常查到的那個Top Not in 或者Max 大部分都不實用&#xff0c;很多都忽略了Order和性能問題。為此上網查了查&#xff0c;順帶把2000和2012版本的也補上了。 先說說網上常見SQL的錯誤或者說局限問題 12345select…

Word中摘要和正文同時分欄后,正文跑到下一頁,怎么辦?或Word分欄后第一頁明明有空位后面的文字卻自動跳到第二頁了,怎么辦?...

問題1&#xff1a;Word中摘要和正文同時分欄后&#xff0c;正文跑到下一頁&#xff0c;怎么辦&#xff1f;或Word分欄后第一頁明明有空位后面的文字卻自動跳到第二頁了&#xff0c;怎么辦&#xff1f; 答&#xff1a;在word2010中&#xff0c;菜單欄中最左側選“文件”->“選…

leetcode 664. 奇怪的打印機(dp)

題目 有臺奇怪的打印機有以下兩個特殊要求&#xff1a; 打印機每次只能打印由 同一個字符 組成的序列。 每次可以在任意起始和結束位置打印新字符&#xff0c;并且會覆蓋掉原來已有的字符。 給你一個字符串 s &#xff0c;你的任務是計算這個打印機打印它需要的最少打印次數。…

SQL數據類型說明和MySQL語法示例

SQL數據類型 (SQL Data Types) Each column in a database table is required to have a name and a data type. 數據庫表中的每一列都必須具有名稱和數據類型。 An SQL developer must decide what type of data that will be stored inside each column when creating a tab…

PHP7.2 redis

為什么80%的碼農都做不了架構師&#xff1f;>>> PHP7.2 的redis安裝方法&#xff1a; 順便說一下PHP7.2的安裝&#xff1a; wget http://cn2.php.net/distributions/php-7.2.4.tar.gz tar -zxvf php-7.2.4.tar.gz cd php-7.2.4./configure --prefix/usr/local/php…

leetcode 1787. 使所有區間的異或結果為零

題目 給你一個整數數組 nums??? 和一個整數 k????? 。區間 [left, right]&#xff08;left < right&#xff09;的 異或結果 是對下標位于 left 和 right&#xff08;包括 left 和 right &#xff09;之間所有元素進行 XOR 運算的結果&#xff1a;nums[left] XOR n…

【JavaScript】網站源碼防止被人另存為

1、禁示查看源代碼 從"查看"菜單下的"源文件"中同樣可以看到源代碼&#xff0c;下面我們就來解決這個問題&#xff1a; 其實這只要使用一個含有<frame></frame>標記的網頁便可以達到目的。 <frameset> <frame src"你要保密的文件…

梯度 cv2.sobel_TensorFlow 2.0中連續策略梯度的最小工作示例

梯度 cv2.sobelAt the root of all the sophisticated actor-critic algorithms that are designed and applied these days is the vanilla policy gradient algorithm, which essentially is an actor-only algorithm. Nowadays, the actor that learns the decision-making …

共享語義 unix語義_語義UI按鈕

共享語義 unix語義什么是語義UI按鈕&#xff1f; (What are Semantic UI Buttons?) A button indicates a possible user action. Semantic UI provides an easy-to-use syntax that simplifies not only the styling of a button, but also the natural language semantics.按…

垃圾回收算法優缺點對比

image.pngGC之前 說明&#xff1a;該文中的GC算法講解不僅僅局限于某種具體開發語言。 mutator mutator 是 Edsger Dijkstra 、 琢磨出來的詞&#xff0c;有“改變某物”的意思。說到要改變什么&#xff0c;那就是 GC 對象間的引用關系。不過光這么說可能大家還是不能理解&…

標準C程序設計七---77

Linux應用 編程深入 語言編程標準C程序設計七---經典C11程序設計 以下內容為閱讀&#xff1a; 《標準C程序設計》&#xff08;第7版&#xff09; 作者&#xff1a;E. Balagurusamy&#xff08;印&#xff09;&#xff0c; 李周芳譯 清華大學出版社…

leetcode 1190. 反轉每對括號間的子串

題目 給出一個字符串 s&#xff08;僅含有小寫英文字母和括號&#xff09;。 請你按照從括號內到外的順序&#xff0c;逐層反轉每對匹配括號中的字符串&#xff0c;并返回最終的結果。 注意&#xff0c;您的結果中 不應 包含任何括號。 示例 1&#xff1a; 輸入&#xff1a…

yolo人臉檢測數據集_自定義數據集上的Yolo-V5對象檢測

yolo人臉檢測數據集計算機視覺 (Computer Vision) Step by step instructions to train Yolo-v5 & do Inference(from ultralytics) to count the blood cells and localize them.循序漸進的說明來訓練Yolo-v5和進行推理(來自Ultralytics )以對血細胞進行計數并將其定位。 …

oauth2-server-php-docs 授權類型

授權碼 概觀 在Authorization Code交付式時使用的客戶端想要請求訪問受保護資源代表其他用戶&#xff08;即第三方&#xff09;。這是最常與OAuth關聯的授予類型。 詳細了解授權碼 用例 代表第三方來電履行 創建一個實例OAuth2\GrantType\AuthorizationCode并將其添加到您的服務…

flask框架視圖和路由_角度視圖,路由和NgModule的解釋

flask框架視圖和路由Angular vs AngularJS (Angular vs AngularJS) AngularJS (versions 1.x) is a JavaScript-based open source framework. It is cross platform and is used to develop Single Page Web Application (SPWA). AngularJS(版本1.x)是一個基于JavaScript的開源…

NGUI EventDelagate事件委托

using System.Collections; using System.Collections.Generic; using UnityEngine;public class BUttonClick : MonoBehaviour {public UIButton button_01;void Start(){if (button_01 null){Debug.Log("button組件丟失了");}else{//首先將腳本中的ClicktheButton…

leetcode 461. 漢明距離(位運算)

兩個整數之間的漢明距離指的是這兩個數字對應二進制位不同的位置的數目。 給出兩個整數 x 和 y&#xff0c;計算它們之間的漢明距離。 注意&#xff1a; 0 ≤ x, y < 231. 示例:輸入: x 1, y 4輸出: 2解釋: 1 (0 0 0 1) 4 (0 1 0 0)↑ ↑上面的箭頭指出了對應二進…

圖深度學習-第2部分

有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as the videos. Of cou…

Linux下 安裝Redis并配置服務

一、簡介 1、 Redis為單進程單線程模式&#xff0c;采用隊列模式將并發訪問變成串行訪問。 2、 Redis不僅僅支持簡單的k/v類型的數據&#xff0c;同時還提供list&#xff0c;set&#xff0c;zset&#xff0c;hash等數據結構的存儲。 3、 Redis支持數據的備份&#xff0c;即mas…

大omega記號_什么是大歐米茄符號?

大omega記號Similar to big O notation, big Omega(Ω) function is used in computer science to describe the performance or complexity of an algorithm.與大O表示法相似&#xff0c;大Omega(Ω)函數在計算機科學中用于描述算法的性能或復雜性。 If a running time is Ω…