您好,登錄后才能下訂單哦!
python怎么去除異常值和缺失值的插值,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
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ù)提?。?quot;) 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'] #用來正常顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus']=False #用來正常顯示負(fù)號 # 數(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 #過濾異常值,將其變?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顏色 -代表直線 plt.title('位移曲線') 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) # 畫出擬合后的曲線 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'] #用來正常顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus']=False #用來正常顯示負(fù)號 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顏色 -代表直線 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怎么去除異常值和缺失值的插值問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。