<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: cristopher Njuguna</title>
    <description>The latest articles on Forem by cristopher Njuguna (@cristopher_njuguna_6381b1).</description>
    <link>https://forem.com/cristopher_njuguna_6381b1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3506190%2Fd980ba0b-bb6e-4331-bc7b-235c63d086ad.png</url>
      <title>Forem: cristopher Njuguna</title>
      <link>https://forem.com/cristopher_njuguna_6381b1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cristopher_njuguna_6381b1"/>
    <language>en</language>
    <item>
      <title>Ridge Regression vs Lasso Regression</title>
      <dc:creator>cristopher Njuguna</dc:creator>
      <pubDate>Sun, 25 Jan 2026 13:40:09 +0000</pubDate>
      <link>https://forem.com/cristopher_njuguna_6381b1/ridge-regression-vs-lassoregression-3aja</link>
      <guid>https://forem.com/cristopher_njuguna_6381b1/ridge-regression-vs-lassoregression-3aja</guid>
      <description>&lt;p&gt;House Price Prediction Context&lt;/p&gt;

&lt;p&gt;Linear regression is a common method for predicting continuous values such as house prices.&lt;br&gt;
While Ordinary Least Squares (OLS) is the standard approach for fitting linear models, it often struggles with real-world data that contains many features, correlated variables, or noise. Regularization techniques such as Ridge and Lasso regression improve model performance by controlling complexity and reducing overfitting.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ordinary Least Squares (OLS)
What is OLS and what objective does it minimize?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ordinary Least Squares (OLS) is a method used to estimate the coefficients of a linear regression model by minimizing the sum of squared residuals—the squared differences between actual and predicted values.&lt;/p&gt;

&lt;p&gt;Loss=i=1∑n​(yi​−y^​i​)2&lt;/p&gt;

&lt;p&gt;Here, yi​ represents the actual house price and y^​i represents the predicted price.&lt;/p&gt;

&lt;p&gt;Why can OLS lead to overfitting in real-world datasets?&lt;/p&gt;

&lt;p&gt;OLS can lead to overfitting when the model becomes too complex, especially in datasets with many features or when features are highly correlated. In such cases, the model captures noise rather than the underlying data patterns, resulting in poor generalization to unseen data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Concept of Regularization
What problem does regularization solve in linear regression?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Regularization reduces overfitting and model instability by discouraging large coefficient values. It helps the model generalize better to new data, especially when features are numerous or correlated.&lt;/p&gt;

&lt;p&gt;Why is adding a penalty term helpful?&lt;/p&gt;

&lt;p&gt;Adding a penalty term helps to constrain the magnitude of the coefficients. This keeps them small and reduces the model complexity, leading to better generalization to new data. Regularization techniques like Lasso and Ridge help in stabilizing the coefficient estimates.&lt;/p&gt;

&lt;p&gt;Regularized Loss = OLS Loss + 𝜆 × Penalty&lt;/p&gt;

&lt;p&gt;The parameter λ controls how strong the penalty is.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ridge Regression (L2 Regularization)
Ridge regression loss function
Loss=∑(yi​−y^​i​)2+λ∑βj2​​&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How the L2 penalty affects coefficients&lt;/p&gt;

&lt;p&gt;The L2 penalty shrinks all coefficients toward zero by penalizing their squared values. Larger coefficients receive stronger penalties, which stabilizes the model—especially when features are correlated.&lt;/p&gt;

&lt;p&gt;Why Ridge regression does not perform feature selection&lt;/p&gt;

&lt;p&gt;Ridge regression does not set coefficients exactly to zero. Instead, it reduces their magnitude. As a result, all features remain in the model, meaning Ridge does not perform feature selection.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lasso Regression (L1 Regularization)
Lasso regression loss function
Loss=∑(yi​−y^​i​)2+λ∑∣βj​∣  ​&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How the L1 penalty differs from L2&lt;/p&gt;

&lt;p&gt;The L1 penalty uses the absolute value of coefficients rather than squared values. This creates a stronger pull toward zero compared to L2.&lt;/p&gt;

&lt;p&gt;Why Lasso can set coefficients exactly to zero&lt;/p&gt;

&lt;p&gt;Lasso regression can set some coefficients exactly to zero because the L1 penalty creates a situation where the optimization problem can yield coefficients that do not contribute at all to the prediction, effectively excluding those features from the model.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Comparison: Ridge vs. Lasso Regression&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Feature Selection:&lt;br&gt;
Ridge: Retains all features, making it less suitable for dimensionality reduction.&lt;br&gt;
Lasso: Performs feature selection by driving some coefficients to zero, making it more suitable for simpler models.&lt;/p&gt;

