Extract array from file using sklearn

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Extract array from file using sklearn



Here is my code:


import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline

X=np.array([[1, 2, 4]]).T
print(X)
y=np.array([1, 4, 16])
print(y)
model = make_pipeline(PolynomialFeatures(degree=2),
LinearRegression(fit_intercept = False))
model.fit(X,y)
X_predict = np.array([[3]])
print(model.predict(X_predict))



Plese, i'd like to extract X and y from a file like this:


x | y
1 | 1
2 | 4
4 | 16



(This is an example. My file contains more than 100 ligne).
What method i have to use ?
Kind regards.





is y = x**2 in all cases ?
– Agile_Eagle
Aug 6 at 16:43





No. my goal is to determine the proper degree of the polynomial by calculating each time the error rate between the value given on column y and the value predicted by the generated model. I need now just how can i read from the file
– user1543915
Aug 6 at 16:48





What is the extension of the file ?
– Agile_Eagle
Aug 6 at 16:50





the extension of the file is .txt
– user1543915
Aug 6 at 16:52





Should X=np.array([[1, 2, 4]]).T be X=np.array([1, 2, 4]).T ?
– Agile_Eagle
Aug 6 at 17:15



X=np.array([[1, 2, 4]]).T


X=np.array([1, 2, 4]).T




1 Answer
1


with open('input.txt') as fp:
for line in fp:
b = line.split("|")
x,y = b



In this code x is the integer before | and y is integer after |.


|


|



So total code would be:


import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
X_arr =
Y_arr =
with open('input.txt') as fp:
for line in fp:
b = line.split("|")
x,y = b
X_arr.append(int(x))
Y_arr.append(int(y))


X=np.array([X_arr]).T
print(X)
y=np.array(Y_arr)
print(y)
model = make_pipeline(PolynomialFeatures(degree=2),
LinearRegression(fit_intercept = False))
model.fit(X,y)
X_predict = np.array([[3]])
print(model.predict(X_predict))





Please, can i do that with open('dropbox.com/s/r7hrpq6go60wxk7/input.txt?dl=0') as fp: using jupyter.org/try ?
– user1543915
Aug 6 at 21:35





You will have to download it
– Agile_Eagle
Aug 7 at 0:40





And then download python and then run it
– Agile_Eagle
Aug 7 at 0:53





@user1543915 remove the first line in text file
– Agile_Eagle
Aug 7 at 11:36






Did it work ? I think it did :)
– Agile_Eagle
Aug 7 at 11:43






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS