Posts

SQL Joins

Image
Joins are used to combine rows from two or more tables based on related columns between them. Joins allow you to retrieve data from multiple tables simultaneously, enabling you to create complex queries that fetch data from different sources. There are different types of joins in SQL, including: INNER JOIN Returns only the rows that have matching values in both tables based on the specified join condition. It discards non-matching rows from both tables. Example:           create table t1(x int); insert into t1 values(1); insert into t1 values(1); insert into t1 values(0); create table t2(y int); insert into t2 values(0); insert into t2 values(1); insert into t2 values(1);           select * from t1 inner join t2 on t1.x = t2.y Output: 2. LEFT JOIN (or) LEFT OUTER JOIN Returns all the rows from the left (or first) table and the matching rows from the rig...

SQL Joins

Image
Joins are used to combine rows from two or more tables based on related columns between them. Joins allow you to retrieve data from multiple tables simultaneously, enabling you to create complex queries that fetch data from different sources. There are different types of joins in SQL, including: INNER JOIN Returns only the rows that have matching values in both tables based on the specified join condition. It discards non-matching rows from both tables. Example:           create table t1(x int); insert into t1 values(1); insert into t1 values(1); insert into t1 values(0); create table t2(y int); insert into t2 values(0); insert into t2 values(1); insert into t2 values(1);           select * from t1 inner join t2 on t1.x = t2.y Output: 2. LEFT JOIN (or) LEFT OUTER JOIN Returns all the rows from the left (or first) table and the matching rows from the rig...

MLFlow Serving Custom Models in production environment

Image
  Serve Word2vec pretrained model using pyfunc flavor through docker. After completing development, if you want to serve your trained model, then it has to be either logged or saved via log_model()/save_model() function using one of the built-in flavors. MLflow does support multiple built-in flavors such as sklearn, keras etc and the flavor type is saved in the MLmodel file. Below is the example to save the model using sklearn flavor. mlflow . sklearn . log_model(sk_model, "sk_models") Flavors are the key concepts which makes MLflow more powerful. Flavors are a convention that deployment tools can use to understand the model, which makes it possible to write tools that work with models from any ML library without having to integrate each tool with each library. And the below command helps to expose the logged model as a service mlflow models serve -m ‘runs:/616e1e58d5814be58f613f13dfd8e9de/’ -h 0.0.0.0 -p 8000 And you can access the exposed model in the postman like this. htt...

Airflow basic custom web view

Folder Structure Plugin |_test_plugin    |_templates       |_test.html    test_plugin.py test_plugin.py from airflow.plugins_manager import AirflowPlugin from flask import Blueprint from flask_admin import BaseView, expose from flask_admin.base import MenuLink class TestView(BaseView):     @expose('/')     def test(self):              return self.render("test.html", content="Hello galaxy!") v = TestView(category="Test Plugin", name="Test View") blue_print_ = Blueprint("test_plugin",                         __name__,                         template_folder='templates') class AirflowTestPlugin(AirflowPlugin):     name = "MenuLinks"     # operators = []     flask_blueprints = [blue_print_]     # hooks = [] ...

Airflow dynamic dag creation and chain sequentially

