AI & Machine Learning

What is Machine Learning? Fundamentals & Your First Model with Python

12 min readMarch 18, 2026
Makine öğrenmesiMachine learningYapay zekaPython MLMakine öğrenmesi nedirML başlangıçScikit-learnSupervised learningUnsupervised learningVeri bilimiMachine learning TürkçeML rehberi

Machine learning (ML) is a branch of artificial intelligence that enables computers to learn from data without being explicitly programmed. From recommendation systems to autonomous vehicles, natural language processing to medical diagnostics, ML is transforming industries across the board. In this guide, we will explore the fundamental concepts of machine learning, the types of learning paradigms, and hands-on Python implementations.

What Is Machine Learning?

In traditional software development, rules are manually defined: "If X, then do Y." In machine learning, algorithms discover patterns from data and derive their own rules. Given sufficient data and the right algorithm, a system can make accurate predictions on examples it has never encountered before.

Standard Workflow

A typical machine learning project follows these steps:

  1. Data Collection: Gathering raw data from various sources
  2. Data Preprocessing: Handling missing values, normalization, feature engineering
  3. Model Selection: Choosing the appropriate algorithm for the problem
  4. Training: Letting the model learn patterns from the training data
  5. Evaluation: Measuring performance on unseen test data
  6. Deployment: Putting the model into production

Types of Learning

Supervised Learning

Works with labeled data. The model learns from input-output pairs and generates predictions for new inputs.

  • Classification: Email spam detection, disease diagnosis
  • Regression: House price prediction, temperature forecasting

Unsupervised Learning

Works with unlabeled data. The model discovers hidden structures and patterns within the data.

  • Clustering: Customer segmentation, document grouping
  • Dimensionality Reduction: Visualizing high-dimensional data using PCA

Reinforcement Learning

An agent learns by interacting with its environment through a reward-and-penalty mechanism. Used in game playing, robot control, and resource optimization.

Machine Learning Fundamentals with Python

Data Preparation with NumPy

Numerical arrays form the backbone of machine learning. NumPy is the cornerstone of the Python ML ecosystem:

python
import numpy as np

# Create a feature matrix (4 samples, 3 features)
X = np.array([
    [5.1, 3.5, 1.4],
    [4.9, 3.0, 1.4],
    [7.0, 3.2, 4.7],
    [6.4, 3.2, 4.5]
])

# Label vector (0: setosa, 1: versicolor)
y = np.array([0, 0, 1, 1])

# Basic statistics
print(f"Mean: {X.mean(axis=0)}")
print(f"Standard Deviation: {X.std(axis=0)}")
print(f"Data Shape: {X.shape}")

Classification with Scikit-learn

Scikit-learn is the most widely used machine learning library in Python. Below is a classification example on the Iris dataset:

python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report

# Load the dataset
iris = load_iris()
X, y = iris.data, iris.target

# Split into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Create and train a Random Forest classifier
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Predict on the test set
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)
print(f"Model Accuracy: {accuracy:.2%}")

# Detailed classification report
print(classification_report(y_test, predictions, target_names=iris.target_names))

Data Preprocessing Pipeline

In real-world projects, data is rarely clean. Scikit-learn's Pipeline structure lets you chain preprocessing steps seamlessly:

python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.svm import SVC

# Build pipeline: Scaling -> PCA -> SVM
pipeline = Pipeline([
    ('scaling', StandardScaler()),          # Normalize features
    ('dim_reduction', PCA(n_components=2)), # Reduce to 2 dimensions
    ('classifier', SVC(kernel='rbf'))       # Classify with SVM
])

# Train the pipeline
pipeline.fit(X_train, y_train)

# Predict in a single step (preprocessing applied automatically)
score = pipeline.score(X_test, y_test)
print(f"Pipeline Accuracy: {score:.2%}")

Feature Engineering

Feature engineering is the process of creating new variables from raw data that help the model learn more effectively. Well-crafted features can dramatically improve model performance regardless of the algorithm used. In practice, data scientists spend a significant portion of their time on this step because the quality of features often matters more than the choice of algorithm.

Common feature engineering techniques include polynomial expansion of numerical features, one-hot encoding of categorical variables, extracting day, month, and year from date columns, and generating numerical features from text data such as TF-IDF or word counts.

python
from sklearn.preprocessing import PolynomialFeatures, OneHotEncoder
from sklearn.compose import ColumnTransformer

# Separate transformations for numerical and categorical features
preprocessor = ColumnTransformer(
    transformers=[
        ('numerical', Pipeline([
            ('scaling', StandardScaler()),
            ('polynomial', PolynomialFeatures(degree=2, include_bias=False))
        ]), [0, 1, 2, 3]),  # Numerical column indices
    ],
    remainder='passthrough'
)

# Integrate into pipeline
full_pipeline = Pipeline([
    ('preprocessor', preprocessor),
    ('classifier', RandomForestClassifier(n_estimators=100, random_state=42))
])

full_pipeline.fit(X_train, y_train)
print(f"Accuracy After Feature Engineering: {full_pipeline.score(X_test, y_test):.2%}")

Cross-Validation

Evaluating a model with a single train/test split can produce misleading results depending on how the data is partitioned. Cross-validation addresses this problem by dividing the data into k folds and using each fold as the test set in turn. This approach provides a more reliable estimate of how well the model generalizes to unseen data.

