0% found this document useful (0 votes)
10 views16 pages

Introduction to Data Science Concepts

Uploaded by

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

Introduction to Data Science Concepts

Uploaded by

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

Data Science

Chapter 1
1. What is Data Science?
Definition:
Data Science is an interdisciplinary field that uses data to extract insights, build models, and
support decision-making.
It combines three key elements:
o Statistics & Mathematics → to understand patterns in data.
o Computer Science (Programming) → to process and analyze data using algorithms.
o Domain Knowledge → to apply findings to real-world problems.
o Example:
o Healthcare: Using data to predict diseases.
o Netflix: Recommending movies based on your watch history.
o Banks: Detecting fraudulent transactions.

2. Why Data Science?


In the modern world, huge amounts of data are generated every second (social media, online
shopping, healthcare, finance, etc.).
Raw data is useless unless converted into meaningful insights.
Data Science helps in decision-making, predictions, and automation.
Applications of Data Science:
Netflix: Movie/TV show recommendations.
Facebook & Instagram: Friend suggestions and content ranking.
Banks: Fraud detection and credit scoring.
Healthcare: Disease risk prediction and drug discovery.

3. The Data Science Workflow


The typical process followed by a data scientist is:
o Define the Problem → What are we trying to solve?
o Collect Data → Gather information from databases, APIs, sensors, etc.
o Clean & Prepare Data → Handle missing values, remove duplicates, fix errors.
o Explore Data (EDA – Exploratory Data Analysis) → Use statistics and visualizations
to understand data.
o Build Models (Machine Learning/AI) → Train algorithms to make predictions.
o Evaluate & Interpret Results → Check accuracy, performance, and meaning.
o Communicate Insights → Present findings through reports, dashboards, or
visualizations.
Think of it as a cycle – data science is an iterative process.

4. Case Study: DataSciencester


To make these ideas practical, Joel Grus (in Data Science from Scratch) introduced a fictional
social network called DataSciencester.
(a) Representing Users
Each user is stored in Python as a dictionary with an id and name.
users = [
{"id": 0, "name": "Hero"},
{"id": 1, "name": "Dunn"},
{"id": 2, "name": "Sue"},
]
Here, id is unique, making it easy to track users.
(b) Representing Friendships
Friendships are stored as pairs of user IDs.
friendships = [
(0, 1),
(0, 2),
(1, 2)
]
(0, 1) means user 0 (Hero) is friends with user 1 (Dunn).
(c) Building a Friend Network
We can attach a list of friends to each user:
for user in users:
user["friends"] = [] # start with empty list
for i, j in friendships:
users[i]["friends"].append(users[j])
users[j]["friends"].append(users[i])
Now:
Hero’s friends → Dunn, Sue
Dunn’s friends → Hero, Sue
(d) Analyzing Connections
Number of Friends:
def number_of_friends(user):
return len(user["friends"])
Average Connections:
total_connections = sum(number_of_friends(user) for user in users)
avg_connections = total connections / len(users)
This tells us how connected people are on average.
(e) Finding Popular Users
We can sort users by their number of friends:
num_friends_by_id = [(user["id"], number_of_friends(user)) for user in users]
print(sorted(num_friends_by_id, key=lambda x: x[1], reverse=True))
This identifies influencers in the network.
(f) Friend-of-a-Friend (Foaf)
We can recommend new friends based on mutual friends:
def friends_of_friend_ids(user):
return [foaf["id"]
for friend in user["friends"]
for foaf in friend["friends"]]
Example: If Hero is friends with Dunn, and Dunn is friends with Sue, then Sue is a friend-of-a-
friend of Hero.

5. Why This Case Study is Important


Teaches data representation → users and relationships stored in Python.
Demonstrates basic analysis → counting, averaging, ranking.
Shows real-world relevance → friend suggestions, influencer ranking, community detection.
This is a mini version of Facebook or LinkedIn.

Roles in Data Science


