In [23]:
x=True
In [24]:
import numpy as np
np.random.seed(0)
In [25]:
not x
Out[25]:
In [26]:
X = np.random.randn(300, 2)
y = np.logical_or(X[:, 0] > 0, X[:, 1] > 0)
y_T = np.where(y)
y_F = np.shape(y is True)
In [27]:
y_T
Out[27]:
In [28]:
6/5
Out[28]:
In [29]:
X[y]
Out[29]:
In [30]:
!pip install xgboost
In [31]:
import xgboost as xgb
In [32]:
'''
Created on 1 Apr 2015
@author: Jamie Hall
'''
import pickle
import numpy as np
import xgboost as xgb
from sklearn.datasets import load_iris, load_digits, load_boston
from sklearn.metrics import confusion_matrix, mean_squared_error
from sklearn.model_selection import KFold, train_test_split, GridSearchCV
rng = np.random.RandomState(31337)
print("Zeros and Ones from the Digits data set: binary classification")
digits = load_digits(2)
y = digits['target']
X = digits['data']
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
for train_index, test_index in kf.split(X):
xgb_model = xgb.XGBClassifier().fit(X[train_index], y[train_index])
predictions = xgb_model.predict(X[test_index])
actuals = y[test_index]
print(confusion_matrix(actuals, predictions))
print("Iris: multi-class classification")
iris = load_iris()
y = iris['target']
X = iris['data']
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
for train_index, test_index in kf.split(X):
xgb_model = xgb.XGBClassifier().fit(X[train_index], y[train_index])
predictions = xgb_model.predict(X[test_index])
actuals = y[test_index]
print(confusion_matrix(actuals, predictions))
print("Boston Housing: regression")
boston = load_boston()
y = boston['target']
X = boston['data']
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
for train_index, test_index in kf.split(X):
xgb_model = xgb.XGBRegressor().fit(X[train_index], y[train_index])
predictions = xgb_model.predict(X[test_index])
actuals = y[test_index]
print(mean_squared_error(actuals, predictions))
print("Parameter optimization")
y = boston['target']
X = boston['data']
xgb_model = xgb.XGBRegressor()
clf = GridSearchCV(xgb_model,
{'max_depth': [2, 4, 6],
'n_estimators': [50, 100, 200]}, verbose=1)
clf.fit(X, y)
print(clf.best_score_)
print(clf.best_params_)
# The sklearn API models are picklable
print("Pickling sklearn API models")
# must open in binary format to pickle
pickle.dump(clf, open("best_boston.pkl", "wb"))
clf2 = pickle.load(open("best_boston.pkl", "rb"))
print(np.allclose(clf.predict(X), clf2.predict(X)))
# Early-stopping
X = digits['data']
y = digits['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
clf = xgb.XGBClassifier()
clf.fit(X_train, y_train, early_stopping_rounds=10, eval_metric="auc",
eval_set=[(X_test, y_test)])
Out[32]:
In [33]:
import numpy as np
n = 5
A = np.random.rand(n,n)
npQ, npR = np.linalg.qr(A)
Check that $Q$ is orthogonal:
In [34]:
np.allclose(np.eye(n), npQ @ npQ.T), np.allclose(np.eye(n), npQ.T @ npQ)
Out[34]:
Check that $R$ is upper triangular
In [36]:
npR
Out[36]:
For each $j$, calculate a single projection $$v_j = P_ja_j$$ where $P_j$ projects onto the space orthogonal to the span of $q_1,\ldots,q_{j-1}$.
In [38]:
def cgs(A):
m, n = A.shape
Q = np.zeros([m,n], dtype=np.float64)
R = np.zeros([n,n], dtype=np.float64)
for j in range(n):
v = A[:,j]
for i in range(j):
R[i,j] = np.dot(Q[:,i], A[:,j])
v = v - (R[i,j] * Q[:,i])
R[j,j] = np.linalg.norm(v)
Q[:, j] = v / R[j,j]
return Q, R
In [39]:
Q, R = cgs(A)
In [40]:
np.allclose(A, Q @ R)
Out[40]:
Check if $Q$ is unitary:
In [41]:
np.allclose(np.eye(len(Q)), Q.dot(Q.T))
Out[41]:
In [42]:
np.allclose(npQ, -Q)
Out[42]:
In [43]:
npQ
Out[43]:
In [44]:
-Q
Out[44]:
In [45]:
np.eye(len(Q))
Out[45]:
In [85]:
np.allclose(np.eye(len(Q)), Q.dot(Q.T))
Out[85]:
In [75]:
a = np.random.randint(4, size=(2, 3))
b = np.random.randint(5, size=(3, 2))
In [76]:
a
Out[76]:
In [77]:
b
Out[77]:
In [81]:
a.dot(b)
Out[81]:
In [83]:
a @ b
Out[83]:
In [86]:
x = np.array([23, 1])
x.dot(x.T)
Out[86]:
In [87]:
x.shape
Out[87]:
In [91]:
x.T.shape
Out[91]:
In [94]:
y = np.expand_dims(x, -1)
In [95]:
y.dot(y.T)
Out[95]:
Modified Gram-Schmidt: for each $j$, calculate $j-1$ projections $$P_j = P_{\perp q_{j-1}\cdots\perp q_{2}\perp q_{1}}$$
In [96]:
import numpy as np
n = 3
A = np.random.rand(n,n).astype(np.float64)
In [97]:
def mgs(A):
V = A.copy()
m, n = A.shape
Q = np.zeros([m,n], dtype=np.float64)
R = np.zeros([n,n], dtype=np.float64)
for i in range(n):
R[i,i] = np.linalg.norm(V[:,i])
Q[:,i] = V[:,i] / R[i,i]
for j in range(i, n):
R[i,j] = np.dot(Q[:,i],V[:,j])
V[:,j] = V[:,j] - R[i,j]*Q[:,i]
return Q, R
In [98]:
Q, R = mgs(A)
In [99]:
np.allclose(np.eye(len(Q)), Q.dot(Q.T.conj()))
Out[99]:
In [100]:
np.allclose(A, np.matmul(Q,R))
Out[100]:
In [102]:
A.conj()
Out[102]:
In [107]:
from numpy.linalg import norm
import numpy as np
In [108]:
?norm
In [112]:
a = np.arange(9).reshape(-1, 3) - 4
In [113]:
a
Out[113]:
norm() 求范数
===== ============================ ==========================
ord norm for matrices norm for vectors
===== ============================ ==========================
None Frobenius norm 2-norm
'fro' Frobenius norm --
'nuc' nuclear norm --
inf max(sum(abs(x), axis=1)) max(abs(x))
-inf min(sum(abs(x), axis=1)) min(abs(x))
0 -- sum(x != 0)
1 max(sum(abs(x), axis=0)) as below
-1 min(sum(abs(x), axis=0)) as below
2 2-norm (largest sing. value) as below
-2 smallest singular value as below
other -- sum(abs(x)**ord)**(1./ord)
===== ============================ ==========================
In [114]:
norm(a)
Out[114]:
In [117]:
16+9+4+1+1+4+9+16
Out[117]:
In [118]:
np.sqrt(_)
Out[118]:
In [119]:
norm(a, np.inf)
Out[119]:
In [120]:
max([4+3+2, 1+0+1, 2+3+4])
Out[120]:
In [121]:
norm(a, -np.inf)
Out[121]:
In [122]:
min([4+3+2, 1+0+1, 2+3+4])
Out[122]:
In [123]:
norm(a, 1)
Out[123]:
In [124]:
max([4 + 1+2,3+0+3,2+1+4])
Out[124]:
In [125]:
import matplotlib.pyplot as plt
%matplotlib inline
In [126]:
x = np.arange(16).reshape(-1, 4) - 4
In [129]:
plt.imshow(x)
Out[129]:
In [127]:
x = x / 16
In [128]:
x
Out[128]:
In [130]:
plt.imshow(x)
Out[130]:
In [139]:
y = np.expand_dims(x.ravel(), -1).T
In [140]:
plt.matshow(y)
Out[140]:
In [141]:
plt.gray()
In [142]:
plt.matshow(x)
Out[142]:
In [146]:
np.index_exp[2::2]
Out[146]:
In [147]:
np.s_[2::2]
Out[147]:
In [148]:
slice(2, None, 2)
Out[148]:
In [149]:
x = np.arange(6)
In [150]:
x
Out[150]:
In [151]:
np.delete(x, np.s_[2::2])
Out[151]:
In [152]:
x
Out[152]:
class slice(stop)
class slice(start, stop[, step])
In [155]:
np.delete(x, slice(2,None,2))
Out[155]:
In [158]:
yt = np.arange(16).reshape(-1, 4)
yt
Out[158]:
In [161]:
# 旋转一次
np.rot90(yt)
Out[161]:
In [162]:
np.rot90(yt, k=2)
Out[162]:
In [164]:
np.rot90(yt, axes=(1, 0))
Out[164]:
In [ ]: