by Joche Ojeda | Dec 5, 2023 | A.I
Brief History and Early Use Cases of Machine Learning
Machine learning began shaping in the mid-20th century, with Alan Turing’s 1950 paper “Computing Machinery and Intelligence” introducing the concept of machines learning like humans. This period marked the start of algorithms based on statistical methods.
The first documented attempts at machine learning focused on pattern recognition and basic learning algorithms. In the 1950s and 1960s, early models like the perceptron emerged, capable of simple learning tasks such as visual pattern differentiation.
Three Early Use Cases of Machine Learning:
- Checker-Playing Program: One of the earliest practical applications was in the late 1950s when Arthur Samuel developed a program that could play checkers, improving its performance over time by learning from each game.
- Speech Recognition: In the 1970s, Carnegie Mellon University developed “Harpy,” a speech recognition system that could comprehend approximately 1,000 words, showcasing early success in machine learning for speech recognition.
- Optical Character Recognition (OCR): Early OCR systems in the 1970s and 1980s used machine learning to recognize text and characters in images, a significant advancement for digital document processing and automation.
How Machine Learning Works
Data Collection: The process starts with the collection of diverse data.
Data Preparation: This data is cleaned and formatted for use in algorithms.
Choosing a Model: A model like decision trees or neural networks is chosen based on the problem.
Training the Model: The model is trained with a portion of the data to learn patterns.
Evaluation: The model is evaluated using a separate dataset to test its effectiveness.
Parameter Tuning: The model is adjusted to improve its performance.
Prediction or Decision Making: The trained model is then used for predictions or decision-making.
A Simple Example: Email Spam Detection
Let’s consider an email spam detection system as an example of machine learning in action:
- Data Collection: Emails are collected and labeled as “spam” or “not spam.”
- Data Preparation: Features such as word presence and email length are extracted.
- Choosing a Model: A decision tree or Naive Bayes classifier is selected.
- Training the Model: The model learns to associate features with spam or non-spam.
- Evaluation: The model’s accuracy is assessed on a different set of emails.
- Parameter Tuning: The model is fine-tuned for improved performance.
- Prediction: Finally, the model is used to identify spam in new emails.
Conclusion
Machine learning, from its theoretical inception to its contemporary applications, has undergone significant evolution. It encompasses the preparation of data, selection and training of a model, and the utilization of that model for prediction or decision-making. The example of email spam detection is just one of the many practical applications of machine learning that impact our daily lives.
by Joche Ojeda | Dec 4, 2023 | Database
Database Table Partitioning
Database table partitioning is a strategy used to divide a large database table into smaller, manageable segments, known as partitions, while maintaining the overall structure and functionality of the table. This technique is implemented in database management systems like Microsoft SQL Server (MSSQL) and PostgreSQL (Postgres).
What is Database Table Partitioning?
Database table partitioning involves breaking down a large table into smaller segments. Each partition contains a subset of the table’s data, based on specific criteria such as date ranges or geographic locations. This allows for more efficient data management and can significantly improve performance for certain types of queries.
Impact of Partitioning on CRUD Operations
- Create: Streamlines the insertion of new records to the appropriate partition, leading to faster insert operations.
- Read: Enhances query performance as searches can be limited to relevant partitions, accelerating read operations.
- Update: Makes updating data more efficient, but may add overhead if data moves across partitions.
- Delete: Simplifies and speeds up deletion, especially when dropping entire partitions.
Advantages of Database Table Partitioning
- Improved Performance: Particularly for read operations, partitioning can significantly enhance query speeds.
- Easier Data Management: Managing smaller partitions is more straightforward.
- Efficient Maintenance: Maintenance tasks can be conducted on individual partitions.
- Organized Data Structure: Helps in logically organizing data.
Disadvantages of Database Table Partitioning
- Increased Complexity: Adds complexity to database management.
- Resource Overhead: May require more disk space and memory.
- Uneven Performance Risks: Incorrect partition sizing or data distribution can lead to bottlenecks.
MSSQL Server: Example Scenario
In MSSQL, table partitioning involves partition functions and schemes. For example, a SalesData table can be partitioned by year, enhancing CRUD operation efficiency. Here’s an example of how you might partition a table in MSSQL:
-- Create a partition function
CREATE PARTITION FUNCTION SalesDataYearPF (int)
AS RANGE RIGHT FOR VALUES (2015, 2016, 2017, 2018, 2019, 2020);
-- Create a partition scheme
CREATE PARTITION SCHEME SalesDataYearPS
AS PARTITION SalesDataYearPF ALL TO ([PRIMARY]);
-- Create a partitioned table
CREATE TABLE SalesData
(
SalesID int IDENTITY(1,1) NOT NULL,
SalesYear int NOT NULL,
SalesAmount decimal(10,2) NOT NULL
) ON SalesDataYearPS (SalesYear);
PostgreSQL: Example Scenario
In Postgres, partitioning uses table inheritance. A rapidly growing Logs table can be partitioned monthly, optimizing CRUD operations. Here’s an example of how you might partition a table in PostgreSQL:
-- Create a master table
CREATE TABLE logs (
logdate DATE NOT NULL,
logevent TEXT
) PARTITION BY RANGE (logdate);
-- Create partitions
CREATE TABLE logs_y2020m01 PARTITION OF logs
FOR VALUES FROM ('2020-01-01') TO ('2020-02-01');
CREATE TABLE logs_y2020m02 PARTITION OF logs
FOR VALUES FROM ('2020-02-01') TO ('2020-03-01');
Conclusion
Database table partitioning in MSSQL and Postgres significantly affects CRUD operations. While offering benefits like improved query speed and streamlined data management, it also introduces complexities and demands careful planning. By understanding the advantages and disadvantages of partitioning, and by using the appropriate SQL commands for your specific database system, you can effectively implement this powerful tool in your data management strategy.
by Joche Ojeda | Dec 4, 2023 | A.I
Understanding AI, AGI, ML, and Language Models
Artificial Intelligence (AI) is a broad field in computer science concerned with building smart machines capable of performing tasks that typically require human intelligence. AI encompasses various subfields, including machine learning, natural language processing, robotics, and more. Its primary goal is to enable computers to perform tasks such as decision-making, problem-solving, perception, and understanding human language.
Machine Learning (ML), a subset of AI, focuses on developing algorithms and statistical models that enable computers to learn from and make predictions or decisions based on data. Unlike traditional programming, where humans explicitly code the behavior, machine learning allows systems to automatically learn and improve from experience. This learning process is driven by feeding algorithms large amounts of data and allowing them to adjust and improve their performance over time.
One of the most notable applications of ML is in the development of Language Models (LMs), which are algorithms designed to understand, interpret, and generate human language. These models are trained on vast datasets of text and can perform a range of language-related tasks, such as translation, summarization, and even generating human-like text. Language models like GPT (Generative Pretrained Transformer) are examples of how AI and ML converge to create sophisticated tools for natural language processing.
Artificial General Intelligence (AGI), on the other hand, represents a level of AI that is far more advanced and versatile. While current AI systems, including language models, are designed for specific tasks (referred to as narrow AI), AGI refers to a hypothetical AI that has the ability to understand, learn, and apply its intelligence broadly and flexibly, much like a human. AGI would possess the ability to reason, solve problems, comprehend complex ideas, learn from experience, and apply its knowledge to a wide range of domains, effectively demonstrating human-like cognitive abilities.
The relationship between AI, ML, AGI, and language models is one of a nested hierarchy. AI is the broadest category, under which ML is a crucial methodology. Language models are specific applications within ML, showcasing its capabilities in understanding and generating human language. AGI, while still theoretical, represents the potential future of AI where systems could perform a wide range of cognitive tasks across different domains, transcending the capabilities of current narrow AI systems.
In summary, AI is a vast field aimed at creating intelligent machines, with machine learning being a key component that focuses on data-driven learning and adaptation. Language models are a product of advancements in ML, designed to handle complex language tasks. AGI remains a goal for the future, representing a stage where AI could match or surpass human cognitive abilities across a broad spectrum of tasks and domains.
by Joche Ojeda | Oct 5, 2023 | Postgres, Search
Postgres Vector Search
Full-text search in PostgreSQL is implemented using a concept called “text search vectors” (or just “tsvector”). Let’s dive into how it works:
- Text Search Vectors (tsvector):
- A tsvector is a sorted list of distinct lexemes, which are words that have been normalized to merge different forms of the same word (e.g., “run” and “running”).
- PostgreSQL provides functions to convert plain text into tsvector format, which typically involves:
- Parsing the text into tokens.
- Converting tokens to lexemes.
- Removing stop words (common words like “and” or “the” that are typically ignored in searches).
- Example: The text “The quick brown fox” might be represented in tsvector as ‘brown’:3 ‘fox’:4 ‘quick’:2.
- Text Search Queries (tsquery):
- A tsquery represents a text search query, which includes lexemes and optional operators.
- Operators can be used to combine lexemes in different ways (e.g., AND, OR, NOT).
- Example: The query “quick & fox” would match any tsvector containing both “quick” and “fox”.
- Searching:
- PostgreSQL provides the @@ operator to search a tsvector column with a tsquery.
- Example: WHERE column @@ to_tsquery(‘english’, ‘quick & fox’).
- Ranking:
- Once you’ve found matches using the @@ operator, you often want to rank them by relevance.
- PostgreSQL provides the ts_rank function to rank results. It returns a number indicating how relevant a tsvector is to a tsquery.
- The ranking is based on various factors, including the frequency of lexemes and their proximity to each other in the text.
- Indexes:
- One of the significant advantages of tsvector is that you can create a GiST or GIN index on it.
- These indexes significantly speed up full-text search queries.
- GIN indexes, in particular, are optimized for tsvector and provide very fast lookups.
- Normalization and Configuration:
- PostgreSQL supports multiple configurations (e.g., “english”, “french”) that determine how text is tokenized and which stop words are used.
- This allows you to tailor your full-text search to specific languages or requirements.
- Highlighting and Snippets:
- In addition to just searching, PostgreSQL provides functions like ts_headline to return snippets of the original text with search terms highlighted.
In summary, PostgreSQL’s full-text search works by converting regular text into a normalized format (tsvector) that is optimized for searching. This combined with powerful query capabilities (tsquery) and indexing options makes it a robust solution for many full-text search needs.
Implementing vector search using E.F Core and Postgres SQL
here are the steps to implement vector search in your dot net project:
Step 1: Add the required nuget packages
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="7.0.11" />
Step 2: Implement a vector in your entities by implementing properties of type NpgsqlTsVector as shown below
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public NpgsqlTsVector SearchVector { get; set; }
}
Step 3: add a computed column in your DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.SearchVector)
.HasComputedColumnSql("to_tsvector('english', \"Blogs\".\"Title\")", stored: true);
}
in this case you are calculating the vector using the value of the title column of the blogs table, you can calculate the vector using a single column or a combination of columns
Now you are ready to use vector search in your queries, please check the example below
var searchTerm = "Jungle"; // Example search term
var searchVector = NpgsqlTsVector.Parse(searchTerm);
var blogs = context.Blogs
.Where(p => p.SearchVector.Matches(searchTerm))
.OrderByDescending(td => td.SearchVector.Rank(EF.Functions.ToTsQuery(searchTerm))).ToList();
In real world scenarios its better to create a vector by joining the values of several columns and weight them according to the relevance for your business case, you can check the test project I have created here : https://github.com/egarim/PostgresVectorSearch
and that’s it for this post, until next time, happy coding ))
by Joche Ojeda | Sep 23, 2023 | Search
Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. It is a technology that is part of the Elastic Stack, along with Logstash, and Kibana, collectively known as the ELK Stack. Elasticsearch is built on top of the open-source Lucene library and provides a multi-tenant capable full-text search engine. It’s designed to be scalable, resilient, and very fast, which makes it a popular choice for many different types of applications including:
1. Search Engines: Full-text search, partial text search, faceted search, and more.
2. Log and Event Data Analysis: Often used with Logstash and Kibana for searching, analyzing, and visualizing log data in real-time.xx
3. Real-time Analytics: Can be used for analyzing large volumes of real-time data efficiently.
4. Data Visualization: Often used with Kibana to visualize the data stored in Elasticsearch.
5. Autocomplete Features: Quick search suggestions.
6. Geospatial Search: Searching based on geographic location.
Key Features:
- Distributed and Scalable: Built to scale horizontally with easy distribution across multiple nodes.
- Schema-free JSON Documents: Uses JSON documents in order to store data, which makes it flexible and easy to use.
- RESTful API: Exposes REST APIs for CRUD operations, allowing interaction via standard HTTP methods.
- Real-time Indexing: As soon as a document is stored, it is available for search.
- Multi-tenancy: Supports multiple indices, and the indices can be divided into shards for better performance.
Basic Concepts:
- Node: A single running instance of Elasticsearch.
- Cluster: A collection of one or more nodes.
- Index: A collection of documents having somewhat similar characteristics.
- Shard: A subset of an index. Each shard is a self-contained index.
- Replica: A copy of a shard for failover and increased performance.
Elasticsearch is widely used in a variety of applications that require complex search features, large-scale logging, or real-time analytics. It’s often compared to other NoSQL databases like MongoDB and Apache Solr.
Choosing to use Elasticsearch depends on your specific needs, but there are several compelling reasons why it might be a good fit for your project:
Speed
- Fast Search: Built on top of Apache Lucene, Elasticsearch is designed for fast, real-time search operations.
- Real-time Indexing: New data is searchable almost immediately after it’s added.
Scalability
- Horizontal Scaling: You can easily add more nodes to your Elasticsearch cluster as your data and query volume grow.
- Distributed Nature: Automatically distributes data and query load across all the available nodes in the cluster.
Flexibility
- Schema-less: You can index JSON documents without a predefined schema.
- RESTful API: Easily interact with the search engine through RESTful APIs, using JSON over HTTP.
- Multiple Data Types: Supports text, numbers, dates, geospatial data, and more.
Robustness
- High Availability: Multiple copies of data (replicas) can be maintained to provide failover.
- Built-in Cluster Health and Monitoring: Tools like Kibana can provide insights into the operations and health of your Elasticsearch cluster.
Rich Query DSL
- Powerful Query Language: Elasticsearch provides a rich, flexible, query language (DSL) that can perform complex queries, filters, and aggregations.
- Relevancy Scoring: Sophisticated algorithms score each document for its relevance to a given search query.
Integration and Extensibility
- Part of the Elastic Stack: Integrates seamlessly with other components like Logstash for data ingestion and Kibana for data visualization.
- Extensible: Supports plugins to add additional features and capabilities.
Multi-Tenancy
- Support for Multiple Indices: You can have multiple indices (databases) and query them all at once if needed.
Use Cases
- Full-text Search: For applications like e-commerce product search, media catalog search, etc.
- Logging and Log Analysis: When combined with Logstash and Kibana, it’s a powerful tool for logging debug information, monitoring, and real-time analytics.
- Real-time Analytics: For business intelligence, performance metrics, and other real-time analytics needs.
- Data Visualization: Can be used with Kibana or other visualization tools to graphically represent your data.
Community and Ecosystem
- Strong Community: A large, active community contributes to its robust set of features.
- Comprehensive Documentation: Extensive online resources are available to help you get the most out of Elasticsearch.
However, it’s important to note that Elasticsearch may not be suitable for all types of projects. It can be resource-intensive, and the learning curve can be steep if you’re new to search and analytics engines. It might also be overkill for simple search needs or small datasets. Always consider your specific requirements and constraints when deciding whether to use Elasticsearch.