Data science is teamwork where different professionals handle different parts of the process.
Data Scientist
Analyzes data to find patterns, insights, and predictions.
Builds machine learning models.
Communicates results to decision-makers.
Example: Predicting which customers are likely to leave a telecom company.
Data Engineer
Designs and manages data pipelines, databases, and storage systems.
Ensures data is clean, accessible, and reliable for analysis.
Example: Building the system that collects streaming data from YouTube users.
Machine Learning Engineer
Focuses on deploying ML models into production.
Optimizes algorithms for speed and accuracy.
Example: Implementing recommendation models that run live on Netflix.
Business Analyst
Acts as a bridge between technical teams and business teams.
Converts insights into strategies and decisions.
Example: Using sales data to advise a retail store on stocking inventory.

Skills Required for Data Science


Programming Skills
Python, R, SQL for data analysis and automation.
Libraries: NumPy, Pandas, Matplotlib, Seaborn, Scikit-Learn.
Mathematics & Statistics
Probability, hypothesis testing, linear algebra.
Understanding distributions, correlation, regression.
Data Visualization
Tools: Tableau, Power BI, Matplotlib, Seaborn.
Skill: Present results in charts and graphs for better storytelling.
Soft Skills
Problem-Solving: Breaking down complex problems into steps.
Communication: Explaining results to non-technical people.
Storytelling with Data: Turning numbers into actionable insights.
Example: Presenting a fraud detection system to bank managers in simple terms.

Scope of Data Science


Data Science is one of the fastest-growing fields with applications across industries.
o Healthcare: Predicting diseases, drug discovery.
o Finance: Fraud detection, credit scoring.
o Retail & E-commerce: Customer segmentation, product recommendation.
o Social Media: News feed ranking, friend recommendations.
o Manufacturing: Predictive maintenance of machines.

Class Activity
Task: Pick one Pakistani company (for example, Careem, Daraz, Jazz, HBL).
Discuss in groups:
How does it already use data science?
If not, how could it use data science to improve services?
Example: Careem uses data science for estimating ride fares, matching drivers with passengers,
and predicting demand in different areas.

Python Basics – Syntax, Data Types, and Loops


Python Setup
Install Python from [Link] or install Anaconda which includes Python, Jupyter Notebook,
and libraries.
o IDEs (Integrated Development Environments):
o Jupyter Notebook: Best for data science.
o VS Code: Lightweight and widely used.
o PyCharm: Professional IDE.

Basic Python Syntax Rules:


Must start with a letter or underscore.
Print Statement Cannot start with a number.
print("Hello, Data Science") Case-sensitive (Name ≠ name).
Output:
Hello, Data Science
Variables
Variables are used to store values.
name = "Aleeza" # string
age = 21 # integer
gpa = 3.7 # float
is_student = True # boolean

Data Types in Python

Text Type Boolean Type


str → string (text in quotes). bool → True or False
message = "Hello World" is_active = True

Collections
List → Ordered, changeable. Numeric Types
fruits = ["apple", "banana", "cherry"] int → integers (e.g., 10)
Tuple → Ordered, unchangeable. float → decimal numbers (e.g., 3.14)
coordinates = (4, 5) x = 10 # int
Dictionary → Key-value pairs. y = 3.14 # float
student = {"name": "Aleeza", "age": 21}

Loops in Python
For Loop
Used when the number of iterations is known.
for i in range(5):
print(i)
Output:
0
1
2
3
4
While Loop
Used when the number of iterations is not fixed.
i=0
while i < 5:
print(i)
i += 1
Output:
0
1
2
3
4

Data Structures in Python


Introduction
Data structures are ways of storing and organizing data so that they can be used efficiently in
programs. Python provides several built-in data structures that are widely used in data science
tasks.
The main data structures are:
o Lists
o Tuples
o Dictionaries
o Sets

Lists
A list is an ordered collection of items. Lists are mutable, which means items can be added,
removed, or changed.
Creating a List
fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40]
Accessing Elements
print(fruits[0]) # apple
print(fruits[2]) # cherry
Modifying Elements
fruits[1] = "mango"
print(fruits) # ['apple', 'mango', 'cherry']
List Methods
[Link]("orange") # add item
[Link]("apple") # remove item
len(fruits) # length of list

