<jupyter_start><jupyter_text>Logistic regression<jupyter_code>df = pd.read_csv('data/train_mobile.xls', sep=';')
df
fig = plt.figure(figsize=(15, 10))
sns.heatmap(df.corr(), annot=True, fmt='.2f')
df.columns
# the max correlation have the following features:
features = ['battery_power', 'px_height', 'px_width', 'ram', 'touch_screen']
df[features].values
X = df[features].values
y = df['price_range'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=31)
model_lr = LogisticRegression()
model_lr.fit(X_train, y_train)
y_pred = model_lr.predict(X_test)
print(f'The precision score is equal to {round(precision_score(y_test, y_pred), 4)}.')
round(log_loss([0, 0, 1, 1], [0.2, 0.8, 1, 0.6], normalize=False), 2)
log_loss([[0, 0, 1], [0, 0, 1], [1, 0, 0]], [[0.2, 0.3, 0.5], [0, 0, 1], [0.1, 0, 0.9]])
def mult_logloss(y_true, y_pred):
    n, m = y_true.shape[0], y_true.shape[1]
    sum_ = []
    for i in range(n):
        for j in range(m):
            if y_true[i][j] != 0 and y_pred[i][j] != 0:
                sum_.append(y_true[i][j] * np.log(y_pred[i][j]))
    sum_ = np.nansum(sum_)
    loss = -(sum_ / n)
    return loss

y_pred = np.array([[0.2, 0.3, 0.5], [0, 0, 1], [0.1, 0, 0.9]])
y_true = np.array([[0, 0, 1], [0, 0, 1], [1, 0, 0]])
np.round(mult_logloss(y_true, y_pred),2)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup