Posts

Polynomial Regression Python Code - Loan Default Prediction

Image
Import necessary libraries import pandas import numpy import matplotlib.pyplot from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures Importing data into project data = pandas.read_csv('loan_default_rate_dataset.csv') print(data) Assign Input Columns to X variable and Output column to Y Variable x = data[['Credit_Score','Annual_Income','Loan_Amount']] y = data['Loan_Default_Probability'] Importing Polynomial Features to do cross connections, squares in x data. So data will expand and model can learn unpredictable patterns poly = PolynomialFeatures(degree=2) training the X variable using the polynomial feature. So it will do cross connections, squares in x data and learn the complex patterns. x_poly = poly.fit_transform(x) Now, since we learned the complex patterns and expanded the x values into x_poly variable, we can do linear regression from the Polynomial X data and Y data. model = LinearRegres...

What is Polynomial Regression? Linear Regression vs Polynomial Regression

Image
 Polynomial Regression is an Advance method of Linear Regression. Linear Regression draws a line between X and Y data values, we can check how well the relationship between X and Y. Polynomial Regression draws a Curved Line which bends when relationship is unpredictable and non Linear. In a non-linear relationship , the connection between X and Y is not consistent.  Sometimes when X increases , Y increases , Other times when X increases , Y decreases , and it can change direction multiple times . Linear Regression → works best when the relationship between X and Y is proportional and predictable . The line just shows how much Y increases when X increases. Polynomial Regression → works best when the relationship is non-linear or unpredictable . The curve bends up and down showing that sometimes X increases Y, sometimes decreases Y, depending on the data pattern.

Linear Regression in Python to find Relationship between two columns – Test and Train Explanation

  You have a dataset with Columns "Study Hours" and "Exam Score" with 1000 rows. You split the rows 800 separately and 200 separately. 80% of rows for training the Linear regression model and 20% of rows for testing the Linear regression model. What is Training Data? Training Data is the data we use to teach the model . We give the model Study Hours Column and Exam Scores Column  so it can find the relationship between these columns using the formula Y = M × X + C This is the Python code we use to train the Model:  model.fit(x,y) After training, the model learns: Value of M which is also called as Slope  Value of C which is also called as Intercept  What is Testing Data? Testing Data is new data that the model has never seen before. We give it only Study Hours (X) and ask the model to predict Exam Scores (Y) . Then we compare the Predicted Y with the Real Y to see how well our model works.

Showing Relationship between X and Y Values using Linear Regression Line in Python - Machine Learning

Image
Importing Libraries for this project import numpy as np  import pandas as pd from sklearn.linear_ model import LinearRegression  import matplotlib.pyplot as plt  Importing Data file into python code  filepath = 'PythonRegressionPracticeWorkbook.csv'  data = pd.read_csv(filepath)  print(data)  Assigning Input / Independent Column to X and assigning dependent / Output Column to Y variables  x = data[['Square Feet']]  y = data['Price']  Training the X and Y variables using Linear Regression Model to find how X affects Y model = LinearRegression()  model.fit(x,y)  Checking Slope, Intercept values from the learned Model slope = model.coef_[0]  intercept = model.intercept_  print("Slope:", slope)  print("Intercept:",intercept)  Predicting the output value / Y value for each X value / input value  y_pred = model.predict(x)  Plotting Scatter plot with Regression Line to show both Original X and Y values, P...

Linear Regression Prediction of future values in Python using Scatter Plot - Machine Learning

Image
We are Importing Necessary Libraries for this project  import pandas as pandas  import numpy as numpy  import matplotlib.pyplot as pyplot  from sklearn.linear_model import LinearRegression Importing Data using Pandas library and creating table dataframe data = pandas.read_csv('python_linear_regression_prediction_python_data.csv')  print(data) Assigning Input / Independent columns to X variable and Assigning Output / Dependent Column to Y Variable x = data[['Experience_Years','Calls_Made','Meetings_Conducted']]  y = data['Sales_Amount'] Asking Linear Regression Model to learn data from X and Y variables model = LinearRegression() model.fit(x,y) Printing Coefficient and Intercept to check their values print("Coefficient:",model.coef_)  print("Intercept",model.intercept_) Adding New Inputs / Independent Values to predict Output / Dependent values new_data = pandas.DataFrame({ 'Experience_Years': [8, 12, 15], '...

Common Objects and their data types in Python

  1. List [] Modifiable (can change values) Ordered (keeps the order of items) Can store mixed data types Example: [1, 2, 3, a, b, c] 2. Tuple () Non Modifiable (cannot change values after creation) Ordered Can store mixed data types Example: (1, 2, 3, a, b, c) 3. Dictionary {} Key-value pair Modifiable Unordered Example: {"name": "Kavi", "age": 25} 4. Set {} Modifiable Unordered (no index) Unique elements only Example: {1, 2, 3} 5. String "" or '' Non Modifiable Ordered Example: "Hello", "world" 6. Integer ( int ) Whole numbers Example: 10 7. Float ( float ) Decimal numbers Example: 3.14 8. Boolean ( bool ) True or False Example: True 9. NoneType ( None ) Represents nothing / null Example: None

What is For Loop in Python?

For loop is used to repeat a code multiple times . It goes through each Value in a Range , one by one, executes the code for each item. How it works: You will write a code inside For Loop. It applies that code to each values in the Range, one by one. You will save time doing this instead of applying the code separately for each values. It only stops the process when it reaches last value in a range.