如何为机器学习项目创建数据

How to create data for machine learning project

我正在从事一个机器学习项目,我正在为用户创建数据。数据包括 his/her 年龄、工作年限、城市、业务类型和任何以前的贷款。数据规则如下

  1. 如果用户年龄大,经验丰富,生意不错,以前没有贷款,那么会向他提供贷款

  2. 如果用户年龄大,经验少,生意不错,之前没有贷款,则不会向他提供贷款

  3. 如果用户年龄大,经验丰富,生意好,有贷款,则不会提供贷款

就像这样,我创建了一个包含所有这些数据的 csv 文件。下面是 link 到 csv 文件

https://drive.google.com/file/d/1zhKr8YR951Yp-_mC23hROy7AgJoRpF0m/view?usp=sharing

此文件包含年龄、经验、城市(用 2-9 的值表示)、业务类型(用 7-8 的值表示)、以前的贷款(用 0 表示)和最终输出为 YES 的数据(1) 或否(0)

我正在使用以下代码来训练模型并预测是否允许用户贷款的天气

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model


data = pd.read_csv("test.csv")
data.head()

X = data[['AGE', 'Experience', 'City', 'Business', 'Previous Loan']]
Y = data["Output"]

train = data[:(int((len(data) * 0.8)))]
test = data[(int((len(data) * 0.8))):]

regr = linear_model.LinearRegression()
train_x = np.array(train[['AGE', 'Experience', 'City', 'Business', 'Previous Loan']])
train_y = np.array(train["Output"])
regr.fit(train_x, train_y)
test_x = np.array(test[['AGE', 'Experience', 'City', 'Business', 'Previous Loan']])
test_y = np.array(test["Output"])

coeff_data = pd.DataFrame(regr.coef_, X.columns, columns=["Coefficients"])
print(coeff_data)

# Now let's do prediction of data:
test_x2 = np.array([[41, 13, 9, 7, 0]])  # <- Here I am using some random values to test 
Y_pred = regr.predict(test_x2)

运行 上面的代码,我得到 Y_pred 的值作为 0.015430.884 或有时 1.034。我无法理解此输出的含义。最初我虽然可能 0.01543 意味着信心不足,因此不会提供贷款,而 0.884 意味着信心高,因此将提供贷款。那是对的吗。谁能帮我理解一下。

任何人都可以向我提供 link 机器学习的基本示例,让我开始处理这些类型的场景。谢谢

不,你做错了!您必须输出 1 或 0。因此,这是一个分类问题,而不是回归问题。使用一些分类算法,如逻辑回归而不是线性回归。

clf = linear_model.LogisticRegression()
train_x = np.array(train[['AGE', 'Experience', 'City', 'Business', 'Previous Loan']])
train_y = np.array(train["Output"])
clf.fit(train_x, train_y)

test_x = np.array(test[['AGE', 'Experience', 'City', 'Business', 'Previous Loan']])
test_y = np.array(test["Output"])

test_x2 = np.array([[41, 13, 9, 7, 0]])
Y_pred = clf.predict(test_x2)

并删除 coeff_data 行,因为它没有用。如果要检查系数,则直接使用此代码:

clf.coef_

检查 this link,它对机器学习贷款审批有很好的解释