<jupyter_start><jupyter_text>Analysing data<jupyter_code># Let's check base information about dataset 
dataset.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 400 entries, 0 to 399
Data columns (total 5 columns):
 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   User ID          400 non-null    int64 
 1   Gender           400 non-null    object
 2   Age              400 non-null    int64 
 3   EstimatedSalary  400 non-null    int64 
 4   Purchased        400 non-null    int64 
dtypes: int64(4), object(1)
memory usage: 15.8+ KB<jupyter_text>On the counterplot below, we can see we have a little bit unbalanced dataset. About 250 people purchased a car and 150 didn't<jupyter_code>countplot = sns.countplot(x='Purchased', data=dataset)<jupyter_output><empty_output><jupyter_text>Both men and women did a similar decision. This same amount of men and women didn't decide to buy an SUV. However, in a group of people who purchased an SUV is a bit more women<jupyter_code>sns.countplot(x='Purchased', hue='Gender', data=dataset)<jupyter_output><empty_output><jupyter_text>Linear plot that compares the salary and age of clients who purchased and didn't purchase an SUV<jupyter_code>sns.lineplot(x="EstimatedSalary", y="Age",
             hue="Purchased",
             data=dataset)<jupyter_output><empty_output><jupyter_text>This linear plot might look a bit messy but we can read from it that older people with lower salary were buying SUVs much more often than younger people with a similar salary. It might be related to that SUV is considered more a family and safe car. Younger people might look more for sports cars. That looks nice. Or very cheap cars that are cheap in exploitation. This trend change when younger people has a bigger income.  Another thing is that even younger people were willing to buy an SUV if their salary was very high around 100k and more. For now, it's hard for me to find the reason. Maybe those are business people who want to look more professional and they choose elegant SUV that suits their job position.   I'll show this same data again but on scatterplot<jupyter_code>sns.scatterplot(x="EstimatedSalary", y="Age", hue="Purchased", data=dataset)<jupyter_output><empty_output><jupyter_text>I think in here it's much more visible that younger people (up to 40 years old) with the lowest salary till 80k didn't purchase an SUV.     PairPlot<jupyter_code>temp_data = dataset.drop("User ID", axis=1)
sns.pairplot(temp_data, hue="Purchased")<jupyter_output><empty_output><jupyter_text>*I removed UserID column for this example because they are just random numbers. And we woudln't be able to read any information from that plot.*<jupyter_code>sns.distplot(dataset['EstimatedSalary'], bins=20)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.preprocessing import StandardScaler<jupyter_output><empty_output><jupyter_text>Let me very quickly prepare datasetBecause gender is categorical data with strings values we have to change it to 0 and 1 values. For example where 1 means it's a man and 0 means it's a woman<jupyter_code>gender = pd.get_dummies(dataset["Gender"])
gender.drop("Female", axis=1, inplace=True)

# Delete Gender column from dataset
dataset.drop("Gender", axis=1, inplace=True)
dataset
dataset.insert(2, "Gender", gender)
print(dataset.head())

# We added processed gender values at 2nd index 
X = dataset.iloc[:,[1,2,3]]
y = dataset.iloc[:,4]
print(X)
# Spllit data into training and testing 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
# Transform data so algorytm can use them to make prediction

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
print(X_test)
X_test = sc.transform(X_test)

classifier = LogisticRegression(random_state=0)
classifier.fit(X_train, y_train)
# make prdiction
y_pred = classifier.predict(X_test)

# check accuracy
accuracy_score(y_test, y_pred)<jupyter_output><empty_output><jupyter_text>Our model can predict if people buy an SUV based age, gender and a salary data. The accuracy of our model is **82%** which is quite good  Let's check probability that student buy an SUVLet's say it's a man, 23 years old, with 17k income (15k was the lowest in the dataset)<jupyter_code>student = X[0:0]
print(student)
a = {'Age':23, 'Gender':1, 'EstimatedSalary':17000}

student = student.append(a, ignore_index=True)

student = sc.transform(student) # transform data
print(student)
probability = classifier.predict(student)
print(probability)<jupyter_output>[0]<jupyter_text>As we analyzed data we got to know young people especially with lower income didn't buy SUVs. And our model is right in this example  Let's check for different example<jupyter_code>student = X[0:0]
print(student)
a = {'Age':40, 'Gender':1, 'EstimatedSalary':100000}

student = student.append(a, ignore_index=True)

student = sc.transform(student) # transform data

probability = classifier.predict(student)
print(probability)<jupyter_output>Empty DataFrame
Columns: [Age, Gender, EstimatedSalary]
Index: []
[1]
{'fraction_non_alphanumeric': 0.08825193286818782, 'fraction_numerical': 0.02017725815576089, 'mean_word_length': 5.210772833723653, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '390742', 'license': 'MIT', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1590, 'n_words': 583, 'path': 'SUV purchase prediction.ipynb', 'repo_name': 'mrozowski/MachineLearning-jupyter', 'size': '5303'}