Tuples
A tuple is similar to a list but immutable (cannot be changed after creation).
Creating a Tuple
coordinates = (10, 20)
Accessing Elements
print(coordinates[0]) # 10
Immutability
coordinates[0] = 50 # Error: cannot modify
Use tuples when data should not change (for example, fixed locations, constant values).

Dictionaries
A dictionary stores data in key-value pairs. It is unordered and mutable.
Creating a Dictionary
student = {"name": "Aleeza", "age": 21, "grade": "A"}
Accessing Values
print(student["name"]) # Aleeza
Adding/Updating Values
student["age"] = 22
student["city"] = "Lahore"
Removing Keys
del student["grade"]
Dictionaries are very useful in data science for structured data like JSON files.

Sets
A set is an unordered collection of unique items.
Creating a Set
numbers = {1, 2, 3, 4, 4, 5}
print(numbers) # {1, 2, 3, 4, 5}
Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
print([Link](B)) # {1, 2, 3, 4, 5}
print([Link](B)) # {3}
print([Link](B)) # {1, 2}
Sets are useful when uniqueness of items is required.

Class Activity

Activity 1
Create a list of five student names. Add two more names and remove one.

students = ["Ali", "Sara", "Hassan", "Fatima", "Omar"]


[Link]("Areeba")
[Link]("Bilal")
[Link]("Omar")
print(students)
# Output: ['Ali', 'Sara', 'Hassan', 'Fatima', 'Areeba', 'Bilal']

Activity 2
Create a tuple of three cities and try to change one element (observe the error).

cities = ("Karachi", "Lahore", "Islamabad")


# cities[0] = "Multan" # This will give an error: TypeError: 'tuple' object does not support item
assignment

Activity 3
Create a dictionary to store details of a book (title, author, year). Update the year.

book = {"title": "Data Science 101", "author": "John Smith", "year": 2018}
book["year"] = 2023
print(book)
# Output: {'title': 'Data Science 101', 'author': 'John Smith', 'year': 2023}
Activity 4
Create two sets of numbers and find their union and intersection.

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print([Link](B)) # {1, 2, 3, 4, 5, 6}
print([Link](B)) # {3, 4}

File Handling and Data Input/Output in Python


Introduction
File handling is an important part of programming because it allows reading and writing data
permanently. Unlike variables that are temporary, files store data even after a program stops. In
Python, we use the built-in open() function for working with files.

Opening and Closing Files


file = open("[Link]", "r") # open file in read mode
[Link]() # always close after use
Modes:
"r" → Read (default, error if file not found)
"w" → Write (creates new file or overwrites existing)
"a" → Append (adds data at the end of file)
"r+" → Read and Write

Writing to a File
file = open("[Link]", "w")
[Link]("Hello, this is my first file.\n")
[Link]("Python makes file handling easy.")
[Link]()
This will create a file named [Link] with two lines of text.

Reading from a File


Method 1: Read entire file
file = open("[Link]", "r")
content = [Link]()
print(content)
[Link]()
Method 2: Read line by line
file = open("[Link]", "r")
for line in file:
print([Link]()) # strip removes newline character
[Link]()
Method 3: Readlines() into list
file = open("[Link]", "r")
lines = [Link]()
print(lines) # ['Hello, this is my first file.\n', 'Python makes file handling easy.']
[Link]()

Using with Statement (Recommended)


The with statement automatically closes the file after use.
with open("[Link]", "r") as file:
content = [Link]()
print(content)

Appending to a File
with open("[Link]", "a") as file:
[Link]("\nThis line is added later.")

Handling CSV Files


CSV (Comma Separated Values) files are very common in data science.
import csv

# Writing CSV
with open("[Link]", "w", newline="") as file:
writer = [Link](file)
[Link](["Name", "Age", "Grade"])
[Link](["Ali", 20, "A"])
[Link](["Sara", 22, "B"])

# Reading CSV
with open("[Link]", "r") as file:
reader = [Link](file)
for row in reader:
print(row)

