python如何去除異常值和缺失值的插值
1.使用箱型法去除異常值:
import numpy as np
import pandas as pd
import matplotlib as plt
import os
data = pd.read_excel('try.xls', header=0)
# print(data.shape)
# print(data.head(10))
# print(data.describe())
neg_list = ['位移']
print("(1)數(shù)據(jù)的行數(shù)為:")
R = data.shape[0]
print(R)
print("(2)小于或大于閾值的數(shù)據(jù)提取:")
for item in neg_list:
neg_item = data[item]<2000
print(item + '小于2000的有' + str(neg_item.sum()) + '個(gè)')
print("(3)異常值的個(gè)數(shù):")
for item in neg_list:
iqr = data[item].quantile(0.75) - data[item].quantile(0.25)
q_abnormal_L = data[item] < data[item].quantile(0.25) - 1.5 * iqr
q_abnormal_U = data[item] > data[item].quantile(0.75) + 1.5 * iqr
print(item + '中有' + str(q_abnormal_L.sum() + q_abnormal_U.sum()) + '個(gè)異常值')
print("(4)箱型圖確定上下限:")
for item in neg_list:
iqr = data[item].quantile(0.75) - data[item].quantile(0.25)
Too_small = data[item].quantile(0.25) - 1.5 * iqr
Too_big = data[item].quantile(0.25) + 1.5 * iqr
print("下限是", Too_small)
print("上限是", Too_big )
print("(5)所有數(shù)據(jù)為:")
a = []
for i in neg_list:
a.append(data[i])
print(a)
print("(6)所有正常數(shù)據(jù):")
b = []
j = 0
while j < R:
if (a[0][j] > Too_small):
if (a[0][j] < Too_big):
b.append(a[0][j])
j += 1
print(b)
print("(7)所有異常數(shù)據(jù):")
c = []
i = 0
while i < R:
if (a[0][i] < Too_small or a[0][i] > Too_big):
c.append(a[0][i])
a[0][i] = None
i +=1
print(c)
print("(8)把所有異常數(shù)據(jù)刪除后:")
print(a)
print("(9)所有數(shù)據(jù)處理后輸出:")
d = []
k = 0
while k < R:
d.append(a[0][k])
k +=1
print(d)
df = pd.DataFrame(d,columns= ['位移'])
df.to_excel("try_result.xls")
2.拉格朗日插值:
import os
import pandas as pd
import numpy as np
from scipy.interpolate import lagrange
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用來(lái)正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus']=False #用來(lái)正常顯示負(fù)號(hào)
# 數(shù)據(jù)的讀取
data = pd.read_excel('try.xls', header=0)
neg_list = ['位移']
# 數(shù)據(jù)的行數(shù)
R = data.shape[0]
# 異常數(shù)據(jù)的個(gè)數(shù)
for item in neg_list:
iqr = data[item].quantile(0.75) - data[item].quantile(0.25)
q_abnormal_L = data[item] < data[item].quantile(0.25) - 1.5 * iqr
q_abnormal_U = data[item] > data[item].quantile(0.75) + 1.5 * iqr
# print(item + '中有' + str(q_abnormal_L.sum() + q_abnormal_U.sum()) + '個(gè)異常值')
# 確定數(shù)據(jù)上限和下限
for item in neg_list:
iqr = data[item].quantile(0.75) - data[item].quantile(0.25)
Too_small = data[item].quantile(0.25) - 1.5 * iqr
Too_big = data[item].quantile(0.25) + 1.5 * iqr
data[u'位移'][(data[u'位移']<Too_small) | (data[u'位移']>Too_big)] = None #過(guò)濾異常值,將其變?yōu)榭罩?
#s為列向量,n為被插值位置,k為取前后的數(shù)據(jù)個(gè)數(shù)
def ployinter(s,n,k=5):
y = s[list(range(n-k,n)) + list(range(n+1,n+1+k))]
y = y[y.notnull()] #剔除空值
return lagrange(y.index,list(y))(n)
#逐個(gè)元素判斷是否需要插值
for i in data.columns:
for j in range(len(data)):
if(data[i].isnull())[j]:
data[i][j] = ployinter(data[i],j)
# print(data[u'位移'])
# 輸出拉格朗日插值后的數(shù)據(jù)
data.to_excel("try_result.xls")
# 把表格列數(shù)據(jù)調(diào)整為arr,arr為修改后的數(shù)據(jù)
print("拉格朗日插值后的數(shù)據(jù):")
d = []
k = 0
while k < R:
d.append(data[u'位移'][k])
k +=1
# print(d)
arr = np.array(d)
print(arr)
# 輸出圖像
x = np.arange(len(d))
plt.plot(x,d,'b-',label="one", marker='*',markersize=4,linewidth=1) # b代表blue顏色 -代表直線(xiàn)
plt.title('位移曲線(xiàn)')
plt.legend(loc='upper left',bbox_to_anchor=(1.0,1.0))
# 直接更改X軸坐標(biāo)數(shù)
# plt.xticks((0,1,2,3,4,5,6,7,8),('0', '1', '2', '3', '4', '5', '6', '7', '8'))
plt.xlabel('時(shí)間/h')
plt.ylabel('位移/mm')
#plt.grid(x1)
plt.show()
3.數(shù)據(jù)擬合:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
def Fun(p, x): # 定義擬合函數(shù)形式
a1, a2, a3 , a4 = p
return a1 * x ** 3 + a2 * x ** 2 + a3 * x + a4
def error(p, x, y): # 擬合殘差
return Fun(p, x) - y
def main():
x = np.linspace(1, 31, 31) # 創(chuàng)建時(shí)間序列
data = pd.read_excel('try.xls', header=0)
y = data[u'位移']
p0 = [0.1, -0.01, 100, 1000] # 擬合的初始參數(shù)設(shè)置
para = leastsq(error, p0, args=(x, y)) # 進(jìn)行擬合
y_fitted = Fun(para[0], x) # 畫(huà)出擬合后的曲線(xiàn)
plt.figure
plt.plot(x, y, 'r', label='Original curve')
plt.plot(x, y_fitted, '-b', label='Fitted curve')
plt.legend()
plt.show()
print(para[0])
if __name__ == '__main__':
main()
4.輸出圖像:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用來(lái)正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus']=False #用來(lái)正常顯示負(fù)號(hào)
jiaodu = ['0', '15', '30', '15', '60', '75', '90', '105', '120']
x = range(len(jiaodu))
y = [85.6801, 7.64586, 86.0956, 159.229, 179.534, 163.238, 96.4436, 10.1619, 90.9262,]
#plt.figure(figsize=(10, 6))
plt.plot(x,y,'b-',label="1", marker='*',markersize=7,linewidth=3) # b代表blue顏色 -代表直線(xiàn)
plt.title('各個(gè)區(qū)域亮度變化')
plt.legend(loc='upper left',bbox_to_anchor=(1.0,1.0))
plt.xticks((0,1,2,3,4,5,6,7,8),('0', '15', '30', '15', '60', '75', '90', '105', '120'))
plt.xlabel('角度')
plt.ylabel('亮度')
#plt.grid(x1)
plt.show()
到此這篇關(guān)于python如何去除異常值和缺失值的插值的文章就介紹到這了,更多相關(guān)python去除異常值和缺失值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python缺失值填充方法示例代碼
- Python數(shù)據(jù)預(yù)處理時(shí)缺失值的不同處理方式總結(jié)
- Python?數(shù)據(jù)清洗刪除缺失值替換缺失值詳情
- python?sklearn與pandas實(shí)現(xiàn)缺失值數(shù)據(jù)預(yù)處理流程詳解
- Python處理缺失值的8種不同方法實(shí)例
- Python缺失值處理方法
- Python3?DataFrame缺失值的處理方法
- Python?Pandas中缺失值NaN的判斷,刪除及替換
- Python數(shù)據(jù)分析之缺失值檢測(cè)與處理詳解
- Python數(shù)據(jù)分析的八種處理缺失值方法詳解
- python缺失值的解決方法總結(jié)
- Python中查找缺失值的三種方法
相關(guān)文章
TensorFlow命名空間和TensorBoard圖節(jié)點(diǎn)實(shí)例
今天小編就為大家分享一篇TensorFlow命名空間和TensorBoard圖節(jié)點(diǎn)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python實(shí)現(xiàn)微信定時(shí)每天和女友發(fā)送消息
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信定時(shí)每天和女友發(fā)送消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
TensorFlow Session會(huì)話(huà)控制&Variable變量詳解
今天小編就為大家分享一篇TensorFlow Session會(huì)話(huà)控制&Variable變量詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python實(shí)現(xiàn)將一段話(huà)txt生成字幕srt文件
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)將一段話(huà)txt生成字幕srt文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02

