python怎么实现数据的异常值的处理
发布网友
发布时间:2022-04-23 03:09
我来回答
共2个回答
热心网友
时间:2022-04-24 13:42
异常值也称离群值,具体地说,判断标准依据实际情况,根据业务知识及实际需要而定.
要是一般地说,可以用公式计算:
upper adjacent value = 75th percentile + (75th percentile – 25th percentile) * 1.5
lower adjacent value = 25th percentile – (75th percentile – 25th percentile) * 1.5
翻译过来:
上界=75%分位数+(75%分位数-25%分位数)*1.5
下界=25%分位数- (75%分位数-25%分位数)*1.5
比上界大的,和比下界小的都是异常值.
热心网友
时间:2022-04-24 15:00
# coding=utf-8
import re
import requests
from lxml import etree
import pymongo
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def getpages(url, total):
nowpage = int(re.search('(\d+)', url, re.S).group(1))
urls = []
for i in range(nowpage, total + 1):
link = re.sub('(\d+)', '%s' % i, url, re.S)
urls.append(link)
return urls
def spider(url):
html = requests.get(url)
selector = etree.HTML(html.text)
book_name = selector.xpath('//*[@id="container"]/ul/li//div/div[2]/a/text()')
book_author = selector.xpath('//*[@id="container"]/ul/li//div/div[2]/div/a/text()')
saveinfo(book_name, book_author)
def saveinfo(book_name, book_author):
connection = pymongo.MongoClient()
BookDB = connection.BookDB
BookTable = BookDB.books
length = len(book_name)
for i in range(0, length):
books = {}
books['name'] = str(book_name[i]).replace('\n','')
books['author'] = str(book_author[i]).replace('\n','')
BookTable.insert_one(books)
if __name__ == '__main__':
url = 'http://readfree.me/shuffle/?page=1'
urls = getpages(url,3)
for each in urls:
spider(each)