久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

Python的JupyterNotebook舉例分析

本篇內(nèi)容主要講解“Python的Jupyter Notebook舉例分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Python的Jupyter Notebook舉例分析”吧!

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供鶴山網(wǎng)站建設(shè)、鶴山做網(wǎng)站、鶴山網(wǎng)站設(shè)計(jì)、鶴山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、鶴山企業(yè)網(wǎng)站模板建站服務(wù),10余年鶴山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

1.Jupyter Notebook基本介紹

Jupyter Notebook(此前被稱為IPython notebook)是一個(gè)交互式筆記本,支持運(yùn)行40多種編程語言。

在開始使用notebook之前,需要先安裝該庫:(1)在命令行中執(zhí)行pip install jupyter來安裝;(2)安裝Anaconda后自帶Jupyter Notebook。

在命令行中執(zhí)行jupyter notebook,就會(huì)在當(dāng)前目錄下啟動(dòng)Jupyter服務(wù)并使用默認(rèn)瀏覽器打開頁面,還可以復(fù)制鏈接在其他瀏覽器中打開。

notebook界面由以下部分組成:(1)notebook名稱;(2)主工具欄,提供了保存、導(dǎo)出、重載notebook,以及重啟內(nèi)核等選項(xiàng);(3)notebook主要區(qū)域,包含了notebook的內(nèi)容編輯區(qū)。

2.Jupyter Notebook的使用

在Jupyter頁面下方的主要區(qū)域,由被稱為單元格的部分組成。每個(gè)notebook由多個(gè)單元格構(gòu)成,而每個(gè)單元格又可以有不同的用途。上圖中看到的是一個(gè)代碼單元格(code cell),以[ ]開頭,在這種類型的單元格中,可以輸入任意代碼并執(zhí)行。例如,輸入1 + 2并按下Shift + Enter,單元格中的代碼就會(huì)被計(jì)算,光標(biāo)也會(huì)被移動(dòng)到一個(gè)新的單元格中。

如果想新建一個(gè)notebook,只需要點(diǎn)擊New,選擇希望啟動(dòng)的notebook類型即可。

notebook可以修改之前的單元格,對(duì)其重新計(jì)算,這樣就可以更新整個(gè)文檔了。如果你不想重新運(yùn)行整個(gè)腳本,只想用不同的參數(shù)測(cè)試某個(gè)程式的話,這個(gè)特性顯得尤其強(qiáng)大。不過,也可以重新計(jì)算整個(gè)notebook,只要點(diǎn)擊Cell -> Run all即可。

再測(cè)試標(biāo)題和其他代碼如下:

Python的Jupyter Notebook舉例分析

可以看到,在頂部添加了一個(gè)notebook的標(biāo)題,還可以執(zhí)行for循環(huán)等語句。

3.Jupyter中使用Python

Jupyter測(cè)試Python變量和數(shù)據(jù)類型如下:

Python的Jupyter Notebook舉例分析

測(cè)試Python模塊如下:

Python的Jupyter Notebook舉例分析

數(shù)據(jù)讀寫很重要,因?yàn)檫M(jìn)行數(shù)據(jù)分析時(shí)必須先讀取數(shù)據(jù),進(jìn)行數(shù)據(jù)處理后也要進(jìn)行保存。

4.數(shù)據(jù)交互案例

加載csv數(shù)據(jù),處理數(shù)據(jù),保存到MongoDB數(shù)據(jù)庫

有csv文件shopproducts.csv和userratings.csv,分別是商品數(shù)據(jù)和用戶評(píng)分?jǐn)?shù)據(jù),如下:

Python的Jupyter Notebook舉例分析

現(xiàn)在需要通過Python將其讀取出來,并將指定的字段保存到MongoDB中,需要在Anaconda中執(zhí)行命令conda install pymongo安裝pymongo。

Python代碼如下:

import pymongo


class Product:
    def __init__(self,productId:int ,name, imageUrl, categories, tags):
        self.productId = productId
        self.name = name
        self.imageUrl = imageUrl
        self.categories = categories
        self.tags = tags

    def __str__(self) -> str:
        return self.productId +'^' + self.name +'^' + self.imageUrl +'^' + self.categories +'^' + self.tags


class Rating:
    def __init__(self, userId:int, productId:int, score:float, timestamp:int):
        self.userId = userId
        self.productId = productId
        self.score = score
        self.timestamp = timestamp

    def __str__(self) -> str:
        return self.userId +'^' + self.productId +'^' + self.score +'^' + self.timestamp


