Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

Tôi đang cố gắng tạo ra một loạt các biến giả từ một biến phân loại sử dụng gấu trúc trong Python. Tôi đã bắt gặp hàm

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
3, nhưng bất cứ khi nào tôi cố gắng gọi nó, tôi nhận được một lỗi mà tên không được xác định.

Bất kỳ suy nghĩ hoặc cách khác để tạo ra các biến giả sẽ được đánh giá cao.

Chỉnh sửa: Vì những người khác dường như đang gặp phải điều này, chức năng

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
3 trong gấu trúc hiện hoạt động hoàn toàn tốt. Điều này có nghĩa là những điều sau đây sẽ hoạt động:: Since others seem to be coming across this, the
import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
3 function in pandas now works perfectly fine. This means the following should work:

import pandas as pd

dummies = pd.get_dummies(df['Category'])

Xem http://blog.yhathq.com/posts/logistic-regression-and-python.html để biết thêm thông tin.

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

Pirsquared

Phim thương hiệu vàng 274K5454 gold badges448 silver badges590 bronze badges

Đã hỏi ngày 20 tháng 7 năm 2012 lúc 22:33Jul 20, 2012 at 22:33

user1074057user1074057user1074057

1.7624 huy hiệu vàng20 Huy hiệu bạc29 Huy hiệu đồng4 gold badges20 silver badges29 bronze badges

Khi tôi nghĩ về các biến giả, tôi nghĩ đến việc sử dụng chúng trong bối cảnh hồi quy OLS, và tôi sẽ làm điều gì đó như thế này:

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()

Đã trả lời ngày 29 tháng 5 năm 2014 lúc 3:26May 29, 2014 at 3:26

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

5

Dựa trên tài liệu chính thức:

dummies = pd.get_dummies(df['Category']).rename(columns=lambda x: 'Category_' + str(x))
df = pd.concat([df, dummies], axis=1)
df = df.drop(['Category'], inplace=True, axis=1)

Ngoài ra còn có một bài viết hay trong blog FASTML.

Đã trả lời ngày 24 tháng 12 năm 2015 lúc 21:07Dec 24, 2015 at 21:07

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

1

Thật khó để suy luận những gì bạn đang tìm kiếm từ câu hỏi, nhưng dự đoán tốt nhất của tôi là như sau.

Nếu chúng tôi giả sử bạn có một khung dữ liệu trong đó một số cột là 'danh mục' và chứa các số nguyên (hoặc các định danh duy nhất) cho các danh mục, thì chúng ta có thể làm như sau.

Gọi DataFrame

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
5 và giả sử rằng với mỗi hàng,
import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
6 là một số giá trị trong tập hợp các số nguyên từ 1 đến N. sau đó,

for elem in dfrm['Category'].unique():
    dfrm[str(elem)] = dfrm['Category'] == elem

Bây giờ sẽ có một cột chỉ báo mới cho mỗi danh mục đúng/sai tùy thuộc vào việc dữ liệu trong hàng đó có trong danh mục đó hay không.

Nếu bạn muốn kiểm soát các tên danh mục, bạn có thể tạo một từ điển, chẳng hạn như

cat_names = {1:'Some_Treatment', 2:'Full_Treatment', 3:'Control'}
for elem in dfrm['Category'].unique():
    dfrm[cat_names[elem]] = dfrm['Category'] == elem

Để dẫn đến việc có các cột có tên được chỉ định, thay vì chỉ chuyển đổi chuỗi của các giá trị danh mục. Trong thực tế, đối với một số loại,

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
7 có thể không sản xuất bất cứ điều gì hữu ích cho bạn.

Đã trả lời ngày 21 tháng 7 năm 2012 lúc 2:29Jul 21, 2012 at 2:29

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

ELLYely

71.6K32 Huy hiệu vàng144 Huy hiệu bạc2222 Huy hiệu đồng32 gold badges144 silver badges222 bronze badges

1

Đối với trường hợp của tôi,

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
8 trong
import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
9 đã giải quyết vấn đề của tôi. Trên thực tế, chức năng này được thiết kế để tạo các biến phụ thuộc và độc lập từ một khung dữ liệu nhất định với chuỗi công thức kiểu R. Nhưng nó có thể được sử dụng cho việc tạo các tính năng giả từ các tính năng phân loại. Tất cả những gì bạn cần làm sẽ là thả cột 'chặn' được tạo bởi
import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
8 tự động bất kể dữ liệu gốc của bạn.

import pandas as pd
from patsy import dmatrices

df_original = pd.DataFrame({
   'A': ['red', 'green', 'red', 'green'],
   'B': ['car', 'car', 'truck', 'truck'],
   'C': [10,11,12,13],
   'D': ['alice', 'bob', 'charlie', 'alice']},
   index=[0, 1, 2, 3])

