<jupyter_start><jupyter_text>Importando Banco de Dados<jupyter_code>df = pd.read_csv('train.csv', index_col='id')
df.head()
df = df.dropna()
for c in df.columns[df.dtypes == object]:
    df[c] = df[c].astype('category')
df.education.cat.reorder_categories(['illiterate', 'basic.4y', 'basic.6y', 'basic.9y', 'high.school', 
       'professional.course', 'university.degree'], ordered=True, inplace=True)
for c in df.columns[df.dtypes == 'category']:
    df[c] = df[c].cat.codes
df.head()
#previsores = df.drop(['default','contact', 'duration', 'previous', 'campaign', 'pdays', 'y'],  axis=1)
previsores = df.drop(['y'],  axis=1)
previsores.head()
classe = df.filter('y')
classe.head()
sum(classe['y'])
#from sklearn.preprocessing import StandardScaler
#scaler = StandardScaler()
#previsores = scaler.fit_transform(previsores)
#classe = scaler.fit_transform(classe)
from sklearn.model_selection import train_test_split
previsores_treinamento, previsores_teste, classe_treinamento, classe_teste = train_test_split(previsores, classe, test_size=0.15, random_state=42, shuffle=True, stratify=classe)
previsores_treinamento.shape, previsores_teste.shape, classe_treinamento.shape, classe_teste.shape<jupyter_output><empty_output><jupyter_text>Análise bayesiana<jupyter_code>from sklearn.naive_bayes import GaussianNB
regressor = GaussianNB()
regressor.fit(previsores_treinamento, classe_treinamento)
regressor.score(previsores_treinamento, classe_treinamento)
y_pred = regressor.predict(previsores_teste)
from sklearn import metrics
print(metrics.classification_report(classe_teste, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.94      0.85      0.89      3589
           1       0.38      0.62      0.47       520

    accuracy                           0.82      4109
   macro avg       0.66      0.73      0.68      4109
weighted avg       0.87      0.82      0.84      4109<jupyter_text>Análise K-means<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn_class = KNeighborsClassifier(algorithm='auto', metric='minkowski', leaf_size=30, n_neighbors=5)
knn_class.fit(previsores_treinamento, classe_treinamento)
knn_class.score(previsores_treinamento, classe_treinamento)
y_pred = knn_class.predict(previsores_teste)
from sklearn import metrics
print(metrics.classification_report(classe_teste, y_pred))
sum(y_pred)<jupyter_output><empty_output><jupyter_text>RandomForest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
regressor_rf = RandomForestClassifier(n_estimators=10000, criterion='entropy', random_state=42, max_depth=1000)
regressor_rf.fit(previsores_treinamento, classe_treinamento)
regressor_rf.score(previsores_treinamento, classe_treinamento)
y_pred = regressor_rf.predict(previsores_teste)
print(metrics.classification_report(classe_teste, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.93      0.96      0.95      3589
           1       0.66      0.54      0.59       520

    accuracy                           0.91      4109
   macro avg       0.80      0.75      0.77      4109
weighted avg       0.90      0.91      0.90      4109<jupyter_text>Análise SVM<jupyter_code>from sklearn.svm import SVC
regressor = SVC(kernel='rbf', random_state=1, C=2)
regressor.fit(previsores_treinamento, classe_treinamento)
regressor.score(previsores_treinamento, classe_treinamento)
y_pred = regressor.predict(previsores_teste)
print(metrics.classification_report(classe_teste, y_pred))
#!pip install tensorflow
#!pip install keras
import keras
from keras.models import Sequential
from keras.layers import Dense

classificador = Sequential( )
classificador.add(Dense(units=40, activation='relu', input_dim=20), )
classificador.add(Dense(units=40, activation='relu'))
model.add(Dropout(0.2))
classificador.add(Dense(units=40, activation='relu'))
model.add(Dropout(0.2))
classificador.add(Dense(units=40, activation='relu'))
model.add(Dropout(0.2))
classificador.add(Dense(units=40, activation='relu'))
classificador.add(Dense(units=1, activation='sigmoid'))
classificador.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
classificador.summary()
classificador.fit(previsores_treinamento, classe_treinamento, batch_size=10, epochs=100)
y_pred = classificador.predict(previsores_teste)
#lista = lis
#for x in y_pred:
 #   print(x)
y_pred = (y_pred > 0.5)
print(metrics.classification_report(classe_teste, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.92      0.96      0.94      3589
           1       0.62      0.46      0.53       520

    accuracy                           0.90      4109
   macro avg       0.77      0.71      0.73      4109
weighted avg       0.89      0.90      0.89      4109<jupyter_text>Kadle<jupyter_code>aval = pd.read_csv('test.csv', index_col='id')
sub = pd.read_csv('sample.csv', index_col='id')
#aval.head()
for c in aval.columns[aval.dtypes == object]:
    aval[c] = aval[c].astype('category')
aval.education.cat.reorder_categories(['illiterate', 'basic.4y', 'basic.6y', 'basic.9y', 'high.school', 
       'professional.course', 'university.degree'], ordered=True, inplace=True)
for c in aval.columns[aval.dtypes == 'category']:
    aval[c] = aval[c].cat.codes
#aval.head()
#previsores = aval.drop(['contact', 'duration', 'previous'],  axis=1)
#aval = scaler.fit_transform(aval)
y_aval = regressor_rf.predict(aval)
#y_aval = (y_aval > 0.1)
y_aval
sub.y = pd.Series(y_aval)
sub.head()
sub.to_csv('Resultado_lucas_rf.csv')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup