Professional Documents
Culture Documents
MACHINE
LEARNING
MODELS
Swipe Left
code_archer
Nilesh Parab
nilesh_parab42
Simple Linear Regression
Importing Libraries
Loading Data
In [3]: df = pd.read_csv("Salary_Data.csv")
df.head()
0 1.1 39343.0
1 1.3 46205.0
2 1.5 37731.0
3 2.0 43525.0
4 2.2 39891.0
Missing Value
In [4]: df['YearsExperience'].fillna(df['YearsExperience'].mean(),inplace=True)
df['Salary'].fillna(df['Salary'].mean(),inplace=True)
Separating Data
In [10]: x=df.iloc[:,[0]]
y=df.iloc[:,1]
Out[12]: LinearRegression()
Evaluation of Result
In [14]: plt.scatter(y,Y_pred)
plt.xlabel("Actual")
plt.ylabel("Predicted")
plt.show()
Multiple Linear Regression
Importing Libraries
Loading Data
In [3]: df = pd.read_csv("Data.csv")
df
Out[3]:
Country Age Salary Purchased
In [4]: df['Age'].fillna(df['Age'].mean(),inplace=True)
df['Salary'].fillna(df['Salary'].mean(),inplace=True)
df['Purchased'].fillna(df['Purchased'].mode(),inplace=True)
Lable Encoding
In [6]: le = LabelEncoder()
df['Purchased'] = le.fit_transform(df['Purchased'])
df['Country'] = le.fit_transform(df['Country'])
Seperating Variables
In [7]: x=df.iloc[:,[0,1,3]]
y=df.iloc[:,2]
Scaling Variables
Linear Regression
Out[10]: LinearRegression()
Evaluating Model
Visualizing Results
In [12]: plt.scatter(y,Y_pred)
plt.xlabel("Actual")
plt.ylabel("Predicted")
plt.show()
Logistic Regression
Import Libraries
Loading Dataset
In [101]: df = pd.read_csv("Social_Network_Ads.csv")
df.head()
0 19 19000 0
1 35 20000 0
2 26 43000 0
3 27 57000 0
4 19 76000 0
In [102]: df['Age'].fillna(df['Age'].mean(),inplace=True)
df['EstimatedSalary'].fillna(df['EstimatedSalary'].mean(),inplace=True)
df['Purchased'].fillna(df['Purchased'].mean(),inplace=True)
Seperating Dataset
In [103]: x=df.iloc[:,[0,1]]
y=df.iloc[:,2]
Scaling Variables
Logistic Regression
Out[106]: LogisticRegression()
Evaluating Results
Visualizing Result
0 19 19000 0 0
1 35 20000 0 0
2 26 43000 0 0
3 27 57000 0 0
4 19 76000 0 0
df.plot.scatter("Age","EstimatedSalary",title="Acctual",ax=axis[0],c=df['Purchased'].map(colors))
df.plot.scatter("Age","EstimatedSalary",title="Predicted",ax=axis[1],c=df['Y_pred'].map(colors))
Import Libraries
Loading Dataset
In [14]: df = pd.read_csv("Social_Network_Ads.csv")
df.head()
Out[14]:
Age EstimatedSalary Purchased
0 19 19000 0
1 35 20000 0
2 26 43000 0
3 27 57000 0
4 19 76000 0
In [15]: df['Age'].fillna(df['Age'].mean(),inplace=True)
df['EstimatedSalary'].fillna(df['EstimatedSalary'].mean(),inplace=True)
df['Purchased'].fillna(df['Purchased'].mean(),inplace=True)
Seperating Dataset
In [16]: x=df.iloc[:,[0,1]]
y=df.iloc[:,2]
Scaling Variables
Random Forest
Out[19]: RandomForestClassifier()
Evaluating Results
Visualizing Result
Out[10]:
Age EstimatedSalary Purchased Y_pred
0 19 19000 0 0
1 35 20000 0 0
2 26 43000 0 0
3 27 57000 0 0
4 19 76000 0 0
df.plot.scatter("Age","EstimatedSalary",title="Acctual",ax=axis[0],c=df['Purchased'].map(colors))
df.plot.scatter("Age","EstimatedSalary",title="Prdicted",ax=axis[1],c=df['Y_pred'].map(colors))
Import Libraries
Loading Dataset
In [2]: df = pd.read_csv("Social_Network_Ads.csv")
df.head()
Out[2]:
Age EstimatedSalary Purchased
0 19 19000 0
1 35 20000 0
2 26 43000 0
3 27 57000 0
4 19 76000 0
In [3]: df['Age'].fillna(df['Age'].mean(),inplace=True)
df['EstimatedSalary'].fillna(df['EstimatedSalary'].mean(),inplace=True)
df['Purchased'].fillna(df['Purchased'].mean(),inplace=True)
Seperating Dataset
In [4]: x=df.iloc[:,[0,1]]
y=df.iloc[:,2]
Scaling Variables
Out[9]: SVC()
Evaluating Results
Visualizing Result
Out[11]:
Age EstimatedSalary Purchased Y_pred
0 19 19000 0 0
1 35 20000 0 0
2 26 43000 0 0
3 27 57000 0 0
4 19 76000 0 0
df.plot.scatter("Age","EstimatedSalary",title="Acctual",ax=axis[0],c=df['Purchased'].map(colors))
df.plot.scatter("Age","EstimatedSalary",title="Prdicted",ax=axis[1],c=df['Y_pred'].map(colors))