_, df_dummyfied = dmatrices('A ~ A + B + C + D', data=df_original, return_type='dataframe')
df_dummyfied = df_dummyfied.drop('Intercept', axis=1)

df_dummyfied.columns    
Index([u'A[T.red]', u'B[T.truck]', u'D[T.bob]', u'D[T.charlie]', u'C'], dtype='object')

df_dummyfied
   A[T.red]  B[T.truck]  D[T.bob]  D[T.charlie]     C
0       1.0         0.0       0.0           0.0  10.0
1       0.0         0.0       1.0           0.0  11.0
2       1.0         1.0       0.0           1.0  12.0
3       0.0         1.0       0.0           0.0  13.0

Đã trả lời ngày 23 tháng 9 năm 2016 lúc 18:06Sep 23, 2016 at 18:06

Erdem Kayaerdem KayaErdem KAYA

4291 Huy hiệu vàng4 Huy hiệu bạc13 Huy hiệu đồng1 gold badge4 silver badges13 bronze badges

Bạn có thể tạo các biến giả để xử lý dữ liệu phân loại

# Creating dummy variables for categorical datatypes
trainDfDummies = pd.get_dummies(trainDf, columns=['Col1', 'Col2', 'Col3', 'Col4'])

Điều này sẽ giảm các cột ban đầu trong TrainDF và nối cột với các biến giả ở cuối DataFdummies DataFrame.trainDf and append the column with dummy variables at the end of the trainDfDummies dataframe.

Nó tự động tạo tên cột bằng cách nối thêm các giá trị ở cuối tên cột gốc.

Đã trả lời ngày 21 tháng 5 năm 2017 lúc 23:28May 21, 2017 at 23:28

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

rzskhrrzskhrrzskhr

87111 Huy hiệu bạc9 Huy hiệu đồng11 silver badges9 bronze badges

Một cách tiếp cận rất đơn giản mà không sử dụng get_dummies nếu bạn có biến rất ít phân loại bằng cách sử dụng numpy và gấu trúc.without using get_dummies if you have very less categorical variable using NumPy and Pandas.

Đặt, tôi có một cột có tên và nó có 3 biến phân loại và chúng tôi muốn gán 0 và 1 cho tương ứng.

Chúng ta có thể làm điều đó với mã đơn giản sau đây.

import numpy as np
import pandas as pd

dataset['NewYork_State'] = np.where(dataset['State']=='New York', 1, 0)
dataset['California_State'] = np.where(dataset['State']=='California', 1, 0)
dataset['Florida_State'] = np.where(dataset['State']=='Florida', 1, 0)
 

Ở trên, chúng tôi tạo ba cột mới để lưu trữ các giá trị "NewYork_State", "California_state", "Florida_state".

dummies = pd.get_dummies(df['Category']).rename(columns=lambda x: 'Category_' + str(x))
df = pd.concat([df, dummies], axis=1)
df = df.drop(['Category'], inplace=True, axis=1)
1

dataset.drop(columns=['State'],axis=1,inplace=True)

Đã trả lời ngày 23 tháng 7 năm 2021 lúc 6:56Jul 23, 2021 at 6:56

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

Vì vậy, tôi thực sự cần một câu trả lời cho câu hỏi này ngày hôm nay (25/7/2013), vì vậy tôi đã viết điều này sớm hơn. Tôi đã thử nghiệm nó với một số ví dụ đồ chơi, hy vọng bạn sẽ nhận được một số dặm từ nó

def categorize_dict(x, y=0):
    # x Requires string or numerical input
    # y is a boolean that specifices whether to return category names along with the dict.
    # default is no
    cats = list(set(x))
    n = len(cats)
    m = len(x)
    outs = {}
    for i in cats:
        outs[i] = [0]*m
    for i in range(len(x)):
        outs[x[i]][i] = 1
    if y:
        return outs,cats
    return outs

Đã trả lời ngày 25 tháng 7 năm 2013 lúc 0:12Jul 25, 2013 at 0:12

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

1

Tôi đã tạo một biến giả cho mọi trạng thái bằng cách sử dụng mã này.

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
0

Tổng quát hơn, tôi sẽ chỉ sử dụng. Apply và chuyển nó một chức năng ẩn danh với sự bất bình đẳng xác định danh mục của bạn.

(Cảm ơn @prpl.mnky.dshwshr cho cái nhìn sâu sắc .unique ())

Đã trả lời ngày 20 tháng 12 năm 2014 lúc 5:51Dec 20, 2014 at 5:51

userfoguserfoguserFog

