Tin học Đánh giá tín dụng khách hàng

Ishigami Senku

Học sinh mới
Thành viên
20 Tháng một 2022
44
62
16
19
Hà Nội
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

*Các câu lệnh *
1. # đọc dữ liệu từ tệp
import pandas as pd
2. # tiền xử lý dữ liệu
from sklearn.preprocessing import LabelEncoder
3. # sơ đồ thể hiện mối quan hệ
import matplotlib.pyplot as plt
4. # sử dụng thuật toán học máy Naive Bayes
from sklearn.naive_bayes import MultinomialNB
5. # Lưu trữ mô hình học ra tệp
import joblib
joblib.dump(model, "tên tệp")
6. # đọc mô hình từ tệp
model_loaded=joblib.load("tên tệp")
7. # Lựa chọn kỹ thuật Naive Bayes cho máy học
model=MultinomialNB()
7. from sklearn.model_selection import train_test_split
8.#kiểm tra thử mô hính
from sklearn.linear_model import LinearRegression
9. import pickle


*DỮ LIỆU CSV *
1. # đọc dữ liệu từ tập CSV
import pandas as pd
df=pd.read_csv("Link")

2. # hiển thị số dòng cột
print("số dòng:",df.shape[0])
print("số cột:",df.shape[1])
df.shape

3. # hiển thị n dòng dữ liệu đầu tiên
df.head(n)

4. # hiển thị kiểu dữ liệu của các thuộc tính
df.dtypes

5. # tiền xử lý dữ liệu
from sklearn.preprocessing import LabelEncoder
lb_make=LabelEncoder()
df["biến độc lập"]=lb_make.fit_transform(df["biến độc lập"])
....
df.head(n)

6. # Tách dữ liệu biến phụ thuộc
df_biến phụ thuộc=df[df['tên cột biến phụ thuộc']== số chuyển đổi của biến phụ thuộc từ chữ sang số]
print("đề bài:"+str(df_biến phụ thuộc.shape[số không phải số chuyển đổi của biến phụ thuộc]))
print9("Dữ liệu biến phụ thuộc")
print(df_biến phụ thuộc)

7. # biểu đồ thể hiện mqh
import matplotlib.pyplot as plt
plt.xlabel('biến độc lập cần thể hiện mqh')
plt.ylabel('biến độc lập cần thể hiện mqh')
plt.suptitle('Mối quan hệ giữa..')
# hiển thị nhãn của các năm
plt.xticks(rotation= max của biến x)
plt.xticks(df_biến phụ thuộc['tên biến độc lập x'])
plt.yticks(rotation= max của biến y)
plt.yticks(df_biến phụ thuộc['tên biến độc lập y'])
plt.bar(df_biến phụ thuộc['biến độc lập x'],df_biến phụ thuộc['biến độc lập y'])
plt.show()

8. # lập mô hình dự đoán 1 biến độc lập
from sklearn.preprocessing import LabelEncoder
ln_make=LabelEncoder()
df["biến độc lập"]=lb_make.fit_transform(df["biến độc lập"])
....
(X=thuộc tính
y=biến độc lập cần dự đoán)
features=['thuộc tính 1, thuộc tính 2...']
target=['biến độc lập dự đoán']
X=df[features]
y=df[target]
print(X)
print(y)
#chia dữ liệu thành 2 tập train&test theo tỷ lệ
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split (X,y,test_size=tỷ lệ % của test)
print(X_train)
print(y_train)
print(X_test)
print(y_test)
#kiểm tra thử mô hình
from sklearn.linear_model import LinearRegression
model=LinearRegression()
print("Mô hình hồi quy có dạng: Tuoi=a+b *Diadiem+c *Thunhap+d *Thanhpham+e")
print(model.intercept_)
print(model.coef_)
y_pred=model.predict(X_test)
print(y_pred)
f=model.score(X_test,y_test)
print("độ chính xác của mô hình",f)


9. # dự đoán khách hàng mới
#biến độc lập 1 - a (gắn số ở bảng chuyển đổi số liệu)
....
new_customer = [[a,b,..]]
predicted_label=model.predicted(new_customer)
if predicted_label == c (gắn số ở bảng chuyển đổi số liệu):
print("biến dự đoán")
else:
print("trường hợp khác")


DỮ LIỆU EXCEL: TEST & TRAIN

# Khai báo thư viện
# Thư viện dùng để đọc dữ liệu từ tệp
import pandas as pd
# Thư viện dùng để tiền xử lý dữ liệu
from sklearn.preprocessing import LabelEncoder
# Thư viện dùng để sử dụng thuật toán học máy Naive Bayes
from sklearn.naive_bayes import MultinomialNB
# Thư viện dùng để lưu trữ mô hình học ra tệp
import joblib

# Đọc dữ liệu từ tệp trong thư mục GG Drive của bạn
train_data = pd.read_excel("/content/drive/My Drive/train.xlsx")
test_data = pd.read_excel("/content/drive/My Drive/test.xlsx")

# Số lượng mẫu và thuộc tính của tập huấn luyện
train_data.shape

# Số lượng mẫu và thuộc tính của tập kiểm thử
test_data.shape

# Hiển thị kiểu dữ liệu của các thuộc tính
train_data.dtypes

# tiền xử lý dữ liệu
from sklearn.preprocessing import LabelEncoder
lb_make=LabelEncoder()
# biến đổi tập train
train_data["biến độc lập"]=lb_make.fit_transform(train_data["biến độc lập"])
# biến đổi tập test
test_data["biến độc lập"]=lb_make.fit_transform(test_data["biến độc lập"])
#xem thử 10 dòng biến đổi
train_data.head(10)

# xây dựng mô hình
# Chuẩn bị tập dữ liệu huấn luyện (train) và tập dữ liệu kiểm thử (test)
# Xác định các thuộc tính độc lập và thuộc tính phụ thuộc
features = ['biến độc lập ',
'biến độc lập',
..........]
target = ['biến cần xây dựng']
X_train = train_data[features]
y_train = train_data[target]
X_test = test_data[features]
y_test = test_data[target]
# Lựa chọn kỹ thuật Naive Bayes cho máy học
model = MultinomialNB()
# Đưa dữ liệu vào huấn luyện mô hình
model = model.fit(X_train, y_train)

# Kiểm thử mô hình & Đánh giá độ chính xác của mô hình
# Kiểm thử mô hình trên tập test
from sklearn import metrics
accuracy_score = metrics.accuracy_score(y_test, model.predict(X_test))
print("Độ chính xác của mô hình xếp hạng tín dụng: ", accuracy_score * 100, "%")
# Các độ đo nâng cao được sử dụng khi bộ dữ liệu mất cân bằng
#balanced_accuracy = metrics.balanced_accuracy_score(y_test, model.predict(X_test))
#f1_score = metrics.f1_score(y_test, model.predict(X_test), average = 'macro')
#print("balanced_accuracy: ", balanced_accuracy)
#print("F1 score: ", f1_score)

# sử dụng mô hình
# gán số bất kì cho các biến đánh giá khách hàng mới
new_customer=[[a,b,c..]]
predicted_label = model.predicted(new_customer)
if predicted_label == 0/1 :
print("đặc điểm KH cần đánh giá")
else:
print("đặc điểm khách hàng cần đánh giá")
 
Top Bottom