Kaggle competition: PMR3508 - Tarefa 1 - 3508 Adult Dataset
<SOS> metric: categorizationaccuracy Kaggle data source: pmr3508-tarefa-1-3508-adult-dataset<concatenate>
import pandas as pd
import sklearn
train=decomposedate(train)
 test=decomposedate(test )<create_dataframe>
adult = pd.read_csv(".. /input/adult-db/train_data.csv",header=0, index_col=0, na_values="?" )
submission=pd.DataFrame(columns=submission.columns )<categorify>
adult["native.country"].value_counts()
l1=LabelEncoder()
l2=LabelEncoder()<categorify>
%matplotlib inline
train['Country_Region']=l1.fit_transform(train['Country_Region'])
 train['Province_State']=l2.fit_transform(train['Province_State'] )<categorify>
adult["sex"].value_counts()
test['Country_Region']=l1.fit_transform(test['Country_Region'])
 test['Province_State']=l2.fit_transform(test['Province_State'] )<prepare_x_and_y>
nadult = adult.dropna()
adult_fill = adult.fillna(-1)
 for col in [1,3,5,6,7,8,9,13]:
nadult = adult.iloc[:,col].dropna()
label_encoder = LabelEncoder().fit(nadult)
 nadult_encoded = label_encoder.transform(nadult)
 adult_fill.iloc[np.where(adult_fill.iloc[:,col].values==-1)[0],col] = label_encoder.inverse_transform([int(mode(nadult_encoded)) ])
 for col in [0,2,4,10,11,12]:
adult_fill.iloc[np.where(adult_fill.iloc[:,col].values==-1)[0],col] = int(np.mean(adult.iloc[:,col].dropna().values))
adult_fill
y1=train[['ConfirmedCases']]
y2=train[['Fatalities']]
test_id=test['ForecastId'].values.tolist()<drop_column>
minmaxscaler = MinMaxScaler()
col_inds = [0,1,4,5,6,7,8,10,11,12]
Xadult_unscaled = adult_fill.iloc[:,col_inds].apply(LabelEncoder().fit_transform)
 Xadult = minmaxscaler.fit_transform(Xadult_unscaled)
 Yadult = adult_fill.income
print(Xadult_unscaled.columns.values )
train.pop('Id')
 test.pop('ForecastId' )<define_variables>
score_medio = np.zeros(50)
 std_score = np.zeros(50)
 for i in range(1,51):
knn = KNeighborsClassifier(n_neighbors=i, p=1)
 scores = cross_val_score(knn, Xadult, Yadult, cv=10)
 score_medio[i-1]=np.mean(scores)
 std_score[i-1]=np.std(scores)
 print(np.argmax(score_medio)+1)
 print(np.amax(score_medio))
features_cols=['Province_State','Country_Region','Day_of_Week','Month','Day','Day_of_Year','Week_of_Year','Quarter']<prepare_x_and_y>
testAdult = pd.read_csv(".. /input/adult-db/test_data.csv",header=0, index_col=0, na_values="?")
 testAdult.shape
train_x=train[features_cols]
test_x = test[features_cols]<train_model>
testAdult_fill = testAdult.fillna(-1)
 for col in [1,3,5,6,7,8,9,13]:
nTestAdult = testAdult.iloc[:,col].dropna()
label_encoder = LabelEncoder().fit(nTestAdult)
 ntestAdult_encoded = label_encoder.transform(nTestAdult)
 testAdult_fill.iloc[np.where(testAdult_fill.iloc[:,col].values==-1)[0],col] = label_encoder.inverse_transform([int(mode(ntestAdult_encoded)) ])
 for col in [0,2,4,10,11,12]:
testAdult_fill.iloc[np.where(testAdult_fill.iloc[:,col].values==-1)[0],col] = int(np.mean(testAdult.iloc[:,col].dropna().values))
testAdult_fill
model_1=DecisionTreeClassifier()
model_2=DecisionTreeClassifier()
model_1.fit(train_x,y1)
 model_2.fit(train_x,y2 )<predict_on_test>
XtestAdult_unscaled = testAdult_fill.iloc[:,col_inds].apply(LabelEncoder().fit_transform)
 XtestAdult = minmaxscaler.transform(XtestAdult_unscaled )
test_y1=model_1.predict(test_x)
 test_y2=model_2.predict(test_x)
 submission=pd.DataFrame(columns=submission.columns)
 submission['ForecastId']=test_id
submission['ConfirmedCases']=test_y1
submission['Fatalities']=test_y2<save_to_csv>
knn = KNeighborsClassifier(n_neighbors=34,p=1)
 knn.fit(Xadult,Yadult )
submission.to_csv('submission.csv',index=False )<load_from_csv>
YtestAdult = knn.predict(XtestAdult)
 YtestAdult
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test=pd.read_csv("/kaggle/input/titanic/test.csv")
<count_missing_values>
prediction = pd.DataFrame(testAdult.index)
 prediction["income"] = YtestAdult
<count_missing_values><EOS> 
prediction.to_csv("adult_prediction_5.csv", index=False )