numpy,scipy,matlabplotlib,pandas學習筆記

numpy參考:https://scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial

  1. 直接賦值是引用,改變 新變量值會改變原變量值。
    使用c = a.view()是建立了一個a的觀測值變量,改變c也會改變a。
    要完全賦值使用深拷貝d = a.copy()
  2. numpy中可以直接用多維索引來建立一個多維array。但注意,不能將多個array作為索引,此時可以用tuple(多個array)將多個array轉(zhuǎn)換成一個tuple

scipy參考:https://docs.scipy.org/doc/scipy/reference/tutorial/

  1. r_連接二維行 c_連接二維列。
    a = np.r_[3,[0]5,-1:1:10j]*
    其中10j的意思是鑄造10個分割點
  2. mgrid鑄造標準網(wǎng)格,類似matlab的meshgrid;ogrid返回的是多個一維的網(wǎng)格向量。
  3. vectorize(函子)傳入函子,返回一個向量化的函數(shù)
  4. select函數(shù)的 意思通過下面這個例子說明:
 x = np.r_[-2:3]
 x
array([-2, -1,  0,  1,  2])
np.select([x > 3, x >= 0], [0, x+2],999) 
array([999, 999, 2, 3, 4])

也就是說x來進行判斷,滿足第一個條件就被0替換,滿足第二個條件被x+2替換,如果都不滿足就被999替換。
而where函數(shù)有兩種用法:
np.where([[True, False], [True, True]],
... [[1, 2], [3, 4]],
... [[9, 8], [7, 6]])
array([[1, 8],
[3, 4]])
滿足條件取第一個,不滿足取第二個
第二種用法:只有一個輸入?yún)?shù),那么就返回True

matlabplotlib參考:https://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb

  1. 注意Matplotlib所用的都是()而不是[].
  2. 可以使用latex格式,但要在

label=r"y = \alpha^3"
注意要加r來區(qū)分和python變量str區(qū)別。

注意plot和subplot的屬性名:

屬性名 簡寫 功能 備注
label="curve1" 線標簽 最后要加ax.legend();
蘋果 $1 6 6
草莓 $1 7 6

a.

ax.plot(x, x2, label="curve1")
ax.plot(x, x
3, label="curve2")
label="curve1"線標簽
ax.legend(loc=0) # let matplotlib decide the optimal location
ax.legend(loc=1) # upper right corner
ax.legend(loc=2) # upper left corner
ax.legend(loc=3) # lower left corner
ax.legend(loc=4) # lower right corner

b.

ax.plot(x, x+13, color="purple", lw=1, ls='-', marker='o', markersize=2)
ax.plot(x, x+14, color="purple", lw=1, ls='-', marker='o', markersize=4)
ax.plot(x, x+15, color="purple", lw=1, ls='-', marker='o', markersize=8, markerfacecolor="red")
ax.plot(x, x+16, color="purple", lw=1, ls='-', marker='s', markersize=8,
markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue");
lw是linewidth,ls是linestyle線形狀,marker是點型

c. 決定坐標范圍

fig, axes = plt.subplots(1, 3, figsize=(12, 4))

axes[0].plot(x, x2, x, x3)
axes[0].set_title("default axes ranges")

axes[1].plot(x, x2, x, x3)
axes[1].axis('tight') #(自決定)
axes[1].set_title("tight axes")

axes[2].plot(x, x2, x, x3)
axes[2].set_ylim([0, 60])
axes[2].set_xlim([2, 5])
axes[2].set_title("custom axes range");

d. 坐標指數(shù)

fig, axes = plt.subplots(1, 2, figsize=(10,4))

axes[0].plot(x, x**2, x, np.exp(x))
axes[0].set_title("Normal scale")

axes[1].plot(x, x**2, x, np.exp(x))
axes[1].set_yscale("log")
axes[1].set_title("Logarithmic scale (y)");

  1. 改變圖像風格:
    plt.style.use('ggplot')
    可以先使用
    print(plt.style.available)
    查看圖像風格

sns.lmplot(x='total_bill', y='tip', data=tips, col='sex')
col,row將不同的分類點畫在不同畫布上;hue='sex'將分類點畫在同一畫布上,但標出不同顏色。
6.二變量畫圖法:
sns.jointplot(x='hp',y='mpg',data=auto,kind='resid')

kind='scatter' uses a scatter plot of the data points
kind='reg' uses a regression plot (default order 1)
kind='resid' uses a residual plot
kind='kde' uses a kernel density estimate of the joint distribution
kind='hex' uses a hexbin plot of the joint distribution
  1. 多變量畫圖:
    注意,傳入的都要是dataframe
    sns.pairplot(data)非常簡單
    sns.heatmap(cov_matrix)相關性的圖

pandas參考:http://pandas.pydata.org/pandas-docs/stable/tutorials.html

  1. 注意深拷貝berri_bikes = bikes[['Berri 1']].copy()一定要用copy,注意bikes后有兩個中括號
  2. rows_with_dashes = requests['Incident Zip'].str.contains('-').fillna(False)
    fillna的含義是充填缺失值
  3. daily_temp_2011 = daily_mean_2011['dry_bulb_faren'].values
    .values讓一個series(pandas)轉(zhuǎn)換成了array(numpy)
    pandas內(nèi)容太多了,建議還是直接看官方cookbook
  4. 一列寫入的時候,要用雙[],否則會當做series沒有列名。列選擇都要加雙[],即
    data[['rnd_1', 'rnd_3']]
    也就是說從dataframe里面選出列名的那些列作為dataframe要加雙括號

sklearn

  1. transform() 和fit_transform()區(qū)別:https://blog.csdn.net/quiet_girl/article/details/72517053

tranform()的作用是通過找中心和縮放等實現(xiàn)標準化
fit_transform()的作用就是先將數(shù)據(jù)擬合模型,然后轉(zhuǎn)化它將其轉(zhuǎn)化為標準形式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 什么是NumPy? NumPy is the fundamental package for scientific...
    黑人不是牙膏閱讀 6,657評論 1 3
  • 一.NumPy的引入 標準安裝的Python中用列表(list)保存一組值,可以用來當作數(shù)組使用,不過由于列...
    wlj1107閱讀 1,133評論 0 2
  • 先決條件 在閱讀這個教程之前,你多少需要知道點python。如果你想從新回憶下,請看看Python Tutoria...
    舒map閱讀 2,720評論 1 13
  • 來源:NumPy Tutorial - TutorialsPoint 譯者:飛龍 協(xié)議:CC BY-NC-SA 4...
    布客飛龍閱讀 33,516評論 6 97
  • 浩如煙海的佛經(jīng)常使人望而卻步、嘆為觀止。然如何使千年傳承之經(jīng)典為忙碌于現(xiàn)代生活的人們掌握并受用,以至于得到...
    傳統(tǒng)文化精華閱讀 1,444評論 5 3

友情鏈接更多精彩內(nèi)容