Machine Learning
Python
Data Science
⭐ Featured
Introduction to Machine Learning with Python
Get started with machine learning using Python. Learn the fundamentals, essential libraries, and build your first ML model with practical examples.
•👤 Dr. Rachel Green•⏱️ 16 min read
👁️1,554 views
❤️112
#machine-learning
#python
#data-science
#ai
#tutorial
Introduction to Machine Learning with Python
Machine Learning is transforming industries and creating new possibilities. Let's explore how to get started with ML using Python.
What is Machine Learning?
Machine Learning is a subset of AI that enables computers to learn and improve from experience without being explicitly programmed.
Types of Machine Learning:
- Supervised Learning: Learning with labeled data
- Unsupervised Learning: Finding patterns in unlabeled data
- Reinforcement Learning: Learning through trial and error
Essential Python Libraries
NumPy
Fundamental package for numerical computing:
import numpy as np
# Create arrays
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2], [3, 4]])
# Mathematical operations
result = np.mean(arr)
Pandas
Data manipulation and analysis:
import pandas as pd
# Create DataFrame
df = pd.DataFrame(\{
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'city': ['NYC', 'LA', 'Chicago']
\})
# Data exploration
print(df.info())
print(df.describe())
Scikit-learn
Machine learning library:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Prepare data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Train model
model = LinearRegression()
model.fit(X_train, y_train)
# Make predictions
predictions = model.predict(X_test)
Your First ML Project
Let's build a simple house price prediction model:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
import matplotlib.pyplot as plt
# Load data
data = pd.read_csv('house_prices.csv')
# Explore data
print(data.head())
print(data.info())
# Prepare features
features = ['sqft', 'bedrooms', 'bathrooms', 'age']
X = data[features]
y = data['price']
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Train model
model = LinearRegression()
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate model
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Absolute Error: \{mae\}")
print(f"R² Score: \{r2\}")
# Visualize results
plt.scatter(y_test, y_pred)
plt.xlabel('Actual Prices')
plt.ylabel('Predicted Prices')
plt.title('Actual vs Predicted Prices')
plt.show()
Data Preprocessing
Handling Missing Data
# Check for missing values
print(df.isnull().sum())
# Fill missing values
df['column'].fillna(df['column'].mean(), inplace=True)
# Drop rows with missing values
df.dropna(inplace=True)
Feature Scaling
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
Categorical Variables
# One-hot encoding
df_encoded = pd.get_dummies(df, columns=['category'])
# Label encoding
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['category_encoded'] = le.fit_transform(df['category'])
Model Evaluation
Cross-Validation
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
print(f"Average CV Score: \{scores.mean()\}")
Metrics for Different Problems
- Regression: MAE, MSE, R²
- Classification: Accuracy, Precision, Recall, F1-score
Next Steps
- Explore more algorithms: Decision Trees, Random Forest, SVM
- Learn feature engineering: Create better input features
- Study deep learning: Neural networks with TensorFlow/PyTorch
- Practice on real datasets: Kaggle competitions
- Understand model interpretation: SHAP, LIME
Machine learning is a journey of continuous learning. Start with simple projects and gradually tackle more complex problems.