&lt;p&gt;Coefficient Behavior:&lt;br&gt;
Ridge: Coefficients are shrunk but not zeroed out, which means all variables remain in the model.&lt;br&gt;
Lasso: Coefficients can be zeroed out, allowing for the model to focus on a subset of features.&lt;/p&gt;

&lt;p&gt;Model Interpretability:&lt;br&gt;
Ridge: Models are more complex and can be harder to interpret due to all features being included.&lt;br&gt;
Lasso: Models are simpler and more interpretable since irrelevant features can be excluded entirely.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Application: House Price Prediction&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Assume features include:&lt;/p&gt;

&lt;p&gt;House size&lt;/p&gt;

&lt;p&gt;Number of bedrooms&lt;/p&gt;

&lt;p&gt;Distance to city&lt;/p&gt;

&lt;p&gt;Number of nearby schools&lt;/p&gt;

&lt;p&gt;Several noisy or weak features&lt;/p&gt;

&lt;p&gt;a. Which regression technique would you choose if all features are believed to contribute to the price? Explain why.&lt;/p&gt;

&lt;p&gt;If all features are believed to contribute to house prices, I would choose Ridge Regression. Since Ridge retains all features and simply shrinks their coefficients, it would provide a robust prediction without excluding any potentially valuable information.&lt;/p&gt;

&lt;p&gt;b. Which technique would you choose if only a few features are truly important and others add noise? Explain why.&lt;/p&gt;

&lt;p&gt;I would opt for Lasso Regression. Lasso's ability to reduce some coefficients to zero allows us to eliminate the irrelevant features, thus simplifying the model and improving its interpretability.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Model Evaluation
Detecting Overfitting Using Training and Testing Data&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To detect overfitting, the dataset is divided into two parts: a training set and a testing set. The model is trained using the training data and then evaluated on the testing data, which the model has not seen before. If the model shows very low error on the training data but significantly higher error on the testing data, this indicates overfitting. In contrast, similar performance on both datasets suggests that the model generalizes well. Evaluation metrics such as Mean Squared Error (MSE) or R² can be used to compare performance across the two datasets.&lt;/p&gt;

&lt;p&gt;Role of Residuals in Evaluating Model Performance&lt;/p&gt;

&lt;p&gt;Residuals are the differences between the actual house prices and the prices predicted by the model. Analyzing residuals helps determine how well the model fits the data. A good model produces residuals that are randomly scattered around zero, indicating that the model has captured the underlying relationship. If residuals show patterns, trends, or large outliers, this suggests issues such as missing important variables, non-linear relationships, or the presence of noise that the model has not handled well.&lt;/p&gt;

&lt;p&gt;OLS is effective for simple datasets but struggles with noise and high dimensionality. Ridge regression improves stability by shrinking coefficients, while Lasso regression improves simplicity by removing irrelevant features. In house price prediction, Ridge is ideal when all features matter, while Lasso is better when only a few key predictors drive prices.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>regression</category>
    </item>
    <item>
      <title>Introduction to Classes in Object-Oriented Programming</title>
      <dc:creator>cristopher Njuguna</dc:creator>
      <pubDate>Thu, 11 Dec 2025 18:37:31 +0000</pubDate>
      <link>https://forem.com/cristopher_njuguna_6381b1/introduction-to-classes-in-object-oriented-programming-36ip</link>
      <guid>https://forem.com/cristopher_njuguna_6381b1/introduction-to-classes-in-object-oriented-programming-36ip</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Understanding Classes in Object-Oriented Programming (OOP)&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Object-Oriented Programming (OOP) is a programming paradigm that helps developers structure code in a more organized and modular way. One of its core features is the &lt;strong&gt;class&lt;/strong&gt;, a blueprint used to create objects that represent real-world entities. Understanding how classes work is essential for building scalable and maintainable software.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What Is a Class?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;class&lt;/strong&gt; is a user-defined blueprint that describes the structure and behavior of an object.&lt;br&gt;
It defines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Attributes&lt;/strong&gt; → the data or characteristics of the object&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods&lt;/strong&gt; → the actions or behaviors the object can perform&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can think of a class like a template for making multiple identical objects, each containing its own data but sharing the same structure.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Why Are Classes Useful?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Classes help solve programming problems in a structured way by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modeling real-world entities&lt;/strong&gt;&lt;br&gt;
For example, a bank account, a student, or a car.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Grouping related data and functions together&lt;/strong&gt;&lt;br&gt;
This makes the program cleaner and easier to understand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reusing code&lt;/strong&gt;&lt;br&gt;
Once a class is created, you can make as many objects from it as needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Encapsulating data&lt;/strong&gt;&lt;br&gt;
Classes keep related operations together, making it harder to accidentally misuse data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Attributes and Methods&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Attributes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Attributes are variables contained inside a class.&lt;br&gt;
They store information about the object.&lt;/p&gt;

