0

> this is the error i am getting, i am new to python please help with this.

##########################################################################

ValueError                                Traceback (most recent call last)
Cell In[97], line 4
      1 LR= LinearRegression()
      3 #fit
----> 4 LR.fit(X,Y)
      6 #predict
      7 y_predict = LR.predict(X_test)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\linear_model\_base.py:649, in LinearRegression.fit(self, X, y, sample_weight)
    645 n_jobs_ = self.n_jobs
    647 accept_sparse = False if self.positive else ["csr", "csc", "coo"]
--> 649 X, y = self._validate_data(
    650     X, y, accept_sparse=accept_sparse, y_numeric=True, multi_output=True
    651 )
    653 sample_weight = _check_sample_weight(
    654     sample_weight, X, dtype=X.dtype, only_non_negative=True
    655 )
    657 X, y, X_offset, y_offset, X_scale = _preprocess_data(
    658     X,
    659     y,
   (...)
    662     sample_weight=sample_weight,
    663 )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\base.py:554, in BaseEstimator._validate_data(self, X, y, reset, validate_separately, **check_params)
    552         y = check_array(y, input_name="y", **check_y_params)
    553     else:
--> 554         X, y = check_X_y(X, y, **check_params)
    555     out = X, y
    557 if not no_val_X and check_params.get("ensure_2d", True):

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\utils\validation.py:1104, in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
   1099         estimator_name = _check_estimator_name(estimator)
   1100     raise ValueError(
   1101         f"{estimator_name} requires y to be passed, but the target y is None"
   1102     )
-> 1104 X = check_array(
   1105     X,
   1106     accept_sparse=accept_sparse,
   1107     accept_large_sparse=accept_large_sparse,
   1108     dtype=dtype,
   1109     order=order,
   1110     copy=copy,
   1111     force_all_finite=force_all_finite,
   1112     ensure_2d=ensure_2d,
   1113     allow_nd=allow_nd,
   1114     ensure_min_samples=ensure_min_samples,
   1115     ensure_min_features=ensure_min_features,
   1116     estimator=estimator,
   1117     input_name="X",
   1118 )
   1120 y = _check_y(y, multi_output=multi_output, y_numeric=y_numeric, estimator=estimator)
   1122 check_consistent_length(X, y)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\utils\validation.py:919, in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)
    913         raise ValueError(
    914             "Found array with dim %d. %s expected <= 2."
    915             % (array.ndim, estimator_name)
    916         )
    918     if force_all_finite:
--> 919         _assert_all_finite(
    920             array,
    921             input_name=input_name,
    922             estimator_name=estimator_name,
    923             allow_nan=force_all_finite == "allow-nan",
    924         )
    926 if ensure_min_samples > 0:
    927     n_samples = _num_samples(array)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\utils\validation.py:111, in _assert_all_finite(X, allow_nan, msg_dtype, estimator_name, input_name)
    109 if X.dtype == np.dtype("object") and not allow_nan:
    110     if _object_dtype_isnan(X).any():
--> 111         raise ValueError("Input contains NaN")
    113 # We need only consider float arrays, hence can early return for all else.
    114 if X.dtype.kind not in "fc":

ValueError: Input contains NaN

###################################################################333

This is the code i am trying and getting the error above.

LR= LinearRegression(normalize=True)

#fit

LR.fit(X,Y)

#predict

y_predict = LR.predict(X_test)

2 Answers2

0

it looks like the x and y values youre passing to the fit function are not properly formatted, which is causing the issue. specifically it seems like one of the values in x might be null. im not familiar with the modules you used but checking to make sure the x and y are set correctly is a good first step.

avnogy
  • 31
  • 3
0

Use train_test_split, LinearRegression simple code:

   X = dataset[['statezip','city','bedrooms','sqft_living','sqft_lot','sqft_above','floors',]]
   y = dataset['price']

    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=0)
    
    
    from sklearn import linear_model          
    import numpy as np
    
    regr = linear_model.LinearRegression()
    regr.fit(X_train,y_train)
    
    y_pred_RMSE = regr.predict(X_test)