9.9711 Huy hiệu vàng14 Huy hiệu bạc7 Huy hiệu đồng1 gold badge14 silver badges7 bronze badges

Xử lý các tính năng phân loại Scikit-Learn mong đợi tất cả các tính năng sẽ có số. Vậy làm thế nào để chúng tôi bao gồm một tính năng phân loại trong mô hình của chúng tôi?

Các loại được đặt hàng: Chuyển đổi chúng thành các giá trị số hợp lý (ví dụ: Small = 1, Medium = 2, Lớn = 3) Danh mục không được đặt hàng: Sử dụng mã hóa giả (0/1) Các tính năng phân loại trong bộ dữ liệu của chúng tôi là gì?

Các loại được đặt hàng: thời tiết (đã được mã hóa với các giá trị số hợp lý) Các loại không được đặt hàng: mùa (nhu cầu mã hóa giả), kỳ nghỉ (đã được mã hóa giả), ngày làm việc (đã được mã hóa giả) cho mùa , 2 = mùa hè, 3 = mùa thu và 4 = mùa đông, bởi vì đó sẽ ngụ ý một mối quan hệ theo thứ tự. Thay vào đó, chúng tôi tạo nhiều biến giả:

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
1

Đã trả lời ngày 5 tháng 4 năm 2018 lúc 7:38Apr 5, 2018 at 7:38

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

Một cách đơn giản và mạnh mẽ để tạo hình nộm dựa trên một cột với các giá trị danh mục của bạn:

import numpy as np
import pandas as pd
import statsmodels.api as sm

my_data = np.array([[5, 'a', 1],
                    [3, 'b', 3],
                    [1, 'b', 2],
                    [3, 'a', 1],
                    [4, 'b', 2],
                    [7, 'c', 1],
                    [7, 'c', 1]])                


df = pd.DataFrame(data=my_data, columns=['y', 'dummy', 'x'])
just_dummies = pd.get_dummies(df['dummy'])

step_1 = pd.concat([df, just_dummies], axis=1)      
step_1.drop(['dummy', 'c'], inplace=True, axis=1)
# to run the regression we want to get rid of the strings 'a', 'b', 'c' (obviously)
# and we want to get rid of one dummy variable to avoid the dummy variable trap
# arbitrarily chose "c", coefficients on "a" an "b" would show effect of "a" and "b"
# relative to "c"
step_1 = step_1.applymap(np.int) 

result = sm.OLS(step_1['y'], sm.add_constant(step_1[['x', 'a', 'b']])).fit()
print result.summary()
2

Nhưng coi chừng khi thực hiện một số hồi quy OLS vì bạn sẽ cần loại trừ một trong các danh mục để bạn không rơi vào biến Bẫy Dummie

Đã trả lời ngày 6 tháng 11 năm 2021 lúc 12:55Nov 6, 2021 at 12:55

Hướng dẫn how do you make a dummy variable in python? - làm thế nào để bạn tạo một biến giả trong python?

RamonramonRamon

356 Huy hiệu Đồng6 bronze badges

Làm thế nào để bạn tạo một biến giả?

Có hai bước để thiết lập thành công các biến giả trong hồi quy bội: (1) tạo các biến giả đại diện cho các loại của biến độc lập phân loại của bạn; và (2) nhập các giá trị vào các biến giả này - được gọi là mã hóa giả - để thể hiện các loại của độc lập phân loại ...create dummy variables that represent the categories of your categorical independent variable; and (2) enter values into these dummy variables – known as dummy coding – to represent the categories of the categorical independent ...

Lệnh nào được sử dụng để có được biến giả trong Python?

Hàm get_dummies () được sử dụng để chuyển đổi biến phân loại thành các biến giả/chỉ báo.get_dummies() function is used to convert categorical variable into dummy/indicator variables.

Làm thế nào để bạn làm mã hóa giả trong Python?

Để thực hiện mã hóa giả, hãy đặt tham số này thành 'Đầu tiên' làm giảm danh mục đầu tiên của mỗi biến.Sắp sáng - Đặt điều này thành FALSE để trả lại đầu ra dưới dạng mảng numpy.Mặc định là đúng trả về một ma trận thưa thớt.set this parameter to 'first' that drops the first category of each variable. sparse — Set this to False to return the output as a NumPy array. The default is True which returns a sparse matrix.

Là biến giả 0 hay 1?

Một biến giả chỉ có 1 và 0.Số 1 và 0 không có ý nghĩa số (định lượng).Hai số được sử dụng để đại diện cho các nhóm.Trong biến giả ngắn là phân loại (định tính).1 and 0 only. The number 1 and 0 have no numerical (quantitative) meaning. The two numbers are used to represent groups. In short dummy variable is categorical (qualitative).