Train Test Split¶
Before you test a supervised model, you’ll need separate, non-overlapping sets for training and testing.
In VerticaPy, the train_test_split() method uses a random number generator to decide how to split the data.
%load_ext verticapy.sql
%sql -c "SELECT SEEDED_RANDOM(0);"
123 SEEDED_RANDOM100% | |
| 1 | 0.548813502304256 |
The SEEDED_RANDOM function chooses a number in the interval [0,1). Since the seed is user-provided, these results are reproducible. In this example, passing 0 as the seed always returns the same value.
%sql -c "SELECT SEEDED_RANDOM(0);"
123 SEEDED_RANDOM100% | |
| 1 | 0.548813502304256 |
A different seed will generate a different value.
%%sql -c "SELECT SEEDED_RANDOM(1);"
123 SEEDED_RANDOM100% | |
| 1 | 0.417021998437122 |
The train_test_split() function generates a random seed and we can then share that seed between the training and testing sets.
from verticapy.datasets import load_titanic
titanic = load_titanic()
train, test = titanic.train_test_split()
titanic.shape()
Out[4]: (1234, 14)
train.shape()
Out[5]: (827, 14)
test.shape()
Out[6]: (407, 14)
Note that SEEDED_RANDOM depends on the order of your data. That is, if your data isn’t sorted by a unique feature, the selected data might be inconsistent. To avoid this, we’ll want to use the order_by parameter.
train, test = titanic.train_test_split(order_by = {"fare": "asc"})
Even if the fare has duplicates, ordering the data alone will drastically decrease the likelihood of a collision.
Let’s create a model and evaluate it.
from verticapy.machine_learning.vertica import LinearRegression
model = LinearRegression()
When fitting the model with the fit() method, you can use the parameter test_relation to score your data on a specific relation.
model.fit(
train,
["age", "fare"],
"survived",
test,
)
=======
details
=======
predictor|coefficient|std_err |t_value |p_value
---------+-----------+--------+--------+--------
Intercept| 0.38856 | 0.04280| 9.07873| 0.00000
age | -0.00292 | 0.00130|-2.25072| 0.02474
fare | 0.00230 | 0.00036| 6.47534| 0.00000
==============
regularization
==============
type| lambda
----+--------
none| 1.00000
===========
call_string
===========
linear_reg('"public"."_verticapy_tmp_linearregression_v_mldb_c6571bb297bd11efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_c6be2e4c97bd11efa8720242ac120002_"', '"survived"', '"age", "fare"'
USING PARAMETERS optimizer='newton', epsilon=1e-06, max_iterations=100, regularization='none', lambda=1, alpha=0.5, fit_intercept=true)
===============
Additional Info
===============
Name |Value
------------------+-----
iteration_count | 1
rejected_row_count| 171
accepted_row_count| 656
model.report()
| value | |
| explained_variance | 0.108102286704887 |
| max_error | 0.735080433990816 |
| median_absolute_error | 0.368522239457606 |
| mean_absolute_error | 0.440148554172583 |
| mean_squared_error | 0.224313432279204 |
| root_mean_squared_error | 0.464134935901705 |
| r2 | 0.106661816740212 |
| r2_adj | 0.101360106453804 |
| aic | -502.076724332947 |
| bic | -490.714887480117 |
All model evaluation abstractions will now use the test relation for the scoring. After that, you can evaluate the efficiency of your model.