コード例 #1
0
import numpy as np
from data_func import import_data, export_data
from regression_func import lasso, add_bias, add_gaussian, plot_err, nonzeros, performance

X = import_data("housing_X_train.csv")
y = import_data("housing_y_train.csv")
X_test = import_data("housing_X_test.csv")
y_test = import_data("housing_y_test.csv")

X = add_bias(X)
X_test = add_bias(X_test)

X = add_gaussian(X, 1000)  #add 1000 irrelevant rows
X_test = add_gaussian(X_test, 1000)

fold = 10
min_lam = 0
max_lam = 100
step = 10
lam = np.arange(min_lam, max_lam + step, step)
k = len(lam)
zeros = np.zeros(k)

(perf_train, perf_valid, perf_test, w) = performance(X, y, fold, lam, X_test,
                                                     y_test, lasso)
for i in range(k):  #count nonzero ratio in last 1000 entries
    zeros[i] = nonzeros(w[-1000:, i])

plot_err(lam, perf_valid, "Q9")
header = [
    "lamda", "training set error", "validation set error", "test set error",
コード例 #2
0
import numpy as np
from data_func import import_data, perceptron, plot

X = import_data("spambase_X.csv")
y = import_data("spambase_y.csv")
max_pass = 500  #set the number of max passes here
for t in range(5):  #try for 5 times
    permutation = np.random.permutation(X.shape[1])  #create randomized indices
    X_s = X[:, permutation]
    y_s = y[permutation]
    w = np.zeros(X.shape[0])  # initialize w and b each time
    b = 0
    (w, b, mistake) = perceptron(X_s, y_s, w, b, max_pass)
    plot(max_pass, mistake,
         "Q4_" + str(t + 1))  #plot 5 figures with name "Q4_t+1.jpg"
    #print mistake[-1]