In this Short, I demonstrate my preferred method for splitting a dataframe into training and test dataframes using pandas and sklearn.
Source code:
Libraries
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
import sklearn
from sklearn.model_selection import train_test_split
Creating SQL Alchemy engine object
engine = create_engine('postgresql://postgres:youtube@localhost:5432/eSports.val')
Initiailizing connection to database
conn = engine.connect()
SQL query as a string to pass into pd.read_sql function
sql = 'SELECT * FROM players'
creating a dataframe
df = pd.read_sql_query(sql,conn)
dropping columns that aren't being analyzed
df = df.drop(['player_name','team', 'country'], axis=1)
Independent variables
X = df.drop('kd', axis=1)
Dependent (outcome) variable
y = df['kd']
train-test split
X_train, X_test, y_train, y_test = train_test_split(
X
, y
, test_size=0.3
, random_state=2)
On this page of the site you can watch the video online Python Train Test Split - Machine Learning with a duration of hours minute second in good quality, which was uploaded by the user Data Science with Josh 30 November 2022, share the link with friends and acquaintances, this video has already been watched 2,149 times on youtube and it was liked by 63 viewers. Enjoy your viewing!