pycharm
撤銷快捷鍵 Ctrl+z
注釋快捷鍵 Ctrl+/
目錄
- split()函式
- append()
- pow(x,y)
- np.concatenate函式
- np.reshape(-1)、np.reshape(-1, 1)、np.reshape(1, -1)詳解
- np.mean()函式
- np.ptp( ) 函式
- numpy.insert() 函式
- numpy.dot() 和 x.dot(y) 為矩陣乘法計算
- python中的“.T”操作
- len()函式
- np.arange()用法
- numpy.std() ,pandas.std()計算矩陣標準差
split()函式
Python split()通過指定分隔符對字串進行切片,如果引數num 有指定值,則僅分隔 num 個子字串
split()方法語法:str.split(str=" ", num=string.count(str)).
#str -- 分隔符,默認為所有的空字符,包括空格、換行(\n)、制表符(\t)等,
#num -- 分割次數,
>>>ipaddr = "10.122.19.101 "
>>>print(ipaddr.split('.'))
['10', '122', '19', '101 ']
>>>ipaddr = "10.122.19.101 nn "
>>>print(ipaddr.split(' ',1))
['10.122.19.101', ' nn ']
append()
此方法用于在串列末尾添加新的物件,
append()方法語法:list.append(obj)
#obj – 添加到串列末尾的物件,
#該方法無回傳值,但是會修改原來的串列
>>>c= [1, 2, 3]
>>>d = 4
>>>y = c.append(d)
>>>print(y)
>>>print(c)
None
[1, 2, 3, 4]
pow(x,y)
二元形式pow(x,y)等效于使用冪運算子:x ** y,
三元形式pow(x,y,z),則將x ** y,取余z, 它的計算比使用pow(x,y)%z更有效,
>>>print(pow(10, 2))
>>>print(pow(-10, 3))
>>>print(pow(10, 2, 3))
100
-1000
1
np.concatenate函式
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6], [7, 8]])
>>> np.concatenate((a,b),axis=0)
[[1, 2],
[3, 4],
[5, 6],
[7, 8]]
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6], [7, 8]])
>>> np.concatenate((a,b),axis=1)
[[1, 2, 5, 6],
[3, 4, 7, 8]]
>>> c = np.concatenate((a,b),axis=1)
>>> c
[[1, 2, 5, 6],
[3, 4, 7, 8]])
>>> c.shape
(2, 4)
np.reshape(-1)、np.reshape(-1, 1)、np.reshape(1, -1)詳解
np.reshape(-1)
>>>original = np.array([2, 4, 1, 3],[1, 2, 5, 2])
>>>orginal.shape
(2, 4)
>>>new_1 = orginal.reshape(-1)
>>>print(new_1)
[2, 4, 1, 3, 1, 2, 5, 2]
np.reshape(-1, 1)
>>>original = np.array([2, 4, 1, 3],[1, 2, 5, 2])
# 設定新排布的列數為1,行數為未知
>>>new_2 = orginal.reshape(-1, 1)
>>>print(new_2) # 新排布為(8,1)
[[ 2],
[ 4],
[ 1],
[ 3],
[ 1],
[ 2],
[ 5],
[ 2]]
np.reshape(1, -1)
>>>original = np.array([2, 4, 1, 3],[1, 2, 5, 2])
# 設定新排布的行數為1,列數為未知
>>>new_3 = orginal.reshape(1, -1)
>>>print(new_3) # 新排布為(1,8)
[2, 4, 1, 3, 1, 2, 5, 2]
np.reshape(-1, 2)
>>>original = np.array([2, 4, 1, 3],[1, 2, 5, 2])
# 設定新排布的列數為2,行數為未知
>>>new_4 = original.reshape(-1, 2)
>>>print(new_4) # 新排布為(4,2)
[[2, 4],
[1, 3],
[1, 2],
[5, 2]]
np.mean()函式

