Skip to content

Instantly share code, notes, and snippets.

@FaisalAl-Tameemi
Created November 6, 2018 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FaisalAl-Tameemi/8ddc8080987aaa4caccb43437ed447e7 to your computer and use it in GitHub Desktop.
Save FaisalAl-Tameemi/8ddc8080987aaa4caccb43437ed447e7 to your computer and use it in GitHub Desktop.
def train_test(df, response_col='price', dummy_na=False, test_size=.3, rand_state=42, plot=False):
#Split into explanatory and response variables
X = df.drop(response_col, axis=1)
y = df[response_col]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=rand_state)
lm_model = LinearRegression(normalize=True)
lm_model.fit(X_train, y_train)
train_preds = lm_model.predict(X_train)
test_preds = lm_model.predict(X_test)
train_score = r2_score(y_train, train_preds)
test_score = r2_score(y_test, test_preds)
return test_score, train_score, lm_model, X_train, X_test, y_train, y_test, test_preds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment