{"cells":[{"cell_type":"markdown","id":"026fe49f-0be4-4037-902c-01618d9d98ad","metadata":{},"outputs":[],"source":["

\n"," \n"," \"Skills\n"," \n","

\n","\n","\n","# Non Linear Regression Analysis\n","\n","\n","Estimated time needed: **20** minutes\n"," \n","\n","## Objectives\n","\n","After completing this lab you will be able to:\n","\n","* Differentiate between linear and non-linear regression\n","* Use non-linear regression model in Python\n"]},{"cell_type":"markdown","id":"15a65d32-f83b-441e-9b17-31ba4e2e5dc2","metadata":{},"outputs":[],"source":["If the data shows a curvy trend, then linear regression will not produce very accurate results when compared to a non-linear regression since linear regression presumes that the data is linear. \n","Let's learn about non linear regressions and apply an example in python. In this notebook, we fit a non-linear model to the datapoints corrensponding to China's GDP from 1960 to 2014. \n"]},{"cell_type":"markdown","id":"73cb29ed-e8ec-4921-ac53-166595235341","metadata":{},"outputs":[],"source":["

Importing required libraries

\n"]},{"cell_type":"code","id":"ec4a49ee-e991-484b-a702-58664e2eee53","metadata":{},"outputs":[],"source":["import numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline"]},{"cell_type":"markdown","id":"e1ce1959-66c9-424d-9e69-202597a1737d","metadata":{},"outputs":[],"source":["Although linear regression can do a great job at modeling some datasets, it cannot be used for all datasets. First recall how linear regression, models a dataset. It models the linear relationship between a dependent variable y and the independent variables x. It has a simple equation, of degree 1, for example y = $2x$ + 3.\n"]},{"cell_type":"code","id":"1ce23c2d-f16a-4dad-99a2-9b8bf259f46c","metadata":{},"outputs":[],"source":["x = np.arange(-5.0, 5.0, 0.1)\n\n##You can adjust the slope and intercept to verify the changes in the graph\ny = 2*(x) + 3\ny_noise = 2 * np.random.normal(size=x.size)\nydata = y + y_noise\n#plt.figure(figsize=(8,6))\nplt.plot(x, ydata, 'bo')\nplt.plot(x,y, 'r') \nplt.ylabel('Dependent Variable')\nplt.xlabel('Independent Variable')\nplt.show()"]},{"cell_type":"markdown","id":"236b4327-37bb-4b7d-ae15-604a89ee5dc5","metadata":{},"outputs":[],"source":["Non-linear regression is a method to model the non-linear relationship between the independent variables $x$ and the dependent variable $y$. Essentially any relationship that is not linear can be termed as non-linear, and is usually represented by the polynomial of $k$ degrees (maximum power of $x$). For example:\n","\n","$$ \\ y = a x^3 + b x^2 + c x + d \\ $$\n","\n","Non-linear functions can have elements like exponentials, logarithms, fractions, and so on. For example: $$ y = \\log(x)$$\n"," \n","We can have a function that's even more complicated such as :\n","$$ y = \\log(a x^3 + b x^2 + c x + d)$$\n"]},{"cell_type":"markdown","id":"4cd7c4e4-ed41-4dde-a6cc-ca6f2080564e","metadata":{},"outputs":[],"source":["Let's take a look at a cubic function's graph.\n"]},{"cell_type":"code","id":"4b87e01f-01dd-4866-b4de-ef40b6234338","metadata":{},"outputs":[],"source":["x = np.arange(-5.0, 5.0, 0.1)\n\n##You can adjust the slope and intercept to verify the changes in the graph\ny = 1*(x**3) + 1*(x**2) + 1*x + 3\ny_noise = 20 * np.random.normal(size=x.size)\nydata = y + y_noise\nplt.plot(x, ydata, 'bo')\nplt.plot(x,y, 'r') \nplt.ylabel('Dependent Variable')\nplt.xlabel('Independent Variable')\nplt.show()"]},{"cell_type":"markdown","id":"8b87c4de-8c9a-490f-b6b5-7fc477e31932","metadata":{},"outputs":[],"source":["As you can see, this function has $x^3$ and $x^2$ as independent variables. Also, the graphic of this function is not a straight line over the 2D plane. So this is a non-linear function.\n"]},{"cell_type":"markdown","id":"b6b64321-1168-467b-8e9f-54d272e46054","metadata":{},"outputs":[],"source":["Some other types of non-linear functions are:\n"]},{"cell_type":"markdown","id":"112b0dad-85f3-4378-b0e2-bf0759b6f129","metadata":{},"outputs":[],"source":["### Quadratic\n"]},{"cell_type":"markdown","id":"31f3f05b-8a22-4b9b-8163-5ff9f10fa343","metadata":{},"outputs":[],"source":["$$ Y = X^2 $$\n"]},{"cell_type":"code","id":"fe65df68-b7a4-48cf-9070-283f080dee65","metadata":{},"outputs":[],"source":["x = np.arange(-5.0, 5.0, 0.1)\n\n##You can adjust the slope and intercept to verify the changes in the graph\n\ny = np.power(x,2)\ny_noise = 2 * np.random.normal(size=x.size)\nydata = y + y_noise\nplt.plot(x, ydata, 'bo')\nplt.plot(x,y, 'r') \nplt.ylabel('Dependent Variable')\nplt.xlabel('Independent Variable')\nplt.show()"]},{"cell_type":"markdown","id":"7d479697-8606-44de-8156-ded445fe2afe","metadata":{},"outputs":[],"source":["### Exponential\n"]},{"cell_type":"markdown","id":"8f77d83e-8240-41c7-b36b-175d8a5618ff","metadata":{},"outputs":[],"source":["An exponential function with base c is defined by $$ Y = a + b c^X$$ where b ≠0, c > 0 , c ≠1, and x is any real number. The base, c, is constant and the exponent, x, is a variable. \n","\n"]},{"cell_type":"code","id":"7355b113-fb6e-4d50-b99f-9133fe124b61","metadata":{},"outputs":[],"source":["X = np.arange(-5.0, 5.0, 0.1)\n\n##You can adjust the slope and intercept to verify the changes in the graph\n\nY= np.exp(X)\n\nplt.plot(X,Y) \nplt.ylabel('Dependent Variable')\nplt.xlabel('Independent Variable')\nplt.show()"]},{"cell_type":"markdown","id":"cf97d373-4d2b-4425-8c69-c4873d4b7254","metadata":{},"outputs":[],"source":["### Logarithmic\n","\n","The response $y$ is a results of applying the logarithmic map from the input $x$ to the output $y$. It is one of the simplest form of __log()__: i.e. $$ y = \\log(x)$$\n","\n","Please consider that instead of $x$, we can use $X$, which can be a polynomial representation of the $x$ values. In general form it would be written as \n","\\begin{equation}\n","y = \\log(X)\n","\\end{equation}\n"]},{"cell_type":"code","id":"26cff054-333a-4cfc-b851-ac5aaa40cca4","metadata":{},"outputs":[],"source":["X = np.arange(-5.0, 5.0, 0.1)\n\nY = np.log(X)\n\nplt.plot(X,Y) \nplt.ylabel('Dependent Variable')\nplt.xlabel('Independent Variable')\nplt.show()"]},{"cell_type":"markdown","id":"07aacb86-eafb-4f32-b827-ecbfe871ea31","metadata":{},"outputs":[],"source":["### Sigmoidal/Logistic\n"]},{"cell_type":"markdown","id":"72ca83a5-d461-4a53-b64a-8a98bd6cbccb","metadata":{},"outputs":[],"source":["$$ Y = a + \\frac{b}{1+ c^{(X-d)}}$$\n"]},{"cell_type":"code","id":"2c907f44-8614-415a-88da-c3c1c88f375e","metadata":{},"outputs":[],"source":["X = np.arange(-5.0, 5.0, 0.1)\n\n\nY = 1-4/(1+np.power(3, X-2))\n\nplt.plot(X,Y) \nplt.ylabel('Dependent Variable')\nplt.xlabel('Independent Variable')\nplt.show()"]},{"cell_type":"markdown","id":"bad7ea3b-3c69-45b5-85fd-ae33e4958703","metadata":{},"outputs":[],"source":["\n","# Non-Linear Regression example\n"]},{"cell_type":"markdown","id":"daff9b5c-1347-48b3-b8a7-22ac8d1a6fd5","metadata":{},"outputs":[],"source":["For an example, we're going to try and fit a non-linear model to the datapoints corresponding to China's GDP from 1960 to 2014. We download a dataset with two columns, the first, a year between 1960 and 2014, the second, China's corresponding annual gross domestic income in US dollars for that year. \n"]},{"cell_type":"code","id":"55bcf34b-cbaa-4c9d-ac82-a677c4ef3b75","metadata":{},"outputs":[],"source":["import numpy as np\nimport pandas as pd\n\n#downloading dataset\n!wget -nv -O china_gdp.csv https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-ML0101EN-SkillsNetwork/labs/Module%202/data/china_gdp.csv\n \ndf = pd.read_csv(\"china_gdp.csv\")\ndf.head(10)"]},{"cell_type":"markdown","id":"8aff85ce-507e-48e1-98f1-1f1ddf35cc10","metadata":{},"outputs":[],"source":["__Did you know?__ When it comes to Machine Learning, you will likely be working with large datasets. As a business, where can you host your data? IBM is offering a unique opportunity for businesses, with 10 Tb of IBM Cloud Object Storage: [Sign up now for free](http://cocl.us/ML0101EN-IBM-Offer-CC)\n"]},{"cell_type":"markdown","id":"6fe1e13f-dfd6-496b-b3c0-87529652c606","metadata":{},"outputs":[],"source":["### Plotting the Dataset ###\n","This is what the datapoints look like. It kind of looks like an either logistic or exponential function. The growth starts off slow, then from 2005 on forward, the growth is very significant. And finally, it decelerates slightly in the 2010s.\n"]},{"cell_type":"code","id":"f1828070-d421-406e-a80c-077e2086d648","metadata":{},"outputs":[],"source":["plt.figure(figsize=(8,5))\nx_data, y_data = (df[\"Year\"].values, df[\"Value\"].values)\nplt.plot(x_data, y_data, 'ro')\nplt.ylabel('GDP')\nplt.xlabel('Year')\nplt.show()"]},{"cell_type":"markdown","id":"85348c47-bc3e-4762-aa6b-8f7d92e31ad0","metadata":{},"outputs":[],"source":["### Choosing a model ###\n","\n","From an initial look at the plot, we determine that the logistic function could be a good approximation,\n","since it has the property of starting with a slow growth, increasing growth in the middle, and then decreasing again at the end; as illustrated below:\n"]},{"cell_type":"code","id":"18f5385e-1c27-4ccc-96b2-604fc3e17275","metadata":{},"outputs":[],"source":["X = np.arange(-5.0, 5.0, 0.1)\nY = 1.0 / (1.0 + np.exp(-X))\n\nplt.plot(X,Y) \nplt.ylabel('Dependent Variable')\nplt.xlabel('Independent Variable')\nplt.show()"]},{"cell_type":"markdown","id":"086e1ee6-a705-4c6b-90dc-da384602a94e","metadata":{},"outputs":[],"source":["\n","\n","The formula for the logistic function is the following:\n","\n","$$ \\hat{Y} = \\frac1{1+e^{-\\beta_1(X-\\beta_2)}}$$\n","\n","$\\beta_1$: Controls the curve's steepness,\n","\n","$\\beta_2$: Slides the curve on the x-axis.\n"]},{"cell_type":"markdown","id":"8cecf20c-876b-4db5-9f5b-52db82f19059","metadata":{},"outputs":[],"source":["### Building The Model ###\n","Now, let's build our regression model and initialize its parameters. \n"]},{"cell_type":"code","id":"ee4d2443-ea9a-455f-b276-89844e5dc980","metadata":{},"outputs":[],"source":["def sigmoid(x, Beta_1, Beta_2):\n y = 1 / (1 + np.exp(-Beta_1*(x-Beta_2)))\n return y"]},{"cell_type":"markdown","id":"968a3821-b9c0-4987-afc8-70b9dc9b4ffc","metadata":{},"outputs":[],"source":["Lets look at a sample sigmoid line that might fit with the data:\n"]},{"cell_type":"code","id":"f2b34c74-2538-498c-bda1-c85850ca40f6","metadata":{},"outputs":[],"source":["beta_1 = 0.10\nbeta_2 = 1990.0\n\n#logistic function\nY_pred = sigmoid(x_data, beta_1 , beta_2)\n\n#plot initial prediction against datapoints\nplt.plot(x_data, Y_pred*15000000000000.)\nplt.plot(x_data, y_data, 'ro')"]},{"cell_type":"markdown","id":"ef95d303-dfab-4b5d-bb62-c4c6c44324b1","metadata":{},"outputs":[],"source":["Our task here is to find the best parameters for our model. Lets first normalize our x and y:\n"]},{"cell_type":"code","id":"70e008ef-dd72-49f4-a7cb-8db947fddea3","metadata":{},"outputs":[],"source":["# Lets normalize our data\nxdata =x_data/max(x_data)\nydata =y_data/max(y_data)"]},{"cell_type":"markdown","id":"c27fdd1d-6f05-4709-bb60-5c4e3b84c274","metadata":{},"outputs":[],"source":["#### How we find the best parameters for our fit line?\n","we can use __curve_fit__ which uses non-linear least squares to fit our sigmoid function, to data. Optimize values for the parameters so that the sum of the squared residuals of sigmoid(xdata, *popt) - ydata is minimized.\n","\n","popt are our optimized parameters.\n"]},{"cell_type":"code","id":"9ddc57d3-6b9e-4fc5-85c0-751ec77f8970","metadata":{},"outputs":[],"source":["from scipy.optimize import curve_fit\npopt, pcov = curve_fit(sigmoid, xdata, ydata)\n#print the final parameters\nprint(\" beta_1 = %f, beta_2 = %f\" % (popt[0], popt[1]))"]},{"cell_type":"markdown","id":"6c52cfbd-921d-4ede-97e0-d279fc41db5e","metadata":{},"outputs":[],"source":["Now we plot our resulting regression model.\n"]},{"cell_type":"code","id":"7fb670ef-ed6e-4bb7-afc9-b29ca83ff264","metadata":{},"outputs":[],"source":["x = np.linspace(1960, 2015, 55)\nx = x/max(x)\nplt.figure(figsize=(8,5))\ny = sigmoid(x, *popt)\nplt.plot(xdata, ydata, 'ro', label='data')\nplt.plot(x,y, linewidth=3.0, label='fit')\nplt.legend(loc='best')\nplt.ylabel('GDP')\nplt.xlabel('Year')\nplt.show()"]},{"cell_type":"markdown","id":"0f56ad38-27a5-4458-a277-6ce6f9390582","metadata":{},"outputs":[],"source":["## Practice\n","Can you calculate what is the accuracy of our model?\n"]},{"cell_type":"code","id":"0d60489d-5693-46e2-af19-b1a78feab0a1","metadata":{},"outputs":[],"source":["# write your code here\n\n\n"]},{"cell_type":"markdown","id":"4a787aae-df05-44da-a53f-ad60db331921","metadata":{},"outputs":[],"source":["
Click here for the solution\n","\n","```python \n","# split data into train/test\n","msk = np.random.rand(len(df)) < 0.8\n","train_x = xdata[msk]\n","test_x = xdata[~msk]\n","train_y = ydata[msk]\n","test_y = ydata[~msk]\n","\n","# build the model using train set\n","popt, pcov = curve_fit(sigmoid, train_x, train_y)\n","\n","# predict using test set\n","y_hat = sigmoid(test_x, *popt)\n","\n","# evaluation\n","print(\"Mean absolute error: %.2f\" % np.mean(np.absolute(y_hat - test_y)))\n","print(\"Residual sum of squares (MSE): %.2f\" % np.mean((y_hat - test_y) ** 2))\n","from sklearn.metrics import r2_score\n","print(\"R2-score: %.2f\" % r2_score(test_y,y_hat) )\n","\n","```\n","\n","
\n"]},{"cell_type":"markdown","id":"1914ffa8-f8db-490a-a821-7f57c398362b","metadata":{},"outputs":[],"source":["

Want to learn more?

\n","\n","IBM SPSS Modeler is a comprehensive analytics platform that has many machine learning algorithms. It has been designed to bring predictive intelligence to decisions made by individuals, by groups, by systems – by your enterprise as a whole. A free trial is available through this course, available here: SPSS Modeler\n","\n","Also, you can use Watson Studio to run these notebooks faster with bigger datasets. Watson Studio is IBM's leading cloud solution for data scientists, built by data scientists. With Jupyter notebooks, RStudio, Apache Spark and popular libraries pre-packaged in the cloud, Watson Studio enables data scientists to collaborate on their projects without having to install anything. Join the fast-growing community of Watson Studio users today with a free account at Watson Studio\n","\n"]},{"cell_type":"markdown","id":"98fa8c61-6330-4942-b756-abb84862c94c","metadata":{},"outputs":[],"source":["### Thank you for completing this lab!\n","\n","\n","## Author\n","\n","Saeed Aghabozorgi\n","\n","\n","### Other Contributors\n","\n","Joseph Santarcangelo\n","\n","\n","##

© IBM Corporation 2020. All rights reserved.

\n","\n","\n","\n","\n","\n"]}],"metadata":{"kernelspec":{"display_name":"Python","language":"python","name":"conda-env-python-py"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"prev_pub_hash":"f873d3177bf529d2d648c46bab1627042a257e5ec6ce42ca68028520459f817e"},"nbformat":4,"nbformat_minor":4}