生成一下上面例子的代码
时间: 2023-12-03 09:04:19 浏览: 64
好的,这里给出一个使用Python和Scikit-learn库进行Logistic回归的例子代码:
```python
# 导入所需的库
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 读取数据集
data = pd.read_csv('data.csv')
# 数据预处理
# ...
# 划分数据集
X = data.drop('purchase', axis=1)
y = data['purchase']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 构建模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 预测新数据
new_data = pd.DataFrame({'age': [30], 'gender': ['male'], 'income': [50000], 'education': ['bachelor'], 'occupation': ['engineer']})
new_data['gender'] = new_data['gender'].apply(lambda x: 1 if x == 'male' else 0)
new_data = pd.get_dummies(new_data, columns=['education', 'occupation'])
y_pred = model.predict(new_data)
# 输出预测结果
print('Prediction:', y_pred[0])
```
需要注意的是,上面的代码仅仅是一个示例,实际应用中需要根据数据集的具体情况进行调整和修改。
阅读全文