Python機(jī)器學(xué)習(xí)基礎(chǔ)教程學(xué)習(xí)筆記(1)——用到的庫(kù)
1 Numpy
numpy數(shù)組
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
print("x:\n{}".format(x))
x:
[[1 2 3]
[4 5 6]]
2 Scipy
scipy中最重要的是scipy.sparse,它可以給出稀疏矩陣
from scipy import sparse
# 創(chuàng)建一個(gè)三維Numpy數(shù)組水由,對(duì)角線為1,其余都為0
eye = np.eye(4)
print("NumPy array:\n{}".format(eye))
NumPy array:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
# 將Numpy數(shù)組轉(zhuǎn)換成CSR格式的稀疏矩陣
# 只保存非零元素
sparse_martrix = sparse.csr_matrix(eye)
print("\nSciPy sparse CSR matrix:\n{}".format(sparse_martrix))
SciPy sparse CSR matrix:
(0, 0) 1.0
(1, 1) 1.0
(2, 2) 1.0
(3, 3) 1.0
# coo格式的稀疏矩陣
data = np.ones(4)
row_indices = np.arange(4)
col_indices = np.arange(4)
eye_coo = sparse.coo_matrix(data,(row_indices,col_indices))
print("COO representation:\n{}".format(eye_coo))
COO representation:
(0, 0) 1.0
(0, 1) 1.0
(0, 2) 1.0
(0, 3) 1.0
3 matplotlib
python的科學(xué)繪圖庫(kù)。
在jupyter notebook中,可以使用%matplotlib notebook
(這種寫法可以交互)和%matplotlib inline
命令蔚龙,將圖像直接顯示在瀏覽器中。否則要調(diào)用plt.show
來顯示圖像
%matplotlib inline
import matplotlib.pyplot as plt
# 在-10和10之間生成一個(gè)數(shù)列映胁,共100個(gè)數(shù)
x = np.linspace(-10,10,100)
# 用正弦函數(shù)創(chuàng)建第二個(gè)數(shù)組
y = np.sin(x)
# 用plot函數(shù)繪制一個(gè)數(shù)組關(guān)于另一個(gè)數(shù)組的折線圖
plt.plot(x,y,marker='x')
# plt.show()
[<matplotlib.lines.Line2D at 0x118f10198>]
4 Pandas
# 利用字典創(chuàng)建DataFrame的一個(gè)小例子
import pandas as pd
from IPython.display import display
# 創(chuàng)建關(guān)于人的簡(jiǎn)單數(shù)據(jù)集
data = {
'name':['john','anan','peter','linda'],
'location':['new york','pairs','berlin','london'],
'age':[24,13,53,33]
}
data_pandas = pd.DataFrame(data)
# IPython.display可以在jupyter notebook中打印出“美觀的”dataframe
display(data_pandas)
name | location | age | |
---|---|---|---|
0 | john | new york | 24 |
1 | anan | pairs | 13 |
2 | peter | berlin | 53 |
3 | linda | london | 33 |
# 選擇年齡大于30的所有行
display(data_pandas[data_pandas.age>30])
name | location | age | |
---|---|---|---|
2 | peter | berlin | 53 |
3 | linda | london | 33 |
5 查看各庫(kù)的版本
import sys
print("Python version:{}".format(sys.version))
import pandas as pd
print("pandas version:{}".format(pd.__version__))
import matplotlib
print("matplotlib version:{}".format(matplotlib.__version__))
import numpy as np
print("numpy version:{}".format(np.__version__))
import scipy as sp
print("scipy version:{}".format(sp.__version__))
import IPython
print("IPython version:{}".format(IPython.__version__))
import sklearn
print("sklearn version:{}".format(sklearn.__version__))
Python version:3.7.3 (default, Mar 27 2019, 16:54:48)
[Clang 4.0.1 (tags/RELEASE_401/final)]
pandas version:0.24.2
matplotlib version:3.0.3
numpy version:1.16.2
scipy version:1.2.1
IPython version:7.4.0
sklearn version:0.20.3