<jupyter_start><jupyter_text>splitting X and y into training and testing sets<jupyter_code>from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y, test_size=0.30, random_state=1)<jupyter_output><empty_output><jupyter_text>create linear regression object<jupyter_code>reg = linear_model.LinearRegression()<jupyter_output><empty_output><jupyter_text>train the model using the training sets<jupyter_code>reg.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>regression coefficients<jupyter_code>print('Regression coefficients are:\n',reg.coef_)
reg.intercept_
X_test<jupyter_output><empty_output><jupyter_text>Predict the output variable<jupyter_code>y_pred = reg.predict(X_test)
plt.plot(y_test, y_pred, '.')

# plot a line, a perfit predict would all fall on this line
x = np.linspace(0, 50, 10)
y = x
plt.plot(x, y)
plt.show()
y_pred
df = pd.DataFrame({'Actual':y_test,'Predicted':y_pred})
df<jupyter_output><empty_output><jupyter_text>Use metrics to calculate the performance<jupyter_code>print('Mean Absolute Error:',metrics.mean_absolute_error(df['Actual'],df['Predicted']))
print('Mean Squared Error:',metrics.mean_squared_error(y_test,y_pred))        ## y_test,y_pred = df['Actual'],df['Predicted']
print('Root Mean Squared Error:',np.sqrt(metrics.mean_squared_error(y_test,y_pred)))<jupyter_output>Mean Absolute Error: 3.3446655035987476
Mean Squared Error: 19.831323672063235
Root Mean Squared Error: 4.45323743719816<jupyter_text>variance score: 1 means perfect prediction (R^2 Value)<jupyter_code>print('Variance score is:',reg.score(X_test,y_test))   ## Our model is 78% accurate
metrics.r2_score(y_test,y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12121212121212122, 'fraction_numerical': 0.03671328671328671, 'mean_word_length': 10.523489932885907, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '604691', 'license': 'MIT', 'n_tokens_mistral': 691, 'n_tokens_neox': 633, 'n_words': 105, 'path': 'Machine Learning/Regression/Linear Regression - Boston Housing Dataset Example.ipynb', 'repo_name': 'mohammedsaqlainattar/Python-for-Data-Science', 'size': '1716'}