Kaggle competition: PMR3508 - Tarefa 1 - 3508 Adult Dataset
<SOS> metric: categorizationaccuracy Kaggle data source: pmr3508-tarefa-1-3508-adult-dataset<load_from_csv>
import pandas as pd
import numpy as np
import sklearn
import os
from matplotlib import pyplot as plt
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 PassengerId = test['PassengerId']
test.head(5 )<data_type_conversions>
filepath = ".. /input/adult-dataset/train_data.csv"
train['Ticket_type'] = train['Ticket'].apply(lambda x: x[0:4])
 train['Ticket_type'] = train['Ticket_type'].astype('category')
 train['Ticket_type'] = train['Ticket_type'].cat.codes
test['Ticket_type'] = test['Ticket'].apply(lambda x: x[0:4])
 test['Ticket_type'] = test['Ticket_type'].astype('category')
 test['Ticket_type'] = test['Ticket_type'].cat.codes
train['Ticket_type'].value_counts().plot.bar(figsize=(30, 5), title = 'Ticket Code Conversion', xlabel = 'Ticket Code', ylabel = 'People Count' )<feature_engineering>
adult = pd.read_csv(filepath,
names=[
"Age", "Workclass", "fnlwgt", "Education", "Education-Num", "Martial Status",
"Occupation", "Relationship", "Race", "Sex", "Capital Gain", "Capital Loss",
"Hours per week", "Country", "Target"],
sep=r'\s*,\s*',
engine='python',
na_values="?" )
train['IsAlone'] = 0
train.loc[train['FamilySize'] == 1, 'IsAlone'] = 1
test['IsAlone'] = 0
test.loc[test['FamilySize'] == 1, 'IsAlone'] = 1<feature_engineering>
listNull = [x for x in [(row,adult[row].isnull().sum())for row in adult] if x[1]!=0]
print(listNull )
train['Fare'] = train['Fare'].fillna(train['Fare'].median())
 test['Fare'] = test['Fare'].fillna(train['Fare'].median())
 train['CategoricalFare'] = pd.qcut(train['Fare'], 4 )<feature_engineering>
adult.drop(adult.index[0], inplace=True )
def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
for dataset in whole_data:
dataset['Title'] = dataset['Name'].apply(get_title)
 for dataset in whole_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 for dataset in whole_data:
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 fig = sns.displot(data=train, x="Title", hue="Survived", multiple="stack", kde = True)
 fig.set(title='Title Mapped vs Survived' )<data_type_conversions>