if __name__ == '__main__':
    myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017/")
    mydb = myclient["goods-users"]
    ## val attr = item.split("\\^")
    ## // 轉(zhuǎn)換成Product
    ## Product(attr(0).toInt, attr(1).trim, attr(4).trim, attr(5).trim, attr(6).trim)

    shopproducts = mydb['shopproducts']
    with open('shopproducts.csv', 'r',encoding='UTF-8') as f:
        item = f.readline()
        while item:
            attr = item.split('^')
            product = Product(int(attr[0]), attr[1].strip(), attr[4].strip(), attr[5].strip(), attr[6].strip())
            shopproducts.insert_one(product.__dict__)
            ## print(product)
            ## print(json.dumps(obj=product.__dict__,ensure_ascii=False))
            item = f.readline()

    ## val attr = item.split(",")
    ## Rating(attr(0).toInt, attr(1).toInt, attr(2).toDouble, attr(3).toInt)
    userratings = mydb['userratings']
    with open('userratings.csv', 'r',encoding='UTF-8') as f:
        item = f.readline()
        while item:
            attr = item.split(',')
            rating = Rating(int(attr[0]), int(attr[1].strip()), float(attr[2].strip()), int(attr[3].strip()))
            userratings.insert_one(rating.__dict__)
            ## print(rating)
            item = f.readline()

在啟動(dòng)MongoDB服務(wù)后,運(yùn)行Python代碼,運(yùn)行完成后,再通過Robo 3T查看數(shù)據(jù)庫如下:

Python的Jupyter Notebook舉例分析

包括名稱、評(píng)論數(shù)、價(jià)格、地址、評(píng)分列表等,其中評(píng)論數(shù)、價(jià)格和評(píng)分均不規(guī)則、需要進(jìn)行數(shù)據(jù)清洗。

Jupyter中處理如下:

可以看到,最后得到了經(jīng)過清洗后的規(guī)則數(shù)據(jù)。

完整Python代碼如下:

## 數(shù)據(jù)讀取
f = open('商鋪數(shù)據(jù).csv', 'r', encoding='utf8')
for i in f.readlines()[1:15]:
    print(i.split(','))


## 創(chuàng)建comment、price、commentlist清洗函數(shù)
def fcomment(s):
    '''comment清洗函數(shù):用空格分段,選取結(jié)果list的第一個(gè)為點(diǎn)評(píng)數(shù),并且轉(zhuǎn)化為整型'''
    if '條' in s:
        return int(s.split(' ')[0])
    else:
        return '缺失數(shù)據(jù)'


def fprice(s):
    '''price清洗函數(shù):用¥分段,選取結(jié)果list的最后一個(gè)為人均價(jià)格,并且轉(zhuǎn)化為浮點(diǎn)型'''
    if '¥' in s:
        return float(s.split('¥')[-1])
    else:
        return '缺失數(shù)據(jù)'


def fcommentl(s):
    '''commentlist清洗函數(shù):用空格分段,分別清洗出質(zhì)量、環(huán)境及服務(wù)數(shù)據(jù),并轉(zhuǎn)化為浮點(diǎn)型'''
    if ' ' in s:
        quality = float(s.split('                                ')[0][2:])
        environment = float(s.split('                                ')[1][2:])
        service = float(s.split('                                ')[2][2:-1])
        return [quality, environment, service]
    else:
        return '缺失數(shù)據(jù)'


## 數(shù)據(jù)處理清洗
datalist = []  ## 創(chuàng)建空列表

f.seek(0)
n = 0  ## 創(chuàng)建計(jì)數(shù)變量
for i in f.readlines():
    data = i.split(',')
    ## print(data)
    classify = data[0]  ## 提取分類
    name = data[1]  ## 提取店鋪名稱
    comment_count = fcomment(data[2])  ## 提取評(píng)論數(shù)量
    star = data[3]  ## 提取星級(jí)
    price = fprice(data[4])  ## 提取人均
    address = data[5]  ## 提取地址
    quality = fcommentl(data[6])[0]  ## 提取質(zhì)量評(píng)分
    env = fcommentl(data[6])[1]  ## 提取環(huán)境評(píng)分
    service = fcommentl(data[6])[2]  ## 提取服務(wù)評(píng)分
    if '缺失數(shù)據(jù)' not in [comment_count, price, quality]:  ## 用于判斷是否有數(shù)據(jù)缺失
        n += 1
        data_re = [['classify', classify],
                   ['name', name],
                   ['comment_count', comment_count],
                   ['star', star],
                   ['price', price],
                   ['address', address],
                   ['quality', quality],
                   ['environment', env],
                   ['service', service]]
        datalist.append(dict(data_re))  ## 字典生成,并存入列表datalist
        print('成功加載%i條數(shù)據(jù)' % n)
    else:
        continue

print(datalist)
print('總共加載%i條數(shù)據(jù)' % n)

f.close()

到此,相信大家對(duì)“Python的Jupyter Notebook舉例分析”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

新聞標(biāo)題:Python的JupyterNotebook舉例分析
網(wǎng)站URL:http://sd-ha.com/article26/josdcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)、網(wǎng)站制作定制網(wǎng)站、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作