JavaScript
x
28
28
1
import matplotlib.pyplot as plt
2
import pandas as pd
3
import numpy as np
4
from sklearn.linear_model import LinearRegression
5
6
col1 = [2011.005, 2012.6543, 2013.3456, 2014.7821, 2015.3421, 2016.7891, 2017.0173, 2018.1974]
7
col2 = [1.4356, "", 5.32245, 6.542, 7.567, .77558, "", ""]
8
col3 = [1.3345, 2.345, "", 5.356, 3.124, 6.12, "", ""]
9
col4 = [0.67, 4.235, "", 6.78, "", "", 9.56, ""]
10
11
col1 = pd.to_numeric(col1, errors='coerce')
12
col2 = pd.to_numeric(col2, errors='coerce')
13
col3 = pd.to_numeric(col3, errors='coerce')
14
col4 = pd.to_numeric(col4, errors='coerce')
15
16
idxy1 = np.isfinite(col1) & np.isfinite(col2)
17
idxy2 = np.isfinite(col1) & np.isfinite(col3)
18
idxy3 = np.isfinite(col1) & np.isfinite(col4)
19
20
m1,b1 = np.polyfit(col1[idxy1], col2[idxy1], 1)
21
m2,b2 = np.polyfit(col1[idxy2], col2[idxy2], 1)
22
m3,b3 = np.polyfit(col1[idxy3], col2[idxy3], 1)
23
24
print(pd.isna(col2))
25
plt.figure()
26
plt.legend()
27
plt.show()
28
I was trying to plot a linear graph using m,b = np.polyfit(x0, y0, 1) function however when I print m2,b2,m3,b3 I get nan. from the empty values. How do I fix this?
Advertisement
Answer
You seem to have a typo in
JavaScript
1
8
1
idxy1 = np.isfinite(col1) & np.isfinite(col2)
2
idxy2 = np.isfinite(col1) & np.isfinite(col3)
3
idxy3 = np.isfinite(col1) & np.isfinite(col4)
4
5
m1,b1 = np.polyfit(col1[idxy1], col2[idxy1], 1)
6
m2,b2 = np.polyfit(col1[idxy2], col2[idxy2], 1) # <- here you need ...(col1[idxy2], col3[idxy2], 1)
7
m3,b3 = np.polyfit(col1[idxy3], col2[idxy3], 1) # <- here you need ...(col1[idxy2], col3[idxy3], 1)
8
It would probably help to rename the variables idxy12,idxy13 and idxy14
or so.
You also could write all this with loops using zip
, needing less copy-paste and making the code easier to change. (The goal is that each change should only happen at exact one spot):
JavaScript
1
23
23
1
import matplotlib.pyplot as plt
2
import pandas as pd
3
import numpy as np
4
5
col1 = [2011.005, 2012.6543, 2013.3456, 2014.7821, 2015.3421, 2016.7891, 2017.0173, 2018.1974]
6
col2 = [1.4356, "", 5.32245, 6.542, 7.567, .77558, "", ""]
7
col3 = [1.3345, 2.345, "", 5.356, 3.124, 6.12, "", ""]
8
col4 = [0.67, 4.235, "", 6.78, "", "", 9.56, ""]
9
col1 = pd.to_numeric(col1, errors='coerce')
10
col2 = pd.to_numeric(col2, errors='coerce')
11
col3 = pd.to_numeric(col3, errors='coerce')
12
col4 = pd.to_numeric(col4, errors='coerce')
13
14
plt.figure()
15
for col, color, label in zip([col2, col3, col4], ['b', 'g', 'r'], ['Sample 1', 'Sample 2', 'Sample 3']):
16
plt.plot(col1, col, ".", color=color, linewidth=1, label=label)
17
idx = np.isfinite(col1) & np.isfinite(col)
18
m, b = np.polyfit(col1[idx], col[idx], 1)
19
plt.plot(col1, m * col1 + b, color=color)
20
plt.grid()
21
plt.legend()
22
plt.show()
23