&lt;p&gt;Example attributes in a bank account:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;account_number&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;owner&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;balance&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Methods&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Methods are functions defined inside a class.&lt;br&gt;
They define the behaviors that the objects created from the class can perform.&lt;/p&gt;

&lt;p&gt;Example methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;deposit()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;withdraw()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;check_balance()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;Example: A Simple BankAccount Class&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Below is an easy implementation of a BankAccount class in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;account_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;account_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;account_number&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Deposited &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;. New balance is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Insufficient funds.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Withdrew &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;. New balance is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_balance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Account Balance: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;How the Class Works&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;The &lt;code&gt;__init__&lt;/code&gt; Method&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This special method initializes a new object.&lt;br&gt;
It sets the starting values for the attributes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;account_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;account_number&lt;/span&gt;
&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;
&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every object created from this class will store these values independently.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Operations Through Methods&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Deposit&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Adds money to the balance.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Withdraw&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Checks if the account has enough funds before withdrawing.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Check Balance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Returns the current balance.&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;strong&gt;Creating an Object From the Class&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Here is how you can create a BankAccount object and perform actions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Create a new bank account
&lt;/span&gt;&lt;span class="n"&gt;account1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ACC123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;John Doe&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Deposit money
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# Withdraw money
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# Check balance
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;check_balance&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Output example&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Deposited 300. New balance is 800.
Withdrew 200. New balance is 600.
Account Balance: 600
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each method changes or accesses the data stored in the object.&lt;/p&gt;




&lt;p&gt;Classes allow you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Structure code based on real-world concepts&lt;/li&gt;
&lt;li&gt;Keep data and behavior packaged together&lt;/li&gt;
&lt;li&gt;Build programs that are easier to expand and maintain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In real applications, the BankAccount class could be expanded with security checks, transaction histories, or links to user profiles. This demonstrates how classes form the foundation of more complex systems.&lt;/p&gt;

</description>
      <category>python</category>
      <category>classes</category>
      <category>programming</category>
    </item>
    <item>
      <title>Connecting Power BI to PostgreSQL (Aiven &amp; Localhost): A Simple Step-by-Step Guide</title>
      <dc:creator>cristopher Njuguna</dc:creator>
      <pubDate>Sat, 22 Nov 2025 20:54:01 +0000</pubDate>
      <link>https://forem.com/cristopher_njuguna_6381b1/connecting-power-bi-to-postgresql-aiven-localhost-a-simple-step-by-step-guide-984</link>
      <guid>https://forem.com/cristopher_njuguna_6381b1/connecting-power-bi-to-postgresql-aiven-localhost-a-simple-step-by-step-guide-984</guid>
      <description>&lt;p&gt;Power BI connects smoothly to both cloud-hosted and local PostgreSQL databases once you know where to enter the right details. This guide explains the simplest possible process for connecting Power BI to:&lt;/p&gt;

&lt;p&gt;PostgreSQL hosted on Aiven, and PostgreSQL running locally on your machine&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Connecting Power BI to PostgreSQL on Aiven
Step 1: Open Power BI and Start a Blank Report&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Launch Power BI Desktop.&lt;br&gt;
Click Blank Report → Get Data → More → search for PostgreSQL Database.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc472sfrhg710biem75o4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc472sfrhg710biem75o4.png" alt=" " width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Enter Aiven Connection Details&lt;/p&gt;

&lt;p&gt;In Aiven:&lt;/p&gt;

&lt;p&gt;Open the Aiven Console&lt;/p&gt;

&lt;p&gt;Click Services&lt;/p&gt;

&lt;p&gt;Select your PostgreSQL service&lt;/p&gt;

&lt;p&gt;View your connection information (host, port, database)&lt;/p&gt;

&lt;p&gt;Use these values in Power BI:&lt;/p&gt;

&lt;p&gt;Server: host:port&lt;br&gt;
Example: pg-12345.aivencloud.com:28643&lt;/p&gt;

&lt;p&gt;Database: your Aiven PostgreSQL database name&lt;/p&gt;

&lt;p&gt;Data Connectivity Mode: Import&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsu8guba0cv9qle8uo98q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsu8guba0cv9qle8uo98q.jpg" alt=" " width="800" height="364"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm4195lr1iqmg0sbr3l3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm4195lr1iqmg0sbr3l3.jpg" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5gay6g79xa5bcn61eyq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5gay6g79xa5bcn61eyq.png" alt=" " width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 3: Enter Aiven Credentials&lt;/p&gt;

&lt;p&gt;Power BI now prompts for authentication.&lt;/p&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;p&gt;Username: Aiven database user&lt;/p&gt;

&lt;p&gt;Password: the associated password&lt;/p&gt;

&lt;p&gt;Step 4: Choose Tables in the Navigator&lt;/p&gt;

&lt;p&gt;Power BI opens the Navigator panel.&lt;br&gt;
Select the tables you want from your Aiven PostgreSQL database and click Load.&lt;/p&gt;

&lt;p&gt;The data imports and appears in the Power BI report interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy6lmz2koyten32k34gc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy6lmz2koyten32k34gc.jpg" alt=" " width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Connecting Power BI to PostgreSQL on Localhost&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The process is almost identical — only the connection details change.&lt;/p&gt;

&lt;p&gt;Step 1: Open Power BI → Blank Report → Get Data → PostgreSQL&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fltx4g0od5avl7btetc7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fltx4g0od5avl7btetc7f.png" alt=" " width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Enter Localhost Database Details&lt;/p&gt;

&lt;p&gt;Fill the fields with your local PostgreSQL configuration:&lt;/p&gt;

&lt;p&gt;Server: localhost:5432&lt;/p&gt;

&lt;p&gt;Database: usually postgres or your custom database name&lt;/p&gt;

&lt;p&gt;Data Connectivity Mode: Import&lt;/p&gt;

&lt;p&gt;Step 3: Enter Local Credentials&lt;/p&gt;

&lt;p&gt;Typical local PostgreSQL credentials:&lt;/p&gt;

&lt;p&gt;Username: postgres&lt;/p&gt;

&lt;p&gt;Password: your local password&lt;/p&gt;

&lt;p&gt;Step 4: Select Tables → Load&lt;/p&gt;

&lt;p&gt;Choose your tables in the Navigator → click Load → Power BI imports the data.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Connecting Power BI to PostgreSQL is straightforward once you know which values to enter. Aiven uses a cloud host and port, while your local setup uses localhost. After that, both connections behave exactly the same inside Power BI.&lt;/p&gt;

</description>
      <category>powerbi</category>
      <category>postgres</category>
      <category>analytics</category>
      <category>aiven</category>
    </item>
    <item>
      <title>Star vs Snowflake Schema: key difference, pros &amp; cons, and when to use each.</title>
      <dc:creator>cristopher Njuguna</dc:creator>
      <pubDate>Mon, 13 Oct 2025 05:55:07 +0000</pubDate>
      <link>https://forem.com/cristopher_njuguna_6381b1/star-vs-snowflake-schema-key-difference-pros-cons-and-when-to-use-each-5ckm</link>
      <guid>https://forem.com/cristopher_njuguna_6381b1/star-vs-snowflake-schema-key-difference-pros-cons-and-when-to-use-each-5ckm</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhknorr8effoe3kz880ob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhknorr8effoe3kz880ob.png" alt=" " width="800" height="559"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyi47v4u9l7742omav1u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyi47v4u9l7742omav1u.png" alt=" " width="800" height="601"&gt;&lt;/a&gt;The star schema and snowflake schema are fundamental dimensional modeling techniques used in data warehousing to optimize data retrieval for analytical queries. Both models comprise a central fact table and associated dimension tables, but they differ primarily in the normalization level of their dimension tables.&lt;br&gt;&lt;br&gt;
A star schema keeps dimensions in one big, denormalised table; a snowflake breaks that table into several normalised, related tables—trading query speed for storage efficiency and stricter data integrity.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;The star schema  *&lt;/em&gt;&lt;br&gt;
The star schema is characterized by a central fact table directly connected to multiple denormalized dimension tables.&lt;br&gt;&lt;br&gt;
This structure resembles a star, with the fact table at the center and dimension tables radiating outwards. In a star schema, each dimension is represented by a single table, which typically contains both descriptive attributes and a primary key for joining with the fact table.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;The advantages of the star schema;  *&lt;/em&gt;&lt;br&gt;
Its simplicity, which makes it easier to understand and implement.&lt;br&gt;&lt;br&gt;
Queries are often faster because they involve fewer joins compared to highly normalized schemas, typically joining the fact table with only one level of dimension tables.&lt;br&gt;&lt;br&gt;
This streamlined joining process enhances query performance for online analytical processing (OLAP) applications.&lt;br&gt;&lt;br&gt;
The straightforward design also makes it more intuitive for business users to navigate and analyze data. Furthermore, star schemas simplify the development of OLAP cubes and reporting tools.&lt;/p&gt;

