import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from math import sqrt
import numpy as np
from scipy import stats
import copy
data_train = pd.read_csv('https://github.com/farhanakbar8/classification-car-dealer/blob/main/kendaraan_train.csv?raw=true')
data_train
idint64
Jenis_Kelaminobject
0
1
Wanita
1
2
Pria
2
3
nan
3
4
Wanita
4
5
Pria
5
6
Pria
6
7
Wanita
7
8
Pria
8
9
Wanita
9
10
nan
data_train = data_train.drop_duplicates()
data_train
idint64
Jenis_Kelaminobject
0
1
Wanita
1
2
Pria
2
3
nan
3
4
Wanita
4
5
Pria
5
6
Pria
6
7
Wanita
7
8
Pria
8
9
Wanita
9
10
nan
data_train = data_train.dropna()
data_train
idint64
Jenis_Kelaminobject
0
1
Wanita
1
2
Pria
3
4
Wanita
5
6
Pria
8
9
Wanita
10
11
Pria
14
15
Pria
15
16
Pria
16
17
Wanita
17
18
Wanita
data_train['Jenis_Kelamin'] = data_train['Jenis_Kelamin'].map({'Pria':0, 'Wanita':1})
data_train['Umur_Kendaraan'] = data_train['Umur_Kendaraan'].map({'< 1 Tahun':0, '1-2 Tahun':1, '> 2 Tahun':2})
data_train['Kendaraan_Rusak'] = data_train['Kendaraan_Rusak'].map({'Pernah':1, 'Tidak':0})
data_train
<ipython-input-5-68540eca33b8>:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
data_train['Jenis_Kelamin'] = data_train['Jenis_Kelamin'].map({'Pria':0, 'Wanita':1})
<ipython-input-5-68540eca33b8>:2: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
data_train['Umur_Kendaraan'] = data_train['Umur_Kendaraan'].map({'< 1 Tahun':0, '1-2 Tahun':1, '> 2 Tahun':2})
<ipython-input-5-68540eca33b8>:3: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
data_train['Kendaraan_Rusak'] = data_train['Kendaraan_Rusak'].map({'Pernah':1, 'Tidak':0})
idint64
Jenis_Kelaminint64
0
1
1
1
2
0
3
4
1
5
6
0
8
9
1
10
11
0
14
15
0
15
16
0
16
17
1
17
18
1
data_train.drop(columns=['id'], inplace=True)
data_train
/shared-libs/python3.9/py/lib/python3.9/site-packages/pandas/core/frame.py:4308: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
return super().drop(
Jenis_Kelaminint64
Umurfloat64
0
1
30
1
0
48
3
1
58
5
0
21
8
1
20
10
0
25
14
0
66
15
0
31
16
1
24
17
1
22
data_train.boxplot()
z_score = np.abs(stats.zscore(data_train))
threshold = 3
data_train = data_train[(z_score < threshold).all(axis = 1)]
data_train
Jenis_Kelaminint64
Umurfloat64
0
1
30
1
0
48
3
1
58
5
0
21
8
1
20
10
0
25
14
0
66
15
0
31
16
1
24
17
1
22
data_test = pd.read_csv('https://github.com/farhanakbar8/classification-car-dealer/blob/main/kendaraan_test.csv?raw=true')
data_test
Jenis_Kelaminobject
Pria54.3%
Wanita45.7%
Umurint64
20 - 85
0
Wanita
49
1
Pria
22
2
Pria
24
3
Pria
46
4
Pria
35
5
Pria
26
6
Wanita
24
7
Wanita
40
8
Pria
23
9
Wanita
43
data_test['Jenis_Kelamin'] = data_test['Jenis_Kelamin'].map({'Pria':0, 'Wanita':1})
data_test['Umur_Kendaraan'] = data_test['Umur_Kendaraan'].map({'< 1 Tahun':0, '1-2 Tahun':1, '> 2 Tahun':2})
data_test['Kendaraan_Rusak'] = data_test['Kendaraan_Rusak'].map({'Pernah':1, 'Tidak':0})
data_test
Jenis_Kelaminint64
0 - 1
Umurint64
20 - 85
0
1
49
1
0
22
2
0
24
3
0
46
4
0
35
5
0
26
6
1
24
7
1
40
8
0
23
9
1
43
data_train_X = data_train[['Jenis_Kelamin', 'Umur', 'Umur_Kendaraan', 'Premi']][:10000]
data_train_X
Jenis_Kelaminint64
0 - 1
Umurfloat64
20.0 - 82.0
0
1
30
1
0
48
3
1
58
5
0
21
8
1
20
10
0
25
14
0
66
15
0
31
16
1
24
17
1
22
data_train_Y = data_train[['Tertarik']][:10000]
data_train_Y
Tertarikint64
0 - 1
0
0
1
0
3
0
5
0
8
0
10
0
14
0
15
0
16
0
17
0
data_test_X = data_test[['Jenis_Kelamin', 'Umur', 'Umur_Kendaraan', 'Premi']][:10000]
data_test_X
Jenis_Kelaminint64
0 - 1
Umurint64
20 - 83
0
1
49
1
0
22
2
0
24
3
0
46
4
0
35
5
0
26
6
1
24
7
1
40
8
0
23
9
1
43
data_test_Y = data_test[['Tertarik']][:10000]
data_test_Y
Tertarikint64
0 - 1
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
1
corr_mat = data_train_X.corr()
fig, ax = plt.subplots(figsize=(15, 15))
sns.heatmap(corr_mat, annot=True, ax=ax,
fmt='.1g', cmap='coolwarm', linewidths=.5,
vmin=-0.6, vmax=1, center= 0)
plt.show()
data_train_X_np = data_train_X.to_numpy()
data_train_Y_np = data_train_Y.to_numpy()
data_test_X_np = data_test_X.to_numpy()
data_test_Y_np = data_test_Y.to_numpy()
data_train_X_flatten = data_train_X_np.reshape(data_train_X_np.shape[0], -1).T
data_test_X_flatten = data_test_X_np.reshape(data_test_X_np.shape[0], -1).T
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def initialize_with_zeros(d):
w = np.zeros((d, 1))
b = float(0)
return w, b
def propagate(w, b, X, Y):
m = X.shape[1]
A = sigmoid(np.dot(w.T, X) + b)
cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A)))
dw = (1 / m) * np.dot(X, (A - Y).T)
db = (1 / m) * np.sum(A - Y)
cost = np.squeeze(np.array(cost))
grads = {"dw": dw,
"db": db}
return grads, cost
def optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False):
w = copy.deepcopy(w)
b = copy.deepcopy(b)
costs = []
for i in range(num_iterations):
grads, cost = propagate(w, b, X, Y)
# Retrieve derivatives from grads
dw = grads["dw"]
db = grads["db"]
# update rule
w = w - learning_rate * dw
b = b - learning_rate * db
# Record the costs
if i % 100 == 0:
costs.append(cost)
# Print the cost every 100 training iterations
if print_cost:
print ("Cost after iteration %i: %f" %(i, cost))
params = {"w": w,
"b": b}
grads = {"dw": dw,
"db": db}
return params, grads, costs
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1, m))
# w = w.reshape(X.shape[0], 1)
# Compute vector "A" predicting the probabilities of
A = sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
return Y_prediction
# GRADED FUNCTION: model
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
"""
Builds the logistic regression model by calling the function you've implemented previously
Arguments:
X_train -- training set represented by a numpy array of shape (num_px * num_px * 3, m_train)
Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train)
X_test -- test set represented by a numpy array of shape (num_px * num_px * 3, m_test)
Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test)
num_iterations -- hyperparameter representing the number of iterations to optimize the parameters
learning_rate -- hyperparameter representing the learning rate used in the update rule of optimize()
print_cost -- Set to True to print the cost every 100 iterations
Returns:
d -- dictionary containing information about the model.
"""
# (≈ 1 line of code)
# initialize parameters with zeros
# w, b = ...
w,b = initialize_with_zeros(X_train.shape[0])
#(≈ 1 line of code)
# Gradient descent
# params, grads, costs = ...
params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
# Retrieve parameters w and b from dictionary "params"
# w = ...
# b = ...
w = params["w"]
b = params["b"]
# Predict test/train set examples (≈ 2 lines of code)
# Y_prediction_test = ...
# Y_prediction_train = ...
# YOUR CODE STARTS HERE
Y_prediction_test = predict(w, b, X_test)
Y_prediction_train = predict(w, b, X_train)
# YOUR CODE ENDS HERE
# Print train/test Errors
if print_cost:
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}
return d
logistic_regression_model = model(data_train_X_flatten, data_train_Y_np, data_test_X_flatten, data_test_Y_np, num_iterations=2000, learning_rate=0.005, print_cost=True)
Cost after iteration 0: 6931.471806
<ipython-input-18-dc64dee5606b>:2: RuntimeWarning: overflow encountered in exp
return 1 / (1 + np.exp(-z))
<ipython-input-20-5a44148aa2c7>:4: RuntimeWarning: divide by zero encountered in log
cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A)))
<ipython-input-20-5a44148aa2c7>:4: RuntimeWarning: invalid value encountered in multiply
cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A)))
logistic_regression_model