1. 程式人生 > 資料庫 >MySQL查詢練習2

MySQL查詢練習2

一。 陣列要比列表效率高很多
    numpy高效的處理資料,提供陣列的支援,python預設沒有陣列。pandas、scipy、matplotlib都依賴numpy。
    pandas主要用於資料探勘,探索,分析
    matplotlib用於作圖,視覺化
    scipy進行數值計算,如:積分,傅立葉變換,微積分
    statsmodels用於統計分析
    Gensim用於文字挖掘
    sklearn機器學習, keras深度學習
二。
    numpy和mkl 下載安裝
    pandas和maiplotlib網路安裝
    scipy 下載安裝
    statsmodels和Gensim網路安裝
三numpy的操作。
    
import numpy # 建立數一維陣列組 # numpy.array([元素1,元素2,......元素n]) x = numpy.array(['a', '9', '8', '1']) # 建立二維陣列格式 # numpy.array([[元素1,元素2,......元素n],[元素1,元素2,......元素n],[元素1,元素2,......元素n]]) y = numpy.array([[3,5,7],[9,2,6],[5,3,0]]) # 排序 x.sort() y.sort() # 取最大值 y1 = y.max()
# 取最小值 y2 = y.main() # 切片 四pandas的操作。 import pandas as pda # 使用pandas生成資料 # Series代表某一串資料 index指定行索引名稱,Series索引預設從零開始 # DataFrame代表行列整合出來的資料框,columns 指定列名 a = pda.Series([8, 9, 2, 1], index=['one', 'two', 'three', 'four']) # 以列表的格式建立資料框 b = pda.DataFrame([[5,6,2,3],[3,5,1,4],[7,9,3,5]], columns=['
one', 'two', 'three', 'four'],index=['one', 'two', 'three']) # 以字典的格式建立資料框 c = pda.DataFrame({ 'one':4, # 會自動補全 'two':[6,2,3], 'three':list(str(982)) }) # b.head(行數)# 預設取前5行頭 # b.tail(行數)# 預設取後5行尾 # b.describe() 統計資料的情況 count mean std min 25% max e = b.head() f = b.describe() # 資料的轉置,及行變成列,列變成行 g = b.T print(e) print(f) print(g) 五python資料的匯入 import pandas as pad f = open('d:/大.csv','rb') # 匯入csv a = pad.read_csv(f, encoding='python') # 顯示多少行多少列 a.shape() a.values[0][2] #第一行第三列 # 描述csv資料 b = a.describe() # 排序 c = a.sort_values() # 匯入excel d = pad.read_excel('d:/大.xls') print(d) print(d.describe()) # 匯入mysql import pymysql conn = pymysql.connect(host='localhost', user='root', passwd='root', db='') sql = 'select * from mydb' e = pad.read_sql(sql, conn) # 匯入html表格資料 需要先安裝 html5lib和bs4 g = pad.read_html('https://book.douban.com/subject/30258976/?icn=index-editionrecommend') # 匯入文字資料 h = pad.read_table('d:/lianjie.txt','rb', engine='python') print(h.describe()) 六matplotlib的使用 # 折線圖/散點圖用plot # 直方圖用hist import matplotlib.pylab as pyl import numpy as npy x = [1,2,4,6,8,9] y = [5,6,7,8,9,0] pyl.plot(x, y) #plot(x軸資料,y軸資料,展現形式) # o散點圖,預設是直線 c cyan青色 r red紅色 m magente品紅色 g green綠色 b blue藍色 y yellow黃色 w white白色 # -直線 --虛線 -. -.形式 :細小虛線 # s方形 h六角形 *星星 + 加號 x x形式 d菱形 p五角星 pyl.plot(x, y, 'D') pyl.title('name') #名稱 pyl.xlabel('xname') #x軸名稱 pyl.ylabel('yname') #y軸名稱 pyl.xlim(0,20) #設定x軸的範圍 pyl.ylim(2,22) #設定y軸的範圍 pyl.show() # 隨機數的生成 data = npy.random.random_integers(1,20,100) #(最小值,最大值,個數) # 生成具有正態分佈的隨機數 data2 = npy.random.normal(10.0, 1.0, 10000) #(均值,西格瑪,個數) # 直方圖hist pyl.hist(data) pyl.hist(data2) # 設定直方圖的上限下限 sty = npy.arange(2,20,2) #步長也表示直方圖的寬度 pyl.hist(data, sty, histtype='stepfilled') # 去除輪廓 # 子圖的繪製和使用 pyl.subplot(2, 2, 2) # (行,列,當前區域) x1 = [2,3,5,8,6,7] y1 = [2,3,5,9,6,7] pyl.plot(x1, y1) pyl.subplot(2, 2, 1) # (行,列,當前區域) x1 = [2,3,5,9,6,7] y1 = [2,3,5,9,6,7] pyl.plot(x1, y1) pyl.subplot(2, 1, 2) # (行,列,當前區域) x1 = [2,3,5,9,6,7] y1 = [2,3,9,5,6,7] pyl.plot(x1, y1) pyl.show()