UNIT 2 — COMPLETE QUESTION BANK (PYTHON ONLY)
1. PEARSON CORRELATION
Basic
1. Compute Pearson correlation using Pandas:
x = [1, 2, 3, 4, 5] y = [10, 20, 30, 40, 50]
Code:
import pandas as pd
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
df = [Link]({"x":x,"y":y})
corrl = df['x'].corr(df['y'])
print("Pearson Correlation is: ",corrl)
Output:
Pearson Correlation is: 1.0
2. Compute correlation using NumPy (corrcoef):
x = [2, 4, 6, 8, 10] y = [5, 10, 15, 20, 25]
Code:
import numpy as np
x = [2, 4, 6, 8, 10]
y = [5, 10, 15, 20, 25]
corrl = [Link](x,y)
print("Pearson Correlation is:\n",corrl)
Output:
Pearson Correlation is:
[[1. 1.]
[1. 1.]]
3. Compute Pearson correlation using SciPy (pearsonr):
x = [1, 3, 5, 7, 9] y = [2, 6, 10, 14, 18]
Code:
import [Link] as sp
x = [1, 3, 5, 7, 9]
y = [2, 6, 10, 14, 18]
corl,pval = [Link](x,y)
print("Pearson Correlation is: ",corl,"\nSignificant Value is:",pval)
Output:
Pearson Correlation is: 1.0
Significant Value is: 0.0
Dataset-Based
4. Create DataFrame and compute correlation:
Hours = [1, 2, 3, 4, 5] Marks = [45, 50, 65, 70, 85]
Code:
import pandas as pd
Hours = [1, 2, 3, 4, 5]
Marks = [45, 50, 65, 70, 85]
df = [Link]({"Hours":Hours,"Marks":Marks})
corl = df["Hours"].corr(df["Marks"])
print("Correlation is: ",corl)
Output:
Correlation is: 0.985329278164293
5. Compute correlation matrix:
A = [10, 20, 30, 40, 50] B = [5, 15, 25, 35, 45] C = [2, 4, 6, 8, 10]
Code:
import pandas as pd
A = [10, 20, 30, 40, 50]
B = [5, 15, 25, 35, 45]
C = [2, 4, 6, 8, 10]
df = [Link]({"A":A,"B":B,"C":C})
corlmatrix = [Link]()
print("Correlation Matrix is:\n",corlmatrix)
Output:
Correlation Matrix is:
A B C
A 1.0 1.0 1.0
B 1.0 1.0 1.0
C 1.0 1.0 1.0
Logic-Based
6. Create dataset showing strong positive correlation:
x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]
Code:
from [Link] import pearsonr
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
corl, pval = pearsonr(x,y)
print("Pearson Correlation is: ",corl,"\nSignificant Value is:",pval)
Output:
Pearson Correlation is: 1.0
Significant Value is: 0.0
7. Create dataset showing negative correlation:
x = [1, 2, 3, 4, 5] y = [50, 40, 30, 20, 10]
Code:
from [Link] import pearsonr
x = [1, 2, 3, 4, 5]
y = [50, 40, 30, 20, 10]
corl, pval = pearsonr(x,y)
print("Pearson Correlation is: ",corl,"\nSignificant Value is:",pval)
Output:
Pearson Correlation is: -0.9999999999999999
Significant Value is: 0.0
8. Create dataset showing no correlation:
x = [1, 2, 3, 4, 5] y = [10, 50, 20, 60, 30]
Code:
from [Link] import pearsonr
x = [1, 2, 3, 4, 5]
y = [10, 50, 20, 60, 30]
corl, pval = pearsonr(x,y)
print("Pearson Correlation is: ",corl,"\nSignificant Value is:",pval)
Output:
Pearson Correlation is: 0.38124642583151175
Significant Value is: 0.5266118441541604
Visualization-Based
9. Compute correlation and plot scatter:
x = [2, 4, 6, 8] y = [5, 10, 15, 20]
Code:
from [Link] import pearsonr
import [Link] as plt
x = [2, 4, 6, 8]
y = [5, 10, 15, 20]
corl, pval = pearsonr(x,y)
print("Pearson Correlation is: ",corl,"\nSignificant Value is:",pval)
[Link](x,y)
[Link]("X vs Y")
[Link]("X")
[Link]("Y")
[Link]()
Output:
Pearson Correlation is: 1.0
Significant Value is: 0.0
10. Compare correlation visually using plot:
x = [1, 2, 3, 4, 5] y1 = [10, 20, 30, 40, 50] y2 = [50, 40, 30, 20, 10]
Code:
from [Link] import pearsonr
import [Link] as plt
x = [1, 2, 3, 4, 5]
y1 = [10, 20, 30, 40, 50]
y2 = [50, 40, 30, 20, 10]
corl_y1, pval_y1 = pearsonr(x,y1)
corl_y2, pval_y2 = pearsonr(x,y2)
print("Pearson Correlation for (x,y1) is: ",corl_y1,"\nSignificant Value
is:",pval_y1)
print("Pearson Correlation for (x,y2) is: ",corl_y2,"\nSignificant Value
is:",pval_y2)
[Link](x,y1)
[Link]("x vs y1")
[Link]("x")
[Link]("y1")
[Link]()
[Link](x,y2)
[Link]("x vs y2")
[Link]("x")
[Link]("y2")
[Link]()
Output:
Pearson Correlation for (x,y1) is: 0.9999999999999999
Significant Value is: 0.0
Pearson Correlation for (x,y2) is: -0.9999999999999999
Significant Value is: 0.0
Combined
11. Compute correlation and plot,scatter with regression line:
x = [1, 2, 3, 4, 5] y = [12, 18, 25, 35, 45]
Code:
from [Link] import pearsonr
import numpy as np
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [12, 18, 25, 35, 45]
x= [Link](x)
y= [Link](y)
corl, pval = pearsonr(x,y)
print("Pearson Correlation is: ",corl,"\nSignificant Value is:",pval)
m,c = [Link](x,y,1) # m is slope, c is intercept(where line starts)
y_pred = m*x + c
[Link](x,y,label="Data Points")
[Link](x,y_pred,label="Regression Line")
[Link]("Scatter Plot with Regression Line")
[Link]("X values")
[Link]("Y values")
[Link]()
[Link]()
Output:
Pearson Correlation is: 0.9934599894894269
Significant Value is: 0.0006342712478166547
2. SPEARMAN RANK CORRELATION
Basic
12. Compute Spearman correlation using SciPy:
x = [1, 2, 3, 4, 5] y = [100, 90, 80, 70, 60]
Code:
from [Link] import spearmanr
x = [1, 2, 3, 4, 5]
y = [100, 90, 80, 70, 60]
corl,pval = spearmanr(x,y)
print("Spearman Correlation is: ",corl,"\nSignificant Value is:",pval)
Output:
Spearman Correlation is: -0.9999999999999999
Significant Value is: 1.4042654220543608e-24
13. Compute Spearman correlation using Pandas:
x = [10, 20, 30, 40, 50] y = [1, 2, 3, 4, 5]
Code:
import pandas as pd
x = [10, 20, 30, 40, 50]
y = [1, 2, 3, 4, 5]
df = [Link]({"x":x,"y":y})
corlmatrix = [Link](method="spearman")
#or
corl= df["x"].corr(df["y"],method="spearman")
print("Spearman Correlation Matrix using pandas is:\n",corlmatrix)
print("Spearman Correlation for x and y using pandas is:",corl)
Output:
Spearman Correlation Matrix using pandas is:
x y
x 1.0 1.0
y 1.0 1.0
Spearman Correlation for x and y using pandas is: 0.9999999999999999
Dataset-Based
14. Compute Spearman correlation:
Ranks = [1, 2, 3, 4, 5] Scores = [60, 65, 70, 75, 80]
Code:
import pandas as pd
Ranks = [1, 2, 3, 4, 5]
Scores = [60, 65, 70, 75, 80]
df = [Link]({"x":Ranks,"y":Scores})
corl= df["x"].corr(df["y"],method="spearman")
print("Spearman Correlation for x and y using pandas is:",corl)
Output:
Spearman Correlation for x and y using pandas is: 0.9999999999999999
3. LINEAR REGRESSION
Basic
15. Fit linear regression model:
X = [[1], [2], [3], [4], [5]] y = [10, 20, 30, 40, 50]
Code:
from sklearn.linear_model import LinearRegression
import numpy as np
x = [[1], [2], [3], [4], [5]]
y = [10, 20, 30, 40, 50]
x = [Link](x)
y = [Link](y)
model = LinearRegression()
[Link](x,y)
slope = model.coef_[0]
intercept = model.intercept_
print("Slope is: ",slope)
print("Intercept is: ",intercept)
y_pred = [Link](x)
print("Predicted values:", y_pred)
Output:
Slope is: 9.999999999999998
Intercept is: 7.105427357601002e-15
Predicted values: [10. 20. 30. 40. 50.]
Prediction-Based
16. Predict value for new input:
X = [[1], [2], [3], [4]] y = [2, 4, 6, 8] Predict for X = 5
Code:
from sklearn.linear_model import LinearRegression
import numpy as np
x = [[1], [2], [3], [4]]
y = [2, 4, 6, 8]
new_x = [[5]]
x = [Link](x)
y = [Link](y)
new_x = [Link](new_x)
model = LinearRegression()
[Link](x,y)
y_pred = [Link](new_x)
print("Predicted Value, when x=5, y would be: ",y_pred[0])
Output:
Predicted Value, when x=5, y would be: 10.0
Visualization-Based
17. Plot regression line:
X = [[1], [2], [3], [4], [5]] y = [3, 6, 9, 12, 15]
Code:
from sklearn.linear_model import LinearRegression
import [Link] as plt
import numpy as np
x = [[1], [2], [3], [4], [5]]
y = [3, 6, 9, 12, 15]
x = [Link](x)
y = [Link](y)
model = LinearRegression()
[Link](x,y)
slope = model.coef_
intercept = model.intercept_
y_pred = [Link](x)
print("Slope: ",slope)
print("Intercept: ",intercept)
print("Predicted y values: ",y_pred)
[Link](x,y,label="Data Points")
[Link](x,y_pred,label="Regression Line")
[Link]("Regression Line And Data Points")
[Link]("X Axis")
[Link]("Y Values")
[Link]()
[Link]()
Output:
Slope: [3.]
Intercept: -3.552713678800501e-15
Predicted y values: [ 3. 6. 9. 12. 15.]
4. MULTIPLE LINEAR REGRESSION
Basic
18. Fit multiple regression:
X = [[1, 2], [2, 3], [3, 4], [4, 5]] y = [10, 20, 30, 40]
Code:
from sklearn.linear_model import LinearRegression
import numpy as np
x = [Link]([[1, 2], [2, 3], [3, 4], [4, 5]])
y = [Link]([10, 20, 30, 40])
model = LinearRegression()
[Link](x,y)
coefficients = model.coef_
intercept = model.intercept_
print("Coefficients are: ",coefficients)
print("Intercept is: ",intercept)
y_pred = [Link](x)
print("Y Prediction is: ",y_pred)
Output:
Coefficients are: [5. 5.]
Intercept is: -4.999999999999989
Y Prediction is: [10. 20. 30. 40.]
Dataset-Based
19. Predict output:
X = [[2, 1], [3, 2], [4, 3], [5, 4]] y = [20, 30, 40, 50] Predict for [6, 5]
Code:
from sklearn.linear_model import LinearRegression
import numpy as np
x = [Link]([[2, 1], [3, 2], [4, 3], [5, 4]])
y = [Link]([20, 30, 40, 50])
new_x = [Link]([[6, 5]])
model = LinearRegression()
[Link](x,y)
coefficients = model.coef_
intercept = model.intercept_
print("Coefficients are: ",coefficients)
print("Intercept is: ",intercept)
y_pred = [Link](new_x)
print("Y Prediction is: ",y_pred)
Output:
Coefficients are: [5. 5.]
Intercept is: 5.000000000000011
Y Prediction is: [60.]
Comparison-Based
20. Compare two regression plots:
X = [[1], [2], [3], [4], [5]] y1 = [2, 4, 6, 8, 10] y2 = [10, 8, 6, 4, 2]
Code:
from sklearn.linear_model import LinearRegression
import numpy as np
import [Link] as plt
X = [Link]([[1], [2], [3], [4], [5]])
y1 = [Link]([2, 4, 6, 8, 10])
y2 = [Link]([10, 8, 6, 4, 2])
model1 = LinearRegression()
[Link](X, y1)
model2 = LinearRegression()
[Link](X, y2)
y1_pred = [Link](X)
y2_pred = [Link](X)
[Link](X, y1)
[Link](X, y1_pred)
[Link](X, y2)
[Link](X, y2_pred)
[Link]("Comparison of Two Regressions")
[Link]("X")
[Link]("y")
[Link]()
Output: