<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5075070519872065209</id><updated>2025-09-13T08:14:23.124-07:00</updated><category term="Dynamo"/><category term="DynamoAI"/><category term="Linear Regression"/><category term="Machine Learning"/><category term="Intent"/><category term="AutoCAD"/><category term="DesignScript"/><category term="Gradient Descent"/><category term="Grapher"/><category term="List.Reduce"/><category term="ObjectARX"/><category term="Polynomial Regression"/><category term="Visual Programming"/><title type='text'>Intent</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://design-intent.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://design-intent.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sharad</name><uri>http://www.blogger.com/profile/06060088005375084545</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_m5OQ0gKYOoVPkPKubQYn9ApsFX0T1pb8W26t2T7n65DXqBZeqlHOlz_JaDEDKHaWMwj-3ednvfb1b7LypjrfWUImp6FhyZPEitqDE7yxRTbJRBv0YwmToxvhuugHA/s220/sharadjaiswal.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5075070519872065209.post-3980568368875327247</id><published>2017-09-27T09:26:00.002-07:00</published><updated>2017-09-28T21:06:38.568-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamo"/><category scheme="http://www.blogger.com/atom/ns#" term="DynamoAI"/><category scheme="http://www.blogger.com/atom/ns#" term="Gradient Descent"/><category scheme="http://www.blogger.com/atom/ns#" term="Linear Regression"/><category scheme="http://www.blogger.com/atom/ns#" term="List.Reduce"/><category scheme="http://www.blogger.com/atom/ns#" term="Machine Learning"/><title type='text'>Gradient Descent in Dynamo</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;custom-html-block&quot;&gt;
&lt;script src=&quot;https://latex.codecogs.com/latexit.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div dir=&quot;auto&quot;&gt;
Hello and Welcome back. So far we have been able to develop a simple regression model and solved it using linear algebra. In this article, I am going to explore another alternative approach called Gradient Descent. It’s an iterative method to optimize a cost function by finding a local/global minimum by taking steps proportional to the gradient of the cost function. To apply the Gradient Descent algorithm we need to first define our cost function for our machine learning problem.&lt;/div&gt;
&lt;div dir=&quot;auto&quot;&gt;
We have a set of features for which the actual output is given we want to develop a prediction model to predict the output for the given set of features. We can represent our feature vectors for the complete training set as &lt;strong&gt;matrix X&lt;/strong&gt;, the corresponding output values as &lt;strong&gt;vector y&lt;/strong&gt;, and the predicted values as vector &lt;strong lang=&quot;latex&quot;&gt;\hat{y}&lt;/strong&gt;. So our prediction model is &lt;strong lang=&quot;latex&quot;&gt;\hat{y} = X*a&lt;/strong&gt; and the error vector for the training set in the model is &lt;strong lang=&quot;latex&quot;&gt;err = \hat{y} - y&lt;/strong&gt;. We would like to choose a cost function such that the overall error of the model could be minimized. Thus we choose&amp;nbsp;the mean square error as our cost function. The dot product of the error vector with itself gives the sum of the square of errors and dividing it by the number of training examples gives us the mean square error. Here is the Dynamo graph to compute mean square error for a given training set.&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkvMDHTCyLMJ1xwCEL4TVzaTbxEDQAAiyVTMvAFXFc7iY3eHC7yKud8MlYZxVoE1DP2Cj818kaJhytT-m3pF_O6GNVMKAePwL7B9RV8hrcOzG_MQJBz2tMC7OPeMezKxLE-lQDqm_IG_Tx/s9999/CostFunction.jpg&quot; style=&quot;max-width: 100%;&quot; width=&quot;2052&quot; /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
Since the cost function we have chosen is quadratic (square of error), it has one global minima and we can find it by traversing along the gradient. Now the gradient of the cost function is obtained by finding the partial derivation of the cost function with the coefficient vector and it can be simplified as &lt;strong lang=&quot;latex&quot;&gt;G = \alpha (\hat{y} - y) * X^{T}&lt;/strong&gt;, the numeric constant &lt;b&gt;alpha&lt;/b&gt; is the learning rate. Using this gradient we can now update the value of coefficient vector ‘a’ as &lt;strong lang=&quot;latex&quot;&gt;a := a - \alpha (\hat{y} - y) * X^{T} &lt;/strong&gt;The Dynamo graph to update any given coefficient vector ‘a’ is shown below. Note that one extra iterator input node, when we wrap it into a custom node &lt;strong&gt;LinearRegressionGradientDescent&lt;/strong&gt;. This iterator is used to run an iteration in Dynamo using List.Reduce node.&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQVmYqf8zB60Sgkg6zrtRTgw9Y_qMUNO3LxSIE26SZ9dfgTkWZf3gHA9-GvF1QpCmh2lcy2EFSBCTCHagioXazuG7Wv0fNYpSVLMu7pF5h6-U0U5qyNiR5y8e1YU2UZmNXITYZrAa2bntY/s9999/GradientDescentUpdate.jpg&quot; style=&quot;max-width: 100%;&quot; width=&quot;2588&quot; /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
We now assemble these pieces together to evaluate our regression model. We need to initialize the coefficient vector with ones and create a list of repeated items with this coefficient vector and the amount of iteration we want to do for evaluation. This list is then fed to the &lt;b&gt;List.Reduce&lt;/b&gt; node with initial/seed coefficient vector and the custom node function as the reducer. This is a unique way to run imperative loop and update the value using nodes in Dynamo. We could have used imperative code block and run a loop to achieve the same, but I prefer to use map, reduce a concept from functional programming paradigm in Dynamo. To get a better result and faster conversion we should also normalize our feature vectors. The complete Dynamo graph is shown below. We can again use test and validation set to find the optimum alpha for our dataset.&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvX3nSymYCdSv0YOpOTn9kksDl3EWMjbj9T2253enXY-wHVSofov7jNUCh0dOZ_7JB6E5M0sC2zmZvRsSjJSRnbEo1e3S97W9k6QEhkYr2L6HAlR85rrXS-83_VBvwFcOHxgtQn4Bkc7FV/s9999/LinearRegressionGD.jpg&quot; style=&quot;max-width: 100%;&quot; width=&quot;2594&quot; /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
&lt;strong&gt;Conclusion:&lt;/strong&gt; In this article, I have shown how to define the cost function and implement gradient descent in Dynamo. In fact, the gradient descent concept can be used to optimize any other cost function for which we know how to compute the gradient. I have also used List.Reduce higher-order function node to do the iterations. I hope you liked this post, please do send your feedback and comments.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://design-intent.blogspot.com/feeds/3980568368875327247/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://design-intent.blogspot.com/2017/09/gradient-descent-in-dynamo.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/3980568368875327247'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/3980568368875327247'/><link rel='alternate' type='text/html' href='http://design-intent.blogspot.com/2017/09/gradient-descent-in-dynamo.html' title='Gradient Descent in Dynamo'/><author><name>Sharad</name><uri>http://www.blogger.com/profile/06060088005375084545</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_m5OQ0gKYOoVPkPKubQYn9ApsFX0T1pb8W26t2T7n65DXqBZeqlHOlz_JaDEDKHaWMwj-3ednvfb1b7LypjrfWUImp6FhyZPEitqDE7yxRTbJRBv0YwmToxvhuugHA/s220/sharadjaiswal.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkvMDHTCyLMJ1xwCEL4TVzaTbxEDQAAiyVTMvAFXFc7iY3eHC7yKud8MlYZxVoE1DP2Cj818kaJhytT-m3pF_O6GNVMKAePwL7B9RV8hrcOzG_MQJBz2tMC7OPeMezKxLE-lQDqm_IG_Tx/s72-c/CostFunction.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5075070519872065209.post-4319911036120027831</id><published>2017-09-13T00:24:00.000-07:00</published><updated>2017-09-18T07:54:41.137-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DynamoAI"/><category scheme="http://www.blogger.com/atom/ns#" term="Linear Regression"/><category scheme="http://www.blogger.com/atom/ns#" term="Machine Learning"/><category scheme="http://www.blogger.com/atom/ns#" term="Polynomial Regression"/><title type='text'>Polynomial regression and model comparison</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;script src=&quot;https://latex.codecogs.com/latexit.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Welcome back, in my previous post I described how we can perform linear regression using normal equation in Dynamo and I left with a question &quot;what if the input and output are not linearly dependent?” Let’s say we have a hypothesis that the housing price doesn’t depend on floor area and number of rooms linearly but it has a following relationship as &lt;strong lang=&quot;latex&quot;&gt;y = a_{0} * x_{0} + a_{1} * x_{1} + a_{2} * x_{2} + a_{3} * x_{1} * x_{2} + a_{4} * x_{1}^2 + a_{5} * x_{2}^2 \:&lt;/strong&gt; where &lt;strong lang=&quot;latex&quot;&gt;x_{0} = 1, \: x_{1}&lt;/strong&gt; is floor area and &lt;strong lang=&quot;latex&quot;&gt;x_{2}&lt;/strong&gt; is number of rooms. Then we can introduce few new parameters &lt;strong lang=&quot;latex&quot;&gt;x_{3} = x_{1} * x_{2}, \: x_{4} = x_{1}^2, \: x_{5} = x_{2}^2&lt;/strong&gt; and then perform the linear regression to find the coefficient matrix.&lt;br /&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
The Dynamo graph to setup the feature vector looks as follows. Once the feature vector is setup rest all is same as previous linear regression example.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgT2mCXURqAWcdG-fhVukAEzM8iM576pEE5f6elFxX3BP_DDTT3SSIX_l1PhHlt5LxEauwfIVdI3RJU6j79LzQb5fMIf_Hd1CswXHPl0px-micaNjVcPyAUBZ-lfdS-5ykNYsuJWIveieC/s9999/Polynomial-Feature-Vector-X%252C-y.png&quot; style=&quot;max-width: 100%;&quot; /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
Note that the price prediction for a given floor area and rooms we need to again construct the same feature vector. The price prediction based on the complete dataset and this new hypothesis for a &lt;strong&gt;3 room house of area 1650 sq. ft.&lt;/strong&gt; is &lt;strong&gt;307,176&lt;/strong&gt; whereas the price predicted based on previous linear regression model is &lt;strong&gt;293,081&lt;/strong&gt;. Now the sellers can argue that the polynomial model is good because it is fetching them the higher price and similarly buyers can argue that linear regression model is better because it gives a lower price for his desired house. To identify the best model we need to build a test set and use that test set to figure out which model gives less error. We can use 80% of the dataset to evaluate the regression model and rest 20% we can use to test both the models. While splitting the dataset into a training set and test set we must shuffle the data before the split just to avoid any bias present in the dataset.&lt;br /&gt;
&lt;br /&gt;
The Dynamo graph to split the dataset into training and testing is shown below. We shuffle the data row-wise and then extract sub matrix based on the percentage of the dataset to be used for training.&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaVfVOTib55Vm5taW9FKgHHBZ4jNrf-6Mpjno3NW5P80AAnpfmGncGq2vBn9M9H-d13tZWL6LD4QQxiijAGYADGNAweXCYrwXHKR613z5L9QdpoY5OHhcWXCtXZFCmcEToCcvppXId179W/s9999/Test-and-Training-Matrix.png&quot; style=&quot;max-width: 100%;&quot; /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
Now we can solve the linear regression model using &lt;strong&gt;&lt;em&gt;Matrix.SolveLinearEquation&lt;/em&gt;&lt;/strong&gt; node, which uses the same technique to evaluate the regression model as discussed in the previous blog post on Linear Regression. Once we have computed the two models (linear and polynomial) we can now run this model with the test set to get the error value. The total error for a model is the sum of the absolute error for each of the predictions as compared to the actual price data. The comparison of the two errors shows that the linear regression model performs better on the test set hence we should choose linear regression as compared to the polynomial regression hypothesis we discussed here. The error computation graph in Dynamo is shown below.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcEnSj653x5xHjAD4KGbGdeus_HoG3PL2pyJsyFQd_nud9naNSz-R7xfytga1UZER8Nfzx2Hf-Grpt4kYZcLvk9SrKpzR8Ae81hA15GpJJJntFrFVzO6j30hKn-RtSnEvqfR2Zn7Q3PzHG/s9999/error-computation.png&quot; style=&quot;max-width: 100%;&quot; /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
The final graph to run both the models and error comparison is shown below and it is shared at &lt;a href=&quot;https://github.com/sharadkjaiswal/Dynamo.AI/blob/master/DynamoPackage/DynamoAI/extra/Polynomial%20regression%20and%20model%20comparison.dyn&quot; target=&quot;_blank&quot;&gt;Github&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpJWoSakeUHlU9liSn4E0OdndHXrUdKn_I7f6dz81O8hHG9lFQ02lSynly8evOCjph6Jft2t9YlYXcGADy46071GSfufmk1Lv61QTOrzKULpl7OHS9je_wRqcKQ6gmFe7wRPu0833Go-p-/s9999/Polynomial-regression-graph.png&quot; style=&quot;max-width: 100%;&quot; /&gt;&lt;/div&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;
&lt;strong&gt;Conclusion:&lt;/strong&gt; In this article, I have demonstrated how we can model polynomial equation based hypothesis and solve for the prediction model using linear regression and compare the two hypothesis based on the error value on a test set which is different than the training set. As always, please do share your feedback and suggestions on this article as well as the DynamoAI package on this page or on Github.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://design-intent.blogspot.com/feeds/4319911036120027831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://design-intent.blogspot.com/2017/09/polynomial-regression-and-model.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/4319911036120027831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/4319911036120027831'/><link rel='alternate' type='text/html' href='http://design-intent.blogspot.com/2017/09/polynomial-regression-and-model.html' title='Polynomial regression and model comparison'/><author><name>Sharad</name><uri>http://www.blogger.com/profile/06060088005375084545</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_m5OQ0gKYOoVPkPKubQYn9ApsFX0T1pb8W26t2T7n65DXqBZeqlHOlz_JaDEDKHaWMwj-3ednvfb1b7LypjrfWUImp6FhyZPEitqDE7yxRTbJRBv0YwmToxvhuugHA/s220/sharadjaiswal.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgT2mCXURqAWcdG-fhVukAEzM8iM576pEE5f6elFxX3BP_DDTT3SSIX_l1PhHlt5LxEauwfIVdI3RJU6j79LzQb5fMIf_Hd1CswXHPl0px-micaNjVcPyAUBZ-lfdS-5ykNYsuJWIveieC/s72-c/Polynomial-Feature-Vector-X%252C-y.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5075070519872065209.post-8543415687197457241</id><published>2017-09-03T20:05:00.003-07:00</published><updated>2017-09-09T21:52:26.755-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamo"/><category scheme="http://www.blogger.com/atom/ns#" term="DynamoAI"/><category scheme="http://www.blogger.com/atom/ns#" term="Grapher"/><category scheme="http://www.blogger.com/atom/ns#" term="Linear Regression"/><category scheme="http://www.blogger.com/atom/ns#" term="Machine Learning"/><title type='text'>Linear Regression in Dynamo</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;script src=&quot;https://latex.codecogs.com/latexit.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;auto&quot;&gt;
Today we are collecting more data than ever but making sense out of the data is very challenging. Machine learning helps us analyze the data and build an analytical model to help us with future prediction. To create a machine learning prediction model we usually develop a hypothesis based on our observation of the collected data and fine tune the model to reduce the cost function by training the model. One of the very simple hypothesis we can develop by assuming a linear relationship between input and output parameters.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
Suppose we have data on housing price and we assume that housing prices are linearly related to the floor area of the house then we can use linear regression to predict the price of a house with specific floor area. One of the most important steps towards building the hypothesis is being able to visualize the data and understand the trend. So let first draw a scatter plot of our data as shown in the figure below. I am using Grapher package to draw the scatter plot. &lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxqBPV7D9a797i0TE6s35oehAKsXDlMyKf9Jwro9lmfoHP763y8XSq0kSYJXqX7jLZRN6eWYJH8HB4pjlAqgmhZA1KCkk8yEDR9Qp26o5_aSj2WnCeI1piSaS49tyBxhwWIBCeZhbvid52/s9999/scatter-plot.png&quot; style=&quot;max-width: 100%;&quot; width=&quot;2650&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div dir=&quot;ltr&quot;&gt;
The scatter plot shows that the relationship between floor area and price is not quite linear but it can be approximated to a linear curve. In another words the linear regression is a curve fitting problem. Let’s say that the desired curve is &lt;strong&gt;y = mx + c&lt;/strong&gt; &lt;br /&gt;
Hence to build our model we need to find out the values of &lt;b&gt;m&lt;/b&gt; and &lt;b&gt;c&lt;/b&gt;. We can use linear algebra and represent this equation as &lt;strong lang=&quot;latex&quot;&gt;y = A*X^{T}&lt;/strong&gt; where A represents a matrix of coefficients i.e. &lt;strong lang=&quot;latex&quot;&gt;A = \begin{bmatrix}c &amp;amp; m\end{bmatrix} and \:X = \begin{bmatrix}1 &amp;amp; x\end{bmatrix}&lt;/strong&gt; and &lt;strong lang=&quot;latex&quot;&gt;X^{T}&lt;/strong&gt; is transpose of X. Now for a give y we can build our model to find the coefficient vector A, which can be obtained as &lt;strong lang=&quot;latex&quot;&gt; A = (X^{T} * X)^{-1} * X^{T} * y&lt;/strong&gt;.&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;
Let’s say that we have a hypothesis that says the house price not just depend on floor area but it also depends on number of bedrooms. The parameters such as floor area or number of bedrooms are called features. To generalize we can express y as &lt;strong lang=&quot;latex&quot;&gt;y = a_{0} + a_{1}*x_{1} + a_{2}*x_{2} + a_{3}*x_{3}...&lt;/strong&gt; where the feature vector X can be represented as &lt;strong lang=&quot;latex&quot;&gt;X = \begin{bmatrix} 1 &amp;amp; x_{1} &amp;amp; x_{2} &amp;amp; x_{3} &amp;amp;...\end{bmatrix}&lt;/strong&gt; and the coefficient vector A can be represented as &lt;strong lang=&quot;latex&quot;&gt;A = \begin{bmatrix} a_{0} &amp;amp; a_{1} &amp;amp; a_{2} &amp;amp; a_{3} &amp;amp;...\end{bmatrix}&lt;/strong&gt;. Suppose we have ‘&lt;b&gt;m&lt;/b&gt;’ samples of data to fit the curve and &lt;b&gt;y&lt;/b&gt; depends on &#39;&lt;b&gt;n&lt;/b&gt;&#39; features, then the size of &lt;b&gt;A&lt;/b&gt; is &#39;&lt;strong&gt;1 x (n+1)&lt;/strong&gt;&#39; whereas the size of &lt;b&gt;X&lt;/b&gt; is ‘&lt;strong&gt;m x (n+1)&lt;/strong&gt;’ where the first column has all 1. To fit the optimal curve we would like to find out the coefficient vector &lt;b&gt;A&lt;/b&gt; so that we can predict the value of &lt;b&gt;y&lt;/b&gt; for any new feature vector &lt;b&gt;X&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
So now let&#39;s look at the Dynamo graph compute the &lt;strong&gt;coefficient vector A&lt;/strong&gt;.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfnB9v0jCvwaFQuRy3mwgprdZLtm7cvM3Bst2MN1THr6p2HezO8DcD-eozCwOgafJm8tOdfhEZnS8sVvbIQEYhm6O8O9KEI-OLyxTWo7R6ZXbbwxLRc4JsJJtHuA3cxnN0CDX7cl84dZdy/s9999/Normal-Equation.png&quot; style=&quot;max-width: 100%;&quot; width=&quot;2158&quot; /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
To predict new price based on the input feature vector we need to take a dot product with the coefficient vector. This provides a simple Linear regression model which works well as long as the inverse of &lt;strong lang=&quot;latex&quot;&gt;(X^{T} * X)&lt;/strong&gt; can be computed efficiently. For a very large number of features, computing inverse could be very expensive also if any two or more features are inter-dependent then also inverse can’t be computed because of singularity. If we take care of these two aspects then it’s pretty effective model.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
What if the linearity is not a good hypothesis and we think that there is a polynomial relationship between input and output. We can still use the same computational model by making the polynomial terms as a new feature in the model. &lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;em&gt;Note: The Dynamo Graph demonstrated here, uses DynamoAI package and is shared at &lt;a href=&quot;https://github.com/sharadkjaiswal/Dynamo.AI/blob/master/DynamoPackage/DynamoAI/extra/Linear%20Regression%20-%20Normal%20Equation.dyn&quot; target=&quot;_blank&quot; title=&quot;Linear Regression - Normal Equation.dyn&quot;&gt;GitHub&lt;/a&gt;. I hope you would find it useful and please do send me your feedback on GitHub or on this page.&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://design-intent.blogspot.com/feeds/8543415687197457241/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://design-intent.blogspot.com/2017/09/linear-regression-in-dynamo.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/8543415687197457241'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/8543415687197457241'/><link rel='alternate' type='text/html' href='http://design-intent.blogspot.com/2017/09/linear-regression-in-dynamo.html' title='Linear Regression in Dynamo'/><author><name>Sharad</name><uri>http://www.blogger.com/profile/06060088005375084545</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_m5OQ0gKYOoVPkPKubQYn9ApsFX0T1pb8W26t2T7n65DXqBZeqlHOlz_JaDEDKHaWMwj-3ednvfb1b7LypjrfWUImp6FhyZPEitqDE7yxRTbJRBv0YwmToxvhuugHA/s220/sharadjaiswal.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxqBPV7D9a797i0TE6s35oehAKsXDlMyKf9Jwro9lmfoHP763y8XSq0kSYJXqX7jLZRN6eWYJH8HB4pjlAqgmhZA1KCkk8yEDR9Qp26o5_aSj2WnCeI1piSaS49tyBxhwWIBCeZhbvid52/s72-c/scatter-plot.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5075070519872065209.post-446382347901581273</id><published>2017-08-16T16:40:00.001-07:00</published><updated>2017-08-16T16:56:59.436-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DesignScript"/><category scheme="http://www.blogger.com/atom/ns#" term="Dynamo"/><category scheme="http://www.blogger.com/atom/ns#" term="Visual Programming"/><title type='text'>DesignScript</title><content type='html'>&lt;p dir=&quot;auto&quot;&gt;It’s been a really long time that I wrote about something. For past many years, I have been busy working on a domain specific language called &lt;strong&gt;DesignScript&lt;/strong&gt; and it&#39;s visual programming interface &lt;strong&gt;Dynamo&lt;/strong&gt;. I am now moving to a world of reality-capture and I’ll miss working on this technology. I find this technology very useful and every now and then I’ll keep coming to it for some geometric and programming experiment. Recently I am learning machine learning and wondering if I can use Dynamo to explain some of the concepts of machine learning. I’ll explore it further but in the mean time let me explain what is DesignScript and Dynamo.&lt;/p&gt;&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;DesignScript&lt;/strong&gt; is a scripting language intended to provide programming capability to designers. Designers can now describe their design-intent effectively using this language. Unlike other procedural languages, it provides two great features such as associativity and replication. The associativity in expressions helps it preserve the intent whereas the replication makes it agnostic to the dimensionality of the data. One can establish relationships easily by defining expressions where left-hand side depends on the parameters used in the right-hand side of the expression. e.g.&lt;br&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;em&gt;a = 5;&lt;br&gt;b = 2 * a; //‘b’ is twice the value of ‘a’, thus ‘b’ depends on ‘a&#39;;&lt;/em&gt;&lt;/blockquote&gt;&lt;p dir=&quot;ltr&quot;&gt;The relationship defined by such expressions are associative in nature, i.e. whenever the dependent parameter(s)/variable(s) is(are) modified the left-hand side variable is automatically updated. And the evaluation of this expression is agnostic to the dimensionality of the parameters used in the expression. The parameter ‘a’ in above expression is a singleton value, but later in the program, if we change the value of a to a collection then the expression gets re-evaluated and ‘b’ is updated to a collection, which means we don’t need to run a for-loop to evaluate the expression.&lt;br&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;em&gt;a= 1..5; //‘a’ is a collection of integers from 1 to 5 and now b = {2, 4, 6, 8, 10} due to associativity of ‘b = 2 * a;’ expression.&lt;/em&gt;&lt;/blockquote&gt;&lt;p dir=&quot;ltr&quot;&gt;There are few other features such as zero touch library import allows users to import any .NET library into DesignScript without modifying the library and the public class and methods defined in the library can be called seamlessly from DesignScript.&lt;/p&gt;&lt;p dir=&quot;ltr&quot;&gt;&lt;strong&gt;Dynamo&lt;/strong&gt; provides a visual programming interface to DesignScript users. They can define their programming logic as a data flow program. Dynamo also support python scripts and users can creates a reusable script using custom nodes. They can also publish their workflow for others to consume it in their design using &lt;a href=&quot;http://dynamopackages.com&quot; target=&quot;_blank&quot;&gt;package manager&lt;/a&gt;. More about DesignScript and Dynamo can be learned from &lt;a href=&quot;http://dynamoprimer.com/&quot; target=&quot;_blank&quot;&gt;http://dynamoprimer.com/&lt;/a&gt; or &lt;a href=&quot;http://dynamobim.org/&quot; target=&quot;_blank&quot;&gt;http://dynamobim.org&lt;/a&gt;  &lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img src=&quot;http://dynamoprimer.com/en/07_Code-Block/images/7-2/nodeToCode.jpg&quot; width=&quot;1200&quot; style=&quot;max-width: 100%;&quot;&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/446382347901581273'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/446382347901581273'/><link rel='alternate' type='text/html' href='http://design-intent.blogspot.com/2017/08/designscript.html' title='DesignScript'/><author><name>Sharad</name><uri>http://www.blogger.com/profile/06060088005375084545</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_m5OQ0gKYOoVPkPKubQYn9ApsFX0T1pb8W26t2T7n65DXqBZeqlHOlz_JaDEDKHaWMwj-3ednvfb1b7LypjrfWUImp6FhyZPEitqDE7yxRTbJRBv0YwmToxvhuugHA/s220/sharadjaiswal.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-5075070519872065209.post-3179847956563961431</id><published>2011-01-29T17:41:00.000-08:00</published><updated>2011-01-29T17:42:15.793-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AutoCAD"/><category scheme="http://www.blogger.com/atom/ns#" term="Intent"/><category scheme="http://www.blogger.com/atom/ns#" term="ObjectARX"/><title type='text'>Associativity in AutoCAD</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span xmlns=&quot;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
CAD applications often implement features or entities which are associative in nature, for example creating an extrude surface in AutoCAD 2011, by extruding a profile creates associative extrude surface. When you modify the profile the extruded surface will update itself to follow the profile.&lt;br /&gt;
AutoCAD developers now can develop fascinating applications using new associative framework. There are APIs available in C++ as well as .NET to implement associative features in AutoCAD. You can download the latest &lt;a href=&quot;http://usa.autodesk.com/adsk/servlet/item?siteID=123112&amp;amp;id=785550&quot;&gt;ObjectARX&lt;/a&gt; to make use of associative framework.&lt;br /&gt;
The associative framework in AutoCAD helps us to maintain relationship between objects. Relations are represented as hierarchical Networks of Objects, Actions and Dependencies between them. The core building blocks are:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Action - Defines behavior or intelligence of application or feature.&lt;/li&gt;
&lt;li&gt;Dependency- Maintains associativity/ relations between objects and notifies changes on dependent objects.&lt;/li&gt;
&lt;li&gt;Network- The associative model&lt;/li&gt;
&lt;li&gt;Object- A regular AutoCAD AcDbObject/AcDbEntity being controlled by an associative action.&lt;/li&gt;
&lt;/ul&gt;Now let&#39;s code a simple action where we want to establish a relationship between a line and a circle, where length of line controls diameter of the circle. For this we need to implement an Action class derived from AcDbAssocActionBody, which provides the evaluation logic.&lt;br /&gt;
&lt;pre class=&quot;brush: cpp&quot;&gt;//A custom action derived from AcDbAssocActionBody, establishes relationship
//between a line and a circle. Length of the line drives diameter of the circle.
//
class MyActionBody : public AcDbAssocActionBody
{
public:
ACRX_DECLARE_MEMBERS(MyActionBody);

virtual void evaluateOverride();

static Acad::ErrorStatus createInstance(AcDbObjectId lineId, AcDbObjectId circleId, AcDbObjectId&amp;amp; actionBodyId);
};
&lt;/pre&gt;&lt;h3&gt;Establishing relationship between objects:&lt;/h3&gt;This action depends on both line and circle, so we need to attach dependencies on both line and circle. Since we are going to read the length of line and write/update diameter of the circle, so we attach read dependency on line and write dependency on circle. These dependencies are owned by our action and they will ensure that line is driving entity and circle is the driven entity.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: cpp&quot;&gt;Acad::ErrorStatus MyActionBody::createInstance(AcDbObjectId lineId, 
    AcDbObjectId circleId, AcDbObjectId&amp;amp; actionBodyId)
{
    //Get the space to which network/action needs to be added.
    AcDbObjectId ownerSpaceId;
    {
        AcDbObjectPointer&amp;lt; AcDbEntity &amp;gt; pEntity(lineId, AcDb::kForRead);
        if(!eOkVerify(pEntity.openStatus()))
            return pEntity.openStatus();
        ownerSpaceId = pEntity-&amp;gt;ownerId();
    }

    //create a new action and action body on the given space.
    AcDbObjectId actionId;
    AcDbAssocActionBody::createActionAndActionBodyAndPostToDatabase(
        MyActionBody::desc(), ownerSpaceId, actionId, actionBodyId);

    AcDbObjectPointer&amp;lt; AcDbAssocAction &amp;gt; pAction(actionId, AcDb::kForWrite);
    if(!eOkVerify(pAction.openStatus()))
        return pAction.openStatus();

    //Read dependency on line, to read it&#39;s length.
    AcDbAssocDependency* pDep = new AcDbAssocDependency();
    pAction-&amp;gt;database()-&amp;gt;addAcDbObject(pDep);
    pDep-&amp;gt;attachToObject(lineId);
    pDep-&amp;gt;setIsReadDependency(true);
    pDep-&amp;gt;setIsWriteDependency(false);
    pAction-&amp;gt;addDependency(pDep-&amp;gt;objectId());
    pDep-&amp;gt;close();

    //Write dependency on circle to set it&#39;s radius equal to length of line.
    pDep = new AcDbAssocDependency();
    pAction-&amp;gt;database()-&amp;gt;addAcDbObject(pDep);
    pDep-&amp;gt;attachToObject(circleId);
    pDep-&amp;gt;setIsReadDependency(false);
    pDep-&amp;gt;setIsWriteDependency(true);
    pAction-&amp;gt;addDependency(pDep-&amp;gt;objectId());
    pDep-&amp;gt;close();

    return Acad::eOk;
}
&lt;/pre&gt;&lt;h3&gt;Evaluation logic:&lt;/h3&gt;Now the evaluation of action needs to ensure that the desired behavior is maintained after every evaluation. This requires implementation of evaluateOverride() method on the action body class.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: cpp&quot;&gt;void MyActionBody::evaluateOverride()
{
    AcDbAssocEvaluationCallback* pCallback = this-&amp;gt;currentEvaluationCallback();

    AcDbObjectIdArray depIds;
    Acad::ErrorStatus es = getDependencies(true, true, depIds);
    AcDbObjectId lineId, circleId;
    for(int i = 0; i &amp;lt; depIds.length(); ++i)
    {
        AcDbObjectPointer&amp;lt; AcDbAssocDependency &amp;gt; pDep(depIds[i], AcDb::kForRead);
        if(!eOkVerify(pDep.openStatus()))
        {
            //Reporting error to the callback.
            if(NULL != pCallback)
            {
                AcDbObjectPointer&amp;lt; AcDbAssocAction &amp;gt; pAction(this-&amp;gt;parentAction(), AcDb::kForRead);
                pCallback-&amp;gt;setActionEvaluationErrorStatus(pAction, pDep.openStatus(), depIds[i]);
            }
            setStatus(kFailedToEvaluateAssocStatus);
            return;
        }

        if(pDep-&amp;gt;status() == kErasedAssocStatus)
        {
            //If any dependent object is erased, the corresponding dependency
            //will be mared as kErasedAssocStatus. For this example, we need to
            //erase the action if dependency is to be erased.
            setStatus(kErasedAssocStatus);
            return;
        }

        if(!pDep-&amp;gt;isWriteDependency()) //read only dependency
            lineId = pDep-&amp;gt;dependentOnObject();
        else if(!pDep-&amp;gt;isReadDependency()) //write only dependency
            circleId = pDep-&amp;gt;dependentOnObject();
    }

    assert(!lineId.isNull() &amp;amp;&amp;amp; !circleId.isNull());
    if(lineId.isNull() || circleId.isNull())
    {
        setStatus(kFailedToEvaluateAssocStatus);
        return; //you may report some error here.
    }
    
    //Evaluate dependencies
    evaluateDependencies();

    double length = 0.0;
    //Use AcDbAssocObjectPointer to open line for read, so that
    //we get the cloned copy created by dragger, when line is dragged.
    AcDbAssocObjectPointer&amp;lt; AcDbLine &amp;gt; pLine(lineId, AcDb::kForRead);
    if(!eOkVerify(pLine.openStatus()))
    {
        setStatus(kFailedToEvaluateAssocStatus);
        return; //you may report some error here.
    }

    AcGePoint3d start, end;
    es = pLine-&amp;gt;getStartPoint(start);
    es = pLine-&amp;gt;getEndPoint(end);

    length = start.distanceTo(end);

    //Use AcDbAssocObjectPointer to open circle for write, so that
    //we get the clone of circle managed by dragger used to draws updated 
    //circle when line is dragged.
    AcDbAssocObjectPointer&amp;lt; AcDbCircle &amp;gt; pCircle(circleId, AcDb::kForWrite);
    if(eOkVerify(pCircle.openStatus()))
        pCircle-&amp;gt;setRadius(length/2);

    setStatus(kIsUpToDateAssocStatus); //Finally set the status as uptodate.
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://design-intent.blogspot.com/feeds/3179847956563961431/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://design-intent.blogspot.com/2011/01/associativity-in-autocad.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/3179847956563961431'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/3179847956563961431'/><link rel='alternate' type='text/html' href='http://design-intent.blogspot.com/2011/01/associativity-in-autocad.html' title='Associativity in AutoCAD'/><author><name>Sharad</name><uri>http://www.blogger.com/profile/06060088005375084545</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_m5OQ0gKYOoVPkPKubQYn9ApsFX0T1pb8W26t2T7n65DXqBZeqlHOlz_JaDEDKHaWMwj-3ednvfb1b7LypjrfWUImp6FhyZPEitqDE7yxRTbJRBv0YwmToxvhuugHA/s220/sharadjaiswal.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5075070519872065209.post-8124733832114373874</id><published>2011-01-28T18:49:00.001-08:00</published><updated>2011-01-29T17:42:55.354-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Intent"/><title type='text'>What’s Intent?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Every action has equal and opposite reaction but today we need to know the intent behind an action to react properly. If you can understand the intent you are smart. So what’s the intent, how do you understand it? It’s a thought, an idea, a concept which can be derived from the context by reading between the lines or understanding the gestures. It’s understanding of “&lt;em&gt;I in you”&lt;/em&gt;. &lt;br /&gt;
Understanding intent leads to providing the best solution for a problem, it leads to innovation. For example, searching for a restaurant on mobile phone has an intent and providing the best search result based on the context/location is innovation. In design paradigm every entity drawn on the canvas has some intent and it can be modified meaningfully only if one understand the intent of the entity. But when author is changed or the tools to modify these entities are changed, most often intents are lost. Capturing intent from actions develops knowledge base and developing knowledge base helps us understand the intent better.&lt;br /&gt;
Welcome to my first edition of &lt;a href=&quot;http://design-intent.blogspot.com/&quot;&gt;Intent&lt;/a&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://design-intent.blogspot.com/feeds/8124733832114373874/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://design-intent.blogspot.com/2011/01/whats-intent_28.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/8124733832114373874'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5075070519872065209/posts/default/8124733832114373874'/><link rel='alternate' type='text/html' href='http://design-intent.blogspot.com/2011/01/whats-intent_28.html' title='What’s Intent?'/><author><name>Sharad</name><uri>http://www.blogger.com/profile/06060088005375084545</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_m5OQ0gKYOoVPkPKubQYn9ApsFX0T1pb8W26t2T7n65DXqBZeqlHOlz_JaDEDKHaWMwj-3ednvfb1b7LypjrfWUImp6FhyZPEitqDE7yxRTbJRBv0YwmToxvhuugHA/s220/sharadjaiswal.jpg'/></author><thr:total>0</thr:total></entry></feed>