data("iris")
# Data k line diye represent korbe
linear_model = lm(Sepal.Length ~ Petal.Length, data = iris)
#The first one (Sepal.Length) is for y and second one (Petal.Length) is for x
summary(linear_model)
Call:
lm(formula = Sepal.Length ~ Petal.Length, data = iris)
Residuals:
Min 1Q Median
-1.24675 -0.29657 -0.01515
3Q Max
0.27676 1.00269
Coefficients:
Estimate
(Intercept) 4.30660
Petal.Length 0.40892
Std. Error
(Intercept) 0.07839
Petal.Length 0.01889
t value Pr(>|t|)
(Intercept) 54.94 <2e-16
Petal.Length 21.65 <2e-16
(Intercept) ***
Petal.Length ***
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01
‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4071 on 148 degrees of freedom
Multiple R-squared: 0.76, Adjusted R-squared: 0.7583
F-statistic: 468.6 on 1 and 148 DF, p-value: < 2.2e-16
x= iris$Petal.Length
y= iris$Sepal.Length
plot(x, y)
NA
NA
pred = predict(linear_model)
Error: object 'linear_model' not found
ggplot(iris, aes(x= Petal.Length, y= Sepal.Length))+
geom_point()+
geom_smooth(method = "lm", level = 0.95)
Error in ggplot(iris, aes(x = Petal.Length, y = Sepal.Length)) :
could not find function "ggplot"
ggplot(iris, aes(x= Petal.Length, y= Sepal.Length, color= Species))+
geom_point()+
geom_smooth(method = "lm", level = 0.95)
Error in ggplot(iris, aes(x = Petal.Length, y = Sepal.Length, color = Species)) :
could not find function "ggplot"
ggplot(iris, aes(x= Petal.Length, y= Sepal.Length, color= Species))+
geom_point()+
geom_smooth(method = "lm",formula = y~poly(x,3), level = 0.95)
Error in ggplot(iris, aes(x = Petal.Length, y = Sepal.Length, color = Species)) :
could not find function "ggplot"
kmeans_result = kmeans(iris[ ,1:4],centers = 3)
#centers diye 3 ta cluster e vag kore
kmeans_result
K-means clustering with 3 clusters of sizes 38, 62, 50
Cluster means:
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 6.850000 3.073684 5.742105 2.071053
2 5.901613 2.748387 4.393548 1.433871
3 5.006000 3.428000 1.462000 0.246000
Clustering vector:
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 1 2 2 2
[57] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 1 1 1 2 1 1 1 1 1
[113] 1 2 2 1 1 1 1 2 1 2 1 2 1 1 2 2 1 1 1 1 1 2 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1 2
Within cluster sum of squares by cluster:
[1] 23.87947 39.82097 15.15100
(between_SS / total_SS = 88.4 %)
Available components:
[1] "cluster" "centers" "totss" "withinss" "tot.withinss" "betweenss" "size"
[8] "iter" "ifault"
library(cluster)
clusplot(iris, kmeans_result$cluster)
data("iris")
iris
library(lattice)
library(e1071)
library(caret)
#SVM- e1071
#caret-evaluation matrix
train_index= createDataPartition(iris$Species, p = 0.8, list = FALSE)
train_data = iris[train_index, ]
test_data = iris[-train_index, ]
train_data
test_data
svm_model = svm(Species ~ Sepal.Length+Sepal.Width+Petal.Length, data = train_data, kernel = "linear")
##SVM model
svm_model = svm(Species ~ Sepal.Length+Sepal.Width
+Petal.Length, data = train_data, kernel = "linear")
test_data[24, ]
##Predict data
predict(svm_model, newdata = test_data[24, ] )
115
virginica
Levels: setosa versicolor virginica
predictions = predict(svm_model, newdata = test_data)
conf_max = confusionMatrix(predictions, test_data$Species)
conf_max
Confusion Matrix and Statistics
Reference
Prediction setosa versicolor virginica
setosa 10 0 0
versicolor 0 10 1
virginica 0 0 9
Overall Statistics
Accuracy : 0.9667
95% CI : (0.8278, 0.9992)
No Information Rate : 0.3333
P-Value [Acc > NIR] : 2.963e-13
Kappa : 0.95
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: setosa Class: versicolor Class: virginica
Sensitivity 1.0000 1.0000 0.9000
Specificity 1.0000 0.9500 1.0000
Pos Pred Value 1.0000 0.9091 1.0000
Neg Pred Value 1.0000 1.0000 0.9524
Prevalence 0.3333 0.3333 0.3333
Detection Rate 0.3333 0.3333 0.3000
Detection Prevalence 0.3333 0.3667 0.3000
Balanced Accuracy 1.0000 0.9750 0.9500
cm = data.frame(conf_max$table)
ggplot(cm, aes(Prediction, Reference, fill = Freq))+
geom_tile()+
geom_text(aes(label = Freq))+
scale_fill_gradient(low = "white", high = "skyblue")
NA