Skip to content

Instantly share code, notes, and snippets.

@rodrigols89
Created August 17, 2020 18:57
Show Gist options
  • Select an option

  • Save rodrigols89/9ff3de194d298706f79b094fbb5bea92 to your computer and use it in GitHub Desktop.

Select an option

Save rodrigols89/9ff3de194d298706f79b094fbb5bea92 to your computer and use it in GitHub Desktop.
training-testing.py
def createRegression(samples,variavel_numbers, n_noise):
from sklearn.datasets import make_regression
x, y = make_regression(n_samples=samples, n_features=variavel_numbers, noise=n_noise)
return x, y
if __name__ =='__main__':
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
reg = createRegression(200, 1, 30)
model = LinearRegression()
# Divide the data into Training and Testing - 30% for testing.
x_train, x_test, y_train, y_test = train_test_split(reg[0], reg[1], test_size=0.30)
# Just the training data is transferred to the fit() function (which finds the best values ​​for m and b).
model.fit(x_train, y_train)
a_coeff = model.coef_ # Take Angular Coefficient - m
l_coeff = model.intercept_ # Take Linear Coefficient - b
print('Angular Coefficient: {0}\nTake Linear Coefficient: {1}'.format(a_coeff, l_coeff))
plt.figure(figsize=(10, 7))
plt.subplot(211)
plt.scatter(reg[0], reg[1])
plt.title('Complete Sample')
plt.plot(x_train, a_coeff*x_train + l_coeff,color='red')
plt.subplot(223)
plt.scatter(x_train, y_train)
plt.title('Training Set (70%)')
plt.plot(x_train, a_coeff*x_train + l_coeff,color='red')
plt.subplot(224)
plt.scatter(x_test, y_test)
plt.title('Testing set (30%)')
plt.plot(x_train, a_coeff*x_train + l_coeff,color='red')
plt.savefig('../images/plot-01.png', format='png')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment