0% found this document useful (0 votes)
1 views6 pages

Dataset Example

The document provides examples of how to load CSV files using pandas in Python, including handling errors when the file is not found. It also demonstrates loading a dataset from a URL, training a logistic regression model on the Pima Indians Diabetes dataset, and making predictions. Additionally, it includes sample outputs and notes on ensuring the CSV file is accessible.

Uploaded by

Ganesan Alagu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Dataset Example

The document provides examples of how to load CSV files using pandas in Python, including handling errors when the file is not found. It also demonstrates loading a dataset from a URL, training a logistic regression model on the Pima Indians Diabetes dataset, and making predictions. Additionally, it includes sample outputs and notes on ensuring the CSV file is accessible.

Uploaded by

Ganesan Alagu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import pandas as pd

# Define the file path.


# Make sure the file is in the same directory as your Python script,
# or provide the full path using forward slashes (e.g., 'C:/Users/[Link]').
csv_file_path = "your_dataset.csv"

# Connect to/read the CSV file to create a DataFrame


try:
df = pd.read_csv(csv_file_path)
print("Dataset connected successfully!")
print(f"Data shape: {[Link]}")
print("First 5 rows of the data:")
print([Link]())
except FileNotFoundError:
print(f"Error: The file '{csv_file_path}' was not found.")
except Exception as e:
print(f"An error occurred: {e}")

Example: Connecting to an online dataset via URL

import pandas as pd

url = '[Link]

dataset = pd.read_csv(url)

print([Link]())

Suppose you have a CSV file named [Link] with the following content:

Name,Age,Grade

Alice,23,A
Bob,24,B

Charlie,22,C

import pandas as pd

# Load the dataset from the CSV file

dataset = pd.read_csv('[Link]')

# Show the first few rows of the dataset

print(dataset)

Sure! Here's a complete example of connecting to a CSV dataset file in Python


using pandas:

### Example: Loading a CSV file

Suppose you have a CSV file named `[Link]` with the following content:

```csv

Name,Age,Grade

Alice,23,A

Bob,24,B

Charlie,22,C
```

### Python code to connect and load this dataset:

```python

import pandas as pd

# Load the dataset from the CSV file

dataset = pd.read_csv('[Link]')

# Show the first few rows of the dataset

print(dataset)

```

### Output:

```

Name Age Grade

0 Alice 23 A

1 Bob 24 B

2 Charlie 22 C

```
Note:
- Make sure the `[Link]` file is in the same directory as your Python script, or provide the
full path to the file.
- You need to have pandas installed (`pip install pandas`).

Example 2

example code to load the Pima Indians Diabetes dataset, train a logistic regression
model to predict diabetes, and then make a prediction on a new sample. I'll include
expected output as well.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score

# Load dataset from URL


url = "[Link]
column_names = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI',
'DiabetesPedigreeFunction', 'Age', 'Outcome']
dataset = pd.read_csv(url, names=column_names)

# Display the first few rows


print("Dataset preview:")
print([Link]())

# Split data into features and target


X = [Link]('Outcome', axis=1)
y = dataset['Outcome']
# Split into training and testing datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the logistic regression model


model = LogisticRegression(max_iter=1000)
[Link](X_train, y_train)

# Predict on the test set


y_pred = [Link](X_test)

# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"\nModel Accuracy on test data: {accuracy:.2f}")

# Make a prediction for a new patient


# Example data: [Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI,
DiabetesPedigreeFunction, Age]
new_patient = [[6, 148, 72, 35, 0, 33.6, 0.627, 50]]
prediction = [Link](new_patient)

print("\nPrediction for new patient:")


print("Diabetes" if prediction[0] == 1 else "No Diabetes")
Dataset preview:
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \
0 6 148 72 35 0 33.6
1 1 85 66 29 0 26.6
2 8 183 64 0 0 23.3
3 1 89 66 23 94 28.1
4 0 137 40 35 168 43.1

DiabetesPedigreeFunction Age Outcome


0 0.627 50 1
1 0.351 31 0
2 0.672 32 1
3 0.167 21 0
4 2.288 33 1

Model Accuracy on test data: 0.77

Prediction for new patient:


Diabetes

You might also like