Certainly! Below is a Python script that generates a synthetic dataset and provides an option to save it as a CSV file. The dataset will include columns for ID, Age, Income, Gender, and Purchase. You can customize the dataset as needed.
python
import pandas as pd
import numpy as np
def generate_synthetic_dataset(num_samples=1000):
# Generate synthetic data
np.random.seed(42) # For reproducibility
data = {
'ID': range(1, num_samples + 1),
'Age': np.random.randint(18, 65, num_samples),
'Income': np.random.normal(50000, 15000, num_samples),
'Gender': np.random.choice(['Male', 'Female'], num_samples),
'Purchase': np.random.choice([0, 1], num_samples, p=[0.7, 0.3])
}
# Create DataFrame
df = pd.DataFrame(data)
return df
def save_to_csv(df, filename='synthetic_dataset.csv'):
df.to_csv(filename, index=False)
print(f"Dataset saved to {filename}")
if __name__ == "__main__":
# Generate dataset
dataset = generate_synthetic_dataset(num_samples=1000)
# Display the first few rows
print("Generated Dataset:")
print(dataset.head())
# Option to save as CSV
save_option = input("Would you like to save the dataset as a CSV file? (yes/no): ").strip().lower()
if save_option == 'yes':
filename = input("Enter the filename (e.g., synthetic_data.csv): ").strip()
save_to_csv(dataset, filename)
else:
print("Dataset not saved.")
How It Works:
Data Generation:
ID: Unique identifiers for each sample.
Age: Random integers between 18 and 65.
Income: Normally distributed values with a mean of 50,000 and a standard deviation of 15,000.
Gender: Randomly chosen between 'Male' and 'Female'.
Purchase: Binary values (0 or 1) with a 70% chance of being 0 and 30% chance of being 1.
Saving to CSV:
The script prompts you to decide whether to save the dataset as a CSV file.
If you choose to save, it asks for a filename and saves the dataset accordingly.
Running the Script:
Copy the script into a Python file, e.g., generate_dataset.py.
Run the script using Python: python generate_dataset.py.
Follow the prompts to save the dataset as a CSV file if desired.
Let me know if you'd like to customize the dataset further!