<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
% matplotlib inline


from sklearn.datasets import load_boston
boston_datasets = load_boston()
boston = pd.DataFrame(boston_datasets.data, columns=boston_datasets.feature_names)
boston ['MEV'] = boston_datasets.target<jupyter_output><empty_output><jupyter_text>ENTENDIMENTO DOS DADOS<jupyter_code>boston.head()
boston.tail()
boston.describe()
boston.hist(column='CRIM')
boston.boxplot(column='CRIM')
boston.hist(column='ZN')
boston.boxplot(column='ZN')
boston.hist(column='INDUS')
boston.boxplot(column='INDUS')
boston.hist('CHAS')
boston.boxplot('CHAS')
boston.hist('NOX')
boston.boxplot('NOX')
boston.hist('RM')
boston.boxplot('RM')
boston.hist('AGE')
boston.boxplot('AGE')
boston.hist('DIS')
boston.boxplot('DIS')
boston.hist('RAD')
boston.boxplot('RAD')
boston.hist('TAX')
boston.boxplot('TAX')
boston.hist('PTRATIO')
boston.boxplot('PTRATIO')
boston.hist('B')
boston.boxplot('B')
boston.hist('LSTAT')
boston.boxplot('LSTAT')
corr_matrix = boston.corr()
corr_matrix
boston.plot(kind='scatter', x='RM', y='MEV')
boston.plot(kind='scatter', x='LSTAT', y='MEV')
df = boston[['RM', 'LSTAT', 'MEV']]
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 506 entries, 0 to 505
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   RM      506 non-null    float64
 1   LSTAT   506 non-null    float64
 2   MEV     506 non-null    float64
dtypes: float64(3)
memory usage: 12.0 KB<jupyter_text>PREPARAÇÃO DOS DADOS<jupyter_code>df.rename(columns={'RM':'Room_Number', 'LSTAT': 'LSTAT(%)', 'MEV': 'MEDV'}, inplace=True)
df.head()
df.info()
df.tail()<jupyter_output><empty_output><jupyter_text>MODELAGEM<jupyter_code>X = df[['Room_Number']]
Y = df['MEDV']
model = LinearRegression()
X_train, X_test, Y_train, Y_test = train_test_split(
    X, Y, test_size=0.3, random_state=1
)
model.fit(X_train, Y_train)
model.coef_.round(2)
model.intercept_.round(2)
y_predict = model.predict(X_test)
y_predict.shape
model.predict([[6.5]])
plt.scatter(X_test, Y_test, label='testing data');plt.plot(X_test, y_predict, label='prediction', linewidth=3)
plt.xlabel('Room_Number'); plt.ylabel('MEDV')
plt.legend(loc='upper left')
residuals = Y_test - y_predict
#plot residuals
plt.scatter(X_test, residuals)
#plot a horizontal line at y=0
plt.hlines(y=0, xmin= X_test.min(), xmax=X_test.max(), linestyle='--')
#set xlim
plt.xlim((4,9))
plt.xlabel('Room_Number'); plt.ylabel('residuals')
from sklearn.metrics import mean_squared_error
mean_squared_error(Y_test, y_predict)
model.score(X_test, Y_test)<jupyter_output><empty_output><jupyter_text>MODELAGEM SEGUNDA VARIÁVEL<jupyter_code>X2 = df[['LSTAT(%)']]

X2_train, X2_test, Y_train, Y_test = train_test_split(X2, Y, test_size=0.3, random_state=1)
model2 = LinearRegression()
model2.fit(X2_train, Y_train)
model2.coef_.round(2)
model2.intercept_.round(2)
y2_predict = model2.predict(X2_test)
model2.predict([[6.5]])
plt.scatter(X2_test, Y_test, label='testing data');plt.plot(X2_test, y2_predict, label='prediction', linewidth=3)
plt.xlabel('STAT(%)'); plt.ylabel('MEDV')
plt.legend(loc='upper right')
residuals2 = Y_test - y2_predict
plt.scatter(X2_test, residuals2)
plt.hlines(y=0, xmin=X2_test.min(), xmax=X2_test.max(), linestyles='--')
plt.xlim((4,9))
plt.xlabel('STATS(%)'); plt.ylabel('residuals2')
mean_squared_error(Y_test, y2_predict)
model.score(X2_test, Y_test )<jupyter_output><empty_output><jupyter_text>MODELANDO AS DUAS VARIÁVEIS<jupyter_code>X3 = df[['Room_Number', 'LSTAT(%)']]
X3_train, X3_test, Y_train, Y_test = train_test_split(X3, Y, test_size=0.3, random_state=1)
model3 = LinearRegression()
model3.fit(X3_train, Y_train)
model3.coef_.round(2)
model3.intercept_.round(2)
y3predict = model3.predict(X3_test)
y3predict.shape
mean_squared_error(Y_test, y_predict)
model.score(X_test, Y_test)
mean_squared_error(Y_test, y2_predict)
model2.score(X2_test, Y_test)
mean_squared_error(Y_test, y3predict)
model3.score(X3_test, Y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16674606955693186, 'fraction_numerical': 0.024773701762744165, 'mean_word_length': 7.534552845528455, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '656251', 'license': 'MIT', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1734, 'n_words': 271, 'path': 'ProgLinearSL.ipynb', 'repo_name': 'Rodrigo-Antonio-Silva/DataScienceProject', 'size': '4198'}