0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Python語(yǔ)言在數(shù)據(jù)分析、挖掘場(chǎng)景中常用特性

馬哥Linux運(yùn)維 ? 來(lái)源:未知 ? 作者:李倩 ? 2018-03-31 09:16 ? 次閱讀

Python語(yǔ)言:

簡(jiǎn)要概括一下Python語(yǔ)言在數(shù)據(jù)分析、挖掘場(chǎng)景中常用特性:

列表(可以被修改),元組(不可以被修改)

字典(結(jié)構(gòu))

集合(同數(shù)學(xué)概念上的集合)

函數(shù)式編程(主要由lambda()、map()、reduce()、filter()構(gòu)成)

Python數(shù)據(jù)分析常用庫(kù):

Python數(shù)據(jù)挖掘相關(guān)擴(kuò)展庫(kù)

NumPy

提供真正的數(shù)組,相比Python內(nèi)置列表來(lái)說(shuō)速度更快,NumPy也是Scipy、Matplotlib、Pandas等庫(kù)的依賴庫(kù),內(nèi)置函數(shù)處理數(shù)據(jù)速度是C語(yǔ)言級(jí)別的,因此使用中應(yīng)盡量使用內(nèi)置函數(shù)。

示例:NumPy基本操作

import numpy as np # 一般以np為別名 a = np.array([2, 0, 1, 5]) print(a) print(a[:3]) print(a.min()) a.sort() # a被覆蓋 print(a) b = np.array([[1, 2, 3], [4, 5, 6]]) print(b*b)

輸出:

[2 0 1 5] [2 0 1] 0 [0 1 2 5] [[ 1 4 9] [16 25 36]]

Scipy

NumPy和Scipy讓Python有了MATLAB味道。Scipy依賴于NumPy,NumPy提供了多維數(shù)組功能,但只是一般的數(shù)組并不是矩陣。比如兩個(gè)數(shù)組相乘時(shí),只是對(duì)應(yīng)元素相乘。Scipy提供了真正的矩陣,以及大量基于矩陣運(yùn)算的對(duì)象與函數(shù)。

Scipy包含功能有最優(yōu)化、線性代數(shù)、積分、插值、擬合、特殊函數(shù)、快速傅里葉變換、信號(hào)處理、圖像處理、常微分方程求解等常用計(jì)算。

示例:Scipy求解非線性方程組和數(shù)值積分

# 求解方程組 from scipy.optimize import fsolve def f(x): x1 = x[0] x2 = x[1] return [2 * x1 - x2 ** 2 - 1, x1 ** 2 - x2 - 2] result = fsolve(f, [1, 1]) print(result) # 積分 from scipy import integrate def g(x): # 定義被積函數(shù) return (1 - x ** 2) ** 0.5 pi_2, err = integrate.quad(g, -1, 1) # 輸出積分結(jié)果和誤差 print(pi_2 * 2, err)

輸出:

[ 1.91963957 1.68501606] 3.141592653589797 1.0002356720661965e-09

Matplotlib

Python中著名的繪圖庫(kù),主要用于二維繪圖,也可以進(jìn)行簡(jiǎn)單的三維繪圖。

示例:Matplotlib繪圖基本操作

import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 10000) # 自變量x,10000為點(diǎn)的個(gè)數(shù) y = np.sin(x) + 1 # 因變量y z = np.cos(x ** 2) + 1 # 因變量z plt.figure(figsize=(8, 4)) # 設(shè)置圖像大小 # plt.rcParams['font.sans-serif'] = 'SimHei' # 標(biāo)簽若有中文,則需設(shè)置字體 # plt.rcParams['axes.unicode_minus'] = False # 保存圖像時(shí)若負(fù)號(hào)顯示不正常,則添加該句 # 兩條曲線 plt.plot(x, y, label='$\sin (x+1)$', color='red', linewidth=2) # 設(shè)置標(biāo)簽,線條顏色,線條大小 plt.plot(x, z, 'b--', label='$\cos x^2+1$') plt.xlim(0, 10) # x坐標(biāo)范圍 plt.ylim(0, 2.5) # y坐標(biāo)范圍 plt.xlabel("Time(s)") # x軸名稱 plt.ylabel("Volt") # y軸名稱 plt.title("Matplotlib Sample") # 圖的標(biāo)題 plt.legend() # 顯示圖例 plt.show() # 顯示作圖結(jié)果

