最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python如何去除異常值和缺失值的插值

 更新時間:2022年01月27日 10:55:52   作者:Word_gebei  
大家好,本篇文章主要講的是python如何去除異常值和缺失值的插值,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下

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()) + '個')
print("(3)異常值的個數(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()) + '個異常值')
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ù)的個數(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()) + '個異常值')

# 確定數(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ù)個數(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)

#逐個元素判斷是否需要插值
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('時間/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)建時間序列
    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('各個區(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python執(zhí)行JS代碼的三種方式

    Python執(zhí)行JS代碼的三種方式

    以前的數(shù)據(jù)靠買,現(xiàn)在的數(shù)據(jù)靠爬”,越來越多的學(xué)者通過網(wǎng)絡(luò)爬蟲來獲取數(shù)據(jù),但是做爬蟲的人都知道,現(xiàn)在的很多網(wǎng)站都在和我們斗智斗勇,防護(hù)普遍越來越好,破解JS加密只是第一步,之后就是如何在我們的Python代碼中直接執(zhí)行JS,下面介紹一下幾種Python中執(zhí)行JS代碼的方法
    2024-01-01
  • TensorFlow命名空間和TensorBoard圖節(jié)點實例

    TensorFlow命名空間和TensorBoard圖節(jié)點實例

    今天小編就為大家分享一篇TensorFlow命名空間和TensorBoard圖節(jié)點實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python實現(xiàn)微信定時每天和女友發(fā)送消息

    python實現(xiàn)微信定時每天和女友發(fā)送消息

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)微信定時每天和女友發(fā)送消息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Pytorch創(chuàng)建張量的四種方法

    Pytorch創(chuàng)建張量的四種方法

    Pytorch創(chuàng)建張量的4種方法主要有:torch.Tensor()、torch.tensor()、torch.as_tensor()、torch.from_numpy(),本文通過實例代碼介紹Pytorch創(chuàng)建張量的四種方法,需要的朋友可以參考下
    2023-05-05
  • 在cmd中查看python的安裝路徑方法

    在cmd中查看python的安裝路徑方法

    在本篇文章里小編給大家整理的是關(guān)于怎樣在cmd中查看python的安裝路徑的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)參考下。
    2019-07-07
  • python使用建議與技巧分享(一)

    python使用建議與技巧分享(一)

    這篇文章主要介紹了python使用建議與技巧分享,幫助大家更高效的使用python,感興趣的朋友可以了解下
    2020-08-08
  • TensorFlow Session會話控制&Variable變量詳解

    TensorFlow Session會話控制&Variable變量詳解

    今天小編就為大家分享一篇TensorFlow Session會話控制&Variable變量詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python字符串拼接、截取及替換方法總結(jié)分析

    Python字符串拼接、截取及替換方法總結(jié)分析

    這篇文章主要介紹了Python字符串拼接、截取及替換方法,結(jié)合實例形式總結(jié)分析了Python針對字符串的拼接、截取與替換的原理與常見使用技巧,需要的朋友可以參考下
    2016-04-04
  • Python實現(xiàn)將一段話txt生成字幕srt文件

    Python實現(xiàn)將一段話txt生成字幕srt文件

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實現(xiàn)將一段話txt生成字幕srt文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-02-02
  • Python?langchain?ReAct?使用范例詳解

    Python?langchain?ReAct?使用范例詳解

    這篇文章主要介紹了Python?langchain?ReAct?使用范例,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-12-12

最新評論

连江县| 海淀区| 柘荣县| 定安县| 于田县| 惠州市| 台州市| 沾益县| 潢川县| 尚义县| 黎城县| 昌乐县| 古蔺县| 张家口市| 民和| 响水县| 雅安市| 中方县| 藁城市| 塘沽区| 准格尔旗| 新河县| 德令哈市| 光泽县| 大兴区| 大姚县| 台山市| 苏尼特右旗| 汨罗市| 海淀区| 招远市| 兖州市| 鄂托克前旗| 申扎县| 邻水| 洞口县| 吉木乃县| 侯马市| 大同县| 双牌县| 佳木斯市|