日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习-数据科学库(第五天)

發(fā)布時間:2025/3/21 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习-数据科学库(第五天) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

31.數(shù)據(jù)的合并和分組聚合——字符串離散化的案例

字符串離散化的案例

剛剛我們學(xué)會了數(shù)據(jù)分合并,那么接下來,我們按照電影分類(genre)信息把數(shù)據(jù)呈現(xiàn)出來

import numpy as np import pandas as pd from matplotlib import pyplot as plt file_path = "/Users/zhucan/Desktop/IMDB-Movie-Data.csv" df = pd.read_csv(file_path)temp_list = df["Genre"].str.split(",").tolist() genre_list = list(set([i for j in temp_list for i in j]))#構(gòu)造全為0的數(shù)組 zeros_df = pd.DataFrame(np.zeros((df.shape[0],len(genre_list))),columns=genre_list) print(zeros_df)#給每個電影出現(xiàn)分類的位置賦值1 for i in range(df.shape[0]):zeros_df.loc[i,temp_list[i]] = 1#統(tǒng)計(jì)每個分類的電影的數(shù)量和 genre_count = zeros_df.sum(axis=0) print(genre_count)genre_count = genre_count.sort_values() _x = genre_count.index _y = genre_count.valuesplt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(_x)),_y) plt.xticks(range(len(_x)),_x) plt.show()

32.數(shù)據(jù)的合并和分組聚合——數(shù)據(jù)合并

數(shù)據(jù)合并之join

數(shù)據(jù)合并之merge?

merge:按照指定的列把數(shù)據(jù)按照一定的方式合并到一起?

默認(rèn)的合并方式inner,交集?

merge outer,并集,NaN補(bǔ)全

merge left,左邊為準(zhǔn),NaN補(bǔ)全。merge right,右邊為準(zhǔn),NaN補(bǔ)全?

33.數(shù)據(jù)分組聚合

數(shù)據(jù)分組聚合

現(xiàn)在我們有一組關(guān)于全球星巴克店鋪的統(tǒng)計(jì)數(shù)據(jù),如果我想知道美國的星巴克數(shù)量和中國的哪個多,或者我想知道中國每個省份星巴克的數(shù)量的情況,那么應(yīng)該怎么辦?

import pandas as pd import numpy as np file_path = "/Users/zhucan/Desktop/starbucks_store_worldwide.csv" df = pd.read_csv(file_path) grouped = df.groupby(by="Country")country_count = grouped["Brand"].count() print(country_count["US"]) print(country_count["CN"]) 13608 2734

34.數(shù)據(jù)分組聚合02

數(shù)據(jù)分組聚合02

import pandas as pd import numpy as np file_path = "/Users/zhucan/Desktop/starbucks_store_worldwide.csv" df = pd.read_csv(file_path)china_data = df[df["Country"] == "CN"] grouped = china_data.groupby(by = "State/Province").count()["Brand"] print(grouped)

分組和聚合

多條件分組?

如果我們需要對國家和省份進(jìn)行分組統(tǒng)計(jì),應(yīng)該怎么操作呢?

grouped = df.groupby(by=[df["Country"],df["State/Province"]])

很多時候我們只希望對獲取分組之后的某一部分?jǐn)?shù)據(jù),或者說我們只希望對某幾列數(shù)據(jù)進(jìn)行分組,這個時候我們應(yīng)該怎么辦呢? 獲取分組之后的某一部分?jǐn)?shù)據(jù): ?

df.groupby(by=["Country","State/Province"])["Country"].count()

對某幾列數(shù)據(jù)進(jìn)行分組: ?

df["Country"].groupby(by=[df["Country"],df["State/Province"]]).count()?

觀察結(jié)果,由于只選擇了一列數(shù)據(jù),所以結(jié)果是一個Series類型 如果我想返回一個DataFrame類型呢?

df[["Country"]].groupby(by=[df["Country"],df["State/Province"]]).count()?

35.數(shù)據(jù)的索引學(xué)習(xí)

索引和復(fù)合索引

  • 簡單的索引操作: 獲取index:df.index
  • 指定index :df.index = ['x','y']
  • 重新設(shè)置index : df.reindex(list("abcedf"))
  • 指定某一列作為index :df.set_index("Country",drop=False)? ?country作為索引
  • 返回index的唯一值:df.set_index("Country").index.unique()
  • import pandas as pd import numpy as np a = pd.DataFrame({'a': range(7),'b': range(7, 0, -1),'c': ['one','one','one','two','two','two', 'two'],'d': list("hjklmno")}) print(a) b = a.set_index(["c","d"]) print(b) a b c d 0 0 7 one h 1 1 6 one j 2 2 5 one k 3 3 4 two l 4 4 3 two m 5 5 2 two n 6 6 1 two oa b c d one h 0 7j 1 6k 2 5 two l 3 4m 4 3n 5 2o 6 1

    36.數(shù)據(jù)分組聚合練習(xí)和總結(jié)

    Series復(fù)合索引

    DataFrame復(fù)合索引

    使用matplotlib呈現(xiàn)出店鋪總數(shù)排名前10的國家

    import matplotlib.pyplot as plt import pandas as pd import numpy as np file_path ="/Users/zhucan/Desktop/starbucks_store_worldwide.csv" df = pd.read_csv(file_path)data1 = df.groupby(by="Country").count()["Brand"].sort_values(ascending=False)[:10] _x = data1.index _y = data1.valuesplt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(_x)),_y) plt.xticks(range(len(_x)),_x) plt.show()

    使用matplotlib呈現(xiàn)出每個中國每個城市的店鋪數(shù)量?

    import matplotlib.pyplot as plt import pandas as pd import numpy as np from matplotlib import font_manager file_path ="/Users/zhucan/Desktop/starbucks_store_worldwide.csv" my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc") df = pd.read_csv(file_path) df = df[df["Country"]=="CN"]data1 = df.groupby(by="City").count()["Brand"].sort_values(ascending=False)[:25]_x = data1.index _y = data1.valuesplt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(_x)),_y,width=0.3,color="orange") plt.xticks(range(len(_x)),_x,fontproperties=my_font) plt.show()

    現(xiàn)在我們有全球排名靠前的10000本書的數(shù)據(jù),那么請統(tǒng)計(jì)一下下面幾個問題:

  • 不同年份書的數(shù)量
  • 不同年份書的平均評分情況
  • import matplotlib.pyplot as plt import pandas as pd import numpy as np from matplotlib import font_manager file_path = "/Users/zhucan/Desktop/books.csv" df = pd.read_csv(file_path)#不同年份書的數(shù)量 print(df.info()) data1 = df[pd.notnull(df["original_publication_year"])] grouped = data1.groupby(by="original_publication_year").count()["title"]#不同年份書的平均評分情況 data1 = df[pd.notnull(df["original_publication_year"])] grouped = data1["average_rating"].groupby(by=data1["original_publication_year"]).mean()_x = grouped.index _y = grouped.valuesplt.figure(figsize=(20,8),dpi=80) plt.plot(range(len(_x)),_y) plt.xticks(list(range(len(_x)))[::10],_x[::10],rotation=45) plt.show()

    總結(jié)

    以上是生活随笔為你收集整理的机器学习-数据科学库(第五天)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。