numpy參考:https://scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial
- 直接賦值是引用,改變 新變量值會改變原變量值。
使用c = a.view()是建立了一個a的觀測值變量,改變c也會改變a。
要完全賦值使用深拷貝d = a.copy() - numpy中可以直接用多維索引來建立一個多維array。但注意,不能將多個array作為索引,此時可以用tuple(多個array)將多個array轉(zhuǎn)換成一個tuple
scipy參考:https://docs.scipy.org/doc/scipy/reference/tutorial/
- r_連接二維行 c_連接二維列。
a = np.r_[3,[0]5,-1:1:10j]*
其中10j的意思是鑄造10個分割點 - mgrid鑄造標準網(wǎng)格,類似matlab的meshgrid;ogrid返回的是多個一維的網(wǎng)格向量。
- vectorize(函子)傳入函子,返回一個向量化的函數(shù)
- 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
- 注意Matplotlib所用的都是()而不是[].
- 可以使用latex格式,但要在
label=r"
"
注意要加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, x3, 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)");
- 改變圖像風格:
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
- 多變量畫圖:
注意,傳入的都要是dataframe
sns.pairplot(data)非常簡單
sns.heatmap(cov_matrix)相關性的圖
pandas參考:http://pandas.pydata.org/pandas-docs/stable/tutorials.html
- 注意深拷貝berri_bikes = bikes[['Berri 1']].copy()一定要用copy,注意bikes后有兩個中括號
- rows_with_dashes = requests['Incident Zip'].str.contains('-').fillna(False)
fillna的含義是充填缺失值 - daily_temp_2011 = daily_mean_2011['dry_bulb_faren'].values
.values讓一個series(pandas)轉(zhuǎn)換成了array(numpy)
pandas內(nèi)容太多了,建議還是直接看官方cookbook - 一列寫入的時候,要用雙[],否則會當做series沒有列名。列選擇都要加雙[],即
data[['rnd_1', 'rnd_3']]
也就是說從dataframe里面選出列名的那些列作為dataframe要加雙括號
sklearn
-
transform() 和fit_transform()區(qū)別:https://blog.csdn.net/quiet_girl/article/details/72517053
tranform()的作用是通過找中心和縮放等實現(xiàn)標準化
fit_transform()的作用就是先將數(shù)據(jù)擬合模型,然后轉(zhuǎn)化它將其轉(zhuǎn)化為標準形式