adult["Workclass"].value_counts()
adult["Occupation"].value_counts()
for dataset in whole_data:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] 						 = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] 							 = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 for dataset in whole_data:
dataset['Sex'] = dataset['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 fig = sns.displot(data=train, x="Sex", hue="Survived", multiple="stack", kde = True)
 fig.set(title='Sex vs Survived')
 fig = sns.displot(data=train, x="Fare", hue="Survived", multiple="stack", kde = True)
 fig.set(title='Fare vs Survived' )<data_type_conversions>
firstW = ["United-States",
"Germany",
"Canada",
"England",
"Italy",
"Japan",
"Poland",
"Portugal",
"Taiwan",
"France",
"Greece",
"Ireland",
"Hong",
"Yugoslavia",
"Hungary",
"Scotland",
"Holand-Netherlands"]
train['Embarked'] = train['Embarked'].fillna('S')
 test['Embarked'] = test['Embarked'].fillna('S')
 train['Embarked'] = train['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test['Embarked'] = test['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 fig = sns.displot(data=train, x="Embarked", hue="Survived", multiple="stack", kde = True)
 fig.set(title='Embarked feature vs Survived' )<drop_column>
adult["Race"] = adult["Race"].transform(lambda x: 1 if x=="White" else 0 if x==x else x)
 adult["Country"] = adult["Country"].transform(lambda x: 1 if x in firstW else -1 if x == "Mexico" else 0 if x==x else x)
 adult["Sex"].replace({"Male":1,"Female":0}, inplace=True )
drop_elements = ['PassengerId', 'Name', 'Ticket', 'Cabin', 'SibSp']
train = train.drop(drop_elements, axis = 1)
 train = train.drop(['CategoricalAge', 'CategoricalFare'], axis = 1)
 test = test.drop(drop_elements, axis = 1)
 train.head(5 )<prepare_x_and_y>
adult["Age"] = adult["Age"].transform(lambda x: int(x))
adult["Age"] = adult["Age"].transform(lambda x: 2*(x - x.mean())/ x.std())
 adult["Workclass"] = adult["Workclass"].transform(lambda x: 1 if x in ["Without-pay", "Never-worked"] else 0 if x==x else x)
 adult["Occupation"] = adult["Occupation"].transform(lambda x: 1 if x in ["Priv-house-serv","Handlers-cleaners","Transport-moving"] else 0 if x==x else x)
y_train = train['Survived'].ravel()
train = train.drop(['Survived'], axis=1)
 x_train = train.values
x_test = test.values<load_from_csv>
adult["MissingCountry"] = adult["Country"].transform(lambda x: 1 if x==x else 0)
 adult["Country"].fillna(adult["Country"].mean() , inplace=True)
 adult["MissingWorkclass"] = adult["Workclass"].transform(lambda x: 1 if x==x else 0)
 adult["Workclass"].fillna(adult["Workclass"].mean() , inplace=True)
 adult["MissingOccupation"] = adult["Occupation"].transform(lambda x:1 if x==x else 0)
 adult["Occupation"].fillna(adult["Occupation"].mean() ,inplace=True )
test_data_with_labels = pd.read_csv("https://github.com/thisisjasonjafari/my-datascientise-handcode/raw/master/005-datavisualization/titanic.csv")
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 for i, name in enumerate(test_data_with_labels['name']):
if '"' in name:
test_data_with_labels['name'][i] = re.sub('"', '', name)
 for i, name in enumerate(test_data['Name']):
if '"' in name:
test_data['Name'][i] = re.sub('"', '', name)
 survived = []
for name in test_data['Name']:
survived.append(int(test_data_with_labels.loc[test_data_with_labels['name'] == name]['survived'].values[-1]))
y_true = np.array(survived )<compute_train_metric>
nadult = adult.dropna()
gbm = xgb.XGBClassifier(n_estimators= 100,
max_depth = 4,
gamma = 0.9,
nthread = -1,
scale_pos_weight=1,
random_state = 3101)
 gbm.fit(x_train, y_train)
 xgb_predictions = gbm.predict(x_test)
 score_gbm = gbm.score(x_train, y_train)
 print(f'Random Forest Classifier score(Train Accuracy): {score_gbm}')
 test_acc_gbm = accuracy_score(y_true, xgb_predictions)
 print(f'Random Forest Classifier score(Test Accuracy): {test_acc_gbm}')
 xgb_submission = pd.DataFrame({ 'PassengerId': PassengerId,
'Survived': xgb_predictions })
 xgb_submission.head()
xgb_submission.to_csv('xgb_submission.csv', index = False )<train_model>
nadult["MissingCountry"].value_counts()
rfc = RandomForestClassifier(n_estimators = 4,
max_features = 5,
random_state = 216)
 rfc.fit(x_train, y_train)
 score_rfc = rfc.score(x_train, y_train)
 out_rfc = rfc.predict(x_test)
 print(f'Random Forest Classifier score(Train Accuracy): {score_rfc}')
 test_acc_rfc = accuracy_score(y_true, out_rfc)
 print(f'Random Forest Classifier score(Test Accuracy): {test_acc_rfc}')
 rfc_submission = pd.DataFrame({ 'PassengerId': PassengerId,
'Survived': out_rfc })
 rfc_submission.head()
rfc_submission.to_csv('rfc_submission.csv', index = False )<train_model>
testfilepath = ".. /input/adult-dataset/test_data.csv"
knn = KNeighborsClassifier(n_neighbors=5)
 knn.fit(x_train, y_train)
 score_knn = knn.score(x_train, y_train)
 out_knn = knn.predict(x_test)
 print(f'K- Nearest Neighbour ClassifierClassifier score(Train Accuracy): {score_knn}')
 test_acc_knn = accuracy_score(y_true, out_knn)
 print(f'K- Nearest Neighbour Classifier Classifier score(Test Accuracy): {test_acc_knn}' )<compute_train_metric>
testAdult = pd.read_csv(testfilepath,
names=[
"Id","Age", "Workclass", "fnlwgt", "Education", "Education-Num", "Martial Status",
"Occupation", "Relationship", "Race", "Sex", "Capital Gain", "Capital Loss",
"Hours per week", "Country", "Target"],
sep=r'\s*,\s*',
engine='python',
na_values="?" )
svc = SVC(C = 5, kernel = 'linear', random_state = 8)
 svc.fit(x_train, y_train)
 score_svc = svc.score(x_train, y_train)
 out_svc = svc.predict(x_test)
 print(f'Support Vector Machine Classifier score(Train Accuracy): {score_svc}')
 test_acc_svc = accuracy_score(y_true, out_svc)
 print(f'K- Support Vector Machine Classifier Classifier score(Test Accuracy): {test_acc_svc}' )<compute_train_metric>
testAdult
testAdult["Workclass"].value_counts()
vclf = VotingClassifier(estimators=[('gb',gbm),('rf',rfc),('knn',knn),('svm',svc)], voting='hard', weights=[2,3,1,2])
 vclf.fit(x_train, y_train)
 out_vclf = vclf.predict(x_test)
 score_voting = vclf.score(x_train, y_train)
 print(f'Voting Classifier score(Train Accuracy): {score_voting}')
 test_acc_voting = accuracy_score(y_true, out_vclf)
 print(f'Voting Classifier score(Test Accuracy): {test_acc_voting}' )<prepare_output>
testAdult.drop(testAdult.index[0], inplace=True )
classifier = ['XGBoost', 	'RandomForest', 'KNN', 'SVC', 'VotingEnsemble']
train_acc = [score_gbm,	score_rfc,	score_knn, score_svc, score_voting]
test_acc = [test_acc_gbm, 	test_acc_rfc, 	test_acc_knn, test_acc_svc, test_acc_voting]
score_df = pd.DataFrame({'classifier': classifier, 'train_acc': train_acc, 'test_acc': test_acc})
 score2 = score_df.copy()
score_df.train_acc =(np.floor(score_df.train_acc*10000)/100 ).map('{:,.2f}'.format)
 score_df.test_acc =(np.floor(score_df.test_acc*10000)/100 ).map('{:,.2f}'.format)
 score_df<import_modules>
testAdult.drop("Id",axis=1, inplace=True)
 testAdult.drop("Target",axis=1, inplace=True )
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.layers import Input, Dense, BatchNormalization, Add, GaussianNoise, Dropout
from keras.models import Model
from keras.layers import Wrapper
from keras.callbacks import ReduceLROnPlateau
from keras.utils import to_categorical
from keras import regularizers, Sequential
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler<load_from_csv>
listNull = [x for x in [(row,testAdult[row].isnull().sum())for row in testAdult] if x[1]!=0]
print(listNull )
train_set = pd.read_csv(".. /input/train.csv")
 train_dfY = train_set['Survived']
test_set = pd.read_csv(".. /input/test.csv")
 submission = test_set['PassengerId'].copy()
print(train_set.shape)
 print(test_set.shape)
 print(train_dfY.shape)
<count_values>
testAdult["Race"] = testAdult["Race"].transform(lambda x: 1 if x=="White" else 0)
 testAdult["Country"] = testAdult["Country"].transform(lambda x: 1 if x in firstW else -1 if x=="Mexico" else 0)
 testAdult["Sex"] = testAdult["Sex"].transform(lambda x: 1 if x=="Male" else 0)
 testAdult["Age"] = testAdult["Age"].transform(int)
 testAdult["Age"] = testAdult["Age"].transform(lambda x: 2*(x - x.mean())/ x.std())
 testAdult["Workclass"] = testAdult["Workclass"].transform(lambda x: 1 if x in ["Without-pay", "Never-worked"] else 0)
 testAdult["Occupation"] = testAdult["Occupation"].transform(lambda x: 1 if x in ["Priv-house-serv","Handlers-cleaners","Transport-moving"] else 0)
train_set['Cabin'].value_counts() 
<feature_engineering>
testAdult["MissingCountry"] = testAdult["Country"].transform(lambda x: 1 if x==x else 0)
 testAdult["Country"].fillna(testAdult["Country"].mean() , inplace=True)
 testAdult["MissingWorkclass"] = testAdult["Workclass"].transform(lambda x: 1 if x==x else 0)
 testAdult["Workclass"].fillna(testAdult["Workclass"].mean() , inplace=True)
 testAdult["MissingOccupation"] = testAdult["Occupation"].transform(lambda x:1 if x==x else 0)
 testAdult["Occupation"].fillna(testAdult["Occupation"].mean() ,inplace=True)
datasets = [train_set, test_set]
originalData = train_set
for dataset in datasets:
dataset['Age'].fillna(dataset["Age"].median() , inplace=True)
 dataset['Embarked'].fillna(dataset["Embarked"].mode() [0], inplace=True)
 dataset['Fare'].fillna(dataset['Fare'].median() , inplace = True)
 dataset.drop(['PassengerId','Cabin', 'Ticket'], axis=1, inplace=True)
 dataset['FamilySize'] = dataset ['SibSp'] + dataset['Parch'] + 1
dataset['IsAlone'] = 1
dataset['IsAlone'].loc[dataset['FamilySize'] > 1] = 0
print('Ahora columnas null en el trainset')
 print(train_set.isnull().sum())
 print('Ahora columnas null en el test set')
 print(test_set.isnull().sum())
<count_values>
testAdult["Country"].value_counts()
for dataset in datasets:
StringArray = dataset['Name'].str.split(", ", expand=True)
 StringArray = StringArray[1].str.split(".", expand=True)
 dataset['Title'] = StringArray[0]
title_names = dataset['Title'].value_counts()
print('===Prior to grouping===')
 print(title_names)
 title_names =(dataset['Title'].value_counts() > 7)
 dataset['Title'] = dataset['Title'].apply(lambda x: 'Misc' if title_names.loc[x] == False else x)
 print('===Post grouping===')
 print(dataset['Title'].value_counts())
<categorify>
nTestAdult = testAdult
label = LabelEncoder()
enc = OneHotEncoder(handle_unknown='ignore')
 train_set = datasets[0]
test_set = datasets[1]
train_set = train_set.drop(['Survived'], axis=1)
 EncodedDataFrames = []
print(train_set.shape, test_set.shape)
 wholeData = pd.concat([train_set, test_set], ignore_index=True)
 wholeData['Sex_Code'] = label.fit_transform(wholeData['Sex'])
 wholeData['Embarked_Code'] = label.fit_transform(wholeData['Embarked'])
 wholeData['Title_Code'] = label.fit_transform(wholeData['Title'])
 sex_encoded = to_categorical(wholeData['Sex_Code'])
 SexOneHot = pd.DataFrame(sex_encoded, columns = ["Female", "Male"])
 embarked_encoded = to_categorical(wholeData['Embarked_Code'])
 EmbarkedOneHot = pd.DataFrame(embarked_encoded, columns = ["C", "Q", "S"])
 title_encoded = to_categorical(wholeData['Title_Code'])
 TitleOneHot = pd.DataFrame(title_encoded, columns = ["Master", "Misc", "Miss", "Mr", "Mrs"])
 pclass_encoded = to_categorical(wholeData['Pclass'])
 PClassOneHot = EmbarkedOneHot = pd.DataFrame(pclass_encoded, columns = ["0th Class", "1th Class", "2nd Class", "3rd Class"])
 PClassOneHot = PClassOneHot.drop("0th Class", axis=1)
 wholeData = pd.concat([wholeData, SexOneHot, EmbarkedOneHot, TitleOneHot, PClassOneHot], axis=1)
 EncodedDataFrames.append(wholeData)
 print(wholeData.shape)
<split>
Xadult = nadult[["Age", "Workclass","Education-Num","Occupation","Race", "Sex","Capital Gain", "Capital Loss", "Hours per week", "Country"]]
wholeData = wholeData.drop(['Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked', 'Title', 'Pclass'], axis= 1)
 [train_set, test_set] = np.split(wholeData, [891], axis= 0)
 train_dfX = train_set
print(train_dfX.shape, train_dfY.shape)
 train_dfX,val_dfX,train_dfY, val_dfY = train_test_split(train_dfX,train_dfY , test_size=0.1, stratify=train_dfY)
 print("Entrenamiento: ",train_dfX.shape, train_dfY.shape)
 print("Validacion : ",val_dfX.shape, val_dfY.shape)
 print("Test: ", test_set.shape)
<choose_model_class>
Yadult = nadult.Target
model = Sequential()
model.add(Dense(40, kernel_initializer = 'glorot_normal', bias_initializer='zeros', activation = 'relu', kernel_regularizer=regularizers.l2(0.01), input_dim = 19))
model.add(Dropout(0.2))
model.add(Dense(20, kernel_initializer = 'glorot_normal', bias_initializer='zeros', activation = 'relu', kernel_regularizer=regularizers.l2(0.01)))
 model.add(Dropout(0.2))
model.add(Dense(1, kernel_initializer = 'glorot_normal', bias_initializer='zeros', activation = 'sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['binary_accuracy'])
 model.summary() 
<train_model>
XtestAdult = nTestAdult[["Age", "Workclass","Education-Num","Occupation","Race", "Sex","Capital Gain", "Capital Loss", "Hours per week", "Country"]]
train_history = model.fit(train_dfX, train_dfY, batch_size=24, epochs= 20, validation_data=(val_dfX, val_dfY))<save_to_csv>
XtestAdult["Country"].value_counts()
y_test = model.predict(test_set)
 y_formatted = np.where(y_test > 0.5, 1, 0)
 y_dataFrame = pd.DataFrame(np.ravel(y_formatted), columns=['Survived'])
 submission = pd.concat([submission, y_dataFrame], axis=1)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
df = pd.read_csv('.. /input/train.csv')
 enumSeries = []
numSeries = []
nonDiscreteSeries = []
for colname in df.columns:
datatype = str(df[colname].dtype)
 if colname == 'Id' or colname == 'SalePrice':
pass
elif datatype == 'int64' or datatype == 'float64':
numSeries.append(colname)
 else:
enumSeries.append(colname)
 df.fillna(pd.Series([0,1970,0, 5, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0], index=['LotFrontage', 'GarageYrBlt', 'MasVnrArea', 'OverallQual', 'GrLivArea10', 'GarageCars', '1stFlrSF10', 'TotalBsmtSF', 'GarageArea', 'BsmtFullBath', 'BsmtUnfSF', 'BsmtHalfBath', 'BsmtFinSF2', 'BsmtFinSF1']), inplace=True)
 assert df[pd.isnull(df[numSeries] ).any(axis=1)][numSeries].shape[0] == 0
df['LotAreaLog10'] = np.log10(df['LotArea'].values)
 df['1stFlrSFLog10'] = np.log10(df['1stFlrSF'].values)
 df['GrLivAreaLog10'] = np.log10(df['GrLivArea'].values)
 df['SalePriceLog10'] = np.log10(df['SalePrice'].values)
 if 'LotArea' in numSeries:
numSeries.remove('LotArea')
 numSeries.append('LotAreaLog10')
 if '1stFlrSF' in numSeries:
numSeries.remove('1stFlrSF')
 numSeries.append('1stFlrSFLog10')
 if 'GrLivArea' in numSeries:
numSeries.remove('GrLivArea')
 numSeries.append('GrLivAreaLog10')
 df[numSeries].head()<count_values>
from sklearn.neighbors import KNeighborsClassifier
allEnums = []
for seriesName in enumSeries:
allEnums.append([])
 enums = df[seriesName].value_counts()
for enum in enums.index.values:
allEnums[-1].append({ 'name': enum, 'series': seriesName, 'mean': df.query(seriesName + ' == "' + enum + '"')['SalePrice'].mean() , 'count': df.query(seriesName + ' == "' + enum + '"')['SalePrice'].count() })
 if pd.isnull(df[seriesName] ).any() :
val = 0.0
allEnums[-1].append({ 'name': None, 'series': seriesName, 'mean': df.query(seriesName + ' != ' + seriesName)['SalePrice'].mean() , 'count': df.query(seriesName + ' == "' + enum + '"')['SalePrice'].count() })
 else:
val = 1.0
allEnums[-1] = sorted(allEnums[-1], key=lambda k: k['mean'])
 meanSumForNull = 0.0
meanCntForNull = 0
for enumDict in allEnums[-1]:
enumDict['value'] = val
meanSumForNull += val * enumDict['count']
meanCntForNull += enumDict['count']
val += 1.0
if not pd.isnull(df[seriesName] ).any() :
allEnums[-1].append({ 'name': None, 'series': seriesName, 'mean': 0, 'value': meanSumForNull / float(meanCntForNull), 'count': 0 })
 for enumCollection in allEnums:
for enumDict in enumCollection:
if enumDict['name']:
print(enumDict['series'] + '-' + enumDict['name'] + ': ' + str(enumDict['value'])+ '(' + str(enumDict['count'])+ ')')
 else:
print(enumDict['series'] + '-NULL: ' + str(enumDict['value'])+ '(' + str(enumDict['count'])+ ')')
<categorify>
from sklearn.neighbors import KNeighborsClassifier
for enumCollection in allEnums:
srcCol = enumCollection[0]['series']
destCol = enumCollection[0]['series'] + 'Numeric'
mapping = {}
naValue = 0.0
for enumDict in enumCollection:
if enumDict['name']:
mapping[enumDict['name']] = enumDict['value']
else:
naValue = enumDict['value']
df[destCol] = df[srcCol].map(mapping)
 df[destCol].fillna(naValue, inplace=True)
 if not destCol in numSeries:
numSeries.append(destCol)
 df[numSeries].head()<train_on_grid>
knn = KNeighborsClassifier(n_neighbors=25 )
ridge = Ridge()
parameters = { 'alpha': [ 1e-15, 1e-10, 1e-8, 1e-4, 1e-3, 1e-2, 1, 5, 10, 20 ]}
ridge_regressor = GridSearchCV(ridge, parameters, scoring='neg_mean_squared_error', cv=5)
 ridge_regressor.fit(df[numSeries].values, df['SalePriceLog10'])
 print(ridge_regressor.best_params_ )<compute_train_metric>
from sklearn.model_selection import cross_val_score
ridge = Ridge(alpha=1)
 ridge.fit(df[numSeries].values, df['SalePriceLog10'])
 ridgePredictions = np.power(10, ridge.predict(df[numSeries].values))
targets = np.power(10, df['SalePriceLog10'])
 mae = mean_absolute_error(ridgePredictions, targets)
 mae<load_from_csv>
scores = cross_val_score(knn, Xadult, Yadult, cv=10 )
tdf = pd.read_csv('.. /input/test.csv')
 tdf.fillna(pd.Series([0,1970,0, 5, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0], index=['LotFrontage', 'GarageYrBlt', 'MasVnrArea', 'OverallQual', 'GrLivArea10', 'GarageCars', '1stFlrSF10', 'TotalBsmtSF', 'GarageArea', 'BsmtFullBath', 'BsmtUnfSF', 'BsmtHalfBath', 'BsmtFinSF2', 'BsmtFinSF1']), inplace=True)
 tdf['LotAreaLog10'] = np.log10(tdf['LotArea'].values)
 tdf['1stFlrSFLog10'] = np.log10(tdf['1stFlrSF'].values)
 tdf['GrLivAreaLog10'] = np.log10(tdf['GrLivArea'].values)
 for enumCollection in allEnums:
srcCol = enumCollection[0]['series']
destCol = enumCollection[0]['series'] + 'Numeric'
mapping = {}
naValue = 0.0
for enumDict in enumCollection:
if enumDict['name']:
mapping[enumDict['name']] = enumDict['value']
else:
naValue = enumDict['value']
tdf[destCol] = tdf[srcCol].map(mapping)
 tdf[destCol].fillna(naValue, inplace=True)
 if not destCol in numSeries:
numSeries.append(destCol)
 assert tdf[pd.isnull(tdf[numSeries] ).any(axis=1)][numSeries].shape[0] == 0
tdf[numSeries].head()<save_to_csv>
knn.fit(Xadult,Yadult )
ridgeFinalPredictions = np.power(10, ridge.predict(tdf[numSeries].values))
assert ridgeFinalPredictions.shape[0] == tdf['Id'].shape[0]
res = pd.DataFrame([tdf['Id'], ridgeFinalPredictions] ).transpose().rename(columns={'Unnamed 0': 'SalePrice'})
 res.Id = res.Id.astype(int)
 res.to_csv('submission.csv', index=False)
 res<import_modules>
YtestPred = knn.predict(XtestAdult )
from sklearn.tree import DecisionTreeRegressor
from sklearn import preprocessing<load_from_csv>
savepath = "results.csv"
df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv')
 df.head()<categorify>
ypanda = pd.DataFrame(YtestPred, columns = ["income"])
 ypanda.to_csv(savepath, index_label="Id")
 ypanda