如何从拆分数据集创建线性回归模型?

时间:2017-04-30 14:22:27

标签: python csv numpy matplotlib linear-regression

我只是将我的数据分成训练和测试集,我的计划是训练线性回归模型,并能够使用我的测试分割来检查性能是什么样的。

我目前的代码是:

import pandas as pd
import numpy as np
from sklearn import datasets, linear_model
import matplotlib.pyplot as plt

df = pd.read_csv('C:/Dataset.csv')
df['split'] = np.random.randn(df.shape[0], 1)
split = np.random.rand(len(df)) <= 0.75
training_set = df[split]
testing_set = df[~split]

我是否应该使用正确的方法从外部文件(例如.csv)绘制线性回归模型?

1 个答案:

答案 0 :(得分:0)

由于你想使用scikit-learn,这是一种使用sklearn.linear_model.LinearRegression的方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    //all other stuff of app here
    ...
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    // for getting menu from navigationView
    Menu menu = navigationView.getMenu();

    // finding menuItem that you want to change
    MenuItem nav_connection = menu.findItem(R.id.nav_connection);

    // set new title to the MenuItem"change name from connection to logout"
    nav_connection.setTitle("Logout");


    // add NavigationItemSelectedListener to check the navigation clicks
    navigationView.setNavigationItemSelectedListener(this);

}

根据您是否需要更多描述性输出,您还可以考虑使用from sklearn.linear_model import LinearRegression model = LinearRegression() X_train, y_train = training_set[x_vars], training_set[y_var] X_test, y_test = testing_test[x_vars], testing_test[y_var] model.fit(X_train, y_train) predictions = model.predict(X_test) 进行线性回归。