DAG1: import airflow.utils.dates import airflow.utils.helpers from airflow import DAG from airflow.operators.dagrun_operator import TriggerDagRunOperator from airflow.operators.python_operator import PythonOperator from airflow.operators.sensors import ExternalTaskSensor from airflow.models.variable import Variable from airflow.utils.state import State from datetime import datetime, timedelta import dateutil.parser dag = DAG(     dag_id="dag1",     default_args={"owner": "airflow", "start_date": airflow.utils.dates.days_ago(2)},     schedule_interval="@once",      catchup=False ) def trigger_dag_with_context(context, dag_run_obj):        dag_run_obj.payload = {'job': context['params']['job']}     return dag_run_obj l1 = [] l2 = [] i = 0 for job in ["job1", "job2"]:      trigger = TriggerDagRunOperator(         task_id= job + "_dag",   ...

SQL Access WebApi with Basic Authentication

exec CALLWEBSERVICE 'username','password' CREATE PROCEDURE CALLWEBSERVICE(@Username NVARCHAR(50), @Password NVARCHAR(50)) AS     BEGIN     Declare @Object as Int;     DECLARE @authHeader VARCHAR(8000);        DECLARE @contentType VARCHAR(8000);     DECLARE @postData NVARCHAR(4000);     Declare @json as table(response nvarchar(max))     SET @postData = '{"method":"GetUserVehicles","userid":"1323","companyid":"0"}';        SET @contentType = 'application/json';     SET @authHeader = @Username + ':' + @Password;         SELECT         @authHeader = 'BASIC ' + CAST(N'' AS XML).value(               'xs:base64Binary(xs:hexBinary(sql:column("bin")))'             , 'VARCHAR(MAX)'...

Postgresql CStore Implementation

Cstore is a foreign data wrapper for postgres used for the analytic workloads . Cstore highly compress and stores the data in the columnar format. The following are the steps to install and implement cstore in the postgresql 9.6 in ubuntu. > sudo apt-get install \ git \ libpq-dev \ libprotobuf-c0-dev \ make \ postgresql-server-dev-9.6 \ protobuf-c-compiler > sudo apt-get install gcc > rm -rf cstore_fdw/ > git clone https://github.com/citusdata/cstore_fdw.git > cd cstore_fdw > make -j8 && \ sudo make install > sudo su - postgres > createdb sample; After the installation , in postgresql.conf specify shared_preload_libraries = 'cstore_fdw' > CREATE EXTENSION cstore_fdw; > CREATE SERVER cstore_server FOREIGN DATA WRAPPER cstore_fdw;

Automate failover in postgres replication using repmgr

repmgr.conf =========== failover=automatic pg_bindir='/usr/lib/postgresql/9.5/bin' promote_command='repmgr standby promote -f /etc/repmgr/repmgr.conf' follow_command='repmgr standby follow -f /etc/repmgr/repmgr.conf' note pg_bindir property otherwise you will pg_ctl unknown command error postgrsql.conf ============== shared_preload_libraries = 'repmgr_funcs' repmgrd helps to monitor the replication , with -d/--daemonize option you can run this as a deamon process Run the following command in standby server 1 & 2 alone , create a log file as postgres user otherwise you will get permission issue > sudo su - postgres > vi repmgr.log > repmgrd -f /etc/repmgr/repmgr.conf --verbose > repmgr.log 2>&1 repmgr=# select * from repmgr_cluster.repl_nodes order by id;  id |  type   | upstream_node_id | cluster | name  |                   conninfo             ...

Postgresql Master Slave Replication Setup using repmgr with EC2 instances

repmgr is an open source tool which helps to implment & monitor postgres replication and also helps in manageing the switchover and automatic failovers. Here we are going to implement one master and two standby servers streaming replication with repmgr. Master (127.0.0.1) Standby1 (127.0.0.2) Standby2 (127.0.0.3) Install Postgresql in all the 3 instances and enable passwordless connection between the instances using ssh. #Remove all the postgresql leftovers from the system sudo apt-get --purge remove postgresql-* sudo rm -Rf /etc/postgresql /var/lib/postgresql #Install Postgress & repmgr sudo apt-get update sudo apt-get install postgresql postgresql-contrib sudo apt-get install postgresql-9.5-repmgr sudo service postgresql stop Edit postgresql.conf and pg_hba.conf files in all the 3 instances with the respective following changes, so that in future standby act as master with minimal changes. > sudo su - postgres > cd /etc/postgresql/9.5/main/ ...

Machine Learning Foundations - Recommender System - Assignment Quiz

1) Which of the artists below have had the most unique users listening to their songs? a) Kanye West b) Foo Fighters c) Taylor Swift d) Lady GaGa 2) Which of the artists below is the most popular artist, the one with highest total listen_count, in the data set? a) Taylor Swift b) Kings of Leon c) Coldplay d) Lady GaGa 3) Which of the artists below is the least popular artist, the one with smallest total listen_count, in the data set? a) William Tabbert b) Velvet Underground & Nico c) Kanye West d) The Cool Kids

Machine Learning Foundations - Recommender System - Quiz

1) Recommending items based on global popularity can (check all that apply): a) provide personalization b) capture context (e.g., time of day) c) none of the above 2) Recommending items using a classification approach can (check all that apply): a) provide personalization b) capture context (e.g., time of day) c) none of the above 3) Recommending items using a simple count based co-occurrence matrix can (check all that apply): a) provide personalization b) capture context (e.g., time of day) c) none of the above 4) Recommending items using featurized matrix factorization can (check all that apply): a) provide personalization b) capture context (e.g., time of day) c) none of the above 5) Normalizing co-occurrence matrices is used primarily to account for: a) people who purchased many items b) items purchased by many people c) eliminating rare products d) none of the above 6) A store has 3 customers and 3 products. Below are the learned feature vectors for...

Machine Learning Foundation - Deep Learning - Programming Assignment

1) What’s the least common category in the training data? a) bird b) dog c) cat d) automobile 2) Of the images below, which is the nearest ‘cat’ labeled image in the training data to the the first image in the test data (image_test[0:1])? f 3) Of the images below, which is the nearest ‘dog’ labeled image in the training data to the the first image in the test data (image_test[0:1])? d 4) For the first image in the test data, in what range is the mean distance between this image and its 5 nearest neighbors that were labeled ‘cat’ in the training data? a) 33 to 35 b) 35 to 37 c) 37 to 39 d) 39 to 41 e) Above 41 5) For the first image in the test data, in what range is the mean distance between this image and its 5 nearest neighbors that were labeled ‘dog’ in the training data? a) 33 to 35 b) 35 to 37 c) 37 to 39 d) 39 to 41 e) Above 41 6) On average, is the first image in the test data closer to its 5 nearest neighbors in the ‘cat’ data or in the ...

Machine Learning Foundations - Deep Learning Summary - Quiz

1) Which of the following statements are true? (Check all that apply) a) Linear classifiers are never useful, because they cannot represent XOR. b) Linear classifiers are useful, because, with enough data, they can represent anything. c) Having good non-linear features can allow us to learn very accurate linear classifiers. d) none of the above 2) A simple linear classifier can represent which of the following functions? (Check all that apply) Hint: If you are stuck, see https://www.coursera.org/learn/ml-foundations/module/nqC1t/discussions/AAIUurrtEeWGphLhfbPAyQ a) x1 OR x2 OR NOT x3 b) x1 AND x2 AND NOT x3 c) x1 OR (x2 AND NOT x3) d) none of the above 3) Which of the the following neural networks can represent the following function? Select all that apply. (x1 AND x2) OR (NOT x1 AND NOT x2) Hint: If you are stuck, see https://www.coursera.org/learn/ml-foundations/module/nqC1t/discussions/AAIUurrtEeWGphLhfbPAyQ d 4) Which of the following statements is true? (Che...

Clustering and Similarity: Retrieving Documents Quiz Answers

1) Top word count words for Elton John (the, john, singer) (england, awards, musician) ( the, in, and) (his, the, since) (rock, artists, best) 2) Top TF-IDF words for Elton John (furnish,elton,billboard) (john,elton,fivedecade) (the,of,has) (awards,rock,john) (elton,john,singer) 3) The cosine distance between 'Elton John's and 'Victoria Beckham's articles (represented with TF-IDF) falls within which range? 0.1 to 0.29 0.3 to 0.49 0.5 to 0.69 0.7 to 0.89 0.9 to 1.0 4) The cosine distance between 'Elton John's and 'Paul McCartney's articles (represented with TF-IDF) falls within which range? 0.1 to 0.29 0.3 to 0.49 0.5 to 0.69 0.7 to 0.89 0.9 to 1 5) Who is closer to 'Elton John', 'Victoria Beckham' or 'Paul McCartney'? Victoria Beckham Paul McCartney 6) Who is the nearest neighbor to 'Elton John' using raw word counts? Billy Joel Cliff Richard Roger Daltrey George Bush 7) Who is ...

Machine Learning Foundation Clustering and Similarity Quiz Answers

1) A country, called Simpleland, has a language with a small vocabulary of just “the”, “on”, “and”, “go”, “round”, “bus”, and “wheels”. For a word count vector with indices ordered as the words appear above, what is the word count vector for a document that simply says “the wheels on the bus go round and round.” Please enter the vector of counts as follows: If the counts were ["the"=1, “on”=3, "and"=2, "go"=1, "round"=2, "bus"=1, "wheels"=1], enter 1321211. Enter answer here 2111211 2) In Simpleland, a reader is enjoying a document with a representation: [1 3 2 1 2 1 1]. Which of the following articles would you recommend to this reader next? a) [7 0 2 1 0 0 1] b) [1 7 0 0 2 0 1] c) [1 0 0 0 7 1 2] d) [0 2 0 0 7 1 1] 3) A corpus in Simpleland has 99 articles. If you pick one article and perform 1-nearest neighbor search to find the closest article to this query article, how many times must you compute the simil...

Machine Learning specialization Classification Quiz Answers

1) Out of the 11 words in  selected_words , which one is most used in the reviews in the dataset? awesome love hate bad great 2) Out of the 11 words in  selected_words , which one is least used in the reviews in the dataset? wow amazing terrible awful love 3)  Out of the 11 words in  selected_words , which one got the most positive weight in the   selected_words_model ? (Tip: when printing the list of coefficients, make sure to use print_rows(rows=12) to print ALL coefficients.) amazing awesome love fantastic terrible 4)  Out of the 11 words in  selected_words , which one got the most negative weight in the   selected_words_model ? (Tip: when printing the list of coefficients, make sure to use print_rows(rows=12) to print ALL coefficients.) horrible terrible awful hate love 5)  Which of the following ranges contains the accuracy of the  selected_words_model ...