python
from sklearn.model_selection import cross_val_score, StratifiedKFold

# 5-fold stratified cross-validation
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

# Cross-validation with Random Forest
rf_scores = cross_val_score(
    RandomForestClassifier(n_estimators=100, random_state=42),
    X, y, cv=cv, scoring='accuracy'
)

print(f"Cross-Validation Scores: {rf_scores}")
print(f"Mean Accuracy: {rf_scores.mean():.2%} (+/- {rf_scores.std():.2%})")

Stratified cross-validation preserves class proportions in each fold, making it particularly valuable for imbalanced datasets. This technique is essential for understanding the true performance of a model, especially with small datasets where a single split can be unrepresentative.

Model Comparison

Different algorithms can perform very differently on the same dataset. Systematic comparison across multiple algorithms is necessary to find the best model for your problem. The table below summarizes the strengths and weaknesses of common algorithms:

| Algorithm | Strengths | Weaknesses | Best Use Case |

|---|---|---|---|

| Random Forest | Overfitting-resistant, feature importance | Slow prediction | Tabular data, general purpose |

| SVM | Effective in high dimensions | Slow on large datasets | Small-medium data, text classification |

| KNN | Simple, no training phase | Slow on large data, requires scaling | Small data, prototyping |

| Gradient Boosting | High accuracy | Hyperparameter sensitivity | Competitions, tabular data |

| Logistic Regression | Interpretable, fast | Weak on non-linear relationships | Binary classification, baseline |

Overfitting vs. Underfitting

The most critical balance in machine learning is between model complexity and generalization ability.

Overfitting occurs when the model memorizes the training data. Training accuracy is very high while test accuracy is low. This typically happens when the model is too complex or the training data is insufficient. Solutions include regularization (L1/L2), dropout, collecting more data, or simplifying the model architecture.

Underfitting occurs when the model performs poorly on both training and test data. This indicates the model is not complex enough or that feature engineering is insufficient. Solutions include using a more complex model, adding more features, or increasing training duration.

The key to good machine learning practice is finding the sweet spot where your model captures the true patterns in the data without memorizing noise. Techniques like cross-validation, learning curves, and validation sets help diagnose where your model falls on this spectrum.

Hyperparameter Tuning with GridSearchCV

Every machine learning algorithm has hyperparameters that affect its performance. GridSearchCV finds the best combination by trying all possibilities with cross-validation:

python
from sklearn.model_selection import GridSearchCV

# Define hyperparameter search space
param_grid = {
    'n_estimators': [50, 100, 200],
    'max_depth': [3, 5, 10, None],
    'min_samples_split': [2, 5, 10],
    'min_samples_leaf': [1, 2, 4]
}

# Find the best parameters with GridSearchCV
grid_search = GridSearchCV(
    RandomForestClassifier(random_state=42),
    param_grid,
    cv=5,
    scoring='accuracy',
    n_jobs=-1,  # Use all CPU cores
    verbose=1
)

grid_search.fit(X_train, y_train)

print(f"Best Parameters: {grid_search.best_params_}")
print(f"Best Cross-Validation Score: {grid_search.best_score_:.2%}")
print(f"Test Set Accuracy: {grid_search.score(X_test, y_test):.2%}")

For large parameter spaces, GridSearchCV can be prohibitively slow. In such cases, you can use RandomizedSearchCV for random sampling or turn to Bayesian optimization tools like Optuna or Hyperopt. These tools explore the parameter space more intelligently by leveraging results from previous trials.

Model Evaluation Metrics

Accuracy alone is not enough to measure a model's success. Especially with imbalanced datasets, you should consider:

  • Precision: Of all positive predictions, how many were correct?
  • Recall: Of all actual positives, how many were captured?
  • F1-Score: The harmonic mean of precision and recall
  • Confusion Matrix: A visual representation of the prediction distribution

Common Pitfalls

Critical points to watch for in machine learning projects:

  1. Overfitting: The model memorizes training data and fails to generalize. Solution: cross-validation, regularization, more data
  2. Data Leakage: Test data leaking into the training process. Solution: perform train/test split before preprocessing
  3. Forgetting Feature Scaling: Distance-based algorithms like SVM and KNN require scaled data
  4. Imbalanced Datasets: Uneven class distribution makes accuracy misleading. Solution: SMOTE, class weights

Next Steps

After grasping the fundamentals, you can explore:

  • Deep Learning: Neural networks, CNN, RNN, Transformer architectures
  • MLOps: Model versioning, CI/CD pipelines, monitoring
  • Feature Engineering: Building powerful features with domain knowledge
  • AutoML: Automated model selection and hyperparameter optimization

Summary

Machine learning is one of the most powerful tools in artificial intelligence. With supervised, unsupervised, and reinforcement learning paradigms, it can tackle a wide range of problem types. The Python ecosystem — NumPy, Scikit-learn, Pandas — enables rapid prototyping and production-grade solutions. By understanding your data through feature engineering, measuring true performance with cross-validation, and optimizing hyperparameters with GridSearchCV, you can build robust ML projects. Mastering the balance between overfitting and underfitting is the fundamental skill every machine learning engineer must develop.

Related Articles

Have a Flutter Project?

I build high-performance Flutter applications for iOS, Android, and web.

Get in Touch