'How do I slices dataset using iloc?
I have a data set looks like the following:
34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45.08327747668339,56.3163717815305,0
61.10666453684766,96.51142588489624,1
I used pandas the read the files and using iloc to separate my x&y values and my boolean values.
df=pd.read_csv("ex2data1.txt",header=None)
X=df.iloc[:,:-1].values
y=df.iloc[:,-1].values
Therefore I got the x[] as
array([[34.62365962, 78.02469282],
[30.28671077, 43.89499752],
[35.84740877, 72.90219803],
[60.18259939, 86.3085521 ],
[79.03273605, 75.34437644],
[45.08327748, 56.31637178],
[61.10666454, 96.51142588]]
and my y[] as
array([0, 0, 0, 1, 1, 0, 1])
How do I separate my my data is my data looks like this
0.57548 0.53938 0.72311 0.23702 0.95864 0.85208 0.68642 0.35431 0.0095435 0.93523 0.21656 0.62107 0.31371 0.82675 0.61655 0.17468 0.12009 0.38317 0.35514 0.39439 1
0.70727 0.88503 0.62762 0.93851 0.20865 0.82238 0.08001 0.22381 0.18949 0.57738 0.39569 0.89592 0.37106 0.71963 0.5582 0.067821 0.29071 0.39012 0.68854 0.077076 1
Where there is no comma separating them and then there is 10 pairs of x&y values and one boolean value for each line
so for example if I just compile the first line of the data, it should return me the x[] as
([0.57548,0.53938], [0.72311,0.23702], [0.95864,0.85208], [0.68642,0.35431], [0.0095435, 0.93523], [0.21656,0.62107], [0.31371,0.82675], .......)
and then my y[] as
([1,1,1,1,1,1,1,1,1,1,1])
Solution 1:[1]
You can use the string function split to make a list which you can cast into float64 and then sort using list access operations. The following function will do the trick
def process_line(line):
my_list = np.array(line.strip().split(' ')).astype(np.float64)
x = np.array([my_list[:-1:2],my_list[1:-1:2]]).T
y = np.array([my_list[-1]]*len(x))
return x,y
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arnau |
