You are on page 1of 1

2)

# Import necessary libraries

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

# Create the User dataset

User = {'User ID': np.random.randint(100, 500, size=200),

'Gender': np.random.choice(['Male', 'Female'], size=200),

'Age': np.random.randint(20, 70, size=200),

'Estimated Salary': np.random.randint(30000, 150000, size=200),

'Purchased': np.random.randint(0, 2, size=200)}

df_User = pd.DataFrame(User)

# Identify the dependent and independent variables

X = df_User[['Age', 'Estimated Salary', 'Gender']]

y = df_User[['Purchased']]

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Print the training and testing sets

print("Training set\n", X_train)

print("Training set targets\n", y_train)

print("Testing set\n", X_test)

print("Testing set targets\n", y_test)

# Build a logistic regression model

log_reg = LogisticRegression()

# Fit the model to the training set

log_reg.fit(X_train, y_train)
# Predict whether a person will buy a car or not
predictions = log_reg.predict(X_test)
# Print the predictions
print("Predictions:", predictions)

You might also like