python作为一种潮流,开源的内容无比强大,因此记录学习的点滴。
#中文“全角”输入,显示空格
11/9/2019
- numpy.asarray 列表变矩阵
numpy.asarray(a, dtype=None, order=None)
>>> a = [1, 2]
>>> np.asarray(a)
array([1, 2])
- numpy.around 取整,保留有效数字
numpy.around(a, decimals=0, out=None)
>>> np.around([0.37, 1.64])
array([0., 2.])
>>> np.around([0.37, 1.64], decimals=1)
array([0.4, 1.6])
>>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
array([0., 2., 2., 4., 4.])
>>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
array([ 1, 2, 3, 11])
>>> np.around([1,2,3,11], decimals=-1)
array([ 0, 0, 0, 10])
- ndarray VS matrix
ndarray可以是1维,2维,多维;matrix是2维。
mat_a*mat_b, mat_a**2, 矩阵乘
arr_a*arr_b, arr_a**2, 元素乘
参见说明 - scipy.sparse.csc_matrix(arg1, shape=None, dtype=None, copy=False)
矩阵稀疏化
>>> row = np.array([0, 2, 2, 0, 1, 2])
>>> col = np.array([0, 0, 1, 2, 2, 1])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csc_matrix((data, (row, col)), shape=(3, 3)).toarray()
array([[1, 0, 4],
[0, 0, 5],
[2, 9, 0]])
csc_matrix((data, (row, col)), shape=(3, 3)).todense()
11/11/2019
- range()
for i in range(10):
print(i, end =" ")
0 1 2 3 4 5 6 7 8 9
for i in range(1, 20):
print(i, end =" ")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
for i in range(0, 30, 3):
print(i, end = " ")
0 3 6 9 12 15 18 21 24 27
Points to remember about Python range() function :
- range() function only works with the integers i.e. whole numbers.
- All argument must be integers. User can not pass a string or float number or any other type in a start, stop and step argument of a range().
- All three arguments can be positive or negative.
- The step value must not be zero. If a step is zero python raises a ValueError exception.
- range() is a type in Python
11/13/2019
- sklearn.cluster.KMeans
举个例子:
(常用:kmeans =KMeans(参数).fit(待聚类矩阵),kmeans.labels_,kmeans.predict(新数据))
>>> from sklearn.cluster import KMeans
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
... [10, 2], [10, 4], [10, 0]])
>>> kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
>>> kmeans.labels_
array([1, 1, 1, 0, 0, 0], dtype=int32)
>>> kmeans.predict([[0, 0], [12, 3]])
array([1, 0], dtype=int32)
>>> kmeans.cluster_centers_
array([[10., 2.],
[ 1., 2.]])
- scipy.signal.stft 和 scipy.signal.istft
f, t, Zxx =scipy.signal.stft(x,
fs=1.0,
window=‘hann’,
nperseg=256,
noverlap=None,
nfft=None,
detrend=False,
return_onesided=True,
boundary=‘zeros’,
padded=True,
axis=-1)
_, xrec =scipy.signal.istft(Zxx,
fs=1.0,
window=‘hann’,
nperseg=None,
noverlap=None,
nfft=None,
input_onesided=True,
boundary=True,
time_axis=-1,
freq_axis=-2)
11/14/2019
- 实例变量
import sys, os
sys.path.append(os.pardir)
import numpy as np
from common.functions import softmax, cross_entropy_error
from common.gradient import numerical_gradient
class simpleNet:
def __init__(self):
self.W = np.random.randn(2,3) # 用高斯分布进行初始化,实例变量
def predict(self, x): #方法
return np.dot(x, self.W)
def loss(self, x, t): #方法
z = self.predict(x)
y = softmax(z)
loss = cross_entropy_error(y, t)
return loss
simpleNet 类只有一个实例变量,即形状为 2×3的权重参数。
它有两个方法,一个是用于预测的 predict(x),另一个是用于求损失函数值的loss(x,t)。
2.lambda 表示法
def f(W):
return net.loss(x, t)
等效于
f = lambda w: net.loss(x, t)
3/5/2020
- from collections import OrderedDict
有序词典,记住录入顺序
链接: https://www.geeksforgeeks.org/ordereddict-in-python/.
3/10/2020
- batch_mask = np.random.choice(60000, 100)
batch_mask =
[2321
16513
53483
17967
51094
56652
53591
54286
]
从 0~60000-1中随机抽出100个数