Kaggle competition: PMR3508 - Tarefa 1 - 3508 Adult Dataset
<SOS> metric: categorizationaccuracy Kaggle data source: pmr3508-tarefa-1-3508-adult-dataset<concatenate>
raw = pd.read_csv(".. /input/badult/train_data.csv",
names= None,
engine='python',
na_values = '?' )
train['is_train'] = 1
train['origin_index'] = train.index
test['is_train'] = 0
test['origin_index'] = test.index
traintest = pd.concat([train, test], axis=0, sort=False )<normalization>
clean = raw.dropna()
clean.info()
min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0,1))
num_cols = ['Age', 'Fare', 'SibSp_parch']
cat_cols = ['Sex', 'Cabin', 'Embarked', 'Pclass', 'SibSp', 'Parch']
train['SibSp_parch'] = train.SibSp + train.Parch
test['SibSp_parch'] = test.SibSp + train.Parch
scaled_train = train[num_cols+cat_cols].copy()
scaled_test = test[num_cols+cat_cols].copy()
one_hot_data = pd.get_dummies(traintest[cat_cols+['is_train', 'origin_index']], columns=cat_cols)
 train_one_hot = one_hot_data[one_hot_data.is_train==1]
test_one_hot = one_hot_data[one_hot_data.is_train==0]
scaled_train = scaled_train.merge(train_one_hot, left_index=True, right_on='origin_index')
 scaled_test = scaled_test.merge(test_one_hot, left_index=True, right_on='origin_index')
 scaled_train = scaled_train.drop(columns=cat_cols + ['origin_index', 'is_train'])
 scaled_test = scaled_test.drop(columns=cat_cols+ ['origin_index', 'is_train'])
 scaled_train = scaled_train.fillna(scaled_train.mean())
 scaled_test = scaled_test.fillna(scaled_train.mean())
 y_train = train['Survived']
X_train = scaled_train
X_test = scaled_test
X_train[num_cols] = min_max_scaler.fit_transform(X_train[num_cols])
 X_test[num_cols] = min_max_scaler.fit_transform(X_test[num_cols] )<import_modules>
obg = raw[['workclass','education','marital.status','occupation','relationship','race','sex','native.country','income']]
obg.nunique()
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.linear_model import LinearRegression<split>
analysis = clean
analysis = analysis.apply(preprocessing.LabelEncoder().fit_transform)
 plt.matshow(analysis.corr() )
x_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=0 )<train_model>
anl0 = analysis.corr().income.sort_values(ascending=True)
 anl0
model_svm_linear = SVC(kernel='linear', C=1 ).fit(x_train, Y_train)
 print("Training score: ", model_svm_linear.score(x_train, Y_train))
print("Validation score: ", model_svm_linear.score(X_val, Y_val))<train_model>
anl1 = pd.get_dummies(clean[['relationship','marital.status','capital.loss', 'sex', 'hours.per.week', 'age', 'education.num', 'capital.gain', 'income']])
 anl1 = anl1.corr().loc[:,'income_>50K'].sort_values(ascending=True)
 anl1
model_svm_sigmoid = SVC(kernel='sigmoid' ).fit(x_train, Y_train)
 print("Training score: ", model_svm_sigmoid.score(x_train, Y_train))
print("Validation score: ", model_svm_sigmoid.score(X_val, Y_val))<train_model>
anl1_5 = pd.get_dummies(clean)
 anl1_5 = anl1_5.corr().loc[:,'income_>50K'].sort_values(ascending=True ).where(lambda x : abs(x)> 0.15 ).dropna()
anl1_5
model_svm_rbf = SVC(kernel='rbf' ).fit(x_train, Y_train)
 print("Training score: ", model_svm_rbf.score(x_train, Y_train))
print("Validation score: ", model_svm_rbf.score(X_val, Y_val))<train_model>
anl2 = clean[['occupation','income','race']]
anl2 = pd.get_dummies(anl2 ).drop(columns = 'income_<=50K')
 anl2 = anl2.corr().loc[:,'income_>50K'].sort_values(ascending=True ).where(lambda x : abs(x)> 0.088 ).dropna()
