Logistic data regr.
Q.2 Create 'User' Data
set having 5 columns
namely: User ID,
Gender,
Age, Estimated Salary
and Purchased. Build a
logistic regression
model
that can predict whether
on the given parameter
a person will buy a car
or not.
import numpy as np
import pandas as pd
from
sklearn.model_selection
import train_test_split
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.preprocessing
import StandardScaler
data = {'User ID': [1, 2,
3, 4, 5],
'Gender': ['Male',
'Female', 'Male',
'Female', 'Male'],
'Age': [20, 30, 25,
35, 40],
'Estimated Salary':
[50000, 60000, 70000,
80000, 90000],
'Purchased': [0, 1,
0, 1, 0]}
df =
pd.DataFrame(data)
X = df[['Gender', 'Age',
'Estimated Salary']]
y = df['Purchased']
X = pd.get_dummies(X,
drop_first=True)
scaler =
StandardScaler()
X_scaled =
scaler.fit_transform(X)
X_train, X_test, y_train,
y_test =
train_test_split(X_scale
d, y, test_size=0.2,
random_state=42)
model =
LogisticRegression()
model.fit(X_train,
y_train)
y_pred =
model.predict(X_test)
accuracy =
model.score(X_test,
y_test)
print("Accuracy:",
accuracy)