輸出:

Pandas

Pandas是Python下非常強(qiáng)大的數(shù)據(jù)分析工具。它建立在NumPy之上,功能很強(qiáng)大,支持類似SQL的增刪改查,并具有豐富的數(shù)據(jù)處理函數(shù),支持時(shí)間序列分析功能,支持靈活處理缺失數(shù)據(jù)等。

Pandas基本數(shù)據(jù)結(jié)構(gòu)是Series和DataFrame。Series就是序列,類似一維數(shù)組,DataFrame則相當(dāng)于一張二維表格,類似二維數(shù)組,它每一列都是一個(gè)Series。為定位Series中的元素,Pandas提供了Index對(duì)象,類似主鍵。

DataFrame本質(zhì)上是Series的容器。

示例:Pandas簡(jiǎn)單操作

import pandas as pd s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) d = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]], columns=['a', 'b', 'c']) d2 = pd.DataFrame(s) print(s) print(d.head()) # 預(yù)覽前5行 print(d.describe()) # 讀取文件(路徑最好別帶中文) df=pd.read_csv("G:\\data.csv", encoding="utf-8") print(df)

輸出:

a 1 b 2 c 3 dtype: int64 a b c 0 1 2 3 1 4 5 6 2 7 8 9 3 10 11 12 4 13 14 15 a b c count 6.000000 6.000000 6.000000 mean 8.500000 9.500000 10.500000 std 5.612486 5.612486 5.612486 min 1.000000 2.000000 3.000000 25% 4.750000 5.750000 6.750000 50% 8.500000 9.500000 10.500000 75% 12.250000 13.250000 14.250000 max 16.000000 17.000000 18.000000 Empty DataFrame Columns: [1068, 12, 蔬果, 1201, 蔬菜, 120104, 花果, 20150430, 201504, DW-1201040010, 散稱, 生鮮, 千克, 0.973, 5.43, 2.58, 否] Index: []

Scikit-Learn

Scikit-Learn依賴NumPy、Scipy和Matplotlib,是Python中強(qiáng)大的機(jī)器學(xué)習(xí)庫(kù),提供了諸如數(shù)據(jù)預(yù)處理、分類、回歸、聚類、預(yù)測(cè)和模型分析等功能。

示例:創(chuàng)建線性回歸模型

from sklearn.linear_model import LinearRegression model= LinearRegression() print(model)

所有模型都提供的接口

model.fit():訓(xùn)練模型,監(jiān)督模型是fit(X,y),無(wú)監(jiān)督模型是fit(X)

監(jiān)督模型提供的接口:

model.predict(X_new):預(yù)測(cè)新樣本model.predict_proba(X_new):預(yù)測(cè)概率,僅對(duì)某些模型有用(LR)

無(wú)監(jiān)督模型提供的接口:

model.ransform():從數(shù)據(jù)中學(xué)到新的“基空間”model.fit_transform():從數(shù)據(jù)中學(xué)到的新的基,并將這個(gè)數(shù)據(jù)按照這組“基”進(jìn)行轉(zhuǎn)換

Scikit-Learn本身自帶了一些數(shù)據(jù)集,如花卉和手寫(xiě)圖像數(shù)據(jù)集等,下面以花卉數(shù)據(jù)集舉個(gè)栗子,訓(xùn)練集包含4個(gè)維度——萼片長(zhǎng)度、寬度,花瓣長(zhǎng)度和寬度,以及四個(gè)亞屬分類結(jié)果。

示例:

from sklearn import datasets # 導(dǎo)入數(shù)據(jù)集 from sklearn import svm iris = datasets.load_iris() # 加載數(shù)據(jù)集 clf = svm.LinearSVC() # 建立線性SVM分類器 clf.fit(iris.data, iris.target) # 用數(shù)據(jù)訓(xùn)練模型 print(clf.predict([[5, 3, 1, 0.2], [5.0, 3.6, 1.3, 0.25]]))

輸出:

[0 0]

Keras

Keras是基于Theano的深度學(xué)習(xí)庫(kù),它不僅可以搭建普通神經(jīng)網(wǎng)絡(luò),還可以搭建各種深度學(xué)習(xí)模型,如自編碼器、循環(huán)神經(jīng)網(wǎng)絡(luò)、遞歸神經(jīng)網(wǎng)絡(luò)、卷積神經(jīng)網(wǎng)絡(luò)等,運(yùn)行速度也很快,簡(jiǎn)化了搭建各種神經(jīng)網(wǎng)絡(luò)模型的步驟,允許普通用戶輕松搭建幾百個(gè)輸入節(jié)點(diǎn)的深層神經(jīng)網(wǎng)絡(luò),定制度也很高。

示例:簡(jiǎn)單的MLP(多層感知器)

from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import SGD model = Sequential() # 模型初始化 model.add(Dense(20, 64)) # 添加輸入層(20節(jié)點(diǎn))、第一隱藏層(64節(jié)點(diǎn))的連接 model.add(Activation('tanh')) # 第一隱藏層用tanh作為激活函數(shù) model.add(Dropout(0.5)) # 使用Dropout防止過(guò)擬合 model.add(Dense(64, 64)) # 添加第一隱藏層(64節(jié)點(diǎn))、第二隱藏層(64節(jié)點(diǎn))的連接 model.add(Activation('tanh')) # 第二隱藏層用tanh作為激活函數(shù) model.add(Dense(64, 1)) # 添加第二隱藏層(64節(jié)點(diǎn))、輸出層(1節(jié)點(diǎn))的連接 model.add(Activation('sigmod')) # 第二隱藏層用sigmod作為激活函數(shù) sgd=SGD(lr=0.1,decay=1e-6,momentum=0.9,nesterov=True) # 定義求解算法 model.compile(loss='mean_squared_error',optimizer=sgd) # 編譯生成模型,損失函數(shù)為平均誤差平方和 model.fit(x_train,y_train,nb_epoch=20,batch_size=16) # 訓(xùn)練模型 score = model.evaluate(X_test,y_test,batch_size=16) # 測(cè)試模型

參考:

Keras中文文檔

如何計(jì)算兩個(gè)文檔的相似度(二)

Genism

Genism主要用來(lái)處理語(yǔ)言方面的任務(wù),如文本相似度計(jì)算、LDA、Word2Vec等。

示例:

import logging from gensim import models logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) sentences = [['first', 'sentence'], ['second', 'sentence']] # 將分好詞的句子按列表形式輸入 model = models.Word2Vec(sentences, min_count=1) # 用以上句子訓(xùn)練詞向量模型 print(model['sentence']) # 輸出單詞sentence的詞向量

輸出:

