Linear Regression with python

0

I need to do the linear regression calculation, but I read that there is no possibility to use / install scipy on windows. Are there any other libraries similar to scipy for this type of calculation? Or if there is some way to install scipy on windows, it's also welcome! Thank you.

    
asked by anonymous 07.05.2017 / 01:05

2 answers

3

From what I read on the site scipy runs on Windows yes.

  

[Google Translate] For most users, especially Windows, the easiest way to install SciPy stack packages is to download one of these Python distributions, which include all major packages: Anaconda , Enthought Canopy , Python (x, y) , WinPython , Pyzo

Try one of these:

  • Anaconda : A free distribution for the SciPy stack. Compatible with Linux, Windows and Mac.
  • Enthought Canopy : Free and commercial versions include SciPy's main stack packages. Supports Linux, Windows and Mac.
  • Python (x, y) : A free distribution including the SciPy stack, based around the Spyder IDE. Windows only.
  • WinPython : A free distribution including the SciPy stack. Windows only.
  • Pyzo : A free distribution based on Anaconda and the interactive development environment of the IEP. Supports Linux, Windows and Mac.
07.05.2017 / 17:13
2

According to own documentation of scipy it does not work very well in Windows because it has some dependencies that work only in linux and mac

As an alternative, I recommend using sklearn , it is a very good lib working with machine learning, and it also has a good documentation as well as several examples.

To install it you can:

pip install -U scikit-learn

or if you use anaconda:

conda install scikit-learn

I made an example using numpy (to work with arrays) and matplot (to work with graphics) To install:

pip install numpy

python -m pip install -U pip setuptools

python -m pip install matplotlib

In anaconda these libs are usually already installed.

Below is the example of creating a linear regression with sklearn

    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn import linear_model

    #Logica x = x*10 + acc
    #acc = acc + 5
    #acc inicia em 0

    #dataSet treino
    #1 - 10 + 0 = 10
    #2 - 20 + 5 = 25
    #3 - 30 + 10 = 40
    #4 - 40 + 15 = 55
    x_train = np.array([ [1], [2], [3], [4] ]);
    y_train = np.array([ 10, 25, 40, 55 ]);

    #dataSet teste
    #5 - 50 + 20 = 70
    #6 - 60 + 25 = 85
    #7 - 70 + 30 = 100
    #8 - 80 + 35 = 115
    x_test = np.array([ [5], [6], [7], [8] ])
    y_test = np.array([ 70, 85, 100, 115 ])

    #cria o modelo e faz o treinamento (fit)
    model = linear_model.LinearRegression().fit(x_train, y_train)

    #exibe algumas informações
    print('Coeficientes: \n', model.coef_)
    print("Erro médio quadrado: %.2f" % np.mean((model.predict(x_test) - y_test) ** 2))
    print('variância de score: %.2f' % model.score(x_test, y_test))

    #monta o plot para exibição do resultado
    plt.scatter(x_test, y_test,  color='black')
    plt.plot(x_test, model.predict(x_test), color='blue', linewidth=3)
    plt.xticks(())
    plt.yticks(())
    plt.show()

This code will generate a graph like this:

There is also an example of the documentation of sklearn with linear regression applied on diabetes tests:

Linear Regression Example

    
07.05.2017 / 16:31