Kaggle competition: San Francisco Crime Classification
result_df = pd.DataFrame(columns=['Accuracy','Variance'], index=['Naive Bayes', 'Logistic Regression', 'Random Forest', 'SVC', 'KNN', 'XGBoost'] )<feature_engineering>
print(f'Training path:{__training_path}
Test path:{__test_path}' )
result_df.iloc[0] = pd.Series({'Accuracy':scores_GNB.mean() ,
'Variance':np.std(scores_GNB)})
 result_df.iloc[1] = pd.Series({'Accuracy':scores_LR.mean() ,
'Variance':np.std(scores_LR)})
 result_df.iloc[2] = pd.Series({'Accuracy':scores_RF.mean() ,
'Variance':np.std(scores_RF)})
 result_df.iloc[3] = pd.Series({'Accuracy':scores_SVC.mean() ,
'Variance':np.std(scores_SVC)})
 result_df.iloc[4] = pd.Series({'Accuracy':scores_KNN.mean() ,
'Variance':np.std(scores_KNN)})
 result_df.iloc[5] = pd.Series({'Accuracy':scores_XGB.mean() ,
'Variance':np.std(scores_XGB)})
<sort_values>
!{sys.executable} -m pip install --upgrade scikit-learn=="0.24.2"
result_df = result_df.sort_values('Accuracy', ascending = False, axis=0)
 result_df.head(6 )<split>
import sklearn; sklearn.show_versions()
X_train, X_test, y_train, y_test = train_test_split(X, y.ravel() , test_size=0.2, random_state=4)
 print('Train set:', X_train.shape, y_train.shape)
 print('Test set:', X_test.shape, y_test.shape )<train_model>
def __load__data(__training_path, __test_path, concat=False):
	
	
	
	__train_dataset = pd.read_csv(__training_path, delimiter=',')
 	__test_dataset = pd.read_csv(__test_path, delimiter=',')
 	return __train_dataset, __test_dataset
__train_dataset, __test_dataset = __load__data(__training_path, __test_path, concat=True)
 __train_dataset.head()
cls = XGBClassifier()
cls.fit(X_train, y_train )<load_from_csv>
__test_dataset_submission_columns = __test_dataset['Id']
file3 = '.. /input/test.csv'
dft = pd.read_csv(file3 )<drop_column>
__train_dataset.drop(['Descript', 'Resolution'], axis=1, inplace=True )
dft = dft.drop(['PassengerId', 'Name', 'Ticket'], axis=1 )<categorify>
_DATE_COLUMNS = ['Dates']
for _col in _DATE_COLUMNS:
train_date_col = pd.to_datetime(__train_dataset[_col], errors='coerce')
 __train_dataset["year"] = train_date_col.dt.year
__train_dataset["month"] = train_date_col.dt.month
__train_dataset["day"] = train_date_col.dt.day
__train_dataset.drop(_col, axis=1, inplace=True)
 test_date_col = pd.to_datetime(__test_dataset[_col], errors='coerce')
 __test_dataset["year"] = test_date_col.dt.year
__test_dataset["month"] = test_date_col.dt.month
__test_dataset["day"] = test_date_col.dt.day
__test_dataset.drop(_col, axis=1, inplace=True )
dft_hot = pd.concat([dft, pd.get_dummies(dft['Sex'])], axis=1)
 dft_hot = dft_hot.drop(['Sex'], axis=1 )<categorify>
__feature_train = __train_dataset.drop(['Category'], axis=1)
 __target_train =__train_dataset['Category']
__feature_test = __test_dataset.drop(['Id'], axis=1)
 _CATEGORICAL_COLS = ['Address', 'PdDistrict', 'DayOfWeek']
__col_indices = [__feature_train.columns.get_loc(col)for col in _CATEGORICAL_COLS]
_ohe = OneHotEncoder(handle_unknown='ignore')
 __ct = ColumnTransformer([("ohe", _ohe, __col_indices)], remainder = 'passthrough')
 __feature_train = __ct.fit_transform(__feature_train)
 __feature_test = __ct.transform(__feature_test )
dft_hot = pd.concat([dft_hot, pd.get_dummies(dft['Pclass'])], axis=1)
 dft_hot = dft_hot.drop(['Pclass'], axis=1 )<rename_columns>
__model = CatBoostClassifier()
__model.fit(__feature_train, __target_train)
 __y_pred = __model.predict_proba(__feature_test )
dft_hot = dft_hot.rename(columns={dft_hot.columns[8]:'Pclass1', dft_hot.columns[9]:'Pclass2', dft_hot.columns[10]:'Pclass3'} )<categorify>
submission = pd.DataFrame(columns=['Id'], data=__test_dataset_submission_columns)
 submission = pd.concat([submission, pd.DataFrame(__y_pred, columns=["ARSON", "ASSAULT", "BAD CHECKS", "BRIBERY", "BURGLARY", "DISORDERLY CONDUCT",
"DRIVING UNDER THE INFLUENCE", "DRUG/NARCOTIC", "DRUNKENNESS", "EMBEZZLEMENT", "EXTORTION",
"FAMILY OFFENSES", "FORGERY/COUNTERFEITING", "FRAUD", "GAMBLING", "KIDNAPPING", "LARCENY/THEFT",
"LIQUOR LAWS", "LOITERING", "MISSING PERSON", "NON-CRIMINAL", "OTHER OFFENSES", "PORNOGRAPHY/OBSCENE MAT",
"PROSTITUTION", "RECOVERED VEHICLE", "ROBBERY", "RUNAWAY", "SECONDARY CODES", "SEX OFFENSES FORCIBLE",
"SEX OFFENSES NON FORCIBLE", "STOLEN PROPERTY", "SUICIDE", "SUSPICIOUS OCC", "TREA", "TRESPASS",
"VANDALISM", "VEHICLE THEFT", "WARRANTS", "WEAPON LAWS"])], axis=1)
 submission.head()
le = LabelEncoder()
T = dft_hot['Embarked']
encoded = le.fit_transform(np.ravel(T))<concatenate>
submission.to_csv("kaggle_submission.csv", index=False )