Kaggle competition: PMR3508 - Tarefa 1 - 3508 Adult Dataset
df['Date'] = pd.to_datetime(df['Date'])
 df['Date'] = pd.to_timedelta(df.Date ).dt.total_seconds().astype(int)
 df = df.replace(np.nan, '', regex=True)
 le_cr = preprocessing.LabelEncoder()
le_ps = preprocessing.LabelEncoder()
le_cr.fit(df['Country_Region'])
 le_ps.fit(df['Province_State'])
 df['Country_Region'] = le_cr.transform(df['Country_Region'])
 df['Province_State'] = le_ps.transform(df['Province_State'] )<train_model>
adult = pd.read_csv(".. /input/dataset-adult/train_data.csv",sep=",", na_values="?" )
regressor_cc = DecisionTreeRegressor(random_state = 0, max_features="auto")
 regressor_f = DecisionTreeRegressor(random_state = 0, max_features="auto")
 regressor_cc.fit(df[['Country_Region','Province_State','Date']], df[['ConfirmedCases']])
 regressor_f.fit(df[['Country_Region','Province_State','Date']], df[['Fatalities']] )<load_from_csv>
adult['native.country'].value_counts()
df_test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv' )<data_type_conversions>
na_adult=adult.set_index("Id" ).dropna()
df_test['Date'] = pd.to_datetime(df_test['Date'])
 df_test['Date'] = pd.to_timedelta(df_test.Date ).dt.total_seconds().astype(int)
 df_test = df_test.replace(np.nan, '', regex=True)
 df_test['Country_Region'] = le_cr.transform(df_test['Country_Region'])
 df_test['Province_State'] = le_ps.transform(df_test['Province_State'] )<predict_on_test>
test_adult= pd.read_csv(".. /input/dataset-adult/test_data.csv",sep=",",na_values="?" )
df_s = pd.DataFrame()
df_s['ForecastId'] = df_test['ForecastId']
df_s['ConfirmedCases'] = regressor_cc.predict(df_test[['Country_Region','Province_State','Date']])
 df_s['Fatalities'] = regressor_f.predict(df_test[['Country_Region','Province_State','Date']] )<save_to_csv>
test_adult=test_adult.set_index("Id" )
df_s.to_csv('submission.csv', index=False )<import_modules>
X_adult = na_adult.iloc[:,:-1]
Y_adult = na_adult.income
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings<set_options>
X_test = test_adult.iloc[:,:]
warnings.filterwarnings("ignore" )<load_from_csv>
num_cols=["age","education.num","capital.gain","capital.loss","hours.per.week"]
X_num=X_adult[num_cols]
Y_num=Y_adult
X_test= X_test[num_cols]
train_data = pd.read_csv('.. /input/train.csv')
 train_data.head()<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
test_data = pd.read_csv('.. /input/test.csv')
 test_data.head()<set_options>
knn=KNeighborsClassifier(n_neighbors=9)
 scores = cross_val_score(knn,X_num,Y_num,cv=10)
 knn.fit(X_num,Y_num)
 Y_testpredict=knn.predict(X_test)
 scores
sns.set(style="white", palette="muted", color_codes=True )<categorify>
convert_cols=['workclass','education','marital.status','occupation','race','relationship',
'sex','native.country']
test_adult[convert_cols] = test_adult[convert_cols].astype(str )
def get_one_hot(array):
return np.array(( array['Pclass'] == 1, array['Pclass'] == 2,
array['Pclass'] == 3, array['Sex'] == 'male',
array['Sex'] == 'female', array['SibSp'],
array['Parch'], array['Fare'],
array['Embarked'] == 'C', array['Embarked'] == 'Q',
array['Embarked'] == 'S')).swapaxes(0, 1 ).astype('float32' )<drop_column>
Xencode_adult= na_adult.iloc[:,:-1].apply(LabelEncoder().fit_transform)
 Xencode_test_adult = test_adult.apply(LabelEncoder().fit_transform)
 X_adult = Xencode_adult
X_test = Xencode_test_adult
x_train = train_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
x_train.head()<categorify>
Yfit_adult= LabelEncoder().fit(na_adult["income"])
 Y_adult = Yfit_adult.transform(na_adult["income"] )
x_train = get_one_hot(x_train)
 x_train[:10]<prepare_x_and_y>
knn =KNeighborsClassifier(n_neighbors=10)
 scores = cross_val_score(knn,X_adult,Y_adult,cv=10)
 knn.fit(X_adult,Y_adult)
 scores
y_train = np.array(train_data['Survived'])
 y_train[:10]<categorify>
Ytest_predict= knn.predict(X_test)
 print(Ytest_predict )
x_test = get_one_hot(x_test)
 x_test[:10]<import_modules>
atributos=atributos=["age","workclass","education.num","occupation","sex","marital.status","capital.gain","capital.loss"]
X_adult = Xencode_adult[atributos]
X_test = Xencode_test_adult[atributos]
from mlxtend.classifier import StackingCVClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.impute import SimpleImputer<normalization>
k_range=list(range(1,35))
k_scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
 scores = cross_val_score(knn, X_adult, Y_adult, cv=10, scoring='accuracy')
 k_scores.append(scores.mean())
 print(k_scores )
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
 imp = imp.fit(x_train)
 x_train_imp = imp.transform(x_train )<choose_model_class>
knn =KNeighborsClassifier(n_neighbors=27)
 knn.fit(X_adult,Y_adult)
 scores
clf1 = RandomForestClassifier()
clf2 = GradientBoostingClassifier()
lr = LogisticRegression()
sclf = StackingCVClassifier(classifiers=[clf1, clf2], meta_classifier=lr )<define_search_space>
Ytest_predict= knn.predict(X_test )
param_test = {'randomforestclassifier__n_estimators': [10, 120],
'randomforestclassifier__max_depth': [2, 15],
'gradientboostingclassifier__n_estimators': [10, 120],
'gradientboostingclassifier__max_depth': [2, 15],
'gradientboostingclassifier__learning_rate' : [0.01, 0.1],
'meta_classifier__C': [0.1, 10.0]}<train_model>
label_out = Yfit_adult.inverse_transform(Ytest_predict)
 df_out = pd.DataFrame({'Id': X_test.index,'income':label_out})
 df_out.to_csv('submission_adult.csv',index=False )
<normalization><EOS> 
pd.read_csv("submission_adult.csv" )