← All algorithms

미분류

Built-in Excel Features for Machine Learning

2023-12-12

1. Excel Solver for Optimization

Many ML algorithms boil down to finding a model parameter, i.e., a coefficient, or W, that minimizes the difference between the data and the model. In other words, it is defined as a problem of (1) determining the cost function, L, and (2) determining the position of the W where the differential value (dL/dW) is 0 to minimize the cost function, and the solution is usually found using a methodology such as gradient descent. In this Codeless ML, we solve this problem using Excel's Solver feature.

​

(1) Installing the Solver

​The Solver menu is one of the menus provided by the analysis of "data" in the main menu, and is displayed as follows.

If you don't see "Solver", you can select the "Development Tools" -> Excel add-in from the main menu of Excel, and the Add-ins window will appear as shown below, where you can select the Solver add-in and click the 'OK' button to install it.

If you don't see "Development Tools" in the main menu of Excel, click on the "Options" menu in the "File" tab, and the options menu will pop up as shown below. If you click "Add-ins" here, you will see the Excel add-ins below, and then press the "Go" tab. The add-ons window will appear as shown above.

(2) How to use the Solver

When you run the solver, you can run it through a window that includes (a) the location of the objective function (e.g. $G$6), (b) the maximum, minimum, or target value (c) the location of the variable, (d) the constraint to be satisfied, and (e) the check box that determines that the variable must be positive, as shown below.

​

(3) Solver example

To illustrate the use of the solver in a simplified way, the following example is provided:

Variables x and y are subject to satisfy the following two inequalities:

-x-y+2≤0, x-y+2≤0

Find the values of x and y that minimize the following objective equation, and their minimum.

½(x2+y2)

To do this, if you check up the worksheet in Excel as shown below, and then run the solution,

It shows that the solution is well found, i.e. when x=0 and y=2, we can see that the lowest value is 2 while satisfying all the constraints.

​

2. Linear Regression - Excel's LINEST

The trendline provided by Excel by default can provide a regression equation for a single variable. However, for polynomial equations, it is limited to a maximum of degree 6 and cannot solve multivariable problems. However, Excel provides a linear prediction function called LINEST, which can be used to solve linear regression. In this tutorial, we will introduce how to solve higher degree polynomial equations and multivariable regression problems using LINEST, INDEX, COLUMN, and other Excel functions.

​

(1) Trend Line

​

(2) LINEST(known_y's,known_x's)

The LINEST function itself returns a vector of slopes and intercepts, and using the INDEX function, I separated and returned each of them individually.

xySlopeIntercept
9189.8=INDEX(LINEST(B2:B9,A2:A9),1)=INDEX(LINEST(B2:B9,A2:A9),2)
8790.23.83943386-253.165769
8681.1​​
8883​​
92.890.9​​
95.2119​​
93.394.9​​
97.7132​​

(3) Linear Regression using 2nd order polynomial LINEST(known_y's, known_x's^COLUMN($A:$B))

Therefore, you can specify the degree of the desired polynomial by using the COLUMN function after the known_x's symbolized by the power sign ^. For a quadratic polynomial, use COLUMN($A:$B), for a quintic polynomial, use COLUMN($A:$E).

​

​

Using the INDEX function allows you to retrieve coefficients of a desired order, whereas if you want to return all coefficients at once, you can use the Array formula feature. To do so, you should follow these steps:
(step 1) First, select the number of cells equal to the number of coefficients you want to return. Assuming we have a cubic equation with 4 coefficients, you would select the range C2:F2.
(step 2) Enter the LINEST function.
(step 3) Press Shift+Ctrl+Enter to execute the Array formula.

​

The coefficients are returned all at once as follows. In this case, the coefficients are returned in descending order from the highest degree to the lowest degree.

​

(4) Obtaining coefficients of multivariable linear equations using LINEST

The LINEST function was used to model z with given x and y as parameters. The usage is the same, but two columns are used as variables (i.e., known_x's). It is important to note that the coefficients are returned in the order of z=(3)+index(2)x+index(1)y, not in the order of z=index(1)x+index(2)y+index(3). The curly braces {} in the function indicate the use of the Array function.

​

3. Random Number Generator

Random numbers are used in various cases:

(1) Initial conditions for problem solving
(2) Exploration for metaheuristics and reinforcement learning
(3) Random sampling of data

...

Excel provides a random number generator called Rand(), which generates random numbers that follow a uniform distribution between 0 and 1. The histogram below represents the distribution of 1000 random numbers generated using rand().

​

To generate a random number following a normal distribution with a mean of 0 and a standard deviation of 1, you can use Norm.Inv(rand(), 0, 1).

​

​

---

4. Excel's Iterative Calculation

In ML algorithms, especially for clustering and metaheuristics, there are various methods for iteratively updating solutions. For example,

(1) PCA and kernal PCA: Obtaining Eigenvalues and Eigenvectors when finding principal components.

(2) Updating the low-dimensional space in t-SNE.

(3) Updating clusters in k-means clustering.

(4) Updating clusters in DBSCAN.

(5) Updating particle positions in PSO.

​

In this case, it is common to perform repetitive calculations using the Copy and Paste method, but it is more convenient to use Excel's built-in iterative calculation.

To enable iterative calculation in Excel, you can go to the 'File' menu, then select 'Options', and under the 'Formulas' option, you can enable the 'Enable iterative calculation' setting.

​

Example) When calculating the principal with compound interest of 8% after 10 years, you can perform the calculation as follows:

To use the iterative calculation feature, (a) first calculate the compound principal as shown below (with the iterative calculation feature turned off), (b) calculate the principal = compound principal, (c) turn on the iterative calculation, and then (d) execute the iterative calculation feature.

(1) Compound interest formula B3=B1*(1+B2)

(2) Set the principal B1=B3

(3) Ignoring the circular reference warning caused by this.

​(4) The following circular reference occurs

​

(6) The compound principal was calculated correctly, as shown below.

​