使用sklearn从文件中提取数组

时间:2018-08-06 16:35:42

标签: python scikit-learn

这是我的代码:

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))

请,我想从这样的文件中提取X和y:

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

(这是一个示例。我的文件包含100多个ligne)。 我必须使用什么方法? 此致。

1 个答案:

答案 0 :(得分:1)

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

在此代码中,x是|之前的整数,y是|之后的整数。

总代码为:

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))