<jupyter_start><jupyter_text>Data Mining     درس داده کاوی                 تمرین های درس داده کاوی، الگوریتم ها و کاربرد ها - دکتر مربی                دانشجو: مصطفی کریمی                  ============== پروژه سوم و چهارم ==============                یک فایل شامل متغیر کمی و متغیر اسمی با حجم بیش از 200 داده که لااقل یکی از متغیر های کمی شامل گمشده نیز باشد انتخاب کرده.  DescriptionAbout data setThis database contains 76 attributes, but all published experiments refer to using a subset of 14 of them. In particular, the Cleveland database is the only one that has been used by ML researchers tothis date.The "target" field refers to the presence of heart disease in the patient. It is integer valued 0 = no/less chance of heart attack and 1 = more chance of heart attackAttribute Information> - 1)  age> - 2)  sex> - 3)  chest pain type (4 values)> - 4)  resting blood pressure> - 5)  serum cholestoral in mg/dl> - 6)  fasting blood sugar > 120 mg/dl> - 7)  resting electrocardiographic results (values 0,1,2)> - 8)  maximum heart rate achieved> - 9)  exercise induced angina> - 10) oldpeak = ST depression induced by exercise relative to rest> - 11) the slope of the peak exercise ST segment> - 12) number of major vessels (0-3) colored by flourosopy> - 13) thal: 0 = normal; 1 = fixed defect; 2 = reversable defect> - 14) target: 0= less chance of heart attack 1= more chance of heart attackReference: [Kaggle](https://www.kaggle.com/nareshbhat/health-care-data-set-on-heart-attack-possibility)<jupyter_code>import pandas as pd

heart = pd.read_csv("heart.csv")

print(heart[1:5])
heart.info()<jupyter_output>age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \
1   37    1   2       130   250    0        1      187      0      3.5      0   
2   41    0   1       130   204    0        0      172      0      1.4      2   
3   56    1   1       120   236    0        1      178      0      0.8      2   
4   57    0   0       120   354    0        1      163      1      0.6      2   

   ca  thal  target   sex_n             thal_n                     target_n  \
1   0     2       1    male  reversable defect  more chance of heart attack   
2   0     2       1  female  reversable defect  more chance of heart attack   
3   0     2       1    male  reversable defect  more chance of heart attack   
4   0     2       1  female  reversable defect  more chance of heart attack   

   chol_n compare  
1     250          
2     204          
3     236          
4     354          
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 303 entries, 0 to 302
Data columns (total 19 col[...]<jupyter_text>فراخوانی داده ها و انتقال آن ها در ماتریس های مشخص<jupyter_code>heart_age = [heart['age'][i] for i in range(303)]
heart_sex = [heart['sex_n'][i] for i in range(303)]
heart_chol = [heart['chol'][i] for i in range(303)]


print("\t Age of Person: \n{}\n\n\t Sex of Person:\n{}\n\n\t Serum Cholestoral:\n{}".format(heart_age, heart_sex, heart_chol))
import random as rd

r = [rd.randint(0, 302) for i in range(3)]

print(r,"\n")

[print("Age Person {} is {}, gender is {} and cholestoral is {} \n".format(r[i], heart_age[r[i]], heart_sex[r[i]], heart_chol[r[i]])) for i in range(3)]<jupyter_output>[125, 145, 30] 

Age Person 125 is 34, gender is female and cholestoral is 210 

Age Person 145 is 70, gender is male and cholestoral is 245 

Age Person 30 is 41, gender is female and cholestoral is 198<jupyter_text>داده های گمشده در متغیر های کمی را جایگذاری کنید و در متغیر های جدید ذخیره کنید. بررسی برای سن افراد<jupyter_code>import matplotlib.pyplot as plt
import numpy as np

bins = np.arange(20, max(heart_age)+20, 2)

a = plt.hist(heart_age, bins = bins, facecolor='g')

plt.xlabel("Age of Person")
plt.ylabel("Count")
plt.title("Histogram of Person Heart Age")
plt.text(20, 25, r'$M-Karimi$')
plt.ylim(0, 38)
plt.grid(True)
plt.show()

import seaborn as sb

sb.distplot(heart_age);<jupyter_output><empty_output><jupyter_text>بررسی برای میزان کلسترول<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import seaborn as sb

bins = np.arange(0, max(heart_chol)+1, 10)

a = plt.hist(heart_chol, bins = bins, facecolor='g')



plt.xlabel("Cholestoral of Person")
plt.ylabel("Count")
plt.title("Histogram of Person Cholestoral")
plt.text(20, 25, r'$M-Karimi$')
plt.ylim(0, 33)
plt.grid(True)
plt.show()

sb.distplot(heart_chol);<jupyter_output><empty_output><jupyter_text>پراکندی داده ها یا Scatterplots<jupyter_code>plt.scatter(data = heart, x = 'age', y = 'chol');

plt.xlabel("Age")
plt.ylabel("Cholestoral");
sb.regplot(data = heart, x = 'age', y = 'chol');<jupyter_output><empty_output><jupyter_text>ایجاد دامنه پراکندگی و جایگزینی آن با میانه کلسترول<jupyter_code>import statistics as stc

h_median = stc.median(heart_chol)
print("Median is: {}".format(h_median))

h_Qua = stc.quantiles(heart_chol)
h_IQR = h_Qua[2] - h_Qua[0]

print("Quantile: {}".format(h_Qua))

h_DisIQR = h_IQR * 1.5
print("Distance of IQR: {}".format(h_DisIQR))

h_IQLow = h_Qua[0] - h_DisIQR
h_IQHig = h_Qua[2] + h_DisIQR

print("Low is [* {} *] and High is [* {} *]".format(h_IQLow, h_IQHig))<jupyter_output>Median is: 240
Quantile: [211.0, 240.0, 275.0]
Distance of IQR: 96.0
Low is [* 115.0 *] and High is [* 371.0 *]<jupyter_text>حذف نوییز ها از داده های کلسترول و جایگزینی آن ها با میانه<jupyter_code>import statistics as stc

h_median = stc.median(heart_chol)

heart_newChol = [0]*303
print(heart_newChol,"\n")

for i in range(len(heart_chol)):
    if heart_chol[i] > h_IQLow and heart_chol[i] < h_IQHig:
        heart_newChol[i] = heart_chol[i]
    else:
        heart_newChol[i] = h_median

print(heart_newChol)
import seaborn as sb



print(heart_newChol)
sb.distplot(heart_newChol);



sb.regplot(data = heart, x = 'age', y = 'chol_n');<jupyter_output><empty_output><jupyter_text>محاسبه رگرسیون بعد از تمیز کاری $$ y = w_{1}x_{1} + w_{2}x_{2} + w_{3}x_{3} + b$$$$ \left\{\begin{matrix}x_{1} = age\\ x_{2} = cholestoral\\ x_{3} = fasting - blood - sugar\end{matrix}\right.$$$$y = target $$<jupyter_code>import numpy as np
from sklearn.linear_model import LinearRegression

x = []
for i in range(len(heart_newChol)):
    x_m = []
    x_m.append(heart['age'][i])
    x_m.append(heart['chol_n'][i])
    x_m.append(heart['fbs'][i])
    x.append(x_m)

y = []    
for j in range(len(heart_newChol)):
    y.append(heart['target'][j])
    
x, y = np.array(x), np.array(y)


print("\n\t Way 1: X = \n\n{}\n\n\t Way 2: Y = \n\n{}".format(x, y))


print(type(x), np.shape(x))<jupyter_output><class 'numpy.ndarray'> (303, 3)<jupyter_text>حل مدل با رگرسیون<jupyter_code>model = LinearRegression().fit(x, y)<jupyter_output><empty_output><jupyter_text>نمایش خروجی<jupyter_code>r_sq = model.score(x, y)
intercept = model.intercept_
slop = model.coef_

print(" Coefficient of Determination:  {} \n Intercept:\t\t\t{} \n Slop:\t\t\t\t{}".format(r_sq, intercept, slop))<jupyter_output>Coefficient of Determination:  0.05684454144214579 
 Intercept:			1.3898493295980205 
 Slop:				[-0.01161194 -0.00087923 -0.00234402]<jupyter_text>پیش بینی<jupyter_code>a = ['Age', 'Cholesteral', 'Fasting Blood Sugar']


x_new = [int(input("Input {}:\n".format(a[i]))) for i in range(3)]

# x_n = np.array(x_new)

# y_new = model.predict(x_new)

# print(y_new)

y_n = [x_new[j]*slop[j] for j in range(3)] 
y_pred = (sum(y_n)*r_sq) + intercept

print("Prediction: {}".format(y_pred))<jupyter_output>Input Age:
55
Input Cholesteral:
190
Input Fasting Blood Sugar:
0
Prediction: 1.3440490430711238<jupyter_text>محاسبه آزمون کای دو آزمون برای میزان کلسترول<jupyter_code>print(heart['chol_n'])
Mn_h = min(heart['chol_n'])
Mx_h = max(heart['chol_n'])

print("Min: ",Mn_h, "| Max: ", Mx_h)

Domain = (Mx_h - Mn_h) / 5

print("Range: ", Domain)

D_ch = [0] * 6
D_ch[0] = Mn_h
for i in range(1, 6):
    D_ch[i] += D_ch[i-1] + Domain

print(D_ch)<jupyter_output>Min:  126 | Max:  360
Range:  46.8
[126, 172.8, 219.60000000000002, 266.40000000000003, 313.20000000000005, 360.00000000000006]
{'fraction_non_alphanumeric': 0.1317372775131612, 'fraction_numerical': 0.05803459513662572, 'mean_word_length': 3.528376844494892, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 60, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '146500', 'license': 'MIT', 'n_tokens_mistral': 3856, 'n_tokens_neox': 3298, 'n_words': 861, 'path': 'Data-Mining/MKarimi_Project03&04/MKarimi-Project03&04.ipynb', 'repo_name': 'University-of-Bojnurd/Optimization-MSc', 'size': '7978'}