Thursday, June 21, 2018

What is a Hadamard (Matrix) Product?

Hadamard Product is obtained by taking two matrices of the same dimension (n x n) to produce another matrix of the same dimension where each element of the new matrix is the product of corresponding elements of the two matrices.

Here is how the new Hadamard product is calculated.


Hadamard_0

Source: https://en.wikipedia.org/wiki/Hadamard_product_(matrices)

Using R you can calculate Hadamard product as shown.

Here are the two matrices.


Hadamard_1


Here is the Hadamard Product.


Hadamard_2

How do you use Direct Query in Power BI?

Launch Power BI (June 5, 2018 Version used for this post).

Click GetData and Choose SQL Server (you may choose a different source and the steps could be different).



The SQL Server database details page opens.


Fill in details about Server and Database (this is better if you want to get directly to data that you need from a database).

Choose the Direct Query Option and click Advanced Options.

Advanced options opens up for query insertion. Timeout is optional. This query is already filtered and should run fast. Insert the query as shown.

Click OK.

The data gets displayed. Load and it gets loaded as shown.



Wednesday, June 20, 2018

How do you find what practice datasets are available in R?

The R Dataset package has many datasets available and you can get a listing of this using the following command.



Here is a complete listing:


                Information on package ‘datasets’
Description:
Package:       datasets
Version:       3.3.2
Priority:      base
Title:         The R Datasets Package
Author:        R Core Team and contributors worldwide
Maintainer:    R Core Team
Description:   Base R datasets.
License:       Part of R 3.3.2
Built:         R 3.3.2; ; 2016-11-18 17:30:57 UTC; windows
Index:
AirPassengers           Monthly Airline Passenger Numbers 1949-1960
BJsales                 Sales Data with Leading Indicator
BOD                     Biochemical Oxygen Demand
CO2                     Carbon Dioxide Uptake in Grass Plants
ChickWeight             Weight versus age of chicks on different diets
DNase                   Elisa assay of DNase
EuStockMarkets          Daily Closing Prices of Major European Stock
                        Indices, 1991-1998
Formaldehyde            Determination of Formaldehyde
HairEyeColor            Hair and Eye Color of Statistics Students
Harman23.cor            Harman Example 2.3
Harman74.cor            Harman Example 7.4
Indometh                Pharmacokinetics of Indomethacin
InsectSprays            Effectiveness of Insect Sprays
JohnsonJohnson          Quarterly Earnings per Johnson & Johnson Share
LakeHuron               Level of Lake Huron 1875-1972
LifeCycleSavings        Intercountry Life-Cycle Savings Data
Loblolly                Growth of Loblolly pine trees
Nile                    Flow of the River Nile
Orange                  Growth of Orange Trees
OrchardSprays           Potency of Orchard Sprays
PlantGrowth             Results from an Experiment on Plant Growth
Puromycin               Reaction Velocity of an Enzymatic Reaction
Theoph                  Pharmacokinetics of Theophylline
Titanic                 Survival of passengers on the Titanic
ToothGrowth             The Effect of Vitamin C on Tooth Growth in
                        Guinea Pigs
UCBAdmissions           Student Admissions at UC Berkeley
UKDriverDeaths          Road Casualties in Great Britain 1969-84
UKLungDeaths            Monthly Deaths from Lung Diseases in the UK
UKgas                   UK Quarterly Gas Consumption
USAccDeaths             Accidental Deaths in the US 1973-1978
USArrests               Violent Crime Rates by US State
USJudgeRatings          Lawyers' Ratings of State Judges in the US
                        Superior Court
USPersonalExpenditure   Personal Expenditure Data
VADeaths                Death Rates in Virginia (1940)
WWWusage                Internet Usage per Minute
WorldPhones             The World's Telephones
ability.cov             Ability and Intelligence Tests
airmiles                Passenger Miles on Commercial US Airlines,
                        1937-1960
airquality              New York Air Quality Measurements
anscombe                Anscombe's Quartet of 'Identical' Simple Linear
                        Regressions
attenu                  The Joyner-Boore Attenuation Data
attitude                The Chatterjee-Price Attitude Data
austres                 Quarterly Time Series of the Number of
                        Australian Residents
beavers                 Body Temperature Series of Two Beavers
cars                    Speed and Stopping Distances of Cars
chickwts                Chicken Weights by Feed Type
co2                     Mauna Loa Atmospheric CO2 Concentration
crimtab                 Student's 3000 Criminals Data
datasets-package        The R Datasets Package
discoveries             Yearly Numbers of Important Discoveries
esoph                   Smoking, Alcohol and (O)esophageal Cancer
euro                    Conversion Rates of Euro Currencies
eurodist                Distances Between European Cities and Between
                        US Cities
faithful                Old Faithful Geyser Data
freeny                  Freeny's Revenue Data
infert                  Infertility after Spontaneous and Induced
                        Abortion
iris                    Edgar Anderson's Iris Data
islands                 Areas of the World's Major Landmasses
lh                      Luteinizing Hormone in Blood Samples
longley                 Longley's Economic Regression Data
lynx                    Annual Canadian Lynx trappings 1821-1934
morley                  Michelson Speed of Light Data
mtcars                  Motor Trend Car Road Tests
nhtemp                  Average Yearly Temperatures in New Haven
nottem                  Average Monthly Temperatures at Nottingham,
                        1920-1939
npk                     Classical N, P, K Factorial Experiment
occupationalStatus      Occupational Status of Fathers and their Sons
precip                  Annual Precipitation in US Cities
presidents              Quarterly Approval Ratings of US Presidents
pressure                Vapor Pressure of Mercury as a Function of
                        Temperature
quakes                  Locations of Earthquakes off Fiji
randu                   Random Numbers from Congruential Generator
                        RANDU
