Different Scikit Learn R^2 results on different computers

Viewed 160

In the code below the 'correct' R2 value for sigma = 1 is 0.33, which I receive when run on my work computer. However on my personal computer I receive R2 = 0.119. This has been confirmed across multiple other computers running my exact code.

Only my personal computer produces this strange 0.119 result (even running the 'solution' code produces the 0.119 result). I have tried multiple clean installs of Anaconda to no avail.

Only thing I can think is that maybe my 'clean' installs aren't 'clean' enough. I have tried a few methods of fully deleting Anaconda and Python, maybe someone has a robust method for this?

x_peak
[2688.126327 2692.813829 2697.501331 2702.188833 2706.876334 2711.563836
 2716.251338 2720.93884  2725.626341 2730.313843 2735.001345 2739.688846
 2744.376348 2749.06385  2753.751352 2758.438853 2763.126355 2767.813857
 2772.501359 2777.18886  2781.876362 2786.563864 2791.251366 2795.938867
 2800.626369 2805.313871 2810.001373 2814.688874 2819.376376 2824.063878
 2828.75138  2833.438881 2838.126383 2842.813885 2847.501387 2852.188888
 2856.87639  2861.563892 2866.251394 2870.938895 2875.626397 2880.313899
 2885.0014   2889.688902 2894.376404 2899.063906 2903.751407 2908.438909
 2913.126411 2917.813913 2922.501414 2927.188916 2931.876418 2936.56392
 2941.251421 2945.938923 2950.626425 2955.313927 2960.001428 2964.68893
 2969.376432 2974.063934 2978.751435 2983.438937 2988.126439 2992.813941
 2997.501442 3002.188944 3006.876446 3011.563948 3016.251449 3020.938951
 3025.626453 3030.313954 3035.001456 3039.688958 3044.37646  3049.063961
 3053.751463 3058.438965 3063.126467 3067.813968 3072.50147  3077.188972
 3081.876474 3086.563975 3091.251477 3095.938979 3100.626481 3105.313982
 3110.001484 3114.688986 3119.376488 3124.063989 3128.751491 3133.438993
 3138.126495 3142.813996 3147.501498 3152.189   ]
y_peak
[0.01  0.011 0.011 0.012 0.013 0.015 0.017 0.018 0.02  0.021 0.024 0.027
 0.029 0.03  0.031 0.033 0.034 0.036 0.037 0.039 0.04  0.043 0.047 0.049
 0.052 0.055 0.058 0.062 0.066 0.071 0.077 0.085 0.097 0.111 0.141 0.169
 0.183 0.235 0.265 0.324 0.35  0.396 0.421 0.45  0.467 0.486 0.514 0.51
 0.464 0.444 0.437 0.432 0.432 0.437 0.442 0.45  0.475 0.501 0.541 0.553
 0.594 0.611 0.611 0.607 0.612 0.607 0.521 0.471 0.424 0.331 0.264 0.216
 0.161 0.114 0.094 0.054 0.034 0.021 0.014 0.008 0.007 0.005 0.004 0.003
 0.003 0.002 0.002 0.002 0.001 0.001 0.001 0.001 0.    0.    0.    0.
 0.    0.    0.    0.   ]
import numpy as np
import pandas as pd
import pylab as plt
from sklearn.linear_model import LinearRegression

df = pd.read_csv('data/ethanol_IR.csv')
x_all = df['wavenumber [cm^-1]'].values
y_all = df['absorbance'].values

x_peak = x_all[475:575]
y_peak = y_all[475:575]

x_train = x_peak[::3]
y_train = y_peak[::3]

sigmas = [1, 10, 50, 100, 150]

def rbf(x_train, x_test=None, gamma=1):
    if x_test is None:
        x_test = x_train
    N = len(x_test) #<- number of data points
    M = len(x_train) #<- number of features
    X = np.zeros((N,M))
    for i in range(N):
        for j in range(M):
            X[i,j] = np.exp(-gamma*(x_test[i] - x_train[j])**2)
    return X

model_rbf = LinearRegression() #create a linear regression model instance

n = len(sigmas)

def gam(sigma):
    gam = 1./(2*sigma**2)
    return gam

for i in range(n):
    total = []
    gamma = gam(sigmas[i])
    print('Sigma = {}'.format(sigmas[i]))
    X_train = rbf(x_train, gamma=gamma)
    model_rbf.fit(X_train, y_train) #fit the model
    r2 = model_rbf.score(X_train, y_train) #get the "score", which is equivalent to r^2
    print('r^2 training = {}'.format(r2))
    X_all = rbf(x_train, x_test=x_peak, gamma=gamma)
    yhat = model_rbf.predict(X_all)
    r2 = model_rbf.score(X_all, y_peak) #get the "score", which is equivalent to r^2
    print('r^2 testing = {}'.format(r2))
0 Answers
Related