Class Activities
Activity 1
Write a program to create a file called [Link] and write three lines into it.
with open("[Link]", "w") as f:
[Link]("This is line 1\n")
[Link]("This is line 2\n")
[Link]("This is line 3\n")
Activity 2
Write a program to read the contents of [Link] and display them.
with open("[Link]", "r") as f:
print([Link]())
Activity 3
Append one more line to [Link] and then display all lines.
with open("[Link]", "a") as f:
[Link]("This is line 4\n")

with open("[Link]", "r") as f:


for line in f:
print([Link]())
Activity 4
Create a CSV file of three employees with their names and salaries. Then read and display the
data.
import csv

with open("[Link]", "w", newline="") as f:


writer = [Link](f)
[Link](["Name", "Salary"])
[Link](["Areeba", 50000])
[Link](["Bilal", 60000])
[Link](["Omar", 55000])

with open("[Link]", "r") as f:


reader = [Link](f)
for row in reader:
print(row)

Introduction to NumPy and Pandas


Introduction
In Data Science, handling and analyzing large datasets efficiently is very important. Python
provides two powerful libraries for this purpose: NumPy and Pandas.
NumPy (Numerical Python): Used for numerical operations, arrays, and mathematical functions.
Pandas: Built on NumPy, used for data manipulation and analysis in tabular (row/column)
format.

Part 1: NumPy Basics


Importing NumPy
import numpy as np