2017-10-24 19:02:40,785 : INFO : collecting all words and their counts 2017-10-24 19:02:40,785 : INFO : PROGRESS: at sentence #0, processed 0 words, keeping 0 word types 2017-10-24 19:02:40,785 : INFO : collected 3 word types from a corpus of 4 raw words and 2 sentences 2017-10-24 19:02:40,785 : INFO : Loading a fresh vocabulary 2017-10-24 19:02:40,785 : INFO : min_count=1 retains 3 unique words (100% of original 3, drops 0) 2017-10-24 19:02:40,785 : INFO : min_count=1 leaves 4 word corpus (100% of original 4, drops 0) 2017-10-24 19:02:40,786 : INFO : deleting the raw counts dictionary of 3 items 2017-10-24 19:02:40,786 : INFO : sample=0.001 downsamples 3 most-common words 2017-10-24 19:02:40,786 : INFO : downsampling leaves estimated 0 word corpus (5.7% of prior 4) 2017-10-24 19:02:40,786 : INFO : estimated required memory for 3 words and 100 dimensions: 3900 bytes 2017-10-24 19:02:40,786 : INFO : resetting layer weights 2017-10-24 19:02:40,786 : INFO : training model with 3 workers on 3 vocabulary and 100 features, using sg=0 hs=0 sample=0.001 negative=5 window=5 2017-10-24 19:02:40,788 : INFO : worker thread finished; awaiting finish of 2 more threads 2017-10-24 19:02:40,788 : INFO : worker thread finished; awaiting finish of 1 more threads 2017-10-24 19:02:40,788 : INFO : worker thread finished; awaiting finish of 0 more threads 2017-10-24 19:02:40,789 : INFO : training on 20 raw words (0 effective words) took 0.0s, 0 effective words/s 2017-10-24 19:02:40,789 : WARNING : under 10 jobs per worker: consider setting a smaller `batch_words' for smoother alpha decay [ -1.54225400e-03 -2.45212857e-03 -2.20486755e-03 -3.64410551e-03 -2.28137174e-03 -1.70348200e-03 -1.05830852e-03 -4.37875278e-03 -4.97106137e-03 3.93485563e-04 -1.97932171e-03 -3.40653211e-03 1.54990738e-03 8.97102174e-04 2.94041773e-03 3.45200230e-03 -4.60584508e-03 3.81468004e-03 3.07120802e-03 2.85422982e-04 7.01598416e-04 2.69670971e-03 4.17246483e-03 -6.48593705e-04 1.11404411e-03 4.02203249e-03 -2.34672683e-03 2.35153269e-03 2.32632101e-05 3.76200466e-03 -3.95653257e-03 3.77303245e-03 8.48884694e-04 1.61545759e-03 2.53374409e-03 -4.25464474e-03 -2.06338940e-03 -6.84972096e-04 -6.92955102e-04 -2.27969326e-03 -2.13766913e-03 3.95324081e-03 3.52649018e-03 1.29243149e-03 4.29229392e-03 -4.34781052e-03 2.42843386e-03 3.12117115e-03 -2.99768522e-03 -1.17538485e-03 6.67148328e-04 -6.86432002e-04 -3.58940102e-03 2.40547652e-03 -4.18888079e-03 -3.12567432e-03 -2.51603196e-03 2.53451476e-03 3.65199335e-03 3.35336081e-03 -2.50071986e-04 4.15537134e-03 -3.89242987e-03 4.88173496e-03 -3.34603712e-03 3.18462006e-03 1.57053335e-04 3.51517834e-03 -1.20337342e-03 -1.81524854e-04 3.57784083e-05 -2.36600707e-03 -3.77405947e-03 -1.70441647e-03 -4.51521482e-03 -9.47134569e-04 4.53894213e-03 1.55767589e-03 8.57840874e-04 -1.12304837e-03 -3.95945460e-03 5.37869288e-04 -2.04461766e-03 5.24829782e-04 3.76719423e-03 -4.38512256e-03 4.81262803e-03 -4.20147832e-03 -3.87057988e-03 1.67581497e-03 1.51928759e-03 -1.31744961e-03 3.28474329e-03 -3.28777428e-03 -9.67226923e-04 4.62622894e-03 1.34165725e-03 3.60148447e-03 4.80416557e-03 -1.98963983e-03]

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 數(shù)據(jù)分析
    +關(guān)注

    關(guān)注

    2

    文章

    1395

    瀏覽量

    33915
  • python
    +關(guān)注

    關(guān)注

    53

    文章

    4753

    瀏覽量

    84105

原文標(biāo)題:Python數(shù)據(jù)分析、挖掘常用工具

