Statistics and maths / Probability and distributions
Beginners Guide to Analysis I in Python:
Contents
Introduction to Analysis in Python
- Analysis refers to the process of examining, cleaning, transforming, and modeling data to uncover patterns, insights, and trends. Python is a popular language for data analysis due to its simplicity, readability, and extensive ecosystem of libraries.
Setting Up the Environment
- To get started with data analysis in Python, you need to install Python and set up the necessary libraries and tools.
# Install required libraries
!pip install pandas numpy matplotlib seaborn
Importing Libraries
- The first step in any Python analysis is to import the required libraries.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
Reading Data
- Python provides various ways to read data from different sources, such as CSV files, Excel sheets, databases, and more.
# Read data from a CSV file
data = pd.read_csv('data.csv')
Exploring Data
- Once you have loaded the data, you can explore it by examining its shape, data types, and summary statistics.
# View the first few rows
print(data.head())
# Check the shape of the data
print(data.shape)
# Get summary statistics
print(data.describe())
Handling Missing Data
- Real-world datasets often contain missing values, which need to be handled appropriately.
# Check for missing values
print(data.isnull().sum())
# Fill missing values with mean
data['column'] = data['column'].fillna(data['column'].mean())
Data Cleaning
- Data cleaning involves handling outliers, removing duplicates, and ensuring data consistency.
# Remove duplicates
data = data.drop_duplicates()
# Handle outliers
data = data[data['column'] < 3 * data['column'].std()]
Data Transformation
- Data transformation involves converting data into a more suitable format for analysis or modeling.
# Convert data types
data['date'] = pd.to_datetime(data['date'])
# Create new features
data['age_group'] = pd.cut(data['age'], bins=[0, 18, 35, 65, 120], labels=['child', 'young', 'adult', 'senior'])
Exploratory Data Analysis (EDA)
- EDA involves visualizing and summarizing data to identify patterns, relationships, and potential issues.
# Plot a histogram
plt.hist(data['column'], bins=20)
plt.show()
# Create a scatter plot
plt.scatter(data['x'], data['y'])
plt.show()
Statistical Analysis
- Python provides various statistical functions and libraries for performing hypothesis testing, correlation analysis, and more.
# Calculate correlation
print(data['column1'].corr(data['column2']))
# Perform a t-test
from scipy.stats import ttest_ind
t_stat, p_val = ttest_ind(group1, group2)
print(f'p-value: {p_val}')
Machine Learning
- Python offers powerful machine learning libraries like scikit-learn for building and evaluating predictive models.
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
X = data[['feature1', 'feature2']]
y = data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LinearRegression()
model.fit(X_train, y_train)