Kaggle competition: UC Irvine Math 10 Winter 2020
<SOS> metric: categorizationaccuracy Kaggle data source: uc-irvine-math-10-winter-2020<count_unique_values>
import numpy as np
import matplotlib.pyplot as plt
missing_countries = predictions[(predictions.ConfirmedCases.isnull())&(pd.isnull(predictions.Province_State)) ]['Country_Region'].unique().tolist()
missing_states = predictions[(predictions.ConfirmedCases.isnull())&(pd.notnull(predictions.Province_State)) ]['Province_State'].unique().tolist()<split>
X_train = np.load('.. /input/uci-math-10-winter2020/kmnist-train-imgs.npz')['X']
y_train = np.load('.. /input/uci-math-10-winter2020/kmnist-train-labels.npz')['y']
X_test = np.load('.. /input/uci-math-10-winter2020/kmnist-test-imgs.npz')['X']
def fill_excl_pred(predictions, training_data_original, test_data):
for country in missing_countries:
print(country)
 pred_loc_df = predictions[predictions['Country_Region'] == country]
train_loc_df = training_data_original[training_data_original['Country_Region'] == country]
series_comf_cases = train_loc_df['ConfirmedCases'].values
series_fatal = train_loc_df['Fatalities'].values
test_start = test_data[test_data['Country_Region'] == country]['Date'].values[0]
series_comf_cases_test = training_data_original[(training_data_original['Country_Region'] == country)&(training_data_original['Date'] >= test_start)]['ConfirmedCases']
series_fatal_test = training_data_original[(training_data_original['Country_Region'] == country)&(training_data_original['Date'] >= test_start)]['Fatalities']
if len(series_comf_cases)> 0:
regressor = LinearRegression()
regressor.fit(np.arange(len(series_comf_cases_test)).reshape(-1, 1), series_comf_cases_test)
 comf_cases_pred = regressor.predict(np.arange(13, 43 ).reshape(-1, 1))
regressor.fit(np.arange(len(series_fatal_test)).reshape(-1, 1), series_fatal_test)
 fatal_pred = regressor.predict(np.arange(13, 43 ).reshape(-1, 1))
else:
comf_cases_pred = []
fatal_pred = []
conf_cases_loc = np.concatenate(( series_comf_cases_test, comf_cases_pred), axis=0)
 fatal_loc = np.concatenate(( series_fatal_test, fatal_pred), axis=0)
 predictions.loc[predictions['Country_Region'] == country, 'ConfirmedCases'] = conf_cases_loc.astype(int)
 predictions.loc[predictions['Country_Region'] == country, 'Fatalities'] = fatal_loc.astype(int)
 for state in missing_states:
print(state)
 pred_loc_df = predictions[predictions['Province_State'] == state]
train_loc_df = training_data_original[training_data_original['Province_State'] == state]
series_comf_cases = train_loc_df['ConfirmedCases'].values
series_fatal = train_loc_df['Fatalities'].values
test_start = test_data[test_data['Province_State'] == state]['Date'].values[0]
series_comf_cases_test = training_data_original[(training_data_original['Province_State'] == state)&(training_data_original['Date'] >= test_start)]['ConfirmedCases']
series_fatal_test = training_data[(training_data_original['Province_State'] == state)&(training_data_original['Date'] >= test_start)]['Fatalities']
regressor = LinearRegression()
regressor.fit(np.arange(len(series_comf_cases_test)).reshape(-1, 1), series_comf_cases_test)
 comf_cases_pred = regressor.predict(np.arange(13, 43 ).reshape(-1, 1))
regressor.fit(np.arange(len(series_fatal_test)).reshape(-1, 1), series_fatal_test)
 fatal_pred = regressor.predict(np.arange(13, 43 ).reshape(-1, 1))
conf_cases_loc = np.concatenate(( series_comf_cases_test, comf_cases_pred), axis=0)
 fatal_loc = np.concatenate(( series_fatal_test, fatal_pred), axis=0)
 predictions.loc[predictions['Province_State'] == state, 'ConfirmedCases'] = conf_cases_loc.astype(int)
 predictions.loc[predictions['Province_State'] == state, 'Fatalities'] = fatal_loc.astype(int)
 return predictions<predict_on_test>
from sklearn.neighbors import KNeighborsClassifier
prediction_final = fill_excl_pred(predictions, training_data_original, test_data )<filter>
from sklearn.neighbors import KNeighborsClassifier
prediction_final[prediction_final['Country_Region'] == 'Italy']<data_type_conversions>
from sklearn.neighbors import KNeighborsClassifier
predictions_csv = pd.DataFrame()
predictions_csv['ForecastId'] = prediction_final['ForecastId'].astype(int)
 predictions_csv['ConfirmedCases'] = prediction_final['ConfirmedCases'].astype(float)
 predictions_csv['Fatalities'] = prediction_final['Fatalities'].astype(float )<save_to_csv>
clf = KNeighborsClassifier(n_neighbors=5, weights='distance')
 clf.fit(X_train, y_train )
predictions_csv.to_csv('submission.csv', index = False )<set_options>
y_pred = clf.predict(X_test )
<load_from_csv><EOS> 
solutions = np.zeros(( X_test.shape[0], 2))
solutions[:,0] = np.arange(1,X_test.shape[0]+1)
 solutions[:,1] = y_pred
solutions = solutions.astype(int)
 np.savetxt("solutions-yournames.csv", solutions,
fmt='%s', header = 'Id,Category', delimiter = ',', comments='' )