'Numpy TypeError: expected non-empty vector for x
I have a dataframe with the columns are time points are the rows are patient test values (a mixture of positive and negative floats, and nan)
I want to fill in the missing data with polynomial regression, and then calculate a trapz score for every row of data (so x is timepoints, y is the rows values).
I wrote this:
import sys
import numpy as np
from sklearn.metrics import auc
import pandas as pd
from numpy import trapz
#time points
x = np.array([i for i in range(0,100)],dtype='int')
print(x)
for row in df.iterrows():
#y values
y = np.array(row[1:])[0][1:] #zero because it's in a double list
print(len(y))
y = y.astype('float64')
#treat the missing data
idx = np.isfinite(x) & np.isfinite(y)
#fit
ab = np.polyfit(x[idx], y[idx], idx.sum())
#replace missing values in list with polyval values
replace_nan = np.polyval(ab,x)
#calculate trapz
trapz = np.trapz(replace_nan)
#print output
print(row[0] + '\t' + str(trapz))
An example of an input y line fed into the system is:
[nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan 0.0 nan
-46203.799407 16874.075280000005 nan nan nan 17521.3281 nan nan nan nan
nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan]
The output and error is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
96 97 98 99]
100
Traceback (most recent call last):
File "polyreg_analysis.py", line 34, in <module>
ab = np.polyfit(x[idx], y[idx], idx.sum())
File "<__array_function__ internals>", line 5, in polyfit
File "/home/slowatkela/conda/envs/embedding_nlp/lib/python3.8/site-packages/numpy/lib/polynomial.py", line 630, in polyfit
raise TypeError("expected non-empty vector for x")
TypeError: expected non-empty vector for x
I don't understand how the x vector is empty, when I print it to screen I can see the list of time points? Can someone help?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
