This is the Airbnb Prediction dataset many have worked on. I would like to print the classification report and export them into a CSV. I have tried methods by print(classification_report(y_pred, y)) but it's giving me an error of "ValueError: Mix type of y not allowed, got types {'continuous-multioutput', 'multiclass'}"
I may not be doing it correctly but would appreciate any help
Here is the code:
import numpy as np # linear algebra
# data processing, CSV file I/O (e.g. pd.read_csv)
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from xgboost.sklearn import XGBClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import classification_report,confusion_matrix
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter)
# will list the files in the input directory
from subprocess import check_output
df_train = pd.read_csv("train_users_2.csv")
df_test = pd.read_csv("test_users.csv")
# Get the values of the country destination for each row
labels = df_train['country_destination'].values
# It's the output variable for the decision tree
df_train = df_train.drop(['country_destination'], axis=1)
id_test = df_test['id']
piv_train = df_train.shape[0]
df_all = pd.concat((df_train, df_test), axis = 0, ignore_index = True)
df_all = df_all.drop(['id','date_first_booking'], axis=1)
# -unknown- is not considered as a missing value so we replace it by nan
df_all.gender.replace('-unknown-', np.nan, inplace=True)
print(df_all.isnull().sum())
df_all = df_all.fillna(-1)
dac = np.vstack(df_all.date_account_created.astype(str).apply(
lambda x: list(map(int, x.split('-')))).values)
print(dac)
df_all['dac_year'] = dac[:,0]
df_all['dac_mounth'] = dac[:,1]
df_all['dac_day'] = dac[:,2]
df_all = df_all.drop(['date_account_created'], axis = 1)
tfa = np.vstack(df_all.timestamp_first_active.astype(str).apply(
lambda x: list(map(int, [x[:4],x[4:6],x[6:8],x[8:10],x[10:12],x[12:14]]))).values)
print(tfa)
df_all['tfa_year'] = tfa[:,0]
df_all['tfa_month'] = tfa[:,1]
df_all['tfa_day'] = tfa[:,2]
df_all = df_all.drop(['timestamp_first_active'], axis=1)
# We can see that the age has some inconsistancy variables
print(df_all.age.describe())
av = df_all.age.values
df_all['age'] = np.where(np.logical_or(av<14, av>100), -1, av)
features = ['gender', 'signup_method', 'signup_flow',
'language', 'affiliate_channel', 'affiliate_provider',
'first_affiliate_tracked', 'signup_app',
'first_device_type', 'first_browser']
for f in features:
df_all_dummy = pd.get_dummies(df_all[f], prefix=f)
df_all = df_all.drop([f], axis=1)
df_all = pd.concat((df_all, df_all_dummy), axis=1)
vals = df_all.values
X = vals[:piv_train]
le = LabelEncoder()
y = le.fit_transform(labels)
X_test = vals[piv_train:]
model = RandomForestClassifier()
model.fit(X,y)
y_pred = model.predict_proba(X_test)
ids = [] #list of ids
cts = [] #list of countries
for i in range(len(id_test)):
idx = id_test[i]
ids += [idx] * 5
cts += le.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist()
The code you posted is executing without any errors (though I am doubtful of what the last for loop is doing). Now you want to do this:
print(classification_report(y_pred, y))
but are facing errors. There are a couple of reasons here:
First is that the order of arguments in wrong. In classification_report()
, actual labels (true ground truths) are first and predicted labels are second. So your command should be:
print(classification_report(y, y_pred))
But, second reason is that your y_pred
is output of model.predict_proba()
. It contains probabilities of each class for each sample. You cannot use that in confusion_matrix
. For classification_report
you need discrete labels predicted by the model. So do this instead:
y_pred = model.predict(X_test)
print(classification_report(y, y_pred))
But still, you will get another error. Because for comparing the values in classification_report()
, you need the actual labels of the same data that you send to model.predict()
. But here you have:
y
= actual labels of training data
y_pred
=model.predict(X_test)
andX_test
= test data
So you cannot compare the labels of training data with test data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.