0

I have a dataset of cryptocurrency. I have features as ['Open', 'High', 'Low', 'Vol.', 'Change %'] and target variable as ['Price'].

I want to fit multiple regression even after converting the price column from string to float by using the code df['Price'] = df['Price'].apply(lambda x: x.replace(',', '') if type(x) is str else x)

After that when I try to fit the model : model = LinearRegression() model.fit(X, y)

It gives me value error as follows :

ValueError                                Traceback (most recent call last)
<ipython-input-23-ea6ca7c3c303> in <cell line: 3>()
      1 # Fit a linear regression model to the data
      2 model = LinearRegression()
----> 3 model.fit(X, y)

5 frames
/usr/local/lib/python3.10/dist-packages/pandas/core/generic.py in __array__(self, dtype)
   2068 
   2069     def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
-> 2070         return np.asarray(self._values, dtype=dtype)
   2071 
   2072     def __array_wrap__(

ValueError: could not convert string to float: '1,004.30'

Please help me in debugging. I am stuck here for long.

I tried with the code as

Convert the 'Price' column from strings to floats

df['Price'] = df['Price'].apply(lambda x: x.replace(',', '') if type(x) is str else x)

Fit a linear regression model to the data

model = LinearRegression() model.fit(X, y)

  • Your code that removes commas from `df['Price']` looks OK. Have you tried restarting the kernel and rerunning the notebook after making that change? – slothrop May 13 '23 at 13:54
  • Yes, I am using goggle colab for coding. – Sweta May 13 '23 at 15:29
  • OK, next question is then: how do you obtain your `X` and `y` from the dataframe? It seems like if you use the `Price` column after converting it, it would be OK. – slothrop May 13 '23 at 15:30
  • # Define the feature variables and target variable X = df[['Open', 'High', 'Low', 'Vol.', 'Change %']] y = df['Price'] – Sweta May 14 '23 at 04:18
  • Is it possible there are commas in any of the columns that make up `X`? Otherwise, can you edit your question to show your full code? The lines that you are showing so far seem to be fine, so the problem may be somewhere else. – slothrop May 14 '23 at 07:27
  • There are no commas in X. I have checked also. I want to send you the full code written by me along with the dataset as well. Where can I send you ?? – Sweta May 16 '23 at 10:57
  • Best is to edit your post to show the full code, and include a sample of your data according to the guidelines here: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – slothrop May 16 '23 at 16:46

0 Answers0