<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 High dimensional embedding
$$\large \Phi (x) = (1,x_1,x_2,x_1 \cdot x_2, x_1^2, x_2^2)$$
$$\large y = \sum_{j=1}^6 \beta_j \Phi(x)_j > 0, \quad \beta = (0.,-2.,8.,-.8,-1.,-.5)$$
Step1:  Transfusion
To demonstrate the RFMTC marketing model (a modified version of RFM), this study adopted the donor database of Blood Transfusion Service Center in Hsin-Chu City in Taiwan. The center passes their blood transfusion service bus to one university in Hsin-Chu City to gather blood donated about every three months. To build a FRMTC model, we selected 748 donors at random from the donor database. These 748 donor data, each one included R (Recency - months since last donation), F (Frequency - total number of donation), M (Monetary - total blood donated in c.c.), T (Time - months since first donation), and a binary variable representing whether he/she donated blood in March 2007 (1 stand for donating blood; 0 stands for not donating blood).
<ASSISTANT_TASK:>
Python Code:
lr = LogisticRegression(fit_intercept=False,C=1e7)
lr.fit(PhiX,Pos)
lr.coef_
PosHatPhi = lr.predict(PhiX)
with plt.style.context(('seaborn-white')):
    plot_confusion_matrix(skmetrics.confusion_matrix(Pos,PosHatPhi),[0,1],
                      title="Confusion matrix for linear fit")
def Phi(x,deg=2):
    x = np.concatenate(([1],x))
    curvec = x.copy()
    for l in xrange(deg-1):
        Xout = np.outer(x,curvec)
        curvec = np.array([Xout[i,j] for i in xrange(len(x)) for j in xrange(i,len(curvec))])
    return(curvec)
PhiX = np.array([Phi(x,deg=2) for x in X])
beta = np.array([0.,-1.,-.5,-2.,8.,-.8])
Pos = PhiX[:,:6].dot(beta) > .3*np.random.randn(n)
_ = plt.scatter(X[~Pos,0],X[~Pos,1],c='b',label='neg')
_ = plt.scatter(X[Pos,0],X[Pos,1],c='r',label='pos')
_ = plt.title("Actual Labels")
_ = plt.legend(loc=2)
Xte = np.random.uniform(size=2*n)
Xte.shape = (n,2)
PXte = np.array([Phi(x) for x in Xte])
lr = LogisticRegression(fit_intercept=False,C=1e7)
lr.fit(PhiX,Pos)
PhiX = np.array([Phi(x,deg=2) for x in X])
PosTe = PXte[:,:6].dot(beta) > .3*np.random.randn(n)
yhatte = lr.decision_function(PXte)
fpr, tpr, _ = skmetrics.roc_curve(PosTe,yhatte)
_ = plt.plot(fpr,tpr)
_ = plt.title('ROC curve')
_ = plt.xlabel('FPR')
_ = plt.ylabel('TPR')
def mse_pred(X,y,Xte,yte,deg=2):
    PhiX = np.array([Phi(x,deg=deg) for x in X])
    lr = LogisticRegression(fit_intercept=False)
    lr.fit(PhiX,y)
    n = len(y)
    PXte = np.array([Phi(x,deg=deg) for x in Xte])
    yhat = lr.predict(PhiX)
    yhatte = lr.predict(PXte)
    mse_tr = np.sum((yhat - y)**2.)/n
    mse_te = np.sum((yhatte - yte)**2.)/n
    return((mse_tr,mse_te))
PXte = np.array([Phi(x) for x in Xte])
PosTe = PXte[:,:6].dot(beta) > .5*np.random.randn(n)
MSEs = [mse_pred(X,Pos*1.,Xte,PosTe*1.,deg=k) for k in xrange(1,9)]
mse_trs, mse_tes = tuple(zip(*MSEs))
plt.plot(range(1,9),mse_trs,label="Training Error")
plt.plot(range(1,9),mse_tes,label="Test Error")
plt.xlabel('degree')
plt.ylabel('0-1 loss')
_ = plt.legend()
Explanation: High dimensional embedding
$$\large \Phi (x) = (1,x_1,x_2,x_1 \cdot x_2, x_1^2, x_2^2)$$
$$\large y = \sum_{j=1}^6 \beta_j \Phi(x)_j > 0, \quad \beta = (0.,-2.,8.,-.8,-1.,-.5)$$
End of explanation
Dtrans = np.loadtxt("transfusion.data",dtype=np.str_,delimiter=",")
X = np.array(D[1:,0:4],dtype=float)
y = np.array(D[1:,4],dtype=float)
X = preprocessing.scale(X)
Xtr, Xva, ytr, yva = cross_validation.train_test_split(X, y, test_size=0.3)
print X.shape, Xte.shape
G = np.arange(10.)*.1 + .1
poly_TE = []
for gamma in G:
    clf = svm.SVC(kernel="poly",degree=3,gamma=gamma,C=1.)
    clf.fit(Xtr,ytr)
    yhat = clf.predict(Xva)
    poly_TE.append(np.sum(np.abs(yhat - yva)))
gamma_poly = G[np.argmin(poly_TE)]
    
G = .5**np.arange(16.)*32.
rbf_TE = []
for gamma in G:
    clf = svm.SVC(kernel="rbf",gamma=gamma,C=3.)
    clf.fit(Xtr,ytr)
    yhat = clf.predict(Xva)
    rbf_TE.append(np.sum(np.abs(yhat - yva)))
gamma_rbf = G[np.argmin(rbf_TE)]
plt.plot(np.log(G),rbf_TE)
plt.ylabel("Test Error")
plt.xlabel("log(bandwidth)")
Explanation: Transfusion
To demonstrate the RFMTC marketing model (a modified version of RFM), this study adopted the donor database of Blood Transfusion Service Center in Hsin-Chu City in Taiwan. The center passes their blood transfusion service bus to one university in Hsin-Chu City to gather blood donated about every three months. To build a FRMTC model, we selected 748 donors at random from the donor database. These 748 donor data, each one included R (Recency - months since last donation), F (Frequency - total number of donation), M (Monetary - total blood donated in c.c.), T (Time - months since first donation), and a binary variable representing whether he/she donated blood in March 2007 (1 stand for donating blood; 0 stands for not donating blood).
End of explanation
<END_TASK>