rivers                  Lengths of Major North American Rivers
rock                    Measurements on Petroleum Rock Samples
sleep                   Student's Sleep Data
stackloss               Brownlee's Stack Loss Plant Data
state                   US State Facts and Figures
sunspot.month           Monthly Sunspot Data, from 1749 to "Present"
sunspot.year            Yearly Sunspot Data, 1700-1988
sunspots                Monthly Sunspot Numbers, 1749-1983
swiss                   Swiss Fertility and Socioeconomic Indicators
                        (1888) Data
treering                Yearly Treering Data, -6000-1979
trees                   Girth, Height and Volume for Black Cherry Trees
uspop                   Populations Recorded by the US Census
volcano                 Topographic Information on Auckland's Maunga
                        Whau Volcano
warpbreaks              The Number of Breaks in Yarn during Weaving
women                   Average Heights and Weights for American Women

What are rbind and cbind for matrices in R?

rbind is short for row bind. rbind binds concatenated datapoints into matrix of compatible dimensions. cbind does the same taking column data.

Let us take the matrix defined in an earlier post:


MTX_0

In the above the matrix is:
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

This has three rows. Let us define the following:

row1=c(1,2,3)
row2=c(4,5,6)
row3=c(7,8,9)


We can define the same matrix using rbind as shown below.

mtx2=rbind(row1,row2,row3)
Then we can form the matrix using the rbind as shown here:


MTX_1
 
There is a slight difference in the displayed results. However, you can perform this to get the matrix displayed in the original format as shown here:


MTX_2

The cbind works the same way as rbind. Instead of binding row-wise you bind column-wise. Of course you need to use the columns correctly.


MTX_3

Monday, June 18, 2018

How do you store application data in a UWP project?

You can store and retrieve composite application data in a UWP project using the Windows Storage. Specifically you will be using the Windows.Storage.ApplicationDataCompositeValue sealed class shown in the Object Browser.


ObjBrowser.png

In this post a project is described where in a pair of values are stored in the Windows Storage's local folder and retrieved. The values are stored using a button click event of a button and retrieved into text boxes in the click event of a second button.
The MainPage.xaml for the project is as shown.



MainPageXaml.png

The code for storing and retrieving is shown in the MainPageXAMLCs. You first define the local settingss using the Windows.Storage.ApplicationDataContainer and the local folder. The composite values are set to the local settings.

The retrieving is by extracting the values in the local settings and reading it into a text box.
The result of running the application is shown here.


The code can be simplified as suggested by the hint shown here:




How do you represent a 3x3 matrix in R?

Matrix (matrices) is a data type in R used in many mathematical problems. A matrix has rows and columns. A 3 by 3 matrix has 3 rows and 3 columns. A matrix can have only columns, only rows and both columns and rows.

Here is an example of a 3x 3 matrix.

1, 2, 3
4, 5, 6
7, 8, 9

Let us create a matrix which is obtained by arranging the data, a vector of values 1,2,3,4,5,6,7,8,9 by assigning three elements each to a row and create 3 columns from the rows. This is how you do it in R.

Matrix_1

If you want to find the elements of this matrix, you provide the row and column where your element is found as shown.

You find 5 in the 2nd row and 2nd column and you find 6 in 2nd row and 3rd column.

Matrix_2
How do you arrange the same vectors column-wise arranged. You use the same definition as in the previous but omit, byrow attribute as shown.

Matrix_3

This is just the basic but you can do a whole lot more using R.

How do you use pivot() oprator in SQL Server?

Pivot() is a relational operator that changes a table-valued expression into another table. It rotates the table-valued expression by turning the unique values from one column  in the expression into multiple columns in the output and performs aggregations where they are required on any remaining column values that are wanted in the final output.

This is the syntax from MSDN for the PIVOT operator.
------------
SELECT
    [first pivoted column] AS
    [second pivoted column] AS
    ... 
    [last pivoted column] AS  
FROM 
    (
------------
Let us take an example frrom Northwind database. Here is a query that Selects lastname of employee and Unitprice from the Order Details table for UnitPrice greater than 50 and Quantity>10.


Pivot_0.png

You can see that Employees figure in many orders (have order details) with different UnitPrices. Now if you want to aggreegate the average Unitprice of articles sold by each employee (or a chosen number of employees) you need to do an aggregate.

For the above query we cannot directly use the PIVOT operator and we need to create an ALIAS as shown. PriceTable is the ALIAS for this query
----------
Select * FROM
(SELECT        Employees.LastName, [Order Details].UnitPrice
FROM            Employees INNER JOIN
                         Orders ON Employees.EmployeeID = Orders.EmployeeID INNER JOIN
                         [Order Details] ON Orders.OrderID = [Order Details].OrderID INNER JOIN
                         Products ON [Order Details].ProductID = Products.ProductID
WHERE [Order Details].UnitPrice >50.00 and [Order Details].Quantity>10)
as PriceTable
-----------
Nothing is changed as far as the Query return is concerned but we now have an  ALIAS.

Now we create a table which aggregates the average of UnitPrice for some named Employees using their LastName from the PriceTable as shown here.
----------
Select * FROM
(SELECT        Employees.LastName, [Order Details].UnitPrice
FROM            Employees INNER JOIN
                         Orders ON Employees.EmployeeID = Orders.EmployeeID INNER JOIN
                         [Order Details] ON Orders.OrderID = [Order Details].OrderID INNER JOIN
                         Products ON [Order Details].ProductID = Products.ProductID
WHERE [Order Details].UnitPrice >50.00 and [Order Details].Quantity>10)
as PriceTable
Pivot(Avg(UnitPrice) For LastName in ([King], [Davolio], [Fuller],[Peacock],[Suyama]))
as StudentPivot
----------------
When you run this the response is a table that has the values we were looking for: