<jupyter_start><jupyter_text>**Etapas do projeto**- Coleta de dados- Análise dos dados- Criação do modelo- Validação do modelo- Apresentação dos resultados<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from pandas_profiling import ProfileReport
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics

%matplotlib inline
def adjusted_R(y, ỹ, N):
    adj_r = (1 - ((1 - metrics.r2_score(y, ỹ)) * (len(y) - 1)) / (len(y) - N - 1))   
    return adj_r<jupyter_output><empty_output><jupyter_text>**Coleta de dados**<jupyter_code>#A base de dados utilizada é oriunda do kaggle
clientes = pd.read_csv('Ecommerce Customers.csv')
clientes.head()
clientes.describe()
clientes.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 500 entries, 0 to 499
Data columns (total 8 columns):
 #   Column                Non-Null Count  Dtype  
---  ------                --------------  -----  
 0   Email                 500 non-null    object 
 1   Address               500 non-null    object 
 2   Avatar                500 non-null    object 
 3   Avg. Session Length   500 non-null    float64
 4   Time on App           500 non-null    float64
 5   Time on Website       500 non-null    float64
 6   Length of Membership  500 non-null    float64
 7   Yearly Amount Spent   500 non-null    float64
dtypes: float64(5), object(3)
memory usage: 31.4+ KB<jupyter_text>**Análise exploratória de dados**<jupyter_code>relatorio = ProfileReport(clientes, title='Relatório de clientes Ecommerce')
relatorio.to_file('relatorio_clientes.html')
sns.jointplot(x='Time on Website', y='Yearly Amount Spent', data=clientes)
sns.jointplot(x='Time on App', y='Yearly Amount Spent', data=clientes)
sns.jointplot(x='Time on App', y='Length of Membership', data=clientes, kind='hex')
sns.pairplot(clientes)
sns.lmplot(x='Time on App', y='Yearly Amount Spent', data=clientes)
sns.lmplot(x='Length of Membership', y='Yearly Amount Spent', data=clientes)<jupyter_output><empty_output><jupyter_text>**Criação de um modelo com todas as variáveis** Separar quem é o x e o y<jupyter_code>clientes.head()
y = clientes['Yearly Amount Spent']
x = clientes[['Avg. Session Length', 'Time on App', 'Time on Website', 'Length of Membership']]<jupyter_output><empty_output><jupyter_text>Dividir o conjunto de dados em treino e teste<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
y_train.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Criar um modelo de regressão linear utilizando o sklearn Guardando o método em uma variável<jupyter_code>lm = LinearRegression()<jupyter_output><empty_output><jupyter_text>Treinando o modelo com o fit()<jupyter_code>lm.fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>Apresentando os coeficientes de cada variável de x<jupyter_code>lm.coef_<jupyter_output><empty_output><jupyter_text>Validando o modelo de treino com o método predict<jupyter_code>ỹ = lm.predict(x_test)
ỹ<jupyter_output><empty_output><jupyter_text>Diagrama de dispersão comparando os valores preditos com o valores reais<jupyter_code>plt.scatter(y_test, ỹ)
plt.xlabel('y real')
plt.ylabel('y predito')<jupyter_output><empty_output><jupyter_text>**Avaliando o Modelo**<jupyter_code>sns.displot(clientes, x='Yearly Amount Spent')
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Avaliação das métricas do método<jupyter_code>print('MAE:', metrics.mean_absolute_error(y_test, ỹ))
print('MSE:', metrics.mean_squared_error(y_test, ỹ))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, ỹ)))
print('R:', metrics.r2_score(y_test,ỹ))
print('R-adjust:', adjusted_R(y_test, ỹ, x_train.shape[1]))<jupyter_output>MAE: 8.426091641432054
MSE: 103.91554136503224
RMSE: 10.193897260863102
R: 0.9808757641125857
R-adjust: 0.9803481989846571<jupyter_text>**Conclusões**<jupyter_code>absolute_error = y_test - ỹ
sns.histplot(data=absolute_error, bins=5)
coef = pd.DataFrame(lm.coef_, x.columns, columns=['Coefs'])
coef<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11803041274438812, 'fraction_numerical': 0.036929761042722664, 'mean_word_length': 5.475, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '589165', 'license': 'MIT', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1554, 'n_words': 346, 'path': 'Ecommerce Customers.ipynb', 'repo_name': 'caioviniciusdelima/E-commerce-customers-sales', 'size': '4143'}