Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ChatGPTにR言語を教えてもらう(仮)

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 ChatGPTにR言語を教えてもらう(仮)

Avatar for NobuakiOshiro

NobuakiOshiro PRO

March 18, 2023
Tweet

More Decks by NobuakiOshiro

Other Decks in Technology

Transcript

  1. ChatGPTʹݴΘΕΔ͕··ɺirisσʔληοτΛ ࢖ͬͨσϞΛ࣮ߦ ### データの概要 head(iris) ## Sepal.Length Sepal.Width Petal.Length Petal.Width

    Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa 7/29
  2. ###基本的な統計量 summary(iris) ## Sepal.Length Sepal.Width Petal.Length Petal.Width ## Min. :4.300

    Min. :2.000 Min. :1.000 Min. :0.100 ## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 ## Median :5.800 Median :3.000 Median :4.350 Median :1.300 ## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199 ## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800 ## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500 ## Species ## setosa :50 ## versicolor:50 ## virginica :50 ## ## ## 8/29
  3. ###散布図行列 pairs(iris[, 1:4], main = "Iris Data", pch = 21,

    bg = c("red", "green3", "blue") [unclass(iris$Species)]) 9/29
  4. ࣮ߦ # 必要なパッケージをインストール #install.packages("caret") #install.packages("rpart") # パッケージを読み込む library(caret) ## Loading

    required package: ggplot2 ## Loading required package: lattice library(rpart) # データセットを学習データとテストデータに分割(例えば、75%を学習データ、25%をテストデータとする) set.seed(42) trainIndex <- createDataPartition(iris$Species, p = 0.75, list = FALSE) train_data <- iris[trainIndex, ] test_data <- iris[-trainIndex, ] # 決定木モデルを構築 model <- rpart(Species ~ ., data = train_data, method = "class") 11/29
  5. # モデルの予測精度を評価 predictions <- predict(model, test_data, type = "class") confusionMatrix(predictions,

    test_data$Species) ## Confusion Matrix and Statistics ## ## Reference ## Prediction setosa versicolor virginica ## setosa 12 0 0 ## versicolor 0 11 4 ## virginica 0 1 8 ## ## Overall Statistics ## ## Accuracy : 0.8611 ## 95% CI : (0.705, 0.9533) ## No Information Rate : 0.3333 ## P-Value [Acc > NIR] : 8.705e-11 ## ## Kappa : 0.7917 ## ## Mcnemar's Test P-Value : NA ## ## Statistics by Class: ## 12/29
  6. irisσʔληοτΛ࢖ͬͨσϞᶅ ࣍ʹɺʮ݁ՌΛ෼͔Γ΍͘͢ggplot2ͰՄࢹԽͯ͘͠Εʯͱݴͬͨ৔߹ · # 必要なパッケージをインストール #install.packages("ggplot2") #install.packages("GGally") # パッケージを読み込む library(ggplot2)

    library(GGally) ## Registered S3 method overwritten by 'GGally': ## method from ## +.gg ggplot2 # テストデータに予測結果を追加 test_data$PredictedSpecies <- predictions # 散布図行列を作成 plot <- ggpairs( test_data, columns = 1:4, mapping = ggplot2::aes(color = PredictedSpecies, shape = Species), 13/29
  7. શવ࿩͕มΘͬͯࢥ͍͖ͭͰ౤͛ͯΈΔ ʮϩέοτͷ؆қγϛϡϨʔγϣϯΛͯ͠ཉ͍͠ʯɺͱݴͬͨ৔߹ͷίʔυ · ###ロケットの簡易シミュレーション(ChatGPTが教えてくれたver) # 定数 g <- 9.81 #

    重力加速度 [m/s^2] mass <- 50 # ロケットの質量 [kg] thrust <- 1000 # ロケットの推力 [N] drag_coefficient <- 0.1 # 空気抵抗係数 air_density <- 1.225 # 空気密度 [kg/m^3] area <- 0.1 # ロケットの断面積 [m^2] # オイラー法による数値積分 time_step <- 0.1 # 時間ステップ [s] time <- seq(0, 10, time_step) velocity <- numeric(length(time)) position <- numeric(length(time)) 17/29
  8. for (i in 2:length(time)) { drag_force <- 0.5 * drag_coefficient

    * air_density * area * velocity[i - 1]^2 acceleration <- (thrust - mass * g - drag_force) / mass velocity[i] <- velocity[i - 1] + acceleration * time_step position[i] <- position[i - 1] + velocity[i - 1] * time_step } 18/29
  9. ࣮ߦ݁Ռͷϓϩοτ plot(time, position, type = "l", xlab = "Time [s]",

    ylab = "Altitude [m]", main = "Rocket Launch Simulation") 19/29