Skip to content

Instantly share code, notes, and snippets.

@apow2
Created January 14, 2021 19:49
Show Gist options
  • Select an option

  • Save apow2/67888e9edd0010a91d90f4ccb67c6605 to your computer and use it in GitHub Desktop.

Select an option

Save apow2/67888e9edd0010a91d90f4ccb67c6605 to your computer and use it in GitHub Desktop.
using DataFrames
using CSV
using Random
using LinearAlgebra
redwine = DataFrames.DataFrame(CSV.File("winequality-red.csv"))
mat = Matrix(redwine)[:, 1:11]
A = mat[:, 1:end-1] #features
y = mat[:, end]; #labels
A = hcat(A, ones(size(A, 1), 1)); #concatenate ones
#do train-test split
ntrain = Int64(floor(size(A, 1)*.8))
ntest = size(A, 1) - ntrain
perm = randperm(MersenneTwister(12), size(A, 1))
A_train = @view A[perm[1:ntrain], :]; y_train = @view y[perm[1:ntrain]]
A_test = @view A[perm[ntrain+1:end], :]; y_test = @view y[perm[ntrain+1:end]];
#compute best predictor
x_star = (A_train'*A_train)\(A_train'*y_train);
#predict on test set
y_test_predict = test_A* reshape(x_star, 11, 1);
@show norm(y_test_predict-y_test)^2/length(y_test) #MSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment