اذهب إلى المحتوى
  • 0

ظهور الخطأ التالي Expected 2D array, got 1D array instead عند محاولة تدريب نموذج في مكتبة Sklearn

Meezo ML

السؤال

عند محاولة تدريب model ( نموذج ) يظهر لي الخطأ التالي Expected 2D array, got 1D array instead  الكود:

from sklearn.preprocessing  import LabelEncoder
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# إنشاءداتا 
data = {'size': [100, 30, 50, 200, 2],
        'class': ['big', 'small', 'medium', 'verybig', 'verysmall']}
df = pd.DataFrame(data)
le = LabelEncoder()
le.fit(df['class'])
df['class'] = le.transform(df['class'])
X_train, X_test, y_train, y_test = train_test_split(df['size'], df['class'], random_state = 42, test_size = 0.33)
linreg = LinearRegression()
linreg.fit(X_train, y_train)
linreg.score(x_test,y_test)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-f740a0daa981> in <module>
     12 X_train, X_test, y_train, y_test = train_test_split(df['size'], df['class'], random_state = 42, test_size = 0.33)
     13 linreg = LinearRegression()
---> 14 linreg.fit(X_train, y_train)
     15 linreg.score(x_test,y_test)

~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in fit(self, X, y, sample_weight)
    490         n_jobs_ = self.n_jobs
    491         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 492                          y_numeric=True, multi_output=True)
    493 
    494         if sample_weight is not None:

~\anaconda3\lib\site-packages\sklearn\utils\validation.py 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, warn_on_dtype, estimator)
    753                     ensure_min_features=ensure_min_features,
    754                     warn_on_dtype=warn_on_dtype,
--> 755                     estimator=estimator)
    756     if multi_output:
    757         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

~\anaconda3\lib\site-packages\sklearn\utils\validation.py 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, warn_on_dtype, estimator)
    554                     "Reshape your data either using array.reshape(-1, 1) if "
    555                     "your data has a single feature or array.reshape(1, -1) "
--> 556                     "if it contains a single sample.".format(array))
    557 
    558         # in the future np.flexible dtypes will be handled like object dtypes

ValueError: Expected 2D array, got 1D array instead:
array=[ 50 100 200].

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

التوابع في مكتبة Sklearn تتعامل مع المصفوفات (numpy arrays) لذا يجب أن تقوم دوماً بتحويل بياناتك إلى numpy arrays قبل أن تدخلها في توابع وكلاسات مكتبة Sklearn.
للتحويل نستخدم التابع ()to_numpy كالتالي:

X=df['size'].to_numpy()
Y=df['class'].to_numpy()
# ثم قد يتطلب منك الأمر إعادة تعيين أبعادها
X=X.reshape(-1,n)# يحيث أن الوسيط الثاني سوف يمثل عدد الميزات

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...