Creating Arrays
arr = [Link]([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
print(type(arr)) # <class '[Link]'>
1D Array: [Link]([1,2,3])
2D Array:
arr2d = [Link]([[1,2,3],[4,5,6]])
print(arr2d)

Useful Array Functions


print([Link](5)) # [0. 0. 0. 0. 0.]
print([Link]((2,3))) # 2x3 array of ones
print([Link](1,10,2)) # [1 3 5 7 9]
print([Link](0,1,5))# [0. 0.25 0.5 0.75 1.]

Array Operations
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

print(a + b) # [5 7 9]
print(a * b) # [ 4 10 18]
print(a ** 2) # [1 4 9]
print([Link](a, b)) # 32 (dot product)

Statistical Functions
arr = [Link]([10, 20, 30, 40, 50])
print([Link](arr)) # 30.0
print([Link](arr)) # 30.0
print([Link](arr)) # standard deviation

Part 2: Pandas Basics


Importing Pandas
import pandas as pd

Series (1D Data)


s = [Link]([10, 20, 30, 40], index=["a","b","c","d"])
print(s)
Output:
a 10
b 20
c 30
d 40

DataFrame (2D Data)


data = {
"Name": ["Ali", "Sara", "Omar"],
"Age": [22, 24, 21],
"Marks": [85, 90, 78]
}
df = [Link](data)
print(df)
Output:
Name Age Marks
0 Ali 22 85
1 Sara 24 90
2 Omar 21 78

Accessing Data in DataFrame


print(df["Name"]) # column access
print([Link][0]) # row by index
print([Link][1, "Marks"]) # specific cell

Basic Operations
print([Link]()) # summary statistics
print([Link](2)) # first 2 rows
print([Link](1)) # last row

Reading and Writing CSV with Pandas


# Save to CSV
df.to_csv("[Link]", index=False)

# Read from CSV


df2 = pd.read_csv("[Link]")
print(df2)

Class Activities
Activity 1: NumPy
Create a NumPy array of numbers from 1 to 10 and calculate their mean and standard deviation.
import numpy as np
arr = [Link](1,11)
print("Mean:", [Link](arr))
print("Standard Deviation:", [Link](arr))

Activity 2: Pandas DataFrame


Create a DataFrame of 3 students with columns: Name, Age, GPA. Then display only the GPA
column.
import pandas as pd
data = {
"Name": ["Hina", "Bilal", "Owais"],
"Age": [20, 21, 22],
"GPA": [3.5, 3.8, 3.2]
}
df = [Link](data)
print(df["GPA"])

Activity 3: CSV Handling with Pandas


Create a DataFrame for 3 products with Price and Quantity, save it to a CSV file, then read it
back.
data = {
"Product": ["Pen", "Notebook", "Eraser"],
"Price": [20, 50, 10],
"Quantity": [5, 2, 10]
}
df = [Link](data)
df.to_csv("[Link]", index=False)

df2 = pd.read_csv("[Link]")
print(df2)

Data Cleaning and Preparation


Introduction
Before analysis or modeling, real-world data usually needs cleaning.
Data is often incomplete, inconsistent, or contains errors.
Data Cleaning and Preparation ensures high-quality, accurate datasets for analysis.

1. Common Problems in Raw Data


Missing Values: Some entries are empty.
Duplicates: Same record appears multiple times.
Incorrect Data Types: Numbers stored as text, dates stored as strings.
Inconsistent Formatting: "Male"/"M", "Female"/"F".
Outliers: Unusual values (e.g., salary = 999999).

2. Handling Missing Data


Checking Missing Data
import pandas as pd
data = {
"Name": ["Ali", "Sara", "Omar", "Hina"],
"Age": [22, None, 21, 23],
"Marks": [85, 90, None, 88]
}
df = [Link](data)

print([Link]()) # shows True where values are missing


print([Link]().sum()) # counts missing values per column
Filling Missing Values
df["Age"].fillna(df["Age"].mean(), inplace=True) # replace with mean
df["Marks"].fillna(0, inplace=True) # replace with 0
Dropping Missing Values
[Link](inplace=True) # removes rows with any missing value

3. Removing Duplicates
df = [Link]({
"Name": ["Ali", "Sara", "Ali"],
"Age": [22, 23, 22]
})
df = df.drop_duplicates()

4. Correcting Data Types


df["Age"] = df["Age"].astype(int) # convert to integer

5. Handling Inconsistent Data


Example: Different labels for gender.
df["Gender"] = df["Gender"].replace({"M":"Male","F":"Female"})

6. Detecting Outliers
Using statistical methods:
import numpy as np
arr = [Link]([10, 12, 15, 14, 100]) # 100 is an outlier
mean = [Link](arr)
std = [Link](arr)

for x in arr:
if abs(x - mean) > 2*std:
print("Outlier:", x)

7. Renaming Columns
[Link](columns={"Marks":"Score"}, inplace=True)

8. Feature Scaling (Normalization/Standardization)


Scaling helps when data values have different ranges.
from [Link] import MinMaxScaler
scaler = MinMaxScaler()
df[["Marks"]] = scaler.fit_transform(df[["Marks"]])
Class Activities and Solutions
Activity 1: Handling Missing Data
Create a DataFrame of 5 students with some missing ages and marks. Replace missing ages with
average age, and missing marks with 0.
data = {
"Name": ["Ali", "Sara", "Omar", "Hina", "Bilal"],
"Age": [22, None, 21, None, 24],
"Marks": [85, 90, None, 88, None]
}
df = [Link](data)
df["Age"].fillna(df["Age"].mean(), inplace=True)
df["Marks"].fillna(0, inplace=True)
print(df)

Activity 2: Removing Duplicates


Create a DataFrame with duplicate rows and remove duplicates.
df = [Link]({
"Product": ["Pen", "Pen", "Notebook", "Eraser"],
"Price": [20, 20, 50, 10]
})
df = df.drop_duplicates()
print(df)

Activity 3: Gender Formatting


A DataFrame has inconsistent gender values. Replace them with standard labels.
df = [Link]({
"Name": ["Ali", "Sara", "Omar"],
"Gender": ["M", "Female", "F"]
})
df["Gender"] = df["Gender"].replace({"M":"Male","F":"Female"})
print(df)

Common questions

Powered by AI

Python's flexibility and extensive library support make it highly popular in data science. It offers robust tools like NumPy and Pandas, which facilitate efficient data handling and analysis. NumPy supports numerical operations and array manipulations, enabling high-performance computations . Pandas, built on NumPy, provides data manipulation and analysis capabilities in a user-friendly tabular format akin to spreadsheets, crucial for handling large datasets . These libraries simplify complex tasks with built-in functions for statistical analysis, data manipulation, and advanced computations, enabling data scientists to focus more on extracting insights rather than coding complexities .

In data science, data engineers, data scientists, and machine learning engineers have distinct roles with interconnected responsibilities. Data engineers focus on designing and managing data pipelines, ensuring data cleanliness, accessibility, and reliability for analysis . Data scientists analyze data to uncover patterns and build predictive models, communicating results to decision-makers . In contrast, machine learning engineers are responsible for deploying these models into production, optimizing algorithms for speed and accuracy . Thus, while data engineers prepare the groundwork, data scientists generate insights, and machine learning engineers implement and ensure these insights' applicability in practical scenarios .

DataSciencester, a fictional social network introduced by Joel Grus, serves as a practical understanding tool by illustrating how data representation and basic analysis can be applied to real-world scenarios such as social networks. It demonstrates data representation through storing users as dictionaries with unique ids and name, and friendships as pairs of user IDs . It further allows for basic analysis such as counting and averaging connections, identifying influencers through sorting users by number of friends, and recommending new friends based on the friend-of-a-friend concept . These activities replicate functionalities like friend suggestions and influencer ranking seen in platforms like Facebook or LinkedIn, thus highlighting its real-world relevance .

Exploratory data analysis (EDA) enhances the understanding of datasets by employing statistical methods and visualization techniques to reveal underlying patterns, trends, and anomalies. This step is essential in the data science workflow as it provides insights into data characteristics, informs subsequent steps like model selection and feature engineering, and helps confirm or challenge initial assumptions about the data . EDA assists in forming hypotheses and understanding the data's narrative, thus guiding more targeted and effective analysis decisions .

Using Python dictionaries for data representation in applications like DataSciencester offers several benefits, including intuitive data storage, fast access to information through key-value associations, and flexibility in handling dynamic datasets . However, challenges include increased memory consumption compared to other data structures due to stored metadata, potential complexity in handling more extensive and nested data relationships, and potential performance overhead in high-frequency operations due to Python's interpreted nature . Effectively managing these challenges requires structured data organization and efficient querying techniques .

Feature scaling is crucial in data science because it ensures that all input features are on a comparable scale, improving the convergence speed and performance of machine learning models. Methods like normalization and standardization adjust values to a common scale, which is particularly important for algorithms sensitive to feature magnitude, such as k-nearest neighbors, gradient descent-based algorithms, and support vector machines . Feature scaling helps prevent bias towards features with larger ranges, thus leading to more accurate and efficient models by providing each feature equal weight .

The significance of the iterative workflow in data science lies in its systematic approach to solving complex problems, allowing for refinement and continuous improvement of models. The steps are: defining the problem to solve, collecting data from various sources like databases and APIs, cleaning and preparing data by handling missing values and fixing errors, exploring data through EDA to discover patterns, building models to make predictions, evaluating and interpreting results for accuracy and performance, and communicating insights via reports or dashboards . The cycle nature ensures adaptability and learning from previous iterations .

Data science combines three key elements: statistics & mathematics, computer science (programming), and domain knowledge. Statistics & mathematics help in understanding patterns and formulating models based on data . Computer science provides the tools and methods to process and analyze data efficiently using algorithms . Domain knowledge ensures that insights are relevant and can be effectively applied to solve real-world problems, such as predicting diseases in healthcare or recommending movies at Netflix .

Handling missing data can involve strategies like filling missing values with statistics such as mean or median, replacing with a constant like zero, or utilizing prediction models to estimate missing entries. Alternatively, rows with missing data can be entirely removed. The trade-offs include potential bias introduction when using imputation, especially if the missing data is not random, and loss of data diversity and information when removing rows, which can reduce dataset quality and may lead to skewed analysis outcomes . Considering the context and the nature of missing data is critical in choosing the appropriate strategy .

The Friend-of-a-Friend (Foaf) concept enhances user engagement in social networks by providing recommendations based on mutual connections, thereby facilitating the discovery of new acquaintances and strengthening the network . This approach not only increases the chances of relevant friend suggestions, enhancing the sense of community among users, but also promotes more frequent interactions and higher engagement levels within the platform as users expand their social circles through shared connections .

You might also like