2

I have the data in a dataframe format that I will use for linear regression calculation using user-built function. Here is the code:

from sklearn.datasets import load_boston
boston = load_boston()

bos = pd.DataFrame(boston.data) # convert to DF
bos.columns = boston.feature_names
bos['PRICE'] = boston.target
y = bos.PRICE
x = bos.drop('PRICE', axis = 1)  # DROP PRICE since only want X-type variables (not Y-target)

xw = df.to_array(x)

xw = np.insert(xw,0,1, axis = 1) # to insert a column of "1" values

However, I am getting the error:

AttributeError                            Traceback (most recent call last)
<ipython-input-131-272f1b4d26ba> in <module>()
  1 import copy
  2 
----> 3 xw = df.to_array(x)

AttributeError: 'int' object has no attribute 'to_array'

I am not sure where the problem. I need to pass an array of values (x in this case) to the function to execute some matrix operations

The insert function was working in a step by step code development but for some reason is failing here.

I tried:

xw = copy.deepcopy(x)

with no success

Any thoughts?

Toly
  • 1,725
  • 6
  • 19
  • 30
  • I even checked the type after conversion (type(xw) and it gave me np.ndarray as a type. Not sure where the problem – Toly Sep 19 '15 at 00:24
  • Do `type(x)`, what do you get? Or possibly include a printout of `x` – Leb Sep 19 '15 at 00:45
  • I think I got it somehow. Not sure but what is working: xw = copy.deepcopy(x) xw = np.c_[np.ones(lnY), xw] Not sure this works (inserting a first column of "1" and the other way did not but here the result (lnY is the size of a target array (Y values) – Toly Sep 19 '15 at 00:50
  • 2
    What is `df`? It's not defined anywhere, but appears to be an `int`. Do you mean `x.as_matrix()`? – askewchan Sep 19 '15 at 02:21

1 Answers1

0

it is x.as_matrix() not df.to_array(x)
Please refer to pandas document for more detail on as_matrix()

Here is the code that work

from sklearn.datasets import load_boston
import pandas as pd
import numpy as np

boston = load_boston()

bos = pd.DataFrame(boston.data) # convert to DF
bos.columns = boston.feature_names
bos['PRICE'] = boston.target
y = bos.PRICE
x = bos.drop('PRICE', axis = 1)  # DROP PRICE since only want X-type variables (not Y-target)

xw = x.as_matrix()

xw = np.insert(xw,0,1, axis = 1) # to insert a column of "1" values
Haha TTpro
  • 3,921
  • 5
  • 28
  • 54