anl2
model_logistic = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial' ).fit(x_train, Y_train)
 print("Training score: ", model_logistic.score(x_train, Y_train))
print("Validation score: ", model_logistic.score(X_val, Y_val))<train_model>
train_clean = pd.get_dummies(clean)
 index = anl1.where(lambda x : abs(x)> 0.07 ).dropna().index[1:-1].append(anl2.index[:-1] )
model = SVC(kernel='linear', C=1 ).fit(X_train, y_train)
 print("Training score: ", model_svm_linear.score(X_train, y_train))
class_predict = model.predict(X_test )<define_variables>
test_raw = pd.read_csv(".. /input/badult/test_data.csv",
names= None,
engine='python' )
test_ID = test['PassengerId']<create_dataframe>
X_train = train_clean[index].drop(columns='sex_Female')
 Y_train = train_clean.loc[:,'income_>50K']
temp = {'PassengerID': test_ID, 'Survived': class_predict}
result = pd.DataFrame(temp )<save_to_csv>
test_clean = pd.get_dummies(test_raw)
 test_clean = test_clean.dropna()
X_test = test_clean[index].drop(columns='sex_Female' )
result.to_csv('result.csv', index=False )<import_modules>
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.callbacks import EarlyStopping
from sklearn import preprocessing<choose_model_class>
k_range = list(range(1, 31))
weight_options = ['uniform', 'distance']
p_options = list(range(1,3))
param_grid = dict(n_neighbors=k_range, p=p_options )
model_neuron = Sequential()
model_neuron.add(Dense(output_dim=256, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dropout(0.5))
model_neuron.add(Dense(output_dim=128, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dropout(0.5))
model_neuron.add(Dense(output_dim=64, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dropout(0.5))
model_neuron.add(Dense(output_dim=32, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dense(output_dim=16, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dense(output_dim=1, input_shape=(X_train.shape[1],), activation='sigmoid'))
model_neuron.compile(loss='mse', optimizer=Adam(lr=1e-5), metrics=['accuracy'])
 model_neuron.summary()<train_model>
knn = KNeighborsClassifier(n_neighbors=5)
 grid = GridSearchCV(knn, param_grid, cv=10, scoring='accuracy', n_jobs = -2 )
history = model_neuron.fit(X_train, y_train, nb_epoch=10000, validation_split=0.2, callbacks=[EarlyStopping(patience=10)] )<choose_model_class>
grid.fit(X_train, Y_train)
 print(grid.best_estimator_)
 print(grid.best_score_ )
model_neuron = Sequential()
model_neuron.add(Dense(output_dim=256, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dense(output_dim=128, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dense(output_dim=64, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dense(output_dim=32, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dense(output_dim=16, input_shape=(X_train.shape[1],), activation='relu'))
model_neuron.add(Dense(output_dim=1, input_shape=(X_train.shape[1],), activation='sigmoid'))
model_neuron.compile(loss='mse', optimizer=Adam(lr=1e-5), metrics=['accuracy'])
 model_neuron.summary()
history = model_neuron.fit(X_train, y_train, nb_epoch=200, callbacks=[EarlyStopping(patience=10)] )<save_to_csv>
f_kNN.fit(X_train,Y_train )
class_predict = model_neuron.predict_classes(X_test)
 class_predict = class_predict.reshape(( class_predict.shape[0],))
test_ID = test['PassengerId']
temp = {'PassengerID': test_ID, 'Survived': class_predict}
result = pd.DataFrame(temp)
 result.to_csv('result_neuron.csv', index=False )<count_unique_values>
Y_test = f_kNN.predict(X_test )
<set_options><EOS> 
Y_test_copy = Y_test
Y_test_copy = Y_test_copy.tolist()
answer = [["Id","income"]]
for output in range(len(Y_test_copy)) :
if Y_test_copy[output] == 0:
Y_test_copy[output] = '<=50K'
else:
Y_test_copy[output] = '>50K'
answer.append([output,Y_test_copy[output]])
 myFile = open("submit.csv", 'w')
 with myFile:
writer = csv.writer(myFile)
 writer.writerows(answer )