# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="_aeeICXStavU" outputId="0a1001a5-47e4-4457-de56-772068584d0e" colab={"base_uri": "https://localhost:8080/"}
#2020-ml-p4-re
# !pip uninstall --y kaggle 
# !pip install --upgrade pip
# !pip install kaggle==1.5.6

# !mkdir -p ~/.kaggle
# !cp kaggle.json ~/.kaggle/
# !chmod 600 ~/.kaggle/kaggle.json
# !kaggle -v


# + id="yw3ysPvoIc7p" outputId="2caed467-4220-4b9d-bad2-89e61406d535" colab={"base_uri": "https://localhost:8080/"}
# !kaggle competitions download -c 2020ml4re
# !unzip 2020ml4re.zip

# + id="F5Og7qkyIjvh" outputId="ef596a79-9723-49da-e1a8-ac1c550f3266" colab={"base_uri": "https://localhost:8080/"}
import seaborn as sns
import pandas as pd 
import numpy as np 

df_train = pd.read_csv('train.csv')
df_test = pd.read_csv('test.csv')


df_train.drop(['age_group','religion'], axis=1, inplace=True)
df_test.drop(['age_group','religion'], axis=1, inplace=True)

full_data = [df_train,df_test]
pd.options.display.max_columns = None
print(df_train.head(20))
print(df_test.head(20)) 

# + id="zstUYQfYAIis" outputId="6f9be2d6-b8a5-48d4-d9d5-30f8d8357c60" colab={"base_uri": "https://localhost:8080/"}
print (df_train[["gender", "voted"]].groupby(['gender'], as_index=False).mean())
print (df_train[["race", "voted"]].groupby(['race'], as_index=False).mean())

for dataset in full_data:
    dataset['gender'] = dataset['gender'].map( {'Female': 0, 'Male': 1} ).astype(int)
    dataset['race'] = dataset['race'].map( {'Arab': 0, 'Asian': 1,'Black': 2, 'Indigenous Australian': 3,'Native American':4,'Other':5,'White':6}).astype(int)

# + id="AezdARIMJsk6" outputId="dfca1e9c-1316-4afa-8185-d0c00e71ca8c" colab={"base_uri": "https://localhost:8080/"}
X = df_train.iloc[:,1:-1]
y = df_train.iloc[:,[-1]]
test_x = df_test.iloc[:,1:]
print(X.dtypes)
print(y.shape)
print(test_x.shape)
#religion    object city 
#gender         object male , female
#age_group      object 30s...
#race           object white .. 

# + id="9fScXL76KCJn"
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=777,stratify=y)

# + id="fCDRVQYNKFTv" outputId="2620d9d8-0440-453d-f582-eb0d14d5caa1" colab={"base_uri": "https://localhost:8080/"}
print('y_train class distribution')
print(y_train.value_counts(normalize=True))

print('y_test class distribution')
print(y_test.value_counts(normalize=True))

# + id="80USsgnjKvim" outputId="50fc362e-b20f-424f-dcbe-72d8d0cffc30" colab={"base_uri": "https://localhost:8080/"}
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier(random_state=0)
clf.fit(X_train, y_train)
y_pred = clf.predict(test_x)
y_pred

# + id="B5PP64XuL13E" outputId="b446900a-d2d9-4630-d862-72aa568fa61a" colab={"base_uri": "https://localhost:8080/", "height": 419}
submit = pd.read_csv('sampleSubmission.csv')

submit

# + id="BE3_tFEsMJSi" outputId="a18441b9-a9fd-4dad-aad3-553fff3918a1" colab={"base_uri": "https://localhost:8080/", "height": 419}
for i in range(len(y_pred)):
    submit['voted'][i] = y_pred[i].item()

submit

# + id="6e_SsZZlMvB8" outputId="eaa70adf-75d6-4b74-e842-00250af5a8e2" colab={"base_uri": "https://localhost:8080/"}
submit.to_csv('result.csv',mode='w',index=False)
# !kaggle competitions submit -c 2020ml4re -f result.csv -m "14010974_이기택"

StarcoderdataJupyterScriptsDedupFiltered