文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    數(shù)據(jù)分析需要的技能

    將原始數(shù)據(jù)轉(zhuǎn)換成方便實(shí)用的格式,是數(shù)據(jù)分析師必備基礎(chǔ)能力,需要使用的工具有Excel、R語(yǔ)言以及python編程語(yǔ)言等;可視化報(bào)表是對(duì)創(chuàng)建和
    發(fā)表于 04-10 15:59

    常用10款數(shù)據(jù)分析編程語(yǔ)言

    增長(zhǎng),使得它更能夠用于先前為R語(yǔ)言保留的統(tǒng)計(jì)分析在數(shù)據(jù)處理中,在規(guī)模和復(fù)雜性之間往往會(huì)有一個(gè)權(quán)衡,于是Python成為了一種折中方案。IPython notebook和NumPy可以
    發(fā)表于 05-08 16:24

    python數(shù)據(jù)分析的類庫(kù)

    Python之所以這么流行,這么好用,就是因?yàn)?b class='flag-5'>Python提供了大量的第三方的庫(kù),開(kāi)箱即用,非常方便,而且還免費(fèi)哦,學(xué)Python的同學(xué)里估計(jì)有30%以上是為了做數(shù)據(jù)分析師或者
    發(fā)表于 05-10 15:18

    怎么有效學(xué)習(xí)Python數(shù)據(jù)分析

    Python在人工智能、機(jī)器學(xué)習(xí)領(lǐng)域受到火熱追捧,很大程度上在于它擁有非常龐大的第三方庫(kù),以及強(qiáng)大的通用編程性能。因此,快速掌握Python進(jìn)行數(shù)據(jù)分析,就是學(xué)習(xí)Python各種第三方
    發(fā)表于 06-28 15:18

    pandas數(shù)據(jù)分析的方法

    pandas數(shù)據(jù)分析中常用方法
    發(fā)表于 06-03 06:16

    python 數(shù)據(jù)分析基礎(chǔ) day12-python調(diào)用mysql

    python 數(shù)據(jù)分析基礎(chǔ) day12-python調(diào)用mysql
    發(fā)表于 10-23 13:34

    python數(shù)據(jù)分析之安裝mysql數(shù)據(jù)庫(kù)

    python 數(shù)據(jù)分析基礎(chǔ) day11-mysql安裝
    發(fā)表于 03-20 11:18

    如何利用Python進(jìn)行數(shù)據(jù)分析

    《利用Python進(jìn)行數(shù)據(jù)分析》 122高階GroupBy應(yīng)用
    發(fā)表于 04-23 07:29

    基于Python數(shù)據(jù)分析

    《利用Python進(jìn)行數(shù)據(jù)分析》 113日期范圍、頻率和移位
    發(fā)表于 05-01 11:24

    數(shù)據(jù)分析挖掘實(shí)戰(zhàn)》總結(jié)及代碼---chap3數(shù)據(jù)探索

    數(shù)據(jù)分析挖掘實(shí)戰(zhàn)》總結(jié)及代碼練習(xí)---chap3 數(shù)據(jù)探索
    發(fā)表于 05-25 13:25

    成為Python數(shù)據(jù)分析師,需要掌握哪些技能

    語(yǔ)言去構(gòu)建以數(shù)據(jù)為中心的應(yīng)用程序。其中:常用數(shù)據(jù)分析庫(kù)NumpyScipyPandasmatplotlib常用高級(jí)
    發(fā)表于 06-23 12:16

    成為Python數(shù)據(jù)分析師,需要掌握哪些技能

    語(yǔ)言去構(gòu)建以數(shù)據(jù)為中心的應(yīng)用程序。其中:常用數(shù)據(jù)分析庫(kù)NumpyScipyPandasmatplotlib常用高級(jí)
    發(fā)表于 06-30 11:42

    想做好數(shù)據(jù)分析,不用Python怎么行?

    Python因?yàn)槠湟鬃x、易學(xué)和高效有了今天的人氣,而人氣高的語(yǔ)言意味著更多的大牛會(huì)開(kāi)發(fā)Python相關(guān)庫(kù),以至于他的用途變得越來(lái)越廣,進(jìn)一步提升可用性。這種良性循環(huán)的氛圍才讓我徹底從一個(gè)excel
    的頭像 發(fā)表于 11-28 18:01 ?2979次閱讀
    想做好<b class='flag-5'>數(shù)據(jù)分析</b>,不用<b class='flag-5'>Python</b>怎么行?

    Python科學(xué)計(jì)算與數(shù)據(jù)分析

    Python科學(xué)計(jì)算與數(shù)據(jù)分析教材下載。
    發(fā)表于 06-01 14:38 ?22次下載

    python數(shù)據(jù)挖掘與機(jī)器學(xué)習(xí)

    python數(shù)據(jù)挖掘與機(jī)器學(xué)習(xí) Python是一個(gè)非常流行的編程語(yǔ)言,被廣泛用于數(shù)據(jù)
    的頭像 發(fā)表于 08-17 16:29 ?1203次閱讀