reshape()函数
(1)reshape()介绍
在创建DataFrame的时候常常使用reshape来更改数据的列数和行数,可用于numpy库里的ndarray和array结构以及pandas库里面的DataFrame和Series结构。
reshape(行,列)可以根据指定的数值将数据转换为特定的行数和列数,通常会遇见reshape(1, -1)or reshape(-1, 1);
import numpy as np
import pandas as pd
-1在这里怎么理解呢?
根据numpy库官网介绍,-1被理解为unspecified value,意思是未指定为给定的,可以理解为一个正整数通配符,它代替任何整数。例如我只需要特定的行数,列数多少我无所谓,我只需要指定行数,那么列数直接用-1代替就行了,反之亦然。
(2)
在吴恩达老师手写数字识别中,有一段创建分类器的代码:
from scipy.optimize import minimize
def one_vs_all(X, y, num_labels, learning_rate):
rows = X.shape[0]
params = X.shape[1]
# k X (n + 1) array for the parameters of each of the k classifiers
all_theta = np.zeros((num_labels, params + 1))
# insert a column of ones at the beginning for the intercept term
X = np.insert(X, 0, values=np.ones(rows), axis=1)
# labels are 1-indexed instead of 0-indexed
for i in range(1, num_labels + 1):
theta = np.zeros(params + 1)
y_i = np.array([1 if label == i else 0 for label in y])
y_i = np.reshape(y_i, (rows, 1))
# minimize the objective function
fmin = minimize(fun=cost, x0=theta, args=(X, y_i, learning_rate), method='TNC', jac=gradient)
all_theta[i-1,:] = fmin.x
return all_theta
其中**np.reshape(y_i, (rows, 1))**的作用是将y_i转换成5000x1的矩阵(训练集中有5000个20x20的样本)。
参考:
https://www.jianshu.com/p/d9df005636a6
https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html