&lt;p&gt;The primary &lt;strong&gt;disadvantage of the star schema&lt;/strong&gt; is data redundancy due to denormalization.&lt;br&gt;&lt;br&gt;
This redundancy can lead to increased storage requirements and potential data integrity issues if updates are not meticulously managed.&lt;br&gt;&lt;br&gt;
The lack of normalization can also make it more challenging to handle complex hierarchical relationships within dimensions.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;The snowflake schema *&lt;/em&gt; &lt;br&gt;
The snowflake schema is an extension of the star schema where dimension tables are normalized into multiple related tables. This means that a dimension table can have sub-dimension tables, forming a hierarchical structure. For example, a Product dimension might be normalized into a Product table and a separate Category table, with the Product table linking to the Category table. This normalization reduces data redundancy by storing commonly repeated attributes in separate tables.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;main advantage of the snowflake schema&lt;/strong&gt; is its reduced data redundancy and improved data integrity due to normalization.&lt;br&gt;&lt;br&gt;
This makes it more storage-efficient, especially for large datasets with many repeating attributes within dimensions.&lt;br&gt;&lt;br&gt;
It also simplifies data maintenance by ensuring that changes to a descriptive attribute only need to be made in one place.&lt;br&gt;&lt;br&gt;
The snowflake schema is particularly suitable for handling complex hierarchical dimensions, as the structure directly supports these relationships.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The snowflake schema's primary disadvantage&lt;/strong&gt; is increased query complexity and potentially slower query performance.&lt;br&gt;&lt;br&gt;
Because dimensions are normalized, queries often require more joins across multiple tables to retrieve the necessary data, which can increase execution time.&lt;br&gt;&lt;br&gt;
This complexity can also make it more difficult for business users to understand and interact with the data model.&lt;br&gt;&lt;br&gt;
The design and maintenance of snowflake schemas can be more intricate compared to star schemas.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The star schema is appropriate when:&lt;/strong&gt;  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Query performance is a top priority: Its denormalized structure and fewer joins typically lead to faster query execution, which is crucial for real-time analytical reporting and decision-making systems.
&lt;/li&gt;
&lt;li&gt;Simplicity and ease of understanding are valued: The straightforward design makes it easier for developers to build and for business users to query and interpret the data.
&lt;/li&gt;
&lt;li&gt;Dimensions are relatively stable and not deeply hierarchical: If dimension attributes do not change frequently and do not have complex multi-level hierarchies, the benefits of normalization in a snowflake schema are less pronounced.
&lt;/li&gt;
&lt;li&gt;Storage costs are not a primary concern: The potential increase in storage due to redundancy is acceptable if query speed is paramount.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;*&lt;em&gt;The snowflake schema is appropriate when:  *&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data integrity and storage efficiency are critical: Normalization minimizes redundancy and ensures data consistency, which is beneficial for very large datasets and environments with strict data quality requirements.
&lt;/li&gt;
&lt;li&gt;Dimensions have complex, multi-level hierarchies: The snowflake structure naturally accommodates these hierarchies by breaking down dimensions into sub-dimensions, offering a more organized and maintainable model for intricate relationships.
&lt;/li&gt;
&lt;li&gt;Data changes frequently within dimension attributes: Normalization reduces the impact of updates by isolating changes to specific, smaller tables, thereby simplifying maintenance.
&lt;/li&gt;
&lt;li&gt;Ad-hoc queries requiring detailed dimension exploration are common: While more complex, the normalized structure can support intricate analytical queries that delve deeply into dimensional attributes.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, the choice between star and snowflake schema involves a trade-off between query performance, data redundancy, storage efficiency, and design complexity.&lt;/p&gt;




&lt;p&gt;References&lt;br&gt;&lt;br&gt;
Iqbal, A. et al. 2020. Comparative study of star &amp;amp; snowflake schemas.&lt;br&gt;&lt;br&gt;
Levene, M. &amp;amp; Loizou, G. 2003. Why is the snowflake schema a good data-warehouse design?&lt;br&gt;&lt;br&gt;
Wijaya, A. et al. 2017. Community complaints snowflake implementation.&lt;/p&gt;

</description>
      <category>database</category>
      <category>datascience</category>
    </item>
  </channel>
</rss>