>>>a = np.array([[1, 2], [3, 4]])
>>>print(a)
>>>print(type(a))
>>>print(np.mean(a))
>>>print(np.mean(a, axis=0)) # axis=0,計算每一列的均值
>>>print(np.mean(a, axis=1)) # 計算每一行的均值
[[1 2]
[3 4]]
<class 'numpy.ndarray'>
2.5
[2. 3.]
[1.5 3.5]
np.ptp( ) 函式
>>>a = np.arange(9).reshape((3,3))
>>>print(a)
[[0 1 2]
[3 4 5]
[6 7 8]]
>>>b = np.ptp(a, axis=0) # axis=0 表示縱向
>>>print(b) # [6 6 6] # 6-0, 7-1, 8-2
6
>>>c = np.ptp(a, axis=1) # axis=1 表示橫向
>>>print(c) # [2 2 2] # 2-0, 5-3, 8-6
2
numpy.insert() 函式
a = np.arange(12).reshape((3, 4))
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(np.insert(a, 2, 100))
# [ 0 1 100 2 3 4 5 6 7 8 9 10 11]
b1 = np.arange(100, 104)
print(b1)
# [100 101 102 103]
print(np.insert(a, 1, b1, axis=0))
# [[ 0 1 2 3]
# [100 101 102 103]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(np.insert(a, 3, b1, axis=0))
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [100 101 102 103]]
print(np.insert(a, [0, 2], b1, axis=0))
# [[100 101 102 103]
# [ 0 1 2 3]
# [ 4 5 6 7]
# [100 101 102 103]
# [ 8 9 10 11]]
print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(np.insert(a, 1, 100, axis=1))
# [[ 0 100 1 2 3]
# [ 4 100 5 6 7]
# [ 8 100 9 10 11]]
c1 = np.arange(100, 103)
print(c1)
# [100 101 102]
print(np.insert(a, 1, c1, axis=1))
# [[ 0 100 1 2 3]
# [ 4 101 5 6 7]
# [ 8 102 9 10 11]]
print(np.insert(a, 3, c1, axis=1))
# [[ 0 1 2 100 3]
# [ 4 5 6 101 7]
# [ 8 9 10 102 11]]
>>>a = np.array([[1, 2], [3, 4]])
>>>print(a.ndim)
>>>print(a.shape)
>>>print(a.size)
2
(2,2)
4
numpy.dot() 和 x.dot(y) 為矩陣乘法計算
>>>import numpy as np
>>>mat1 = np.array([[1, 2, 3],
[4, 5, 6]])
>>>mat2 = np.array([[1, 2],
[1, 2],
[1, 2]])
>>>np.dot(mat1, mat2) # numpy.dot()
array([[ 6, 12],
[15, 30]])
>>>mat1.dot(mat2) # x.dot(y)
array([[ 6, 12],
[15, 30]])
python中的“.T”操作
其實就是對一個矩陣的轉置
>>>a=array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>>a.T
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
numpy中默認的一維陣列形式可能是
x = np.array([1,2,3,4])
此時對此陣列進行轉置操作 x.T 沒用,因為轉置操作只能用在二維陣列上
要對其變為列向量,采用以下操作
x = np.array([x])
此時變為array([[1, 2, 3, 4]]) 二維陣列,然后轉置
len()函式
>>>import numpy as np
>>>X1=np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])
>>>length1=len(X1) #回傳物件的長度 不是元素的個數,可以想象成資料長度/條數,即資料的行數
>>>print("length of X1:",length1)
length of X1: 3
>>>X2 = np.array([1, 2, 3, 4])
>>>length2=len(X2) #回傳物件的長度 不是元素的個數,可以想象成資料長度/條數
>>>print("length of X2:",length2)
length of X2: 4
np.arange()用法
#一個引數 默認起點0,步長為1 輸出:[0 1 2]
a = np.arange(3)
#兩個引數 默認步長為1 輸出[3 4 5 6 7 8]
a = np.arange(3,9)
#三個引數 起點為0,終點為3,步長為0.1 輸出
#[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9]
a = np.arange(0, 3, 0.1)
numpy.std() ,pandas.std()計算矩陣標準差
計算得出的默認標準偏差型別在 numpy 的 .std() 和 pandas 的 .std() 函式之間是不同的,
默認情況下,numpy 計算的是總體標準偏差,ddof = 0,
另一方面,pandas 計算的是樣本標準偏差,ddof = 1,
如果我們知道所有的分數,那么我們就有了總體
因此,要使用 pandas 進行歸一化處理,我們需要將“ddof”設定為 0,
numpy.std() 求標準差的時候默認ddof = 0是除以 n 的,即是有偏的,
numpy.std無偏樣本標準差方式為加入引數 ddof = 1;
pandas.std() 默認ddof = 1是除以n-1 的,即是無偏的,
如果想有偏,需要加上引數ddof=0 ,即pandas.std(ddof=0)
>>> a = np.array([[1, 2], [3, 4]])
>>> np.std(a) # 計算全域標準差
1.1180339887498949
>>> np.std(a, axis=0) # axis=0計算每一列的標準差
array([ 1., 1.])
>>> np.std(a, axis=1) # 計算每一行的標準差
array([ 0.5, 0.5])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255217.html
標籤:python
